pack_i16.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Specific implementation of the PACK intrinsic
  2. Copyright (C) 2002-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. Ligbfortran 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 <string.h>
  22. #if defined (HAVE_GFC_INTEGER_16)
  23. /* PACK is specified as follows:
  24. 13.14.80 PACK (ARRAY, MASK, [VECTOR])
  25. Description: Pack an array into an array of rank one under the
  26. control of a mask.
  27. Class: Transformational function.
  28. Arguments:
  29. ARRAY may be of any type. It shall not be scalar.
  30. MASK shall be of type LOGICAL. It shall be conformable with ARRAY.
  31. VECTOR (optional) shall be of the same type and type parameters
  32. as ARRAY. VECTOR shall have at least as many elements as
  33. there are true elements in MASK. If MASK is a scalar
  34. with the value true, VECTOR shall have at least as many
  35. elements as there are in ARRAY.
  36. Result Characteristics: The result is an array of rank one with the
  37. same type and type parameters as ARRAY. If VECTOR is present, the
  38. result size is that of VECTOR; otherwise, the result size is the
  39. number /t/ of true elements in MASK unless MASK is scalar with the
  40. value true, in which case the result size is the size of ARRAY.
  41. Result Value: Element /i/ of the result is the element of ARRAY
  42. that corresponds to the /i/th true element of MASK, taking elements
  43. in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
  44. present and has size /n/ > /t/, element /i/ of the result has the
  45. value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
  46. Examples: The nonzero elements of an array M with the value
  47. | 0 0 0 |
  48. | 9 0 0 | may be "gathered" by the function PACK. The result of
  49. | 0 0 7 |
  50. PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
  51. VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
  52. There are two variants of the PACK intrinsic: one, where MASK is
  53. array valued, and the other one where MASK is scalar. */
  54. void
  55. pack_i16 (gfc_array_i16 *ret, const gfc_array_i16 *array,
  56. const gfc_array_l1 *mask, const gfc_array_i16 *vector)
  57. {
  58. /* r.* indicates the return array. */
  59. index_type rstride0;
  60. GFC_INTEGER_16 * restrict rptr;
  61. /* s.* indicates the source array. */
  62. index_type sstride[GFC_MAX_DIMENSIONS];
  63. index_type sstride0;
  64. const GFC_INTEGER_16 *sptr;
  65. /* m.* indicates the mask array. */
  66. index_type mstride[GFC_MAX_DIMENSIONS];
  67. index_type mstride0;
  68. const GFC_LOGICAL_1 *mptr;
  69. index_type count[GFC_MAX_DIMENSIONS];
  70. index_type extent[GFC_MAX_DIMENSIONS];
  71. int zero_sized;
  72. index_type n;
  73. index_type dim;
  74. index_type nelem;
  75. index_type total;
  76. int mask_kind;
  77. dim = GFC_DESCRIPTOR_RANK (array);
  78. mptr = mask->base_addr;
  79. /* Use the same loop for all logical types, by using GFC_LOGICAL_1
  80. and using shifting to address size and endian issues. */
  81. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  82. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  83. #ifdef HAVE_GFC_LOGICAL_16
  84. || mask_kind == 16
  85. #endif
  86. )
  87. {
  88. /* Do not convert a NULL pointer as we use test for NULL below. */
  89. if (mptr)
  90. mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
  91. }
  92. else
  93. runtime_error ("Funny sized logical array");
  94. zero_sized = 0;
  95. for (n = 0; n < dim; n++)
  96. {
  97. count[n] = 0;
  98. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  99. if (extent[n] <= 0)
  100. zero_sized = 1;
  101. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  102. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  103. }
  104. if (sstride[0] == 0)
  105. sstride[0] = 1;
  106. if (mstride[0] == 0)
  107. mstride[0] = mask_kind;
  108. if (zero_sized)
  109. sptr = NULL;
  110. else
  111. sptr = array->base_addr;
  112. if (ret->base_addr == NULL || unlikely (compile_options.bounds_check))
  113. {
  114. /* Count the elements, either for allocating memory or
  115. for bounds checking. */
  116. if (vector != NULL)
  117. {
  118. /* The return array will have as many
  119. elements as there are in VECTOR. */
  120. total = GFC_DESCRIPTOR_EXTENT(vector,0);
  121. if (total < 0)
  122. {
  123. total = 0;
  124. vector = NULL;
  125. }
  126. }
  127. else
  128. {
  129. /* We have to count the true elements in MASK. */
  130. total = count_0 (mask);
  131. }
  132. if (ret->base_addr == NULL)
  133. {
  134. /* Setup the array descriptor. */
  135. GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
  136. ret->offset = 0;
  137. /* xmallocarray allocates a single byte for zero size. */
  138. ret->base_addr = xmallocarray (total, sizeof (GFC_INTEGER_16));
  139. if (total == 0)
  140. return;
  141. }
  142. else
  143. {
  144. /* We come here because of range checking. */
  145. index_type ret_extent;
  146. ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
  147. if (total != ret_extent)
  148. runtime_error ("Incorrect extent in return value of PACK intrinsic;"
  149. " is %ld, should be %ld", (long int) total,
  150. (long int) ret_extent);
  151. }
  152. }
  153. rstride0 = GFC_DESCRIPTOR_STRIDE(ret,0);
  154. if (rstride0 == 0)
  155. rstride0 = 1;
  156. sstride0 = sstride[0];
  157. mstride0 = mstride[0];
  158. rptr = ret->base_addr;
  159. while (sptr && mptr)
  160. {
  161. /* Test this element. */
  162. if (*mptr)
  163. {
  164. /* Add it. */
  165. *rptr = *sptr;
  166. rptr += rstride0;
  167. }
  168. /* Advance to the next element. */
  169. sptr += sstride0;
  170. mptr += mstride0;
  171. count[0]++;
  172. n = 0;
  173. while (count[n] == extent[n])
  174. {
  175. /* When we get to the end of a dimension, reset it and increment
  176. the next dimension. */
  177. count[n] = 0;
  178. /* We could precalculate these products, but this is a less
  179. frequently used path so probably not worth it. */
  180. sptr -= sstride[n] * extent[n];
  181. mptr -= mstride[n] * extent[n];
  182. n++;
  183. if (n >= dim)
  184. {
  185. /* Break out of the loop. */
  186. sptr = NULL;
  187. break;
  188. }
  189. else
  190. {
  191. count[n]++;
  192. sptr += sstride[n];
  193. mptr += mstride[n];
  194. }
  195. }
  196. }
  197. /* Add any remaining elements from VECTOR. */
  198. if (vector)
  199. {
  200. n = GFC_DESCRIPTOR_EXTENT(vector,0);
  201. nelem = ((rptr - ret->base_addr) / rstride0);
  202. if (n > nelem)
  203. {
  204. sstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
  205. if (sstride0 == 0)
  206. sstride0 = 1;
  207. sptr = vector->base_addr + sstride0 * nelem;
  208. n -= nelem;
  209. while (n--)
  210. {
  211. *rptr = *sptr;
  212. rptr += rstride0;
  213. sptr += sstride0;
  214. }
  215. }
  216. }
  217. }
  218. #endif