1 /* Implementation of the MINLOC intrinsic
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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"
29 #if defined (HAVE_GFC_REAL_16) && defined (HAVE_GFC_INTEGER_16)
32 extern void minloc1_16_r16 (gfc_array_i16
* const restrict
,
33 gfc_array_r16
* const restrict
, const index_type
* const restrict
);
34 export_proto(minloc1_16_r16
);
37 minloc1_16_r16 (gfc_array_i16
* const restrict retarray
,
38 gfc_array_r16
* const restrict array
,
39 const index_type
* const restrict pdim
)
41 index_type count
[GFC_MAX_DIMENSIONS
];
42 index_type extent
[GFC_MAX_DIMENSIONS
];
43 index_type sstride
[GFC_MAX_DIMENSIONS
];
44 index_type dstride
[GFC_MAX_DIMENSIONS
];
45 const GFC_REAL_16
* restrict base
;
46 GFC_INTEGER_16
* restrict dest
;
54 /* Make dim zero based to avoid confusion. */
55 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
58 if (unlikely (dim
< 0 || dim
> rank
))
60 runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
61 "is %ld, should be between 1 and %ld",
62 (long int) dim
+ 1, (long int) rank
+ 1);
65 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
68 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
70 for (n
= 0; n
< dim
; n
++)
72 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
73 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
78 for (n
= dim
; n
< rank
; n
++)
80 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
81 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
87 if (retarray
->base_addr
== NULL
)
89 size_t alloc_size
, str
;
91 for (n
= 0; n
< rank
; n
++)
96 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
98 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
102 retarray
->offset
= 0;
103 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
105 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
107 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_16
));
110 /* Make sure we have a zero-sized array. */
111 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
118 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
119 runtime_error ("rank of return array incorrect in"
120 " MINLOC intrinsic: is %ld, should be %ld",
121 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
124 if (unlikely (compile_options
.bounds_check
))
125 bounds_ifunction_return ((array_t
*) retarray
, extent
,
126 "return value", "MINLOC");
129 for (n
= 0; n
< rank
; n
++)
132 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
137 base
= array
->base_addr
;
138 dest
= retarray
->base_addr
;
141 while (continue_loop
)
143 const GFC_REAL_16
* restrict src
;
144 GFC_INTEGER_16 result
;
149 #if defined (GFC_REAL_16_INFINITY)
150 minval
= GFC_REAL_16_INFINITY
;
152 minval
= GFC_REAL_16_HUGE
;
159 for (n
= 0; n
< len
; n
++, src
+= delta
)
162 #if defined (GFC_REAL_16_QUIET_NAN)
166 result
= (GFC_INTEGER_16
)n
+ 1;
170 for (; n
< len
; n
++, src
+= delta
)
176 result
= (GFC_INTEGER_16
)n
+ 1;
183 /* Advance to the next element. */
188 while (count
[n
] == extent
[n
])
190 /* When we get to the end of a dimension, reset it and increment
191 the next dimension. */
193 /* We could precalculate these products, but this is a less
194 frequently used path so probably not worth it. */
195 base
-= sstride
[n
] * extent
[n
];
196 dest
-= dstride
[n
] * extent
[n
];
200 /* Break out of the loop. */
215 extern void mminloc1_16_r16 (gfc_array_i16
* const restrict
,
216 gfc_array_r16
* const restrict
, const index_type
* const restrict
,
217 gfc_array_l1
* const restrict
);
218 export_proto(mminloc1_16_r16
);
221 mminloc1_16_r16 (gfc_array_i16
* const restrict retarray
,
222 gfc_array_r16
* const restrict array
,
223 const index_type
* const restrict pdim
,
224 gfc_array_l1
* const restrict mask
)
226 index_type count
[GFC_MAX_DIMENSIONS
];
227 index_type extent
[GFC_MAX_DIMENSIONS
];
228 index_type sstride
[GFC_MAX_DIMENSIONS
];
229 index_type dstride
[GFC_MAX_DIMENSIONS
];
230 index_type mstride
[GFC_MAX_DIMENSIONS
];
231 GFC_INTEGER_16
* restrict dest
;
232 const GFC_REAL_16
* restrict base
;
233 const GFC_LOGICAL_1
* restrict mbase
;
243 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
246 if (unlikely (dim
< 0 || dim
> rank
))
248 runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
249 "is %ld, should be between 1 and %ld",
250 (long int) dim
+ 1, (long int) rank
+ 1);
253 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
257 mbase
= mask
->base_addr
;
259 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
261 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
262 #ifdef HAVE_GFC_LOGICAL_16
266 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
268 runtime_error ("Funny sized logical array");
270 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
271 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
273 for (n
= 0; n
< dim
; n
++)
275 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
276 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
277 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
283 for (n
= dim
; n
< rank
; n
++)
285 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
286 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
287 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
293 if (retarray
->base_addr
== NULL
)
295 size_t alloc_size
, str
;
297 for (n
= 0; n
< rank
; n
++)
302 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
304 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
308 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
310 retarray
->offset
= 0;
311 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
315 /* Make sure we have a zero-sized array. */
316 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
320 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_16
));
325 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
326 runtime_error ("rank of return array incorrect in MINLOC intrinsic");
328 if (unlikely (compile_options
.bounds_check
))
330 bounds_ifunction_return ((array_t
*) retarray
, extent
,
331 "return value", "MINLOC");
332 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
333 "MASK argument", "MINLOC");
337 for (n
= 0; n
< rank
; n
++)
340 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
345 dest
= retarray
->base_addr
;
346 base
= array
->base_addr
;
350 const GFC_REAL_16
* restrict src
;
351 const GFC_LOGICAL_1
* restrict msrc
;
352 GFC_INTEGER_16 result
;
358 #if defined (GFC_REAL_16_INFINITY)
359 minval
= GFC_REAL_16_INFINITY
;
361 minval
= GFC_REAL_16_HUGE
;
363 #if defined (GFC_REAL_16_QUIET_NAN)
364 GFC_INTEGER_16 result2
= 0;
367 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
372 #if defined (GFC_REAL_16_QUIET_NAN)
374 result2
= (GFC_INTEGER_16
)n
+ 1;
379 result
= (GFC_INTEGER_16
)n
+ 1;
384 #if defined (GFC_REAL_16_QUIET_NAN)
385 if (unlikely (n
>= len
))
389 for (; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
391 if (*msrc
&& *src
< minval
)
394 result
= (GFC_INTEGER_16
)n
+ 1;
399 /* Advance to the next element. */
405 while (count
[n
] == extent
[n
])
407 /* When we get to the end of a dimension, reset it and increment
408 the next dimension. */
410 /* We could precalculate these products, but this is a less
411 frequently used path so probably not worth it. */
412 base
-= sstride
[n
] * extent
[n
];
413 mbase
-= mstride
[n
] * extent
[n
];
414 dest
-= dstride
[n
] * extent
[n
];
418 /* Break out of the loop. */
434 extern void sminloc1_16_r16 (gfc_array_i16
* const restrict
,
435 gfc_array_r16
* const restrict
, const index_type
* const restrict
,
437 export_proto(sminloc1_16_r16
);
440 sminloc1_16_r16 (gfc_array_i16
* const restrict retarray
,
441 gfc_array_r16
* const restrict array
,
442 const index_type
* const restrict pdim
,
443 GFC_LOGICAL_4
* mask
)
445 index_type count
[GFC_MAX_DIMENSIONS
];
446 index_type extent
[GFC_MAX_DIMENSIONS
];
447 index_type dstride
[GFC_MAX_DIMENSIONS
];
448 GFC_INTEGER_16
* restrict dest
;
456 minloc1_16_r16 (retarray
, array
, pdim
);
459 /* Make dim zero based to avoid confusion. */
461 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
463 if (unlikely (dim
< 0 || dim
> rank
))
465 runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
466 "is %ld, should be between 1 and %ld",
467 (long int) dim
+ 1, (long int) rank
+ 1);
470 for (n
= 0; n
< dim
; n
++)
472 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
478 for (n
= dim
; n
< rank
; n
++)
481 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
487 if (retarray
->base_addr
== NULL
)
489 size_t alloc_size
, str
;
491 for (n
= 0; n
< rank
; n
++)
496 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
498 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
502 retarray
->offset
= 0;
503 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
505 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
509 /* Make sure we have a zero-sized array. */
510 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
514 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_16
));
518 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
519 runtime_error ("rank of return array incorrect in"
520 " MINLOC intrinsic: is %ld, should be %ld",
521 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
524 if (unlikely (compile_options
.bounds_check
))
526 for (n
=0; n
< rank
; n
++)
528 index_type ret_extent
;
530 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
531 if (extent
[n
] != ret_extent
)
532 runtime_error ("Incorrect extent in return value of"
533 " MINLOC intrinsic in dimension %ld:"
534 " is %ld, should be %ld", (long int) n
+ 1,
535 (long int) ret_extent
, (long int) extent
[n
]);
540 for (n
= 0; n
< rank
; n
++)
543 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
546 dest
= retarray
->base_addr
;
554 while (count
[n
] == extent
[n
])
556 /* When we get to the end of a dimension, reset it and increment
557 the next dimension. */
559 /* We could precalculate these products, but this is a less
560 frequently used path so probably not worth it. */
561 dest
-= dstride
[n
] * extent
[n
];