1 /* Implementation of the MINLOC intrinsic
2 Copyright 2002, 2007 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
37 #if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_INTEGER_16)
40 extern void minloc1_16_r4 (gfc_array_i16
* const restrict
,
41 gfc_array_r4
* const restrict
, const index_type
* const restrict
);
42 export_proto(minloc1_16_r4
);
45 minloc1_16_r4 (gfc_array_i16
* const restrict retarray
,
46 gfc_array_r4
* const restrict array
,
47 const index_type
* const restrict pdim
)
49 index_type count
[GFC_MAX_DIMENSIONS
];
50 index_type extent
[GFC_MAX_DIMENSIONS
];
51 index_type sstride
[GFC_MAX_DIMENSIONS
];
52 index_type dstride
[GFC_MAX_DIMENSIONS
];
53 const GFC_REAL_4
* restrict base
;
54 GFC_INTEGER_16
* restrict dest
;
62 /* Make dim zero based to avoid confusion. */
64 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
66 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
69 delta
= array
->dim
[dim
].stride
;
71 for (n
= 0; n
< dim
; n
++)
73 sstride
[n
] = array
->dim
[n
].stride
;
74 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
79 for (n
= dim
; n
< rank
; n
++)
81 sstride
[n
] = array
->dim
[n
+ 1].stride
;
83 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
89 if (retarray
->data
== NULL
)
93 for (n
= 0; n
< rank
; n
++)
95 retarray
->dim
[n
].lbound
= 0;
96 retarray
->dim
[n
].ubound
= extent
[n
]-1;
98 retarray
->dim
[n
].stride
= 1;
100 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
103 retarray
->offset
= 0;
104 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
106 alloc_size
= sizeof (GFC_INTEGER_16
) * retarray
->dim
[rank
-1].stride
111 /* Make sure we have a zero-sized array. */
112 retarray
->dim
[0].lbound
= 0;
113 retarray
->dim
[0].ubound
= -1;
117 retarray
->data
= internal_malloc_size (alloc_size
);
121 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
122 runtime_error ("rank of return array incorrect in"
123 " MINLOC intrinsic: is %ld, should be %ld",
124 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
127 if (compile_options
.bounds_check
)
129 for (n
=0; n
< rank
; n
++)
131 index_type ret_extent
;
133 ret_extent
= retarray
->dim
[n
].ubound
+ 1
134 - retarray
->dim
[n
].lbound
;
135 if (extent
[n
] != ret_extent
)
136 runtime_error ("Incorrect extent in return value of"
137 " MINLOC intrinsic in dimension %ld:"
138 " is %ld, should be %ld", (long int) n
+ 1,
139 (long int) ret_extent
, (long int) extent
[n
]);
144 for (n
= 0; n
< rank
; n
++)
147 dstride
[n
] = retarray
->dim
[n
].stride
;
153 dest
= retarray
->data
;
156 while (continue_loop
)
158 const GFC_REAL_4
* restrict src
;
159 GFC_INTEGER_16 result
;
164 minval
= GFC_REAL_4_HUGE
;
170 for (n
= 0; n
< len
; n
++, src
+= delta
)
173 if (*src
< minval
|| !result
)
176 result
= (GFC_INTEGER_16
)n
+ 1;
182 /* Advance to the next element. */
187 while (count
[n
] == extent
[n
])
189 /* When we get to the end of a dimension, reset it and increment
190 the next dimension. */
192 /* We could precalculate these products, but this is a less
193 frequently used path so probably not worth it. */
194 base
-= sstride
[n
] * extent
[n
];
195 dest
-= dstride
[n
] * extent
[n
];
199 /* Break out of the look. */
214 extern void mminloc1_16_r4 (gfc_array_i16
* const restrict
,
215 gfc_array_r4
* const restrict
, const index_type
* const restrict
,
216 gfc_array_l1
* const restrict
);
217 export_proto(mminloc1_16_r4
);
220 mminloc1_16_r4 (gfc_array_i16
* const restrict retarray
,
221 gfc_array_r4
* const restrict array
,
222 const index_type
* const restrict pdim
,
223 gfc_array_l1
* const restrict mask
)
225 index_type count
[GFC_MAX_DIMENSIONS
];
226 index_type extent
[GFC_MAX_DIMENSIONS
];
227 index_type sstride
[GFC_MAX_DIMENSIONS
];
228 index_type dstride
[GFC_MAX_DIMENSIONS
];
229 index_type mstride
[GFC_MAX_DIMENSIONS
];
230 GFC_INTEGER_16
* restrict dest
;
231 const GFC_REAL_4
* restrict base
;
232 const GFC_LOGICAL_1
* restrict mbase
;
242 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
244 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
250 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
252 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
253 #ifdef HAVE_GFC_LOGICAL_16
257 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
259 runtime_error ("Funny sized logical array");
261 delta
= array
->dim
[dim
].stride
;
262 mdelta
= mask
->dim
[dim
].stride
* mask_kind
;
264 for (n
= 0; n
< dim
; n
++)
266 sstride
[n
] = array
->dim
[n
].stride
;
267 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
268 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
274 for (n
= dim
; n
< rank
; n
++)
276 sstride
[n
] = array
->dim
[n
+ 1].stride
;
277 mstride
[n
] = mask
->dim
[n
+ 1].stride
* mask_kind
;
279 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
285 if (retarray
->data
== NULL
)
289 for (n
= 0; n
< rank
; n
++)
291 retarray
->dim
[n
].lbound
= 0;
292 retarray
->dim
[n
].ubound
= extent
[n
]-1;
294 retarray
->dim
[n
].stride
= 1;
296 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
299 alloc_size
= sizeof (GFC_INTEGER_16
) * retarray
->dim
[rank
-1].stride
302 retarray
->offset
= 0;
303 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
307 /* Make sure we have a zero-sized array. */
308 retarray
->dim
[0].lbound
= 0;
309 retarray
->dim
[0].ubound
= -1;
313 retarray
->data
= internal_malloc_size (alloc_size
);
318 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
319 runtime_error ("rank of return array incorrect in MINLOC intrinsic");
321 if (compile_options
.bounds_check
)
323 for (n
=0; n
< rank
; n
++)
325 index_type ret_extent
;
327 ret_extent
= retarray
->dim
[n
].ubound
+ 1
328 - retarray
->dim
[n
].lbound
;
329 if (extent
[n
] != ret_extent
)
330 runtime_error ("Incorrect extent in return value of"
331 " MINLOC intrinsic in dimension %ld:"
332 " is %ld, should be %ld", (long int) n
+ 1,
333 (long int) ret_extent
, (long int) extent
[n
]);
335 for (n
=0; n
<= rank
; n
++)
337 index_type mask_extent
, array_extent
;
339 array_extent
= array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
340 mask_extent
= mask
->dim
[n
].ubound
+ 1 - mask
->dim
[n
].lbound
;
341 if (array_extent
!= mask_extent
)
342 runtime_error ("Incorrect extent in MASK argument of"
343 " MINLOC intrinsic in dimension %ld:"
344 " is %ld, should be %ld", (long int) n
+ 1,
345 (long int) mask_extent
, (long int) array_extent
);
350 for (n
= 0; n
< rank
; n
++)
353 dstride
[n
] = retarray
->dim
[n
].stride
;
358 dest
= retarray
->data
;
363 const GFC_REAL_4
* restrict src
;
364 const GFC_LOGICAL_1
* restrict msrc
;
365 GFC_INTEGER_16 result
;
371 minval
= GFC_REAL_4_HUGE
;
377 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
380 if (*msrc
&& (*src
< minval
|| !result
))
383 result
= (GFC_INTEGER_16
)n
+ 1;
389 /* Advance to the next element. */
395 while (count
[n
] == extent
[n
])
397 /* When we get to the end of a dimension, reset it and increment
398 the next dimension. */
400 /* We could precalculate these products, but this is a less
401 frequently used path so probably not worth it. */
402 base
-= sstride
[n
] * extent
[n
];
403 mbase
-= mstride
[n
] * extent
[n
];
404 dest
-= dstride
[n
] * extent
[n
];
408 /* Break out of the look. */
424 extern void sminloc1_16_r4 (gfc_array_i16
* const restrict
,
425 gfc_array_r4
* const restrict
, const index_type
* const restrict
,
427 export_proto(sminloc1_16_r4
);
430 sminloc1_16_r4 (gfc_array_i16
* const restrict retarray
,
431 gfc_array_r4
* const restrict array
,
432 const index_type
* const restrict pdim
,
433 GFC_LOGICAL_4
* mask
)
435 index_type count
[GFC_MAX_DIMENSIONS
];
436 index_type extent
[GFC_MAX_DIMENSIONS
];
437 index_type sstride
[GFC_MAX_DIMENSIONS
];
438 index_type dstride
[GFC_MAX_DIMENSIONS
];
439 GFC_INTEGER_16
* restrict dest
;
447 minloc1_16_r4 (retarray
, array
, pdim
);
450 /* Make dim zero based to avoid confusion. */
452 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
454 for (n
= 0; n
< dim
; n
++)
456 sstride
[n
] = array
->dim
[n
].stride
;
457 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
463 for (n
= dim
; n
< rank
; n
++)
465 sstride
[n
] = array
->dim
[n
+ 1].stride
;
467 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
473 if (retarray
->data
== NULL
)
477 for (n
= 0; n
< rank
; n
++)
479 retarray
->dim
[n
].lbound
= 0;
480 retarray
->dim
[n
].ubound
= extent
[n
]-1;
482 retarray
->dim
[n
].stride
= 1;
484 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
487 retarray
->offset
= 0;
488 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
490 alloc_size
= sizeof (GFC_INTEGER_16
) * retarray
->dim
[rank
-1].stride
495 /* Make sure we have a zero-sized array. */
496 retarray
->dim
[0].lbound
= 0;
497 retarray
->dim
[0].ubound
= -1;
501 retarray
->data
= internal_malloc_size (alloc_size
);
505 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
506 runtime_error ("rank of return array incorrect in"
507 " MINLOC intrinsic: is %ld, should be %ld",
508 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
511 if (compile_options
.bounds_check
)
513 for (n
=0; n
< rank
; n
++)
515 index_type ret_extent
;
517 ret_extent
= retarray
->dim
[n
].ubound
+ 1
518 - retarray
->dim
[n
].lbound
;
519 if (extent
[n
] != ret_extent
)
520 runtime_error ("Incorrect extent in return value of"
521 " MINLOC intrinsic in dimension %ld:"
522 " is %ld, should be %ld", (long int) n
+ 1,
523 (long int) ret_extent
, (long int) extent
[n
]);
528 for (n
= 0; n
< rank
; n
++)
531 dstride
[n
] = retarray
->dim
[n
].stride
;
534 dest
= retarray
->data
;
542 while (count
[n
] == extent
[n
])
544 /* When we get to the end of a dimension, reset it and increment
545 the next dimension. */
547 /* We could precalculate these products, but this is a less
548 frequently used path so probably not worth it. */
549 dest
-= dstride
[n
] * extent
[n
];