1 /* Implementation of the MAXLOC intrinsic
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig
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"
33 #if defined (HAVE_GFC_INTEGER_1) && defined (HAVE_GFC_INTEGER_1)
36 compare_fcn (const GFC_INTEGER_1
*a
, const GFC_INTEGER_1
*b
, gfc_charlen_type n
)
38 if (sizeof (GFC_INTEGER_1
) == 1)
39 return memcmp (a
, b
, n
);
41 return memcmp_char4 (a
, b
, n
);
47 extern void maxval0_s1 (GFC_INTEGER_1
* restrict
,
49 gfc_array_s1
* const restrict array
, gfc_charlen_type
);
50 export_proto(maxval0_s1
);
53 maxval0_s1 (GFC_INTEGER_1
* restrict ret
,
54 gfc_charlen_type xlen
,
55 gfc_array_s1
* const restrict array
, gfc_charlen_type len
)
57 index_type count
[GFC_MAX_DIMENSIONS
];
58 index_type extent
[GFC_MAX_DIMENSIONS
];
59 index_type sstride
[GFC_MAX_DIMENSIONS
];
60 const GFC_INTEGER_1
*base
;
64 rank
= GFC_DESCRIPTOR_RANK (array
);
66 runtime_error ("Rank of array needs to be > 0");
70 /* Initialize return value. */
71 memset (ret
, INITVAL
, sizeof(*ret
) * len
);
73 for (n
= 0; n
< rank
; n
++)
75 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
) * len
;
76 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
82 base
= array
->base_addr
;
86 const GFC_INTEGER_1
*retval
;
93 /* Implementation start. */
95 if (compare_fcn (base
, retval
, len
) > 0)
99 /* Implementation end. */
100 /* Advance to the next element. */
103 while (++count
[0] != extent
[0]);
107 /* When we get to the end of a dimension, reset it and increment
108 the next dimension. */
110 /* We could precalculate these products, but this is a less
111 frequently used path so probably not worth it. */
112 base
-= sstride
[n
] * extent
[n
];
116 /* Break out of the loop. */
126 while (count
[n
] == extent
[n
]);
128 memcpy (ret
, retval
, len
* sizeof (*ret
));
133 extern void mmaxval0_s1 (GFC_INTEGER_1
* restrict
,
134 gfc_charlen_type
, gfc_array_s1
* const restrict array
,
135 gfc_array_l1
* const restrict mask
, gfc_charlen_type len
);
136 export_proto(mmaxval0_s1
);
139 mmaxval0_s1 (GFC_INTEGER_1
* const restrict ret
,
140 gfc_charlen_type xlen
, gfc_array_s1
* const restrict array
,
141 gfc_array_l1
* const restrict mask
, gfc_charlen_type len
)
143 index_type count
[GFC_MAX_DIMENSIONS
];
144 index_type extent
[GFC_MAX_DIMENSIONS
];
145 index_type sstride
[GFC_MAX_DIMENSIONS
];
146 index_type mstride
[GFC_MAX_DIMENSIONS
];
147 const GFC_INTEGER_1
*base
;
148 GFC_LOGICAL_1
*mbase
;
153 rank
= GFC_DESCRIPTOR_RANK (array
);
155 runtime_error ("Rank of array needs to be > 0");
157 assert (xlen
== len
);
159 /* Initialize return value. */
160 memset (ret
, INITVAL
, sizeof(*ret
) * len
);
162 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
164 mbase
= mask
->base_addr
;
166 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
167 #ifdef HAVE_GFC_LOGICAL_16
171 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
173 runtime_error ("Funny sized logical array");
175 for (n
= 0; n
< rank
; n
++)
177 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
) * len
;
178 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
179 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
185 base
= array
->base_addr
;
188 const GFC_INTEGER_1
*retval
;
196 /* Implementation start. */
198 if (*mbase
&& compare_fcn (base
, retval
, len
) > 0)
202 /* Implementation end. */
203 /* Advance to the next element. */
207 while (++count
[0] != extent
[0]);
211 /* When we get to the end of a dimension, reset it and increment
212 the next dimension. */
214 /* We could precalculate these products, but this is a less
215 frequently used path so probably not worth it. */
216 base
-= sstride
[n
] * extent
[n
];
217 mbase
-= mstride
[n
] * extent
[n
];
221 /* Break out of the loop. */
232 while (count
[n
] == extent
[n
]);
234 memcpy (ret
, retval
, len
* sizeof (*ret
));
239 extern void smaxval0_s1 (GFC_INTEGER_1
* restrict
,
241 gfc_array_s1
* const restrict array
, GFC_LOGICAL_4
*, gfc_charlen_type
);
242 export_proto(smaxval0_s1
);
245 smaxval0_s1 (GFC_INTEGER_1
* restrict ret
,
246 gfc_charlen_type xlen
, gfc_array_s1
* const restrict array
,
247 GFC_LOGICAL_4
*mask
, gfc_charlen_type len
)
252 maxval0_s1 (ret
, xlen
, array
, len
);
255 memset (ret
, INITVAL
, sizeof (*ret
) * len
);