in_pack.m4 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. include(iparm.m4)dnl
  24. `#if defined (HAVE_'rtype_name`)
  25. /* Allocates a block of memory with internal_malloc if the array needs
  26. repacking. */
  27. '
  28. dnl The kind (ie size) is used to name the function for logicals, integers
  29. dnl and reals. For complex, it's c4 or c8.
  30. rtype_name` *
  31. internal_pack_'rtype_ccode` ('rtype` * source)
  32. {
  33. index_type count[GFC_MAX_DIMENSIONS];
  34. index_type extent[GFC_MAX_DIMENSIONS];
  35. index_type stride[GFC_MAX_DIMENSIONS];
  36. index_type stride0;
  37. index_type dim;
  38. index_type ssize;
  39. const 'rtype_name` *src;
  40. 'rtype_name` * restrict dest;
  41. 'rtype_name` *destptr;
  42. int n;
  43. int packed;
  44. /* TODO: Investigate how we can figure out if this is a temporary
  45. since the stride=0 thing has been removed from the frontend. */
  46. dim = GFC_DESCRIPTOR_RANK (source);
  47. ssize = 1;
  48. packed = 1;
  49. for (n = 0; n < dim; n++)
  50. {
  51. count[n] = 0;
  52. stride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
  53. extent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
  54. if (extent[n] <= 0)
  55. {
  56. /* Do nothing. */
  57. packed = 1;
  58. break;
  59. }
  60. if (ssize != stride[n])
  61. packed = 0;
  62. ssize *= extent[n];
  63. }
  64. if (packed)
  65. return source->base_addr;
  66. /* Allocate storage for the destination. */
  67. destptr = xmallocarray (ssize, sizeof ('rtype_name`));
  68. dest = destptr;
  69. src = source->base_addr;
  70. stride0 = stride[0];
  71. while (src)
  72. {
  73. /* Copy the data. */
  74. *(dest++) = *src;
  75. /* Advance to the next element. */
  76. src += stride0;
  77. count[0]++;
  78. /* Advance to the next source element. */
  79. n = 0;
  80. while (count[n] == extent[n])
  81. {
  82. /* When we get to the end of a dimension, reset it and increment
  83. the next dimension. */
  84. count[n] = 0;
  85. /* We could precalculate these products, but this is a less
  86. frequently used path so probably not worth it. */
  87. src -= stride[n] * extent[n];
  88. n++;
  89. if (n == dim)
  90. {
  91. src = NULL;
  92. break;
  93. }
  94. else
  95. {
  96. count[n]++;
  97. src += stride[n];
  98. }
  99. }
  100. }
  101. return destptr;
  102. }
  103. #endif
  104. '