* target-def.h (TARGET_ASM_TTYPE): Correct typo of TARGET_ARM_TTYPE.
[official-gcc.git] / libgfortran / generated / sum_i2.c
blob35d3ae05f3a2ccb00b357994db5589cbbf664c7a
1 /* Implementation of the SUM 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"
27 #include <stdlib.h>
28 #include <assert.h>
31 #if defined (HAVE_GFC_INTEGER_2) && defined (HAVE_GFC_INTEGER_2)
34 extern void sum_i2 (gfc_array_i2 * const restrict,
35 gfc_array_i2 * const restrict, const index_type * const restrict);
36 export_proto(sum_i2);
38 void
39 sum_i2 (gfc_array_i2 * const restrict retarray,
40 gfc_array_i2 * 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_2 * restrict base;
48 GFC_INTEGER_2 * restrict dest;
49 index_type rank;
50 index_type n;
51 index_type len;
52 index_type delta;
53 index_type dim;
54 int continue_loop;
56 /* Make dim zero based to avoid confusion. */
57 dim = (*pdim) - 1;
58 rank = GFC_DESCRIPTOR_RANK (array) - 1;
60 len = GFC_DESCRIPTOR_EXTENT(array,dim);
61 if (len < 0)
62 len = 0;
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);
70 if (extent[n] < 0)
71 extent[n] = 0;
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);
78 if (extent[n] < 0)
79 extent[n] = 0;
82 if (retarray->data == NULL)
84 size_t alloc_size, str;
86 for (n = 0; n < rank; n++)
88 if (n == 0)
89 str = 1;
90 else
91 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
93 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
97 retarray->offset = 0;
98 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
100 alloc_size = sizeof (GFC_INTEGER_2) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
101 * extent[rank-1];
103 if (alloc_size == 0)
105 /* Make sure we have a zero-sized array. */
106 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
107 return;
110 else
111 retarray->data = internal_malloc_size (alloc_size);
113 else
115 if (rank != GFC_DESCRIPTOR_RANK (retarray))
116 runtime_error ("rank of return array incorrect in"
117 " SUM intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
119 (long int) rank);
121 if (unlikely (compile_options.bounds_check))
122 bounds_ifunction_return ((array_t *) retarray, extent,
123 "return value", "SUM");
126 for (n = 0; n < rank; n++)
128 count[n] = 0;
129 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
130 if (extent[n] <= 0)
131 len = 0;
134 base = array->data;
135 dest = retarray->data;
137 continue_loop = 1;
138 while (continue_loop)
140 const GFC_INTEGER_2 * restrict src;
141 GFC_INTEGER_2 result;
142 src = base;
145 result = 0;
146 if (len <= 0)
147 *dest = 0;
148 else
150 for (n = 0; n < len; n++, src += delta)
153 result += *src;
155 *dest = result;
158 /* Advance to the next element. */
159 count[0]++;
160 base += sstride[0];
161 dest += dstride[0];
162 n = 0;
163 while (count[n] == extent[n])
165 /* When we get to the end of a dimension, reset it and increment
166 the next dimension. */
167 count[n] = 0;
168 /* We could precalculate these products, but this is a less
169 frequently used path so probably not worth it. */
170 base -= sstride[n] * extent[n];
171 dest -= dstride[n] * extent[n];
172 n++;
173 if (n == rank)
175 /* Break out of the look. */
176 continue_loop = 0;
177 break;
179 else
181 count[n]++;
182 base += sstride[n];
183 dest += dstride[n];
190 extern void msum_i2 (gfc_array_i2 * const restrict,
191 gfc_array_i2 * const restrict, const index_type * const restrict,
192 gfc_array_l1 * const restrict);
193 export_proto(msum_i2);
195 void
196 msum_i2 (gfc_array_i2 * const restrict retarray,
197 gfc_array_i2 * const restrict array,
198 const index_type * const restrict pdim,
199 gfc_array_l1 * const restrict mask)
201 index_type count[GFC_MAX_DIMENSIONS];
202 index_type extent[GFC_MAX_DIMENSIONS];
203 index_type sstride[GFC_MAX_DIMENSIONS];
204 index_type dstride[GFC_MAX_DIMENSIONS];
205 index_type mstride[GFC_MAX_DIMENSIONS];
206 GFC_INTEGER_2 * restrict dest;
207 const GFC_INTEGER_2 * restrict base;
208 const GFC_LOGICAL_1 * restrict mbase;
209 int rank;
210 int dim;
211 index_type n;
212 index_type len;
213 index_type delta;
214 index_type mdelta;
215 int mask_kind;
217 dim = (*pdim) - 1;
218 rank = GFC_DESCRIPTOR_RANK (array) - 1;
220 len = GFC_DESCRIPTOR_EXTENT(array,dim);
221 if (len <= 0)
222 return;
224 mbase = mask->data;
226 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
228 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
229 #ifdef HAVE_GFC_LOGICAL_16
230 || mask_kind == 16
231 #endif
233 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
234 else
235 runtime_error ("Funny sized logical array");
237 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
238 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
240 for (n = 0; n < dim; n++)
242 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
243 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
244 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
246 if (extent[n] < 0)
247 extent[n] = 0;
250 for (n = dim; n < rank; n++)
252 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
253 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
254 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
256 if (extent[n] < 0)
257 extent[n] = 0;
260 if (retarray->data == NULL)
262 size_t alloc_size, str;
264 for (n = 0; n < rank; n++)
266 if (n == 0)
267 str = 1;
268 else
269 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
271 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
275 alloc_size = sizeof (GFC_INTEGER_2) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
276 * extent[rank-1];
278 retarray->offset = 0;
279 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
281 if (alloc_size == 0)
283 /* Make sure we have a zero-sized array. */
284 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
285 return;
287 else
288 retarray->data = internal_malloc_size (alloc_size);
291 else
293 if (rank != GFC_DESCRIPTOR_RANK (retarray))
294 runtime_error ("rank of return array incorrect in SUM intrinsic");
296 if (unlikely (compile_options.bounds_check))
298 bounds_ifunction_return ((array_t *) retarray, extent,
299 "return value", "SUM");
300 bounds_equal_extents ((array_t *) mask, (array_t *) array,
301 "MASK argument", "SUM");
305 for (n = 0; n < rank; n++)
307 count[n] = 0;
308 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
309 if (extent[n] <= 0)
310 return;
313 dest = retarray->data;
314 base = array->data;
316 while (base)
318 const GFC_INTEGER_2 * restrict src;
319 const GFC_LOGICAL_1 * restrict msrc;
320 GFC_INTEGER_2 result;
321 src = base;
322 msrc = mbase;
325 result = 0;
326 if (len <= 0)
327 *dest = 0;
328 else
330 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
333 if (*msrc)
334 result += *src;
336 *dest = result;
339 /* Advance to the next element. */
340 count[0]++;
341 base += sstride[0];
342 mbase += mstride[0];
343 dest += dstride[0];
344 n = 0;
345 while (count[n] == extent[n])
347 /* When we get to the end of a dimension, reset it and increment
348 the next dimension. */
349 count[n] = 0;
350 /* We could precalculate these products, but this is a less
351 frequently used path so probably not worth it. */
352 base -= sstride[n] * extent[n];
353 mbase -= mstride[n] * extent[n];
354 dest -= dstride[n] * extent[n];
355 n++;
356 if (n == rank)
358 /* Break out of the look. */
359 base = NULL;
360 break;
362 else
364 count[n]++;
365 base += sstride[n];
366 mbase += mstride[n];
367 dest += dstride[n];
374 extern void ssum_i2 (gfc_array_i2 * const restrict,
375 gfc_array_i2 * const restrict, const index_type * const restrict,
376 GFC_LOGICAL_4 *);
377 export_proto(ssum_i2);
379 void
380 ssum_i2 (gfc_array_i2 * const restrict retarray,
381 gfc_array_i2 * const restrict array,
382 const index_type * const restrict pdim,
383 GFC_LOGICAL_4 * mask)
385 index_type count[GFC_MAX_DIMENSIONS];
386 index_type extent[GFC_MAX_DIMENSIONS];
387 index_type sstride[GFC_MAX_DIMENSIONS];
388 index_type dstride[GFC_MAX_DIMENSIONS];
389 GFC_INTEGER_2 * restrict dest;
390 index_type rank;
391 index_type n;
392 index_type dim;
395 if (*mask)
397 sum_i2 (retarray, array, pdim);
398 return;
400 /* Make dim zero based to avoid confusion. */
401 dim = (*pdim) - 1;
402 rank = GFC_DESCRIPTOR_RANK (array) - 1;
404 for (n = 0; n < dim; n++)
406 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
407 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
409 if (extent[n] <= 0)
410 extent[n] = 0;
413 for (n = dim; n < rank; n++)
415 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
416 extent[n] =
417 GFC_DESCRIPTOR_EXTENT(array,n + 1);
419 if (extent[n] <= 0)
420 extent[n] = 0;
423 if (retarray->data == NULL)
425 size_t alloc_size, str;
427 for (n = 0; n < rank; n++)
429 if (n == 0)
430 str = 1;
431 else
432 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
434 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
438 retarray->offset = 0;
439 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
441 alloc_size = sizeof (GFC_INTEGER_2) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
442 * extent[rank-1];
444 if (alloc_size == 0)
446 /* Make sure we have a zero-sized array. */
447 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
448 return;
450 else
451 retarray->data = internal_malloc_size (alloc_size);
453 else
455 if (rank != GFC_DESCRIPTOR_RANK (retarray))
456 runtime_error ("rank of return array incorrect in"
457 " SUM intrinsic: is %ld, should be %ld",
458 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
459 (long int) rank);
461 if (unlikely (compile_options.bounds_check))
463 for (n=0; n < rank; n++)
465 index_type ret_extent;
467 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
468 if (extent[n] != ret_extent)
469 runtime_error ("Incorrect extent in return value of"
470 " SUM intrinsic in dimension %ld:"
471 " is %ld, should be %ld", (long int) n + 1,
472 (long int) ret_extent, (long int) extent[n]);
477 for (n = 0; n < rank; n++)
479 count[n] = 0;
480 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
483 dest = retarray->data;
485 while(1)
487 *dest = 0;
488 count[0]++;
489 dest += dstride[0];
490 n = 0;
491 while (count[n] == extent[n])
493 /* When we get to the end of a dimension, reset it and increment
494 the next dimension. */
495 count[n] = 0;
496 /* We could precalculate these products, but this is a less
497 frequently used path so probably not worth it. */
498 dest -= dstride[n] * extent[n];
499 n++;
500 if (n == rank)
501 return;
502 else
504 count[n]++;
505 dest += dstride[n];
511 #endif