1 /* Implementation of the MINLOC 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"
32 #if defined (HAVE_GFC_INTEGER_8) && defined (HAVE_GFC_INTEGER_4)
35 extern void minloc1_4_i8 (gfc_array_i4
* const restrict
,
36 gfc_array_i8
* const restrict
, const index_type
* const restrict
);
37 export_proto(minloc1_4_i8
);
40 minloc1_4_i8 (gfc_array_i4
* const restrict retarray
,
41 gfc_array_i8
* 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_8
* restrict base
;
49 GFC_INTEGER_4
* 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_4
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
106 /* Make sure we have a zero-sized array. */
107 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
112 retarray
->data
= internal_malloc_size (alloc_size
);
116 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
117 runtime_error ("rank of return array incorrect in"
118 " MINLOC intrinsic: is %ld, should be %ld",
119 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
122 if (unlikely (compile_options
.bounds_check
))
124 for (n
=0; n
< rank
; n
++)
126 index_type ret_extent
;
128 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
129 if (extent
[n
] != ret_extent
)
130 runtime_error ("Incorrect extent in return value of"
131 " MINLOC 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
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
147 dest
= retarray
->data
;
150 while (continue_loop
)
152 const GFC_INTEGER_8
* restrict src
;
153 GFC_INTEGER_4 result
;
157 GFC_INTEGER_8 minval
;
158 minval
= GFC_INTEGER_8_HUGE
;
164 for (n
= 0; n
< len
; n
++, src
+= delta
)
167 if (*src
< minval
|| !result
)
170 result
= (GFC_INTEGER_4
)n
+ 1;
176 /* Advance to the next element. */
181 while (count
[n
] == extent
[n
])
183 /* When we get to the end of a dimension, reset it and increment
184 the next dimension. */
186 /* We could precalculate these products, but this is a less
187 frequently used path so probably not worth it. */
188 base
-= sstride
[n
] * extent
[n
];
189 dest
-= dstride
[n
] * extent
[n
];
193 /* Break out of the look. */
208 extern void mminloc1_4_i8 (gfc_array_i4
* const restrict
,
209 gfc_array_i8
* const restrict
, const index_type
* const restrict
,
210 gfc_array_l1
* const restrict
);
211 export_proto(mminloc1_4_i8
);
214 mminloc1_4_i8 (gfc_array_i4
* const restrict retarray
,
215 gfc_array_i8
* const restrict array
,
216 const index_type
* const restrict pdim
,
217 gfc_array_l1
* const restrict mask
)
219 index_type count
[GFC_MAX_DIMENSIONS
];
220 index_type extent
[GFC_MAX_DIMENSIONS
];
221 index_type sstride
[GFC_MAX_DIMENSIONS
];
222 index_type dstride
[GFC_MAX_DIMENSIONS
];
223 index_type mstride
[GFC_MAX_DIMENSIONS
];
224 GFC_INTEGER_4
* restrict dest
;
225 const GFC_INTEGER_8
* restrict base
;
226 const GFC_LOGICAL_1
* restrict mbase
;
236 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
238 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
244 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
246 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
247 #ifdef HAVE_GFC_LOGICAL_16
251 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
253 runtime_error ("Funny sized logical array");
255 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
256 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
258 for (n
= 0; n
< dim
; n
++)
260 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
261 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
262 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
268 for (n
= dim
; n
< rank
; n
++)
270 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
271 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
272 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
278 if (retarray
->data
== NULL
)
280 size_t alloc_size
, str
;
282 for (n
= 0; n
< rank
; n
++)
287 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
289 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
293 alloc_size
= sizeof (GFC_INTEGER_4
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
296 retarray
->offset
= 0;
297 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
301 /* Make sure we have a zero-sized array. */
302 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
306 retarray
->data
= internal_malloc_size (alloc_size
);
311 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
312 runtime_error ("rank of return array incorrect in MINLOC intrinsic");
314 if (unlikely (compile_options
.bounds_check
))
316 for (n
=0; n
< rank
; n
++)
318 index_type ret_extent
;
320 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
321 if (extent
[n
] != ret_extent
)
322 runtime_error ("Incorrect extent in return value of"
323 " MINLOC intrinsic in dimension %ld:"
324 " is %ld, should be %ld", (long int) n
+ 1,
325 (long int) ret_extent
, (long int) extent
[n
]);
327 for (n
=0; n
<= rank
; n
++)
329 index_type mask_extent
, array_extent
;
331 array_extent
= GFC_DESCRIPTOR_EXTENT(array
,n
);
332 mask_extent
= GFC_DESCRIPTOR_EXTENT(mask
,n
);
333 if (array_extent
!= mask_extent
)
334 runtime_error ("Incorrect extent in MASK argument of"
335 " MINLOC intrinsic in dimension %ld:"
336 " is %ld, should be %ld", (long int) n
+ 1,
337 (long int) mask_extent
, (long int) array_extent
);
342 for (n
= 0; n
< rank
; n
++)
345 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
350 dest
= retarray
->data
;
355 const GFC_INTEGER_8
* restrict src
;
356 const GFC_LOGICAL_1
* restrict msrc
;
357 GFC_INTEGER_4 result
;
362 GFC_INTEGER_8 minval
;
363 minval
= GFC_INTEGER_8_HUGE
;
369 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
372 if (*msrc
&& (*src
< minval
|| !result
))
375 result
= (GFC_INTEGER_4
)n
+ 1;
381 /* Advance to the next element. */
387 while (count
[n
] == extent
[n
])
389 /* When we get to the end of a dimension, reset it and increment
390 the next dimension. */
392 /* We could precalculate these products, but this is a less
393 frequently used path so probably not worth it. */
394 base
-= sstride
[n
] * extent
[n
];
395 mbase
-= mstride
[n
] * extent
[n
];
396 dest
-= dstride
[n
] * extent
[n
];
400 /* Break out of the look. */
416 extern void sminloc1_4_i8 (gfc_array_i4
* const restrict
,
417 gfc_array_i8
* const restrict
, const index_type
* const restrict
,
419 export_proto(sminloc1_4_i8
);
422 sminloc1_4_i8 (gfc_array_i4
* const restrict retarray
,
423 gfc_array_i8
* const restrict array
,
424 const index_type
* const restrict pdim
,
425 GFC_LOGICAL_4
* mask
)
427 index_type count
[GFC_MAX_DIMENSIONS
];
428 index_type extent
[GFC_MAX_DIMENSIONS
];
429 index_type sstride
[GFC_MAX_DIMENSIONS
];
430 index_type dstride
[GFC_MAX_DIMENSIONS
];
431 GFC_INTEGER_4
* restrict dest
;
439 minloc1_4_i8 (retarray
, array
, pdim
);
442 /* Make dim zero based to avoid confusion. */
444 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
446 for (n
= 0; n
< dim
; n
++)
448 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
449 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
455 for (n
= dim
; n
< rank
; n
++)
457 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
459 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
465 if (retarray
->data
== NULL
)
467 size_t alloc_size
, str
;
469 for (n
= 0; n
< rank
; n
++)
474 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
476 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
480 retarray
->offset
= 0;
481 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
483 alloc_size
= sizeof (GFC_INTEGER_4
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
488 /* Make sure we have a zero-sized array. */
489 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
493 retarray
->data
= internal_malloc_size (alloc_size
);
497 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
498 runtime_error ("rank of return array incorrect in"
499 " MINLOC intrinsic: is %ld, should be %ld",
500 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
503 if (unlikely (compile_options
.bounds_check
))
505 for (n
=0; n
< rank
; n
++)
507 index_type ret_extent
;
509 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
510 if (extent
[n
] != ret_extent
)
511 runtime_error ("Incorrect extent in return value of"
512 " MINLOC intrinsic in dimension %ld:"
513 " is %ld, should be %ld", (long int) n
+ 1,
514 (long int) ret_extent
, (long int) extent
[n
]);
519 for (n
= 0; n
< rank
; n
++)
522 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
525 dest
= retarray
->data
;
533 while (count
[n
] == extent
[n
])
535 /* When we get to the end of a dimension, reset it and increment
536 the next dimension. */
538 /* We could precalculate these products, but this is a less
539 frequently used path so probably not worth it. */
540 dest
-= dstride
[n
] * extent
[n
];