1 /* Implementation of the MINLOC intrinsic
2 Copyright 2002, 2007, 2009, 2010 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"
32 #if defined (HAVE_GFC_INTEGER_16) && defined (HAVE_GFC_INTEGER_16)
35 extern void minloc1_16_i16 (gfc_array_i16
* const restrict
,
36 gfc_array_i16
* const restrict
, const index_type
* const restrict
);
37 export_proto(minloc1_16_i16
);
40 minloc1_16_i16 (gfc_array_i16
* const restrict retarray
,
41 gfc_array_i16
* const restrict array
,
42 const index_type
* const restrict pdim
)
44 index_type count
[GFC_MAX_DIMENSIONS
];
45 index_type extent
[GFC_MAX_DIMENSIONS
];
46 index_type sstride
[GFC_MAX_DIMENSIONS
];
47 index_type dstride
[GFC_MAX_DIMENSIONS
];
48 const GFC_INTEGER_16
* restrict base
;
49 GFC_INTEGER_16
* restrict dest
;
57 /* Make dim zero based to avoid confusion. */
59 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
61 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
64 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
66 for (n
= 0; n
< dim
; n
++)
68 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
69 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
74 for (n
= dim
; n
< rank
; n
++)
76 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
77 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
83 if (retarray
->data
== NULL
)
85 size_t alloc_size
, str
;
87 for (n
= 0; n
< rank
; n
++)
92 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
94 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
99 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
101 alloc_size
= sizeof (GFC_INTEGER_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
104 retarray
->data
= internal_malloc_size (alloc_size
);
107 /* Make sure we have a zero-sized array. */
108 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
115 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
116 runtime_error ("rank of return array incorrect in"
117 " MINLOC intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
121 if (unlikely (compile_options
.bounds_check
))
122 bounds_ifunction_return ((array_t
*) retarray
, extent
,
123 "return value", "MINLOC");
126 for (n
= 0; n
< rank
; n
++)
129 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
135 dest
= retarray
->data
;
138 while (continue_loop
)
140 const GFC_INTEGER_16
* restrict src
;
141 GFC_INTEGER_16 result
;
145 GFC_INTEGER_16 minval
;
146 #if defined (GFC_INTEGER_16_INFINITY)
147 minval
= GFC_INTEGER_16_INFINITY
;
149 minval
= GFC_INTEGER_16_HUGE
;
156 for (n
= 0; n
< len
; n
++, src
+= delta
)
159 #if defined (GFC_INTEGER_16_QUIET_NAN)
163 result
= (GFC_INTEGER_16
)n
+ 1;
167 for (; n
< len
; n
++, src
+= delta
)
173 result
= (GFC_INTEGER_16
)n
+ 1;
180 /* Advance to the next element. */
185 while (count
[n
] == extent
[n
])
187 /* When we get to the end of a dimension, reset it and increment
188 the next dimension. */
190 /* We could precalculate these products, but this is a less
191 frequently used path so probably not worth it. */
192 base
-= sstride
[n
] * extent
[n
];
193 dest
-= dstride
[n
] * extent
[n
];
197 /* Break out of the look. */
212 extern void mminloc1_16_i16 (gfc_array_i16
* const restrict
,
213 gfc_array_i16
* const restrict
, const index_type
* const restrict
,
214 gfc_array_l1
* const restrict
);
215 export_proto(mminloc1_16_i16
);
218 mminloc1_16_i16 (gfc_array_i16
* const restrict retarray
,
219 gfc_array_i16
* const restrict array
,
220 const index_type
* const restrict pdim
,
221 gfc_array_l1
* const restrict mask
)
223 index_type count
[GFC_MAX_DIMENSIONS
];
224 index_type extent
[GFC_MAX_DIMENSIONS
];
225 index_type sstride
[GFC_MAX_DIMENSIONS
];
226 index_type dstride
[GFC_MAX_DIMENSIONS
];
227 index_type mstride
[GFC_MAX_DIMENSIONS
];
228 GFC_INTEGER_16
* restrict dest
;
229 const GFC_INTEGER_16
* restrict base
;
230 const GFC_LOGICAL_1
* restrict mbase
;
240 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
242 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
248 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
250 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
251 #ifdef HAVE_GFC_LOGICAL_16
255 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
257 runtime_error ("Funny sized logical array");
259 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
260 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
262 for (n
= 0; n
< dim
; n
++)
264 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
265 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
266 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
272 for (n
= dim
; n
< rank
; n
++)
274 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
275 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
276 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
282 if (retarray
->data
== NULL
)
284 size_t alloc_size
, str
;
286 for (n
= 0; n
< rank
; n
++)
291 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
293 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
297 alloc_size
= sizeof (GFC_INTEGER_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
300 retarray
->offset
= 0;
301 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
305 /* Make sure we have a zero-sized array. */
306 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
310 retarray
->data
= internal_malloc_size (alloc_size
);
315 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
316 runtime_error ("rank of return array incorrect in MINLOC intrinsic");
318 if (unlikely (compile_options
.bounds_check
))
320 bounds_ifunction_return ((array_t
*) retarray
, extent
,
321 "return value", "MINLOC");
322 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
323 "MASK argument", "MINLOC");
327 for (n
= 0; n
< rank
; n
++)
330 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
335 dest
= retarray
->data
;
340 const GFC_INTEGER_16
* restrict src
;
341 const GFC_LOGICAL_1
* restrict msrc
;
342 GFC_INTEGER_16 result
;
347 GFC_INTEGER_16 minval
;
348 #if defined (GFC_INTEGER_16_INFINITY)
349 minval
= GFC_INTEGER_16_INFINITY
;
351 minval
= GFC_INTEGER_16_HUGE
;
353 #if defined (GFC_INTEGER_16_QUIET_NAN)
354 GFC_INTEGER_16 result2
= 0;
361 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
366 #if defined (GFC_INTEGER_16_QUIET_NAN)
368 result2
= (GFC_INTEGER_16
)n
+ 1;
373 result
= (GFC_INTEGER_16
)n
+ 1;
378 #if defined (GFC_INTEGER_16_QUIET_NAN)
379 if (unlikely (n
>= len
))
383 for (; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
385 if (*msrc
&& *src
< minval
)
388 result
= (GFC_INTEGER_16
)n
+ 1;
394 /* Advance to the next element. */
400 while (count
[n
] == extent
[n
])
402 /* When we get to the end of a dimension, reset it and increment
403 the next dimension. */
405 /* We could precalculate these products, but this is a less
406 frequently used path so probably not worth it. */
407 base
-= sstride
[n
] * extent
[n
];
408 mbase
-= mstride
[n
] * extent
[n
];
409 dest
-= dstride
[n
] * extent
[n
];
413 /* Break out of the look. */
429 extern void sminloc1_16_i16 (gfc_array_i16
* const restrict
,
430 gfc_array_i16
* const restrict
, const index_type
* const restrict
,
432 export_proto(sminloc1_16_i16
);
435 sminloc1_16_i16 (gfc_array_i16
* const restrict retarray
,
436 gfc_array_i16
* const restrict array
,
437 const index_type
* const restrict pdim
,
438 GFC_LOGICAL_4
* mask
)
440 index_type count
[GFC_MAX_DIMENSIONS
];
441 index_type extent
[GFC_MAX_DIMENSIONS
];
442 index_type dstride
[GFC_MAX_DIMENSIONS
];
443 GFC_INTEGER_16
* restrict dest
;
451 minloc1_16_i16 (retarray
, array
, pdim
);
454 /* Make dim zero based to avoid confusion. */
456 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
458 for (n
= 0; n
< dim
; n
++)
460 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
466 for (n
= dim
; n
< rank
; n
++)
469 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
475 if (retarray
->data
== NULL
)
477 size_t alloc_size
, str
;
479 for (n
= 0; n
< rank
; n
++)
484 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
486 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
490 retarray
->offset
= 0;
491 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
493 alloc_size
= sizeof (GFC_INTEGER_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
498 /* Make sure we have a zero-sized array. */
499 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
503 retarray
->data
= internal_malloc_size (alloc_size
);
507 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
508 runtime_error ("rank of return array incorrect in"
509 " MINLOC intrinsic: is %ld, should be %ld",
510 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
513 if (unlikely (compile_options
.bounds_check
))
515 for (n
=0; n
< rank
; n
++)
517 index_type ret_extent
;
519 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
520 if (extent
[n
] != ret_extent
)
521 runtime_error ("Incorrect extent in return value of"
522 " MINLOC intrinsic in dimension %ld:"
523 " is %ld, should be %ld", (long int) n
+ 1,
524 (long int) ret_extent
, (long int) extent
[n
]);
529 for (n
= 0; n
< rank
; n
++)
532 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
535 dest
= retarray
->data
;
543 while (count
[n
] == extent
[n
])
545 /* When we get to the end of a dimension, reset it and increment
546 the next dimension. */
548 /* We could precalculate these products, but this is a less
549 frequently used path so probably not worth it. */
550 dest
-= dstride
[n
] * extent
[n
];