1 /* Implementation of the MINLOC intrinsic
2 Copyright 2002 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
35 #include "libgfortran.h"
38 #if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_INTEGER_4)
41 extern void minloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
42 gfc_array_r4
* const restrict array
);
43 export_proto(minloc0_4_r4
);
46 minloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
47 gfc_array_r4
* const restrict array
)
49 index_type count
[GFC_MAX_DIMENSIONS
];
50 index_type extent
[GFC_MAX_DIMENSIONS
];
51 index_type sstride
[GFC_MAX_DIMENSIONS
];
53 const GFC_REAL_4
*base
;
58 rank
= GFC_DESCRIPTOR_RANK (array
);
60 runtime_error ("Rank of array needs to be > 0");
62 if (retarray
->data
== NULL
)
64 retarray
->dim
[0].lbound
= 0;
65 retarray
->dim
[0].ubound
= rank
-1;
66 retarray
->dim
[0].stride
= 1;
67 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
69 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
73 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
74 runtime_error ("rank of return array does not equal 1");
76 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
77 runtime_error ("dimension of return array incorrect");
80 dstride
= retarray
->dim
[0].stride
;
81 dest
= retarray
->data
;
82 for (n
= 0; n
< rank
; n
++)
84 sstride
[n
] = array
->dim
[n
].stride
;
85 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
89 /* Set the return value. */
90 for (n
= 0; n
< rank
; n
++)
91 dest
[n
* dstride
] = 0;
98 /* Initialize the return value. */
99 for (n
= 0; n
< rank
; n
++)
100 dest
[n
* dstride
] = 0;
105 minval
= GFC_REAL_4_HUGE
;
110 /* Implementation start. */
112 if (*base
< minval
|| !dest
[0])
115 for (n
= 0; n
< rank
; n
++)
116 dest
[n
* dstride
] = count
[n
] + 1;
118 /* Implementation end. */
120 /* Advance to the next element. */
124 while (count
[n
] == extent
[n
])
126 /* When we get to the end of a dimension, reset it and increment
127 the next dimension. */
129 /* We could precalculate these products, but this is a less
130 frequently used path so probably not worth it. */
131 base
-= sstride
[n
] * extent
[n
];
135 /* Break out of the loop. */
150 extern void mminloc0_4_r4 (gfc_array_i4
* const restrict
,
151 gfc_array_r4
* const restrict
, gfc_array_l4
* const restrict
);
152 export_proto(mminloc0_4_r4
);
155 mminloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
156 gfc_array_r4
* const restrict array
,
157 gfc_array_l4
* const restrict mask
)
159 index_type count
[GFC_MAX_DIMENSIONS
];
160 index_type extent
[GFC_MAX_DIMENSIONS
];
161 index_type sstride
[GFC_MAX_DIMENSIONS
];
162 index_type mstride
[GFC_MAX_DIMENSIONS
];
165 const GFC_REAL_4
*base
;
166 GFC_LOGICAL_4
*mbase
;
170 rank
= GFC_DESCRIPTOR_RANK (array
);
172 runtime_error ("Rank of array needs to be > 0");
174 if (retarray
->data
== NULL
)
176 retarray
->dim
[0].lbound
= 0;
177 retarray
->dim
[0].ubound
= rank
-1;
178 retarray
->dim
[0].stride
= 1;
179 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
180 retarray
->offset
= 0;
181 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
185 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
186 runtime_error ("rank of return array does not equal 1");
188 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
189 runtime_error ("dimension of return array incorrect");
192 dstride
= retarray
->dim
[0].stride
;
193 dest
= retarray
->data
;
194 for (n
= 0; n
< rank
; n
++)
196 sstride
[n
] = array
->dim
[n
].stride
;
197 mstride
[n
] = mask
->dim
[n
].stride
;
198 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
202 /* Set the return value. */
203 for (n
= 0; n
< rank
; n
++)
204 dest
[n
* dstride
] = 0;
212 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
214 /* This allows the same loop to be used for all logical types. */
215 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
216 for (n
= 0; n
< rank
; n
++)
218 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
222 /* Initialize the return value. */
223 for (n
= 0; n
< rank
; n
++)
224 dest
[n
* dstride
] = 0;
229 minval
= GFC_REAL_4_HUGE
;
234 /* Implementation start. */
236 if (*mbase
&& (*base
< minval
|| !dest
[0]))
239 for (n
= 0; n
< rank
; n
++)
240 dest
[n
* dstride
] = count
[n
] + 1;
242 /* Implementation end. */
244 /* Advance to the next element. */
249 while (count
[n
] == extent
[n
])
251 /* When we get to the end of a dimension, reset it and increment
252 the next dimension. */
254 /* We could precalculate these products, but this is a less
255 frequently used path so probably not worth it. */
256 base
-= sstride
[n
] * extent
[n
];
257 mbase
-= mstride
[n
] * extent
[n
];
261 /* Break out of the loop. */
277 extern void sminloc0_4_r4 (gfc_array_i4
* const restrict
,
278 gfc_array_r4
* const restrict
, GFC_LOGICAL_4
*);
279 export_proto(sminloc0_4_r4
);
282 sminloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
283 gfc_array_r4
* const restrict array
,
284 GFC_LOGICAL_4
* mask
)
293 minloc0_4_r4 (retarray
, array
);
297 rank
= GFC_DESCRIPTOR_RANK (array
);
300 runtime_error ("Rank of array needs to be > 0");
302 if (retarray
->data
== NULL
)
304 retarray
->dim
[0].lbound
= 0;
305 retarray
->dim
[0].ubound
= rank
-1;
306 retarray
->dim
[0].stride
= 1;
307 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
308 retarray
->offset
= 0;
309 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
313 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
314 runtime_error ("rank of return array does not equal 1");
316 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
317 runtime_error ("dimension of return array incorrect");
320 dstride
= retarray
->dim
[0].stride
;
321 dest
= retarray
->data
;
322 for (n
= 0; n
<rank
; n
++)
323 dest
[n
* dstride
] = 0 ;