]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/generated/maxloc1_4_i1.c
1 /* Implementation of the MAXLOC 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_1) && defined (HAVE_GFC_INTEGER_4)
35 extern void maxloc1_4_i1 (gfc_array_i4
* const restrict
,
36 gfc_array_i1
* const restrict
, const index_type
* const restrict
);
37 export_proto(maxloc1_4_i1
);
40 maxloc1_4_i1 (gfc_array_i4
* const restrict retarray
,
41 gfc_array_i1
* 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_1
* 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 " MAXLOC intrinsic: is %ld, should be %ld",
119 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
122 if (unlikely (compile_options
.bounds_check
))
123 bounds_ifunction_return ((array_t
*) retarray
, extent
,
124 "return value", "MAXLOC");
127 for (n
= 0; n
< rank
; n
++)
130 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
136 dest
= retarray
->data
;
139 while (continue_loop
)
141 const GFC_INTEGER_1
* restrict src
;
142 GFC_INTEGER_4 result
;
146 GFC_INTEGER_1 maxval
;
147 maxval
= (-GFC_INTEGER_1_HUGE
-1);
153 for (n
= 0; n
< len
; n
++, src
+= delta
)
156 if (*src
> maxval
|| !result
)
159 result
= (GFC_INTEGER_4
)n
+ 1;
165 /* Advance to the next element. */
170 while (count
[n
] == extent
[n
])
172 /* When we get to the end of a dimension, reset it and increment
173 the next dimension. */
175 /* We could precalculate these products, but this is a less
176 frequently used path so probably not worth it. */
177 base
-= sstride
[n
] * extent
[n
];
178 dest
-= dstride
[n
] * extent
[n
];
182 /* Break out of the look. */
197 extern void mmaxloc1_4_i1 (gfc_array_i4
* const restrict
,
198 gfc_array_i1
* const restrict
, const index_type
* const restrict
,
199 gfc_array_l1
* const restrict
);
200 export_proto(mmaxloc1_4_i1
);
203 mmaxloc1_4_i1 (gfc_array_i4
* const restrict retarray
,
204 gfc_array_i1
* const restrict array
,
205 const index_type
* const restrict pdim
,
206 gfc_array_l1
* const restrict mask
)
208 index_type count
[GFC_MAX_DIMENSIONS
];
209 index_type extent
[GFC_MAX_DIMENSIONS
];
210 index_type sstride
[GFC_MAX_DIMENSIONS
];
211 index_type dstride
[GFC_MAX_DIMENSIONS
];
212 index_type mstride
[GFC_MAX_DIMENSIONS
];
213 GFC_INTEGER_4
* restrict dest
;
214 const GFC_INTEGER_1
* restrict base
;
215 const GFC_LOGICAL_1
* restrict mbase
;
225 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
227 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
233 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
235 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
236 #ifdef HAVE_GFC_LOGICAL_16
240 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
242 runtime_error ("Funny sized logical array");
244 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
245 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
247 for (n
= 0; n
< dim
; n
++)
249 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
250 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
251 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
257 for (n
= dim
; n
< rank
; n
++)
259 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
260 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
261 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
267 if (retarray
->data
== NULL
)
269 size_t alloc_size
, str
;
271 for (n
= 0; n
< rank
; n
++)
276 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
278 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
282 alloc_size
= sizeof (GFC_INTEGER_4
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
285 retarray
->offset
= 0;
286 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
290 /* Make sure we have a zero-sized array. */
291 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
295 retarray
->data
= internal_malloc_size (alloc_size
);
300 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
301 runtime_error ("rank of return array incorrect in MAXLOC intrinsic");
303 if (unlikely (compile_options
.bounds_check
))
305 bounds_ifunction_return ((array_t
*) retarray
, extent
,
306 "return value", "MAXLOC");
307 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
308 "MASK argument", "MAXLOC");
312 for (n
= 0; n
< rank
; n
++)
315 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
320 dest
= retarray
->data
;
325 const GFC_INTEGER_1
* restrict src
;
326 const GFC_LOGICAL_1
* restrict msrc
;
327 GFC_INTEGER_4 result
;
332 GFC_INTEGER_1 maxval
;
333 maxval
= (-GFC_INTEGER_1_HUGE
-1);
339 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
342 if (*msrc
&& (*src
> maxval
|| !result
))
345 result
= (GFC_INTEGER_4
)n
+ 1;
351 /* Advance to the next element. */
357 while (count
[n
] == extent
[n
])
359 /* When we get to the end of a dimension, reset it and increment
360 the next dimension. */
362 /* We could precalculate these products, but this is a less
363 frequently used path so probably not worth it. */
364 base
-= sstride
[n
] * extent
[n
];
365 mbase
-= mstride
[n
] * extent
[n
];
366 dest
-= dstride
[n
] * extent
[n
];
370 /* Break out of the look. */
386 extern void smaxloc1_4_i1 (gfc_array_i4
* const restrict
,
387 gfc_array_i1
* const restrict
, const index_type
* const restrict
,
389 export_proto(smaxloc1_4_i1
);
392 smaxloc1_4_i1 (gfc_array_i4
* const restrict retarray
,
393 gfc_array_i1
* const restrict array
,
394 const index_type
* const restrict pdim
,
395 GFC_LOGICAL_4
* mask
)
397 index_type count
[GFC_MAX_DIMENSIONS
];
398 index_type extent
[GFC_MAX_DIMENSIONS
];
399 index_type sstride
[GFC_MAX_DIMENSIONS
];
400 index_type dstride
[GFC_MAX_DIMENSIONS
];
401 GFC_INTEGER_4
* restrict dest
;
409 maxloc1_4_i1 (retarray
, array
, pdim
);
412 /* Make dim zero based to avoid confusion. */
414 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
416 for (n
= 0; n
< dim
; n
++)
418 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
419 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
425 for (n
= dim
; n
< rank
; n
++)
427 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
429 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
435 if (retarray
->data
== NULL
)
437 size_t alloc_size
, str
;
439 for (n
= 0; n
< rank
; n
++)
444 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
446 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
450 retarray
->offset
= 0;
451 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
453 alloc_size
= sizeof (GFC_INTEGER_4
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
458 /* Make sure we have a zero-sized array. */
459 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
463 retarray
->data
= internal_malloc_size (alloc_size
);
467 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
468 runtime_error ("rank of return array incorrect in"
469 " MAXLOC intrinsic: is %ld, should be %ld",
470 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
473 if (unlikely (compile_options
.bounds_check
))
475 for (n
=0; n
< rank
; n
++)
477 index_type ret_extent
;
479 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
480 if (extent
[n
] != ret_extent
)
481 runtime_error ("Incorrect extent in return value of"
482 " MAXLOC intrinsic in dimension %ld:"
483 " is %ld, should be %ld", (long int) n
+ 1,
484 (long int) ret_extent
, (long int) extent
[n
]);
489 for (n
= 0; n
< rank
; n
++)
492 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
495 dest
= retarray
->data
;
503 while (count
[n
] == extent
[n
])
505 /* When we get to the end of a dimension, reset it and increment
506 the next dimension. */
508 /* We could precalculate these products, but this is a less
509 frequently used path so probably not worth it. */
510 dest
-= dstride
[n
] * extent
[n
];