lower-bitint: Fix lowering of non-_BitInt to _BitInt cast merged with some wider...
[official-gcc.git] / libgfortran / generated / findloc1_c10.c
blobfa68971ff3856061ae37126e8b838984ad25995c
1 /* Implementation of the FINDLOC intrinsic
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
3 Contributed by Thomas König <tk@tkoenig.net>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
27 #include <assert.h>
29 #if defined (HAVE_GFC_COMPLEX_10)
30 extern void findloc1_c10 (gfc_array_index_type * const restrict retarray,
31 gfc_array_c10 * const restrict array, GFC_COMPLEX_10 value,
32 const index_type * restrict pdim, GFC_LOGICAL_4 back);
33 export_proto(findloc1_c10);
35 extern void
36 findloc1_c10 (gfc_array_index_type * const restrict retarray,
37 gfc_array_c10 * const restrict array, GFC_COMPLEX_10 value,
38 const index_type * restrict pdim, GFC_LOGICAL_4 back)
40 index_type count[GFC_MAX_DIMENSIONS];
41 index_type extent[GFC_MAX_DIMENSIONS];
42 index_type sstride[GFC_MAX_DIMENSIONS];
43 index_type dstride[GFC_MAX_DIMENSIONS];
44 const GFC_COMPLEX_10 * restrict base;
45 index_type * restrict dest;
46 index_type rank;
47 index_type n;
48 index_type len;
49 index_type delta;
50 index_type dim;
51 int continue_loop;
53 /* Make dim zero based to avoid confusion. */
54 rank = GFC_DESCRIPTOR_RANK (array) - 1;
55 dim = (*pdim) - 1;
57 if (unlikely (dim < 0 || dim > rank))
59 runtime_error ("Dim argument incorrect in FINDLOC intrinsic: "
60 "is %ld, should be between 1 and %ld",
61 (long int) dim + 1, (long int) rank + 1);
64 len = GFC_DESCRIPTOR_EXTENT(array,dim);
65 if (len < 0)
66 len = 0;
67 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
69 for (n = 0; n < dim; n++)
71 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
72 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
74 if (extent[n] < 0)
75 extent[n] = 0;
77 for (n = dim; n < rank; n++)
79 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
80 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
82 if (extent[n] < 0)
83 extent[n] = 0;
86 if (retarray->base_addr == NULL)
88 size_t alloc_size, str;
90 for (n = 0; n < rank; n++)
92 if (n == 0)
93 str = 1;
94 else
95 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
97 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
101 retarray->offset = 0;
102 retarray->dtype.rank = rank;
104 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
106 retarray->base_addr = xmallocarray (alloc_size, sizeof (index_type));
107 if (alloc_size == 0)
108 return;
110 else
112 if (rank != GFC_DESCRIPTOR_RANK (retarray))
113 runtime_error ("rank of return array incorrect in"
114 " FINDLOC intrinsic: is %ld, should be %ld",
115 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
116 (long int) rank);
118 if (unlikely (compile_options.bounds_check))
119 bounds_ifunction_return ((array_t *) retarray, extent,
120 "return value", "FINDLOC");
123 for (n = 0; n < rank; n++)
125 count[n] = 0;
126 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
127 if (extent[n] <= 0)
128 return;
131 dest = retarray->base_addr;
132 continue_loop = 1;
134 base = array->base_addr;
135 while (continue_loop)
137 const GFC_COMPLEX_10 * restrict src;
138 index_type result;
140 result = 0;
141 if (back)
143 src = base + (len - 1) * delta * 1;
144 for (n = len; n > 0; n--, src -= delta * 1)
146 if (*src == value)
148 result = n;
149 break;
153 else
155 src = base;
156 for (n = 1; n <= len; n++, src += delta * 1)
158 if (*src == value)
160 result = n;
161 break;
165 *dest = result;
167 count[0]++;
168 base += sstride[0] * 1;
169 dest += dstride[0];
170 n = 0;
171 while (count[n] == extent[n])
173 count[n] = 0;
174 base -= sstride[n] * extent[n] * 1;
175 dest -= dstride[n] * extent[n];
176 n++;
177 if (n >= rank)
179 continue_loop = 0;
180 break;
182 else
184 count[n]++;
185 base += sstride[n] * 1;
186 dest += dstride[n];
191 extern void mfindloc1_c10 (gfc_array_index_type * const restrict retarray,
192 gfc_array_c10 * const restrict array, GFC_COMPLEX_10 value,
193 const index_type * restrict pdim, gfc_array_l1 *const restrict mask,
194 GFC_LOGICAL_4 back);
195 export_proto(mfindloc1_c10);
197 extern void
198 mfindloc1_c10 (gfc_array_index_type * const restrict retarray,
199 gfc_array_c10 * const restrict array, GFC_COMPLEX_10 value,
200 const index_type * restrict pdim, gfc_array_l1 *const restrict mask,
201 GFC_LOGICAL_4 back)
203 index_type count[GFC_MAX_DIMENSIONS];
204 index_type extent[GFC_MAX_DIMENSIONS];
205 index_type sstride[GFC_MAX_DIMENSIONS];
206 index_type mstride[GFC_MAX_DIMENSIONS];
207 index_type dstride[GFC_MAX_DIMENSIONS];
208 const GFC_COMPLEX_10 * restrict base;
209 const GFC_LOGICAL_1 * restrict mbase;
210 index_type * restrict dest;
211 index_type rank;
212 index_type n;
213 index_type len;
214 index_type delta;
215 index_type mdelta;
216 index_type dim;
217 int mask_kind;
218 int continue_loop;
220 /* Make dim zero based to avoid confusion. */
221 rank = GFC_DESCRIPTOR_RANK (array) - 1;
222 dim = (*pdim) - 1;
224 if (unlikely (dim < 0 || dim > rank))
226 runtime_error ("Dim argument incorrect in FINDLOC intrinsic: "
227 "is %ld, should be between 1 and %ld",
228 (long int) dim + 1, (long int) rank + 1);
231 len = GFC_DESCRIPTOR_EXTENT(array,dim);
232 if (len < 0)
233 len = 0;
235 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
236 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
238 mbase = mask->base_addr;
240 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
242 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
243 #ifdef HAVE_GFC_LOGICAL_16
244 || mask_kind == 16
245 #endif
247 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
248 else
249 internal_error (NULL, "Funny sized logical array");
251 for (n = 0; n < dim; n++)
253 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
254 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
255 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
257 if (extent[n] < 0)
258 extent[n] = 0;
260 for (n = dim; n < rank; n++)
262 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
263 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
264 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
266 if (extent[n] < 0)
267 extent[n] = 0;
270 if (retarray->base_addr == NULL)
272 size_t alloc_size, str;
274 for (n = 0; n < rank; n++)
276 if (n == 0)
277 str = 1;
278 else
279 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
281 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
285 retarray->offset = 0;
286 retarray->dtype.rank = rank;
288 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
290 retarray->base_addr = xmallocarray (alloc_size, sizeof (index_type));
291 if (alloc_size == 0)
292 return;
294 else
296 if (rank != GFC_DESCRIPTOR_RANK (retarray))
297 runtime_error ("rank of return array incorrect in"
298 " FINDLOC intrinsic: is %ld, should be %ld",
299 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
300 (long int) rank);
302 if (unlikely (compile_options.bounds_check))
303 bounds_ifunction_return ((array_t *) retarray, extent,
304 "return value", "FINDLOC");
307 for (n = 0; n < rank; n++)
309 count[n] = 0;
310 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
311 if (extent[n] <= 0)
312 return;
315 dest = retarray->base_addr;
316 continue_loop = 1;
318 base = array->base_addr;
319 while (continue_loop)
321 const GFC_COMPLEX_10 * restrict src;
322 const GFC_LOGICAL_1 * restrict msrc;
323 index_type result;
325 result = 0;
326 if (back)
328 src = base + (len - 1) * delta * 1;
329 msrc = mbase + (len - 1) * mdelta;
330 for (n = len; n > 0; n--, src -= delta * 1, msrc -= mdelta)
332 if (*msrc && *src == value)
334 result = n;
335 break;
339 else
341 src = base;
342 msrc = mbase;
343 for (n = 1; n <= len; n++, src += delta * 1, msrc += mdelta)
345 if (*msrc && *src == value)
347 result = n;
348 break;
352 *dest = result;
354 count[0]++;
355 base += sstride[0] * 1;
356 mbase += mstride[0];
357 dest += dstride[0];
358 n = 0;
359 while (count[n] == extent[n])
361 count[n] = 0;
362 base -= sstride[n] * extent[n] * 1;
363 mbase -= mstride[n] * extent[n];
364 dest -= dstride[n] * extent[n];
365 n++;
366 if (n >= rank)
368 continue_loop = 0;
369 break;
371 else
373 count[n]++;
374 base += sstride[n] * 1;
375 dest += dstride[n];
380 extern void sfindloc1_c10 (gfc_array_index_type * const restrict retarray,
381 gfc_array_c10 * const restrict array, GFC_COMPLEX_10 value,
382 const index_type * restrict pdim, GFC_LOGICAL_4 *const restrict mask,
383 GFC_LOGICAL_4 back);
384 export_proto(sfindloc1_c10);
386 extern void
387 sfindloc1_c10 (gfc_array_index_type * const restrict retarray,
388 gfc_array_c10 * const restrict array, GFC_COMPLEX_10 value,
389 const index_type * restrict pdim, GFC_LOGICAL_4 *const restrict mask,
390 GFC_LOGICAL_4 back)
392 index_type count[GFC_MAX_DIMENSIONS];
393 index_type extent[GFC_MAX_DIMENSIONS];
394 index_type dstride[GFC_MAX_DIMENSIONS];
395 index_type * restrict dest;
396 index_type rank;
397 index_type n;
398 index_type len;
399 index_type dim;
400 bool continue_loop;
402 if (mask == NULL || *mask)
404 findloc1_c10 (retarray, array, value, pdim, back);
405 return;
407 /* Make dim zero based to avoid confusion. */
408 rank = GFC_DESCRIPTOR_RANK (array) - 1;
409 dim = (*pdim) - 1;
411 if (unlikely (dim < 0 || dim > rank))
413 runtime_error ("Dim argument incorrect in FINDLOC intrinsic: "
414 "is %ld, should be between 1 and %ld",
415 (long int) dim + 1, (long int) rank + 1);
418 len = GFC_DESCRIPTOR_EXTENT(array,dim);
419 if (len < 0)
420 len = 0;
422 for (n = 0; n < dim; n++)
424 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
426 if (extent[n] <= 0)
427 extent[n] = 0;
430 for (n = dim; n < rank; n++)
432 extent[n] =
433 GFC_DESCRIPTOR_EXTENT(array,n + 1);
435 if (extent[n] <= 0)
436 extent[n] = 0;
440 if (retarray->base_addr == NULL)
442 size_t alloc_size, str;
444 for (n = 0; n < rank; n++)
446 if (n == 0)
447 str = 1;
448 else
449 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
451 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
454 retarray->offset = 0;
455 retarray->dtype.rank = rank;
457 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
459 retarray->base_addr = xmallocarray (alloc_size, sizeof (index_type));
460 if (alloc_size == 0)
461 return;
463 else
465 if (rank != GFC_DESCRIPTOR_RANK (retarray))
466 runtime_error ("rank of return array incorrect in"
467 " FINDLOC intrinsic: is %ld, should be %ld",
468 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
469 (long int) rank);
471 if (unlikely (compile_options.bounds_check))
472 bounds_ifunction_return ((array_t *) retarray, extent,
473 "return value", "FINDLOC");
476 for (n = 0; n < rank; n++)
478 count[n] = 0;
479 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
480 if (extent[n] <= 0)
481 return;
483 dest = retarray->base_addr;
484 continue_loop = 1;
486 while (continue_loop)
488 *dest = 0;
490 count[0]++;
491 dest += dstride[0];
492 n = 0;
493 while (count[n] == extent[n])
495 count[n] = 0;
496 dest -= dstride[n] * extent[n];
497 n++;
498 if (n >= rank)
500 continue_loop = 0;
501 break;
503 else
505 count[n]++;
506 dest += dstride[n];
511 #endif