]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Implementation of the MAXVAL intrinsic |
748086b7 | 2 | Copyright 2002, 2007, 2009 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Paul Brook <paul@nowt.org> |
4 | ||
57dea9f6 | 5 | This file is part of the GNU Fortran 95 runtime library (libgfortran). |
6de9cd9a DN |
6 | |
7 | Libgfortran is free software; you can redistribute it and/or | |
57dea9f6 | 8 | modify it under the terms of the GNU General Public |
6de9cd9a | 9 | License as published by the Free Software Foundation; either |
748086b7 | 10 | version 3 of the License, or (at your option) any later version. |
6de9cd9a DN |
11 | |
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 | |
57dea9f6 | 15 | GNU General Public License for more details. |
6de9cd9a | 16 | |
748086b7 JJ |
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. | |
20 | ||
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/>. */ | |
6de9cd9a | 25 | |
36ae8a61 | 26 | #include "libgfortran.h" |
6de9cd9a DN |
27 | #include <stdlib.h> |
28 | #include <assert.h> | |
6de9cd9a | 29 | |
7d7b8bfe | 30 | |
644cb69f FXC |
31 | #if defined (HAVE_GFC_INTEGER_8) && defined (HAVE_GFC_INTEGER_8) |
32 | ||
33 | ||
64acfd99 JB |
34 | extern void maxval_i8 (gfc_array_i8 * const restrict, |
35 | gfc_array_i8 * const restrict, const index_type * const restrict); | |
7f68c75f | 36 | export_proto(maxval_i8); |
7d7b8bfe | 37 | |
6de9cd9a | 38 | void |
64acfd99 JB |
39 | maxval_i8 (gfc_array_i8 * const restrict retarray, |
40 | gfc_array_i8 * const restrict array, | |
41 | const index_type * const restrict pdim) | |
6de9cd9a | 42 | { |
e33e218b TK |
43 | index_type count[GFC_MAX_DIMENSIONS]; |
44 | index_type extent[GFC_MAX_DIMENSIONS]; | |
45 | index_type sstride[GFC_MAX_DIMENSIONS]; | |
46 | index_type dstride[GFC_MAX_DIMENSIONS]; | |
64acfd99 JB |
47 | const GFC_INTEGER_8 * restrict base; |
48 | GFC_INTEGER_8 * restrict dest; | |
6de9cd9a DN |
49 | index_type rank; |
50 | index_type n; | |
51 | index_type len; | |
52 | index_type delta; | |
53 | index_type dim; | |
da96f5ab | 54 | int continue_loop; |
6de9cd9a DN |
55 | |
56 | /* Make dim zero based to avoid confusion. */ | |
57 | dim = (*pdim) - 1; | |
58 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
e33e218b | 59 | |
dfb55fdc | 60 | len = GFC_DESCRIPTOR_EXTENT(array,dim); |
da96f5ab TK |
61 | if (len < 0) |
62 | len = 0; | |
dfb55fdc | 63 | delta = GFC_DESCRIPTOR_STRIDE(array,dim); |
6de9cd9a DN |
64 | |
65 | for (n = 0; n < dim; n++) | |
66 | { | |
dfb55fdc TK |
67 | sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n); |
68 | extent[n] = GFC_DESCRIPTOR_EXTENT(array,n); | |
80ee04b9 TK |
69 | |
70 | if (extent[n] < 0) | |
71 | extent[n] = 0; | |
6de9cd9a DN |
72 | } |
73 | for (n = dim; n < rank; n++) | |
74 | { | |
dfb55fdc TK |
75 | sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1); |
76 | extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1); | |
80ee04b9 TK |
77 | |
78 | if (extent[n] < 0) | |
79 | extent[n] = 0; | |
6de9cd9a DN |
80 | } |
81 | ||
6c167c45 VL |
82 | if (retarray->data == NULL) |
83 | { | |
dfb55fdc | 84 | size_t alloc_size, str; |
80ee04b9 | 85 | |
6c167c45 VL |
86 | for (n = 0; n < rank; n++) |
87 | { | |
6c167c45 | 88 | if (n == 0) |
dfb55fdc | 89 | str = 1; |
6c167c45 | 90 | else |
dfb55fdc TK |
91 | str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; |
92 | ||
93 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
94 | ||
6c167c45 VL |
95 | } |
96 | ||
efd4dc1a | 97 | retarray->offset = 0; |
50dd63a9 | 98 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
80ee04b9 | 99 | |
dfb55fdc | 100 | alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
80ee04b9 TK |
101 | * extent[rank-1]; |
102 | ||
103 | if (alloc_size == 0) | |
104 | { | |
105 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 106 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
80ee04b9 | 107 | return; |
dfb55fdc | 108 | |
80ee04b9 TK |
109 | } |
110 | else | |
111 | retarray->data = internal_malloc_size (alloc_size); | |
6c167c45 | 112 | } |
50dd63a9 TK |
113 | else |
114 | { | |
50dd63a9 | 115 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
fd6590f8 | 116 | runtime_error ("rank of return array incorrect in" |
ccacefc7 TK |
117 | " MAXVAL intrinsic: is %ld, should be %ld", |
118 | (long int) (GFC_DESCRIPTOR_RANK (retarray)), | |
119 | (long int) rank); | |
fd6590f8 | 120 | |
9731c4a3 | 121 | if (unlikely (compile_options.bounds_check)) |
16bff921 TK |
122 | bounds_ifunction_return ((array_t *) retarray, extent, |
123 | "return value", "MAXVAL"); | |
50dd63a9 TK |
124 | } |
125 | ||
6de9cd9a DN |
126 | for (n = 0; n < rank; n++) |
127 | { | |
128 | count[n] = 0; | |
dfb55fdc | 129 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
6de9cd9a DN |
130 | if (extent[n] <= 0) |
131 | len = 0; | |
132 | } | |
133 | ||
134 | base = array->data; | |
135 | dest = retarray->data; | |
136 | ||
da96f5ab TK |
137 | continue_loop = 1; |
138 | while (continue_loop) | |
6de9cd9a | 139 | { |
64acfd99 | 140 | const GFC_INTEGER_8 * restrict src; |
6de9cd9a DN |
141 | GFC_INTEGER_8 result; |
142 | src = base; | |
143 | { | |
144 | ||
88116029 | 145 | result = (-GFC_INTEGER_8_HUGE-1); |
6de9cd9a | 146 | if (len <= 0) |
88116029 | 147 | *dest = (-GFC_INTEGER_8_HUGE-1); |
6de9cd9a DN |
148 | else |
149 | { | |
150 | for (n = 0; n < len; n++, src += delta) | |
151 | { | |
152 | ||
153 | if (*src > result) | |
154 | result = *src; | |
155 | } | |
156 | *dest = result; | |
157 | } | |
158 | } | |
159 | /* Advance to the next element. */ | |
160 | count[0]++; | |
161 | base += sstride[0]; | |
162 | dest += dstride[0]; | |
163 | n = 0; | |
164 | while (count[n] == extent[n]) | |
165 | { | |
166 | /* When we get to the end of a dimension, reset it and increment | |
167 | the next dimension. */ | |
168 | count[n] = 0; | |
169 | /* We could precalculate these products, but this is a less | |
5d7adf7a | 170 | frequently used path so probably not worth it. */ |
6de9cd9a DN |
171 | base -= sstride[n] * extent[n]; |
172 | dest -= dstride[n] * extent[n]; | |
173 | n++; | |
174 | if (n == rank) | |
175 | { | |
176 | /* Break out of the look. */ | |
da96f5ab TK |
177 | continue_loop = 0; |
178 | break; | |
6de9cd9a DN |
179 | } |
180 | else | |
181 | { | |
182 | count[n]++; | |
183 | base += sstride[n]; | |
184 | dest += dstride[n]; | |
185 | } | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
7d7b8bfe | 190 | |
64acfd99 JB |
191 | extern void mmaxval_i8 (gfc_array_i8 * const restrict, |
192 | gfc_array_i8 * const restrict, const index_type * const restrict, | |
28dc6b33 | 193 | gfc_array_l1 * const restrict); |
7f68c75f | 194 | export_proto(mmaxval_i8); |
7d7b8bfe | 195 | |
6de9cd9a | 196 | void |
64acfd99 JB |
197 | mmaxval_i8 (gfc_array_i8 * const restrict retarray, |
198 | gfc_array_i8 * const restrict array, | |
199 | const index_type * const restrict pdim, | |
28dc6b33 | 200 | gfc_array_l1 * const restrict mask) |
6de9cd9a | 201 | { |
e33e218b TK |
202 | index_type count[GFC_MAX_DIMENSIONS]; |
203 | index_type extent[GFC_MAX_DIMENSIONS]; | |
204 | index_type sstride[GFC_MAX_DIMENSIONS]; | |
205 | index_type dstride[GFC_MAX_DIMENSIONS]; | |
206 | index_type mstride[GFC_MAX_DIMENSIONS]; | |
64acfd99 JB |
207 | GFC_INTEGER_8 * restrict dest; |
208 | const GFC_INTEGER_8 * restrict base; | |
28dc6b33 | 209 | const GFC_LOGICAL_1 * restrict mbase; |
6de9cd9a DN |
210 | int rank; |
211 | int dim; | |
212 | index_type n; | |
213 | index_type len; | |
214 | index_type delta; | |
215 | index_type mdelta; | |
28dc6b33 | 216 | int mask_kind; |
6de9cd9a DN |
217 | |
218 | dim = (*pdim) - 1; | |
219 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
e33e218b | 220 | |
dfb55fdc | 221 | len = GFC_DESCRIPTOR_EXTENT(array,dim); |
6de9cd9a DN |
222 | if (len <= 0) |
223 | return; | |
28dc6b33 TK |
224 | |
225 | mbase = mask->data; | |
226 | ||
227 | mask_kind = GFC_DESCRIPTOR_SIZE (mask); | |
228 | ||
229 | if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8 | |
230 | #ifdef HAVE_GFC_LOGICAL_16 | |
231 | || mask_kind == 16 | |
232 | #endif | |
233 | ) | |
234 | mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind); | |
235 | else | |
236 | runtime_error ("Funny sized logical array"); | |
237 | ||
dfb55fdc TK |
238 | delta = GFC_DESCRIPTOR_STRIDE(array,dim); |
239 | mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim); | |
6de9cd9a DN |
240 | |
241 | for (n = 0; n < dim; n++) | |
242 | { | |
dfb55fdc TK |
243 | sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n); |
244 | mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n); | |
245 | extent[n] = GFC_DESCRIPTOR_EXTENT(array,n); | |
80ee04b9 TK |
246 | |
247 | if (extent[n] < 0) | |
248 | extent[n] = 0; | |
249 | ||
6de9cd9a DN |
250 | } |
251 | for (n = dim; n < rank; n++) | |
252 | { | |
dfb55fdc TK |
253 | sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1); |
254 | mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1); | |
255 | extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1); | |
80ee04b9 TK |
256 | |
257 | if (extent[n] < 0) | |
258 | extent[n] = 0; | |
6de9cd9a DN |
259 | } |
260 | ||
50dd63a9 TK |
261 | if (retarray->data == NULL) |
262 | { | |
dfb55fdc | 263 | size_t alloc_size, str; |
80ee04b9 | 264 | |
50dd63a9 TK |
265 | for (n = 0; n < rank; n++) |
266 | { | |
50dd63a9 | 267 | if (n == 0) |
dfb55fdc | 268 | str = 1; |
50dd63a9 | 269 | else |
dfb55fdc TK |
270 | str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; |
271 | ||
272 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
273 | ||
50dd63a9 TK |
274 | } |
275 | ||
dfb55fdc | 276 | alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
80ee04b9 TK |
277 | * extent[rank-1]; |
278 | ||
efd4dc1a | 279 | retarray->offset = 0; |
50dd63a9 | 280 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
80ee04b9 TK |
281 | |
282 | if (alloc_size == 0) | |
283 | { | |
284 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 285 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
80ee04b9 TK |
286 | return; |
287 | } | |
288 | else | |
289 | retarray->data = internal_malloc_size (alloc_size); | |
290 | ||
50dd63a9 TK |
291 | } |
292 | else | |
293 | { | |
50dd63a9 | 294 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
fd6590f8 TK |
295 | runtime_error ("rank of return array incorrect in MAXVAL intrinsic"); |
296 | ||
9731c4a3 | 297 | if (unlikely (compile_options.bounds_check)) |
fd6590f8 | 298 | { |
16bff921 TK |
299 | bounds_ifunction_return ((array_t *) retarray, extent, |
300 | "return value", "MAXVAL"); | |
301 | bounds_equal_extents ((array_t *) mask, (array_t *) array, | |
302 | "MASK argument", "MAXVAL"); | |
fd6590f8 | 303 | } |
50dd63a9 TK |
304 | } |
305 | ||
6de9cd9a DN |
306 | for (n = 0; n < rank; n++) |
307 | { | |
308 | count[n] = 0; | |
dfb55fdc | 309 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
6de9cd9a DN |
310 | if (extent[n] <= 0) |
311 | return; | |
312 | } | |
313 | ||
314 | dest = retarray->data; | |
315 | base = array->data; | |
6de9cd9a DN |
316 | |
317 | while (base) | |
318 | { | |
64acfd99 | 319 | const GFC_INTEGER_8 * restrict src; |
28dc6b33 | 320 | const GFC_LOGICAL_1 * restrict msrc; |
6de9cd9a DN |
321 | GFC_INTEGER_8 result; |
322 | src = base; | |
323 | msrc = mbase; | |
324 | { | |
325 | ||
88116029 | 326 | result = (-GFC_INTEGER_8_HUGE-1); |
6de9cd9a | 327 | if (len <= 0) |
88116029 | 328 | *dest = (-GFC_INTEGER_8_HUGE-1); |
6de9cd9a DN |
329 | else |
330 | { | |
331 | for (n = 0; n < len; n++, src += delta, msrc += mdelta) | |
332 | { | |
333 | ||
334 | if (*msrc && *src > result) | |
335 | result = *src; | |
336 | } | |
337 | *dest = result; | |
338 | } | |
339 | } | |
340 | /* Advance to the next element. */ | |
341 | count[0]++; | |
342 | base += sstride[0]; | |
343 | mbase += mstride[0]; | |
344 | dest += dstride[0]; | |
345 | n = 0; | |
346 | while (count[n] == extent[n]) | |
347 | { | |
348 | /* When we get to the end of a dimension, reset it and increment | |
349 | the next dimension. */ | |
350 | count[n] = 0; | |
351 | /* We could precalculate these products, but this is a less | |
5d7adf7a | 352 | frequently used path so probably not worth it. */ |
6de9cd9a DN |
353 | base -= sstride[n] * extent[n]; |
354 | mbase -= mstride[n] * extent[n]; | |
355 | dest -= dstride[n] * extent[n]; | |
356 | n++; | |
357 | if (n == rank) | |
358 | { | |
359 | /* Break out of the look. */ | |
360 | base = NULL; | |
361 | break; | |
362 | } | |
363 | else | |
364 | { | |
365 | count[n]++; | |
366 | base += sstride[n]; | |
367 | mbase += mstride[n]; | |
368 | dest += dstride[n]; | |
369 | } | |
370 | } | |
371 | } | |
372 | } | |
373 | ||
97a62038 TK |
374 | |
375 | extern void smaxval_i8 (gfc_array_i8 * const restrict, | |
376 | gfc_array_i8 * const restrict, const index_type * const restrict, | |
377 | GFC_LOGICAL_4 *); | |
378 | export_proto(smaxval_i8); | |
379 | ||
380 | void | |
381 | smaxval_i8 (gfc_array_i8 * const restrict retarray, | |
382 | gfc_array_i8 * const restrict array, | |
383 | const index_type * const restrict pdim, | |
384 | GFC_LOGICAL_4 * mask) | |
385 | { | |
802367d7 TK |
386 | index_type count[GFC_MAX_DIMENSIONS]; |
387 | index_type extent[GFC_MAX_DIMENSIONS]; | |
388 | index_type sstride[GFC_MAX_DIMENSIONS]; | |
389 | index_type dstride[GFC_MAX_DIMENSIONS]; | |
390 | GFC_INTEGER_8 * restrict dest; | |
97a62038 TK |
391 | index_type rank; |
392 | index_type n; | |
802367d7 TK |
393 | index_type dim; |
394 | ||
97a62038 TK |
395 | |
396 | if (*mask) | |
397 | { | |
398 | maxval_i8 (retarray, array, pdim); | |
399 | return; | |
400 | } | |
802367d7 TK |
401 | /* Make dim zero based to avoid confusion. */ |
402 | dim = (*pdim) - 1; | |
403 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
404 | ||
405 | for (n = 0; n < dim; n++) | |
406 | { | |
dfb55fdc TK |
407 | sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n); |
408 | extent[n] = GFC_DESCRIPTOR_EXTENT(array,n); | |
802367d7 TK |
409 | |
410 | if (extent[n] <= 0) | |
411 | extent[n] = 0; | |
412 | } | |
413 | ||
414 | for (n = dim; n < rank; n++) | |
415 | { | |
dfb55fdc | 416 | sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1); |
802367d7 | 417 | extent[n] = |
dfb55fdc | 418 | GFC_DESCRIPTOR_EXTENT(array,n + 1); |
802367d7 TK |
419 | |
420 | if (extent[n] <= 0) | |
421 | extent[n] = 0; | |
422 | } | |
97a62038 TK |
423 | |
424 | if (retarray->data == NULL) | |
425 | { | |
dfb55fdc | 426 | size_t alloc_size, str; |
802367d7 TK |
427 | |
428 | for (n = 0; n < rank; n++) | |
429 | { | |
802367d7 | 430 | if (n == 0) |
dfb55fdc | 431 | str = 1; |
802367d7 | 432 | else |
dfb55fdc TK |
433 | str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; |
434 | ||
435 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
436 | ||
802367d7 TK |
437 | } |
438 | ||
97a62038 | 439 | retarray->offset = 0; |
802367d7 TK |
440 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
441 | ||
dfb55fdc | 442 | alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
802367d7 TK |
443 | * extent[rank-1]; |
444 | ||
445 | if (alloc_size == 0) | |
446 | { | |
447 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 448 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
802367d7 TK |
449 | return; |
450 | } | |
451 | else | |
452 | retarray->data = internal_malloc_size (alloc_size); | |
97a62038 TK |
453 | } |
454 | else | |
455 | { | |
802367d7 TK |
456 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
457 | runtime_error ("rank of return array incorrect in" | |
458 | " MAXVAL intrinsic: is %ld, should be %ld", | |
459 | (long int) (GFC_DESCRIPTOR_RANK (retarray)), | |
460 | (long int) rank); | |
461 | ||
9731c4a3 | 462 | if (unlikely (compile_options.bounds_check)) |
fd6590f8 | 463 | { |
802367d7 TK |
464 | for (n=0; n < rank; n++) |
465 | { | |
466 | index_type ret_extent; | |
97a62038 | 467 | |
dfb55fdc | 468 | ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n); |
802367d7 TK |
469 | if (extent[n] != ret_extent) |
470 | runtime_error ("Incorrect extent in return value of" | |
471 | " MAXVAL intrinsic in dimension %ld:" | |
472 | " is %ld, should be %ld", (long int) n + 1, | |
473 | (long int) ret_extent, (long int) extent[n]); | |
474 | } | |
fd6590f8 TK |
475 | } |
476 | } | |
97a62038 | 477 | |
802367d7 TK |
478 | for (n = 0; n < rank; n++) |
479 | { | |
480 | count[n] = 0; | |
dfb55fdc | 481 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
802367d7 TK |
482 | } |
483 | ||
484 | dest = retarray->data; | |
485 | ||
486 | while(1) | |
487 | { | |
488 | *dest = (-GFC_INTEGER_8_HUGE-1); | |
489 | count[0]++; | |
490 | dest += dstride[0]; | |
491 | n = 0; | |
492 | while (count[n] == extent[n]) | |
493 | { | |
494 | /* When we get to the end of a dimension, reset it and increment | |
495 | the next dimension. */ | |
496 | count[n] = 0; | |
497 | /* We could precalculate these products, but this is a less | |
498 | frequently used path so probably not worth it. */ | |
499 | dest -= dstride[n] * extent[n]; | |
500 | n++; | |
501 | if (n == rank) | |
502 | return; | |
503 | else | |
504 | { | |
505 | count[n]++; | |
506 | dest += dstride[n]; | |
507 | } | |
508 | } | |
509 | } | |
97a62038 TK |
510 | } |
511 | ||
644cb69f | 512 | #endif |