in_pack_c16.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Helper function for repacking arrays.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <stdlib.h>
  22. #include <assert.h>
  23. #if defined (HAVE_GFC_COMPLEX_16)
  24. /* Allocates a block of memory with internal_malloc if the array needs
  25. repacking. */
  26. GFC_COMPLEX_16 *
  27. internal_pack_c16 (gfc_array_c16 * source)
  28. {
  29. index_type count[GFC_MAX_DIMENSIONS];
  30. index_type extent[GFC_MAX_DIMENSIONS];
  31. index_type stride[GFC_MAX_DIMENSIONS];
  32. index_type stride0;
  33. index_type dim;
  34. index_type ssize;
  35. const GFC_COMPLEX_16 *src;
  36. GFC_COMPLEX_16 * restrict dest;
  37. GFC_COMPLEX_16 *destptr;
  38. int n;
  39. int packed;
  40. /* TODO: Investigate how we can figure out if this is a temporary
  41. since the stride=0 thing has been removed from the frontend. */
  42. dim = GFC_DESCRIPTOR_RANK (source);
  43. ssize = 1;
  44. packed = 1;
  45. for (n = 0; n < dim; n++)
  46. {
  47. count[n] = 0;
  48. stride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
  49. extent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
  50. if (extent[n] <= 0)
  51. {
  52. /* Do nothing. */
  53. packed = 1;
  54. break;
  55. }
  56. if (ssize != stride[n])
  57. packed = 0;
  58. ssize *= extent[n];
  59. }
  60. if (packed)
  61. return source->base_addr;
  62. /* Allocate storage for the destination. */
  63. destptr = xmallocarray (ssize, sizeof (GFC_COMPLEX_16));
  64. dest = destptr;
  65. src = source->base_addr;
  66. stride0 = stride[0];
  67. while (src)
  68. {
  69. /* Copy the data. */
  70. *(dest++) = *src;
  71. /* Advance to the next element. */
  72. src += stride0;
  73. count[0]++;
  74. /* Advance to the next source element. */
  75. n = 0;
  76. while (count[n] == extent[n])
  77. {
  78. /* When we get to the end of a dimension, reset it and increment
  79. the next dimension. */
  80. count[n] = 0;
  81. /* We could precalculate these products, but this is a less
  82. frequently used path so probably not worth it. */
  83. src -= stride[n] * extent[n];
  84. n++;
  85. if (n == dim)
  86. {
  87. src = NULL;
  88. break;
  89. }
  90. else
  91. {
  92. count[n]++;
  93. src += stride[n];
  94. }
  95. }
  96. }
  97. return destptr;
  98. }
  99. #endif