in_pack_i16.c 3.2 KB

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