2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / libgfortran / generated / product_i4.c
blobd5af367956199a57b107ec6734a9ca605fdb9be7
1 /* Implementation of the PRODUCT 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_4) && defined (HAVE_GFC_INTEGER_4)
34 extern void product_i4 (gfc_array_i4 * const restrict,
35 gfc_array_i4 * const restrict, const index_type * const restrict);
36 export_proto(product_i4);
38 void
39 product_i4 (gfc_array_i4 * const restrict retarray,
40 gfc_array_i4 * 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_4 * restrict base;
48 GFC_INTEGER_4 * 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_4) * 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 " PRODUCT intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
119 (long int) rank);
121 if (unlikely (compile_options.bounds_check))
123 for (n=0; n < rank; n++)
125 index_type ret_extent;
127 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
128 if (extent[n] != ret_extent)
129 runtime_error ("Incorrect extent in return value of"
130 " PRODUCT intrinsic in dimension %ld:"
131 " is %ld, should be %ld", (long int) n + 1,
132 (long int) ret_extent, (long int) extent[n]);
137 for (n = 0; n < rank; n++)
139 count[n] = 0;
140 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
141 if (extent[n] <= 0)
142 len = 0;
145 base = array->data;
146 dest = retarray->data;
148 continue_loop = 1;
149 while (continue_loop)
151 const GFC_INTEGER_4 * restrict src;
152 GFC_INTEGER_4 result;
153 src = base;
156 result = 1;
157 if (len <= 0)
158 *dest = 1;
159 else
161 for (n = 0; n < len; n++, src += delta)
164 result *= *src;
166 *dest = result;
169 /* Advance to the next element. */
170 count[0]++;
171 base += sstride[0];
172 dest += dstride[0];
173 n = 0;
174 while (count[n] == extent[n])
176 /* When we get to the end of a dimension, reset it and increment
177 the next dimension. */
178 count[n] = 0;
179 /* We could precalculate these products, but this is a less
180 frequently used path so probably not worth it. */
181 base -= sstride[n] * extent[n];
182 dest -= dstride[n] * extent[n];
183 n++;
184 if (n == rank)
186 /* Break out of the look. */
187 continue_loop = 0;
188 break;
190 else
192 count[n]++;
193 base += sstride[n];
194 dest += dstride[n];
201 extern void mproduct_i4 (gfc_array_i4 * const restrict,
202 gfc_array_i4 * const restrict, const index_type * const restrict,
203 gfc_array_l1 * const restrict);
204 export_proto(mproduct_i4);
206 void
207 mproduct_i4 (gfc_array_i4 * const restrict retarray,
208 gfc_array_i4 * const restrict array,
209 const index_type * const restrict pdim,
210 gfc_array_l1 * const restrict mask)
212 index_type count[GFC_MAX_DIMENSIONS];
213 index_type extent[GFC_MAX_DIMENSIONS];
214 index_type sstride[GFC_MAX_DIMENSIONS];
215 index_type dstride[GFC_MAX_DIMENSIONS];
216 index_type mstride[GFC_MAX_DIMENSIONS];
217 GFC_INTEGER_4 * restrict dest;
218 const GFC_INTEGER_4 * restrict base;
219 const GFC_LOGICAL_1 * restrict mbase;
220 int rank;
221 int dim;
222 index_type n;
223 index_type len;
224 index_type delta;
225 index_type mdelta;
226 int mask_kind;
228 dim = (*pdim) - 1;
229 rank = GFC_DESCRIPTOR_RANK (array) - 1;
231 len = GFC_DESCRIPTOR_EXTENT(array,dim);
232 if (len <= 0)
233 return;
235 mbase = mask->data;
237 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
239 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
240 #ifdef HAVE_GFC_LOGICAL_16
241 || mask_kind == 16
242 #endif
244 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
245 else
246 runtime_error ("Funny sized logical array");
248 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
249 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
251 for (n = 0; n < dim; n++)
253 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
254 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
255 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
257 if (extent[n] < 0)
258 extent[n] = 0;
261 for (n = dim; n < rank; n++)
263 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
264 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
265 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
267 if (extent[n] < 0)
268 extent[n] = 0;
271 if (retarray->data == NULL)
273 size_t alloc_size, str;
275 for (n = 0; n < rank; n++)
277 if (n == 0)
278 str = 1;
279 else
280 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
282 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
286 alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
287 * extent[rank-1];
289 retarray->offset = 0;
290 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
292 if (alloc_size == 0)
294 /* Make sure we have a zero-sized array. */
295 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
296 return;
298 else
299 retarray->data = internal_malloc_size (alloc_size);
302 else
304 if (rank != GFC_DESCRIPTOR_RANK (retarray))
305 runtime_error ("rank of return array incorrect in PRODUCT intrinsic");
307 if (unlikely (compile_options.bounds_check))
309 for (n=0; n < rank; n++)
311 index_type ret_extent;
313 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
314 if (extent[n] != ret_extent)
315 runtime_error ("Incorrect extent in return value of"
316 " PRODUCT intrinsic in dimension %ld:"
317 " is %ld, should be %ld", (long int) n + 1,
318 (long int) ret_extent, (long int) extent[n]);
320 for (n=0; n<= rank; n++)
322 index_type mask_extent, array_extent;
324 array_extent = GFC_DESCRIPTOR_EXTENT(array,n);
325 mask_extent = GFC_DESCRIPTOR_EXTENT(mask,n);
326 if (array_extent != mask_extent)
327 runtime_error ("Incorrect extent in MASK argument of"
328 " PRODUCT intrinsic in dimension %ld:"
329 " is %ld, should be %ld", (long int) n + 1,
330 (long int) mask_extent, (long int) array_extent);
335 for (n = 0; n < rank; n++)
337 count[n] = 0;
338 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
339 if (extent[n] <= 0)
340 return;
343 dest = retarray->data;
344 base = array->data;
346 while (base)
348 const GFC_INTEGER_4 * restrict src;
349 const GFC_LOGICAL_1 * restrict msrc;
350 GFC_INTEGER_4 result;
351 src = base;
352 msrc = mbase;
355 result = 1;
356 if (len <= 0)
357 *dest = 1;
358 else
360 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
363 if (*msrc)
364 result *= *src;
366 *dest = result;
369 /* Advance to the next element. */
370 count[0]++;
371 base += sstride[0];
372 mbase += mstride[0];
373 dest += dstride[0];
374 n = 0;
375 while (count[n] == extent[n])
377 /* When we get to the end of a dimension, reset it and increment
378 the next dimension. */
379 count[n] = 0;
380 /* We could precalculate these products, but this is a less
381 frequently used path so probably not worth it. */
382 base -= sstride[n] * extent[n];
383 mbase -= mstride[n] * extent[n];
384 dest -= dstride[n] * extent[n];
385 n++;
386 if (n == rank)
388 /* Break out of the look. */
389 base = NULL;
390 break;
392 else
394 count[n]++;
395 base += sstride[n];
396 mbase += mstride[n];
397 dest += dstride[n];
404 extern void sproduct_i4 (gfc_array_i4 * const restrict,
405 gfc_array_i4 * const restrict, const index_type * const restrict,
406 GFC_LOGICAL_4 *);
407 export_proto(sproduct_i4);
409 void
410 sproduct_i4 (gfc_array_i4 * const restrict retarray,
411 gfc_array_i4 * const restrict array,
412 const index_type * const restrict pdim,
413 GFC_LOGICAL_4 * mask)
415 index_type count[GFC_MAX_DIMENSIONS];
416 index_type extent[GFC_MAX_DIMENSIONS];
417 index_type sstride[GFC_MAX_DIMENSIONS];
418 index_type dstride[GFC_MAX_DIMENSIONS];
419 GFC_INTEGER_4 * restrict dest;
420 index_type rank;
421 index_type n;
422 index_type dim;
425 if (*mask)
427 product_i4 (retarray, array, pdim);
428 return;
430 /* Make dim zero based to avoid confusion. */
431 dim = (*pdim) - 1;
432 rank = GFC_DESCRIPTOR_RANK (array) - 1;
434 for (n = 0; n < dim; n++)
436 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
437 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
439 if (extent[n] <= 0)
440 extent[n] = 0;
443 for (n = dim; n < rank; n++)
445 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
446 extent[n] =
447 GFC_DESCRIPTOR_EXTENT(array,n + 1);
449 if (extent[n] <= 0)
450 extent[n] = 0;
453 if (retarray->data == NULL)
455 size_t alloc_size, str;
457 for (n = 0; n < rank; n++)
459 if (n == 0)
460 str = 1;
461 else
462 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
464 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
468 retarray->offset = 0;
469 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
471 alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
472 * extent[rank-1];
474 if (alloc_size == 0)
476 /* Make sure we have a zero-sized array. */
477 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
478 return;
480 else
481 retarray->data = internal_malloc_size (alloc_size);
483 else
485 if (rank != GFC_DESCRIPTOR_RANK (retarray))
486 runtime_error ("rank of return array incorrect in"
487 " PRODUCT intrinsic: is %ld, should be %ld",
488 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
489 (long int) rank);
491 if (unlikely (compile_options.bounds_check))
493 for (n=0; n < rank; n++)
495 index_type ret_extent;
497 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
498 if (extent[n] != ret_extent)
499 runtime_error ("Incorrect extent in return value of"
500 " PRODUCT intrinsic in dimension %ld:"
501 " is %ld, should be %ld", (long int) n + 1,
502 (long int) ret_extent, (long int) extent[n]);
507 for (n = 0; n < rank; n++)
509 count[n] = 0;
510 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
513 dest = retarray->data;
515 while(1)
517 *dest = 1;
518 count[0]++;
519 dest += dstride[0];
520 n = 0;
521 while (count[n] == extent[n])
523 /* When we get to the end of a dimension, reset it and increment
524 the next dimension. */
525 count[n] = 0;
526 /* We could precalculate these products, but this is a less
527 frequently used path so probably not worth it. */
528 dest -= dstride[n] * extent[n];
529 n++;
530 if (n == rank)
531 return;
532 else
534 count[n]++;
535 dest += dstride[n];
541 #endif