1 /* Implementation of the MINVAL 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_INTEGER_1) && defined (HAVE_GFC_INTEGER_1)
34 extern void minval_i1 (gfc_array_i1
* const restrict
,
35 gfc_array_i1
* const restrict
, const index_type
* const restrict
);
36 export_proto(minval_i1
);
39 minval_i1 (gfc_array_i1
* const restrict retarray
,
40 gfc_array_i1
* 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_INTEGER_1
* restrict base
;
48 GFC_INTEGER_1
* restrict dest
;
56 /* Make dim zero based to avoid confusion. */
58 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
60 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
63 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
65 for (n
= 0; n
< dim
; n
++)
67 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
68 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
73 for (n
= dim
; n
< rank
; n
++)
75 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
76 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
82 if (retarray
->data
== NULL
)
84 size_t alloc_size
, str
;
86 for (n
= 0; n
< rank
; n
++)
91 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
93 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
98 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
100 alloc_size
= sizeof (GFC_INTEGER_1
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
105 /* Make sure we have a zero-sized array. */
106 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 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 " MINVAL 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
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
128 if (extent
[n
] != ret_extent
)
129 runtime_error ("Incorrect extent in return value of"
130 " MINVAL intrinsic in dimension %ld:"
131 " is %ld, should be %ld", (long int) n
+ 1,
132 (long int) ret_extent
, (long int) extent
[n
]);
137 for (n
= 0; n
< rank
; n
++)
140 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
146 dest
= retarray
->data
;
149 while (continue_loop
)
151 const GFC_INTEGER_1
* restrict src
;
152 GFC_INTEGER_1 result
;
156 result
= GFC_INTEGER_1_HUGE
;
158 *dest
= GFC_INTEGER_1_HUGE
;
161 for (n
= 0; n
< len
; n
++, src
+= delta
)
170 /* Advance to the next element. */
175 while (count
[n
] == extent
[n
])
177 /* When we get to the end of a dimension, reset it and increment
178 the next dimension. */
180 /* We could precalculate these products, but this is a less
181 frequently used path so probably not worth it. */
182 base
-= sstride
[n
] * extent
[n
];
183 dest
-= dstride
[n
] * extent
[n
];
187 /* Break out of the look. */
202 extern void mminval_i1 (gfc_array_i1
* const restrict
,
203 gfc_array_i1
* const restrict
, const index_type
* const restrict
,
204 gfc_array_l1
* const restrict
);
205 export_proto(mminval_i1
);
208 mminval_i1 (gfc_array_i1
* const restrict retarray
,
209 gfc_array_i1
* const restrict array
,
210 const index_type
* const restrict pdim
,
211 gfc_array_l1
* const restrict mask
)
213 index_type count
[GFC_MAX_DIMENSIONS
];
214 index_type extent
[GFC_MAX_DIMENSIONS
];
215 index_type sstride
[GFC_MAX_DIMENSIONS
];
216 index_type dstride
[GFC_MAX_DIMENSIONS
];
217 index_type mstride
[GFC_MAX_DIMENSIONS
];
218 GFC_INTEGER_1
* restrict dest
;
219 const GFC_INTEGER_1
* restrict base
;
220 const GFC_LOGICAL_1
* restrict mbase
;
230 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
232 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
238 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
240 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
241 #ifdef HAVE_GFC_LOGICAL_16
245 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
247 runtime_error ("Funny sized logical array");
249 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
250 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
252 for (n
= 0; n
< dim
; n
++)
254 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
255 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
256 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
262 for (n
= dim
; n
< rank
; n
++)
264 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
265 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
266 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
272 if (retarray
->data
== NULL
)
274 size_t alloc_size
, str
;
276 for (n
= 0; n
< rank
; n
++)
281 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
283 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
287 alloc_size
= sizeof (GFC_INTEGER_1
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
290 retarray
->offset
= 0;
291 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
295 /* Make sure we have a zero-sized array. */
296 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
300 retarray
->data
= internal_malloc_size (alloc_size
);
305 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
306 runtime_error ("rank of return array incorrect in MINVAL intrinsic");
308 if (unlikely (compile_options
.bounds_check
))
310 for (n
=0; n
< rank
; n
++)
312 index_type ret_extent
;
314 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
315 if (extent
[n
] != ret_extent
)
316 runtime_error ("Incorrect extent in return value of"
317 " MINVAL intrinsic in dimension %ld:"
318 " is %ld, should be %ld", (long int) n
+ 1,
319 (long int) ret_extent
, (long int) extent
[n
]);
321 for (n
=0; n
<= rank
; n
++)
323 index_type mask_extent
, array_extent
;
325 array_extent
= GFC_DESCRIPTOR_EXTENT(array
,n
);
326 mask_extent
= GFC_DESCRIPTOR_EXTENT(mask
,n
);
327 if (array_extent
!= mask_extent
)
328 runtime_error ("Incorrect extent in MASK argument of"
329 " MINVAL intrinsic in dimension %ld:"
330 " is %ld, should be %ld", (long int) n
+ 1,
331 (long int) mask_extent
, (long int) array_extent
);
336 for (n
= 0; n
< rank
; n
++)
339 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
344 dest
= retarray
->data
;
349 const GFC_INTEGER_1
* restrict src
;
350 const GFC_LOGICAL_1
* restrict msrc
;
351 GFC_INTEGER_1 result
;
356 result
= GFC_INTEGER_1_HUGE
;
358 *dest
= GFC_INTEGER_1_HUGE
;
361 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
364 if (*msrc
&& *src
< result
)
370 /* Advance to the next element. */
376 while (count
[n
] == extent
[n
])
378 /* When we get to the end of a dimension, reset it and increment
379 the next dimension. */
381 /* We could precalculate these products, but this is a less
382 frequently used path so probably not worth it. */
383 base
-= sstride
[n
] * extent
[n
];
384 mbase
-= mstride
[n
] * extent
[n
];
385 dest
-= dstride
[n
] * extent
[n
];
389 /* Break out of the look. */
405 extern void sminval_i1 (gfc_array_i1
* const restrict
,
406 gfc_array_i1
* const restrict
, const index_type
* const restrict
,
408 export_proto(sminval_i1
);
411 sminval_i1 (gfc_array_i1
* const restrict retarray
,
412 gfc_array_i1
* const restrict array
,
413 const index_type
* const restrict pdim
,
414 GFC_LOGICAL_4
* mask
)
416 index_type count
[GFC_MAX_DIMENSIONS
];
417 index_type extent
[GFC_MAX_DIMENSIONS
];
418 index_type sstride
[GFC_MAX_DIMENSIONS
];
419 index_type dstride
[GFC_MAX_DIMENSIONS
];
420 GFC_INTEGER_1
* restrict dest
;
428 minval_i1 (retarray
, array
, pdim
);
431 /* Make dim zero based to avoid confusion. */
433 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
435 for (n
= 0; n
< dim
; n
++)
437 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
438 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
444 for (n
= dim
; n
< rank
; n
++)
446 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
448 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
454 if (retarray
->data
== NULL
)
456 size_t alloc_size
, str
;
458 for (n
= 0; n
< rank
; n
++)
463 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
465 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
469 retarray
->offset
= 0;
470 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
472 alloc_size
= sizeof (GFC_INTEGER_1
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
477 /* Make sure we have a zero-sized array. */
478 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
482 retarray
->data
= internal_malloc_size (alloc_size
);
486 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
487 runtime_error ("rank of return array incorrect in"
488 " MINVAL intrinsic: is %ld, should be %ld",
489 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
492 if (unlikely (compile_options
.bounds_check
))
494 for (n
=0; n
< rank
; n
++)
496 index_type ret_extent
;
498 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
499 if (extent
[n
] != ret_extent
)
500 runtime_error ("Incorrect extent in return value of"
501 " MINVAL intrinsic in dimension %ld:"
502 " is %ld, should be %ld", (long int) n
+ 1,
503 (long int) ret_extent
, (long int) extent
[n
]);
508 for (n
= 0; n
< rank
; n
++)
511 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
514 dest
= retarray
->data
;
518 *dest
= GFC_INTEGER_1_HUGE
;
522 while (count
[n
] == extent
[n
])
524 /* When we get to the end of a dimension, reset it and increment
525 the next dimension. */
527 /* We could precalculate these products, but this is a less
528 frequently used path so probably not worth it. */
529 dest
-= dstride
[n
] * extent
[n
];