2006-06-30 Andrew Pinski <pinskia@gmail.com>
[official-gcc.git] / libgfortran / generated / maxval_r16.c
blobabb41bb18099773c0e8912a26f483c1f18471267
1 /* Implementation of the MAXVAL 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
19 executable.)
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. */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <float.h>
35 #include "libgfortran.h"
38 #if defined (HAVE_GFC_REAL_16) && defined (HAVE_GFC_REAL_16)
41 extern void maxval_r16 (gfc_array_r16 * const restrict,
42 gfc_array_r16 * const restrict, const index_type * const restrict);
43 export_proto(maxval_r16);
45 void
46 maxval_r16 (gfc_array_r16 * const restrict retarray,
47 gfc_array_r16 * const restrict array,
48 const index_type * const restrict pdim)
50 index_type count[GFC_MAX_DIMENSIONS];
51 index_type extent[GFC_MAX_DIMENSIONS];
52 index_type sstride[GFC_MAX_DIMENSIONS];
53 index_type dstride[GFC_MAX_DIMENSIONS];
54 const GFC_REAL_16 * restrict base;
55 GFC_REAL_16 * restrict dest;
56 index_type rank;
57 index_type n;
58 index_type len;
59 index_type delta;
60 index_type dim;
62 /* Make dim zero based to avoid confusion. */
63 dim = (*pdim) - 1;
64 rank = GFC_DESCRIPTOR_RANK (array) - 1;
66 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
67 delta = array->dim[dim].stride;
69 for (n = 0; n < dim; n++)
71 sstride[n] = array->dim[n].stride;
72 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
74 for (n = dim; n < rank; n++)
76 sstride[n] = array->dim[n + 1].stride;
77 extent[n] =
78 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
81 if (retarray->data == NULL)
83 for (n = 0; n < rank; n++)
85 retarray->dim[n].lbound = 0;
86 retarray->dim[n].ubound = extent[n]-1;
87 if (n == 0)
88 retarray->dim[n].stride = 1;
89 else
90 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
93 retarray->data
94 = internal_malloc_size (sizeof (GFC_REAL_16)
95 * retarray->dim[rank-1].stride
96 * extent[rank-1]);
97 retarray->offset = 0;
98 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
100 else
102 if (rank != GFC_DESCRIPTOR_RANK (retarray))
103 runtime_error ("rank of return array incorrect");
106 for (n = 0; n < rank; n++)
108 count[n] = 0;
109 dstride[n] = retarray->dim[n].stride;
110 if (extent[n] <= 0)
111 len = 0;
114 base = array->data;
115 dest = retarray->data;
117 while (base)
119 const GFC_REAL_16 * restrict src;
120 GFC_REAL_16 result;
121 src = base;
124 result = -GFC_REAL_16_HUGE;
125 if (len <= 0)
126 *dest = -GFC_REAL_16_HUGE;
127 else
129 for (n = 0; n < len; n++, src += delta)
132 if (*src > result)
133 result = *src;
135 *dest = result;
138 /* Advance to the next element. */
139 count[0]++;
140 base += sstride[0];
141 dest += dstride[0];
142 n = 0;
143 while (count[n] == extent[n])
145 /* When we get to the end of a dimension, reset it and increment
146 the next dimension. */
147 count[n] = 0;
148 /* We could precalculate these products, but this is a less
149 frequently used path so proabably not worth it. */
150 base -= sstride[n] * extent[n];
151 dest -= dstride[n] * extent[n];
152 n++;
153 if (n == rank)
155 /* Break out of the look. */
156 base = NULL;
157 break;
159 else
161 count[n]++;
162 base += sstride[n];
163 dest += dstride[n];
170 extern void mmaxval_r16 (gfc_array_r16 * const restrict,
171 gfc_array_r16 * const restrict, const index_type * const restrict,
172 gfc_array_l4 * const restrict);
173 export_proto(mmaxval_r16);
175 void
176 mmaxval_r16 (gfc_array_r16 * const restrict retarray,
177 gfc_array_r16 * const restrict array,
178 const index_type * const restrict pdim,
179 gfc_array_l4 * const restrict mask)
181 index_type count[GFC_MAX_DIMENSIONS];
182 index_type extent[GFC_MAX_DIMENSIONS];
183 index_type sstride[GFC_MAX_DIMENSIONS];
184 index_type dstride[GFC_MAX_DIMENSIONS];
185 index_type mstride[GFC_MAX_DIMENSIONS];
186 GFC_REAL_16 * restrict dest;
187 const GFC_REAL_16 * restrict base;
188 const GFC_LOGICAL_4 * restrict mbase;
189 int rank;
190 int dim;
191 index_type n;
192 index_type len;
193 index_type delta;
194 index_type mdelta;
196 dim = (*pdim) - 1;
197 rank = GFC_DESCRIPTOR_RANK (array) - 1;
199 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
200 if (len <= 0)
201 return;
202 delta = array->dim[dim].stride;
203 mdelta = mask->dim[dim].stride;
205 for (n = 0; n < dim; n++)
207 sstride[n] = array->dim[n].stride;
208 mstride[n] = mask->dim[n].stride;
209 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
211 for (n = dim; n < rank; n++)
213 sstride[n] = array->dim[n + 1].stride;
214 mstride[n] = mask->dim[n + 1].stride;
215 extent[n] =
216 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
219 if (retarray->data == NULL)
221 for (n = 0; n < rank; n++)
223 retarray->dim[n].lbound = 0;
224 retarray->dim[n].ubound = extent[n]-1;
225 if (n == 0)
226 retarray->dim[n].stride = 1;
227 else
228 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
231 retarray->data
232 = internal_malloc_size (sizeof (GFC_REAL_16)
233 * retarray->dim[rank-1].stride
234 * extent[rank-1]);
235 retarray->offset = 0;
236 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
238 else
240 if (rank != GFC_DESCRIPTOR_RANK (retarray))
241 runtime_error ("rank of return array incorrect");
244 for (n = 0; n < rank; n++)
246 count[n] = 0;
247 dstride[n] = retarray->dim[n].stride;
248 if (extent[n] <= 0)
249 return;
252 dest = retarray->data;
253 base = array->data;
254 mbase = mask->data;
256 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
258 /* This allows the same loop to be used for all logical types. */
259 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
260 for (n = 0; n < rank; n++)
261 mstride[n] <<= 1;
262 mdelta <<= 1;
263 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
266 while (base)
268 const GFC_REAL_16 * restrict src;
269 const GFC_LOGICAL_4 * restrict msrc;
270 GFC_REAL_16 result;
271 src = base;
272 msrc = mbase;
275 result = -GFC_REAL_16_HUGE;
276 if (len <= 0)
277 *dest = -GFC_REAL_16_HUGE;
278 else
280 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
283 if (*msrc && *src > result)
284 result = *src;
286 *dest = result;
289 /* Advance to the next element. */
290 count[0]++;
291 base += sstride[0];
292 mbase += mstride[0];
293 dest += dstride[0];
294 n = 0;
295 while (count[n] == extent[n])
297 /* When we get to the end of a dimension, reset it and increment
298 the next dimension. */
299 count[n] = 0;
300 /* We could precalculate these products, but this is a less
301 frequently used path so proabably not worth it. */
302 base -= sstride[n] * extent[n];
303 mbase -= mstride[n] * extent[n];
304 dest -= dstride[n] * extent[n];
305 n++;
306 if (n == rank)
308 /* Break out of the look. */
309 base = NULL;
310 break;
312 else
314 count[n]++;
315 base += sstride[n];
316 mbase += mstride[n];
317 dest += dstride[n];
324 extern void smaxval_r16 (gfc_array_r16 * const restrict,
325 gfc_array_r16 * const restrict, const index_type * const restrict,
326 GFC_LOGICAL_4 *);
327 export_proto(smaxval_r16);
329 void
330 smaxval_r16 (gfc_array_r16 * const restrict retarray,
331 gfc_array_r16 * const restrict array,
332 const index_type * const restrict pdim,
333 GFC_LOGICAL_4 * mask)
335 index_type rank;
336 index_type n;
337 index_type dstride;
338 GFC_REAL_16 *dest;
340 if (*mask)
342 maxval_r16 (retarray, array, pdim);
343 return;
345 rank = GFC_DESCRIPTOR_RANK (array);
346 if (rank <= 0)
347 runtime_error ("Rank of array needs to be > 0");
349 if (retarray->data == NULL)
351 retarray->dim[0].lbound = 0;
352 retarray->dim[0].ubound = rank-1;
353 retarray->dim[0].stride = 1;
354 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
355 retarray->offset = 0;
356 retarray->data = internal_malloc_size (sizeof (GFC_REAL_16) * rank);
358 else
360 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
361 runtime_error ("rank of return array does not equal 1");
363 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
364 runtime_error ("dimension of return array incorrect");
367 dstride = retarray->dim[0].stride;
368 dest = retarray->data;
370 for (n = 0; n < rank; n++)
371 dest[n * dstride] = -GFC_REAL_16_HUGE ;
374 #endif