pack_i2.c 7.4 KB

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