1 /* Implementation of the MAXVAL intrinsic
2 Copyright 2002, 2007, 2009 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
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"
31 #if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_REAL_4)
34 extern void maxval_r4 (gfc_array_r4
* const restrict
,
35 gfc_array_r4
* const restrict
, const index_type
* const restrict
);
36 export_proto(maxval_r4
);
39 maxval_r4 (gfc_array_r4
* const restrict retarray
,
40 gfc_array_r4
* const restrict array
,
41 const index_type
* const restrict pdim
)
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
];
47 const GFC_REAL_4
* restrict base
;
48 GFC_REAL_4
* restrict dest
;
56 /* Make dim zero based to avoid confusion. */
58 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
60 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
63 delta
= array
->dim
[dim
].stride
;
65 for (n
= 0; n
< dim
; n
++)
67 sstride
[n
] = array
->dim
[n
].stride
;
68 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
73 for (n
= dim
; n
< rank
; n
++)
75 sstride
[n
] = array
->dim
[n
+ 1].stride
;
77 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
83 if (retarray
->data
== NULL
)
87 for (n
= 0; n
< rank
; n
++)
89 retarray
->dim
[n
].lbound
= 0;
90 retarray
->dim
[n
].ubound
= extent
[n
]-1;
92 retarray
->dim
[n
].stride
= 1;
94 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
98 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
100 alloc_size
= sizeof (GFC_REAL_4
) * retarray
->dim
[rank
-1].stride
105 /* Make sure we have a zero-sized array. */
106 retarray
->dim
[0].lbound
= 0;
107 retarray
->dim
[0].ubound
= -1;
111 retarray
->data
= internal_malloc_size (alloc_size
);
115 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
116 runtime_error ("rank of return array incorrect in"
117 " MAXVAL intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
121 if (unlikely (compile_options
.bounds_check
))
123 for (n
=0; n
< rank
; n
++)
125 index_type ret_extent
;
127 ret_extent
= retarray
->dim
[n
].ubound
+ 1
128 - retarray
->dim
[n
].lbound
;
129 if (extent
[n
] != ret_extent
)
130 runtime_error ("Incorrect extent in return value of"
131 " MAXVAL intrinsic in dimension %ld:"
132 " is %ld, should be %ld", (long int) n
+ 1,
133 (long int) ret_extent
, (long int) extent
[n
]);
138 for (n
= 0; n
< rank
; n
++)
141 dstride
[n
] = retarray
->dim
[n
].stride
;
147 dest
= retarray
->data
;
150 while (continue_loop
)
152 const GFC_REAL_4
* restrict src
;
157 result
= -GFC_REAL_4_HUGE
;
159 *dest
= -GFC_REAL_4_HUGE
;
162 for (n
= 0; n
< len
; n
++, src
+= delta
)
171 /* Advance to the next element. */
176 while (count
[n
] == extent
[n
])
178 /* When we get to the end of a dimension, reset it and increment
179 the next dimension. */
181 /* We could precalculate these products, but this is a less
182 frequently used path so probably not worth it. */
183 base
-= sstride
[n
] * extent
[n
];
184 dest
-= dstride
[n
] * extent
[n
];
188 /* Break out of the look. */
203 extern void mmaxval_r4 (gfc_array_r4
* const restrict
,
204 gfc_array_r4
* const restrict
, const index_type
* const restrict
,
205 gfc_array_l1
* const restrict
);
206 export_proto(mmaxval_r4
);
209 mmaxval_r4 (gfc_array_r4
* const restrict retarray
,
210 gfc_array_r4
* const restrict array
,
211 const index_type
* const restrict pdim
,
212 gfc_array_l1
* const restrict mask
)
214 index_type count
[GFC_MAX_DIMENSIONS
];
215 index_type extent
[GFC_MAX_DIMENSIONS
];
216 index_type sstride
[GFC_MAX_DIMENSIONS
];
217 index_type dstride
[GFC_MAX_DIMENSIONS
];
218 index_type mstride
[GFC_MAX_DIMENSIONS
];
219 GFC_REAL_4
* restrict dest
;
220 const GFC_REAL_4
* restrict base
;
221 const GFC_LOGICAL_1
* restrict mbase
;
231 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
233 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
239 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
241 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
242 #ifdef HAVE_GFC_LOGICAL_16
246 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
248 runtime_error ("Funny sized logical array");
250 delta
= array
->dim
[dim
].stride
;
251 mdelta
= mask
->dim
[dim
].stride
* mask_kind
;
253 for (n
= 0; n
< dim
; n
++)
255 sstride
[n
] = array
->dim
[n
].stride
;
256 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
257 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
263 for (n
= dim
; n
< rank
; n
++)
265 sstride
[n
] = array
->dim
[n
+ 1].stride
;
266 mstride
[n
] = mask
->dim
[n
+ 1].stride
* mask_kind
;
268 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
274 if (retarray
->data
== NULL
)
278 for (n
= 0; n
< rank
; n
++)
280 retarray
->dim
[n
].lbound
= 0;
281 retarray
->dim
[n
].ubound
= extent
[n
]-1;
283 retarray
->dim
[n
].stride
= 1;
285 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
288 alloc_size
= sizeof (GFC_REAL_4
) * retarray
->dim
[rank
-1].stride
291 retarray
->offset
= 0;
292 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
296 /* Make sure we have a zero-sized array. */
297 retarray
->dim
[0].lbound
= 0;
298 retarray
->dim
[0].ubound
= -1;
302 retarray
->data
= internal_malloc_size (alloc_size
);
307 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
308 runtime_error ("rank of return array incorrect in MAXVAL intrinsic");
310 if (unlikely (compile_options
.bounds_check
))
312 for (n
=0; n
< rank
; n
++)
314 index_type ret_extent
;
316 ret_extent
= retarray
->dim
[n
].ubound
+ 1
317 - retarray
->dim
[n
].lbound
;
318 if (extent
[n
] != ret_extent
)
319 runtime_error ("Incorrect extent in return value of"
320 " MAXVAL intrinsic in dimension %ld:"
321 " is %ld, should be %ld", (long int) n
+ 1,
322 (long int) ret_extent
, (long int) extent
[n
]);
324 for (n
=0; n
<= rank
; n
++)
326 index_type mask_extent
, array_extent
;
328 array_extent
= array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
329 mask_extent
= mask
->dim
[n
].ubound
+ 1 - mask
->dim
[n
].lbound
;
330 if (array_extent
!= mask_extent
)
331 runtime_error ("Incorrect extent in MASK argument of"
332 " MAXVAL intrinsic in dimension %ld:"
333 " is %ld, should be %ld", (long int) n
+ 1,
334 (long int) mask_extent
, (long int) array_extent
);
339 for (n
= 0; n
< rank
; n
++)
342 dstride
[n
] = retarray
->dim
[n
].stride
;
347 dest
= retarray
->data
;
352 const GFC_REAL_4
* restrict src
;
353 const GFC_LOGICAL_1
* restrict msrc
;
359 result
= -GFC_REAL_4_HUGE
;
361 *dest
= -GFC_REAL_4_HUGE
;
364 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
367 if (*msrc
&& *src
> result
)
373 /* Advance to the next element. */
379 while (count
[n
] == extent
[n
])
381 /* When we get to the end of a dimension, reset it and increment
382 the next dimension. */
384 /* We could precalculate these products, but this is a less
385 frequently used path so probably not worth it. */
386 base
-= sstride
[n
] * extent
[n
];
387 mbase
-= mstride
[n
] * extent
[n
];
388 dest
-= dstride
[n
] * extent
[n
];
392 /* Break out of the look. */
408 extern void smaxval_r4 (gfc_array_r4
* const restrict
,
409 gfc_array_r4
* const restrict
, const index_type
* const restrict
,
411 export_proto(smaxval_r4
);
414 smaxval_r4 (gfc_array_r4
* const restrict retarray
,
415 gfc_array_r4
* const restrict array
,
416 const index_type
* const restrict pdim
,
417 GFC_LOGICAL_4
* mask
)
419 index_type count
[GFC_MAX_DIMENSIONS
];
420 index_type extent
[GFC_MAX_DIMENSIONS
];
421 index_type sstride
[GFC_MAX_DIMENSIONS
];
422 index_type dstride
[GFC_MAX_DIMENSIONS
];
423 GFC_REAL_4
* restrict dest
;
431 maxval_r4 (retarray
, array
, pdim
);
434 /* Make dim zero based to avoid confusion. */
436 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
438 for (n
= 0; n
< dim
; n
++)
440 sstride
[n
] = array
->dim
[n
].stride
;
441 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
447 for (n
= dim
; n
< rank
; n
++)
449 sstride
[n
] = array
->dim
[n
+ 1].stride
;
451 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
457 if (retarray
->data
== NULL
)
461 for (n
= 0; n
< rank
; n
++)
463 retarray
->dim
[n
].lbound
= 0;
464 retarray
->dim
[n
].ubound
= extent
[n
]-1;
466 retarray
->dim
[n
].stride
= 1;
468 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
471 retarray
->offset
= 0;
472 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
474 alloc_size
= sizeof (GFC_REAL_4
) * retarray
->dim
[rank
-1].stride
479 /* Make sure we have a zero-sized array. */
480 retarray
->dim
[0].lbound
= 0;
481 retarray
->dim
[0].ubound
= -1;
485 retarray
->data
= internal_malloc_size (alloc_size
);
489 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
490 runtime_error ("rank of return array incorrect in"
491 " MAXVAL intrinsic: is %ld, should be %ld",
492 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
495 if (unlikely (compile_options
.bounds_check
))
497 for (n
=0; n
< rank
; n
++)
499 index_type ret_extent
;
501 ret_extent
= retarray
->dim
[n
].ubound
+ 1
502 - retarray
->dim
[n
].lbound
;
503 if (extent
[n
] != ret_extent
)
504 runtime_error ("Incorrect extent in return value of"
505 " MAXVAL intrinsic in dimension %ld:"
506 " is %ld, should be %ld", (long int) n
+ 1,
507 (long int) ret_extent
, (long int) extent
[n
]);
512 for (n
= 0; n
< rank
; n
++)
515 dstride
[n
] = retarray
->dim
[n
].stride
;
518 dest
= retarray
->data
;
522 *dest
= -GFC_REAL_4_HUGE
;
526 while (count
[n
] == extent
[n
])
528 /* When we get to the end of a dimension, reset it and increment
529 the next dimension. */
531 /* We could precalculate these products, but this is a less
532 frequently used path so probably not worth it. */
533 dest
-= dstride
[n
] * extent
[n
];