maxval0_s1.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Implementation of the MAXLOC intrinsic
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas Koenig
  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 <string.h>
  23. #include <assert.h>
  24. #include <limits.h>
  25. #if defined (HAVE_GFC_UINTEGER_1) && defined (HAVE_GFC_UINTEGER_1)
  26. static inline int
  27. compare_fcn (const GFC_UINTEGER_1 *a, const GFC_UINTEGER_1 *b, gfc_charlen_type n)
  28. {
  29. if (sizeof (GFC_UINTEGER_1) == 1)
  30. return memcmp (a, b, n);
  31. else
  32. return memcmp_char4 (a, b, n);
  33. }
  34. #define INITVAL 0
  35. extern void maxval0_s1 (GFC_UINTEGER_1 * restrict,
  36. gfc_charlen_type,
  37. gfc_array_s1 * const restrict array, gfc_charlen_type);
  38. export_proto(maxval0_s1);
  39. void
  40. maxval0_s1 (GFC_UINTEGER_1 * restrict ret,
  41. gfc_charlen_type xlen,
  42. gfc_array_s1 * const restrict array, gfc_charlen_type len)
  43. {
  44. index_type count[GFC_MAX_DIMENSIONS];
  45. index_type extent[GFC_MAX_DIMENSIONS];
  46. index_type sstride[GFC_MAX_DIMENSIONS];
  47. const GFC_UINTEGER_1 *base;
  48. index_type rank;
  49. index_type n;
  50. rank = GFC_DESCRIPTOR_RANK (array);
  51. if (rank <= 0)
  52. runtime_error ("Rank of array needs to be > 0");
  53. assert (xlen == len);
  54. /* Initialize return value. */
  55. memset (ret, INITVAL, sizeof(*ret) * len);
  56. for (n = 0; n < rank; n++)
  57. {
  58. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n) * len;
  59. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  60. count[n] = 0;
  61. if (extent[n] <= 0)
  62. return;
  63. }
  64. base = array->base_addr;
  65. {
  66. const GFC_UINTEGER_1 *retval;
  67. retval = ret;
  68. while (base)
  69. {
  70. do
  71. {
  72. /* Implementation start. */
  73. if (compare_fcn (base, retval, len) > 0)
  74. {
  75. retval = base;
  76. }
  77. /* Implementation end. */
  78. /* Advance to the next element. */
  79. base += sstride[0];
  80. }
  81. while (++count[0] != extent[0]);
  82. n = 0;
  83. do
  84. {
  85. /* When we get to the end of a dimension, reset it and increment
  86. the next dimension. */
  87. count[n] = 0;
  88. /* We could precalculate these products, but this is a less
  89. frequently used path so probably not worth it. */
  90. base -= sstride[n] * extent[n];
  91. n++;
  92. if (n >= rank)
  93. {
  94. /* Break out of the loop. */
  95. base = NULL;
  96. break;
  97. }
  98. else
  99. {
  100. count[n]++;
  101. base += sstride[n];
  102. }
  103. }
  104. while (count[n] == extent[n]);
  105. }
  106. memcpy (ret, retval, len * sizeof (*ret));
  107. }
  108. }
  109. extern void mmaxval0_s1 (GFC_UINTEGER_1 * restrict,
  110. gfc_charlen_type, gfc_array_s1 * const restrict array,
  111. gfc_array_l1 * const restrict mask, gfc_charlen_type len);
  112. export_proto(mmaxval0_s1);
  113. void
  114. mmaxval0_s1 (GFC_UINTEGER_1 * const restrict ret,
  115. gfc_charlen_type xlen, gfc_array_s1 * const restrict array,
  116. gfc_array_l1 * const restrict mask, gfc_charlen_type len)
  117. {
  118. index_type count[GFC_MAX_DIMENSIONS];
  119. index_type extent[GFC_MAX_DIMENSIONS];
  120. index_type sstride[GFC_MAX_DIMENSIONS];
  121. index_type mstride[GFC_MAX_DIMENSIONS];
  122. const GFC_UINTEGER_1 *base;
  123. GFC_LOGICAL_1 *mbase;
  124. int rank;
  125. index_type n;
  126. int mask_kind;
  127. if (mask == NULL)
  128. {
  129. maxval0_s1 (ret, xlen, array, len);
  130. return;
  131. }
  132. rank = GFC_DESCRIPTOR_RANK (array);
  133. if (rank <= 0)
  134. runtime_error ("Rank of array needs to be > 0");
  135. assert (xlen == len);
  136. /* Initialize return value. */
  137. memset (ret, INITVAL, sizeof(*ret) * len);
  138. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  139. mbase = mask->base_addr;
  140. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  141. #ifdef HAVE_GFC_LOGICAL_16
  142. || mask_kind == 16
  143. #endif
  144. )
  145. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  146. else
  147. runtime_error ("Funny sized logical array");
  148. for (n = 0; n < rank; n++)
  149. {
  150. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n) * len;
  151. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  152. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  153. count[n] = 0;
  154. if (extent[n] <= 0)
  155. return;
  156. }
  157. base = array->base_addr;
  158. {
  159. const GFC_UINTEGER_1 *retval;
  160. retval = ret;
  161. while (base)
  162. {
  163. do
  164. {
  165. /* Implementation start. */
  166. if (*mbase && compare_fcn (base, retval, len) > 0)
  167. {
  168. retval = base;
  169. }
  170. /* Implementation end. */
  171. /* Advance to the next element. */
  172. base += sstride[0];
  173. mbase += mstride[0];
  174. }
  175. while (++count[0] != extent[0]);
  176. n = 0;
  177. do
  178. {
  179. /* When we get to the end of a dimension, reset it and increment
  180. the next dimension. */
  181. count[n] = 0;
  182. /* We could precalculate these products, but this is a less
  183. frequently used path so probably not worth it. */
  184. base -= sstride[n] * extent[n];
  185. mbase -= mstride[n] * extent[n];
  186. n++;
  187. if (n >= rank)
  188. {
  189. /* Break out of the loop. */
  190. base = NULL;
  191. break;
  192. }
  193. else
  194. {
  195. count[n]++;
  196. base += sstride[n];
  197. mbase += mstride[n];
  198. }
  199. }
  200. while (count[n] == extent[n]);
  201. }
  202. memcpy (ret, retval, len * sizeof (*ret));
  203. }
  204. }
  205. extern void smaxval0_s1 (GFC_UINTEGER_1 * restrict,
  206. gfc_charlen_type,
  207. gfc_array_s1 * const restrict array, GFC_LOGICAL_4 *, gfc_charlen_type);
  208. export_proto(smaxval0_s1);
  209. void
  210. smaxval0_s1 (GFC_UINTEGER_1 * restrict ret,
  211. gfc_charlen_type xlen, gfc_array_s1 * const restrict array,
  212. GFC_LOGICAL_4 *mask, gfc_charlen_type len)
  213. {
  214. if (mask == NULL || *mask)
  215. {
  216. maxval0_s1 (ret, xlen, array, len);
  217. return;
  218. }
  219. memset (ret, INITVAL, sizeof (*ret) * len);
  220. }
  221. #endif