]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/sum_r8.c
re PR tree-optimization/40676 (internal compiler error: verify_ssa error: definition...
[thirdparty/gcc.git] / libgfortran / generated / sum_r8.c
CommitLineData
6de9cd9a 1/* Implementation of the SUM intrinsic
748086b7 2 Copyright 2002, 2007, 2009 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook <paul@nowt.org>
4
57dea9f6 5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6de9cd9a
DN
6
7Libgfortran is free software; you can redistribute it and/or
57dea9f6 8modify it under the terms of the GNU General Public
6de9cd9a 9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
6de9cd9a
DN
11
12Libgfortran is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57dea9f6 15GNU General Public License for more details.
6de9cd9a 16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
6de9cd9a 25
36ae8a61 26#include "libgfortran.h"
6de9cd9a
DN
27#include <stdlib.h>
28#include <assert.h>
6de9cd9a 29
7d7b8bfe 30
644cb69f
FXC
31#if defined (HAVE_GFC_REAL_8) && defined (HAVE_GFC_REAL_8)
32
33
64acfd99
JB
34extern void sum_r8 (gfc_array_r8 * const restrict,
35 gfc_array_r8 * const restrict, const index_type * const restrict);
7f68c75f 36export_proto(sum_r8);
7d7b8bfe 37
6de9cd9a 38void
64acfd99
JB
39sum_r8 (gfc_array_r8 * const restrict retarray,
40 gfc_array_r8 * const restrict array,
41 const index_type * const restrict pdim)
6de9cd9a 42{
e33e218b
TK
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];
64acfd99
JB
47 const GFC_REAL_8 * restrict base;
48 GFC_REAL_8 * restrict dest;
6de9cd9a
DN
49 index_type rank;
50 index_type n;
51 index_type len;
52 index_type delta;
53 index_type dim;
da96f5ab 54 int continue_loop;
6de9cd9a
DN
55
56 /* Make dim zero based to avoid confusion. */
57 dim = (*pdim) - 1;
58 rank = GFC_DESCRIPTOR_RANK (array) - 1;
e33e218b 59
dfb55fdc 60 len = GFC_DESCRIPTOR_EXTENT(array,dim);
da96f5ab
TK
61 if (len < 0)
62 len = 0;
dfb55fdc 63 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
6de9cd9a
DN
64
65 for (n = 0; n < dim; n++)
66 {
dfb55fdc
TK
67 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
68 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
80ee04b9
TK
69
70 if (extent[n] < 0)
71 extent[n] = 0;
6de9cd9a
DN
72 }
73 for (n = dim; n < rank; n++)
74 {
dfb55fdc
TK
75 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
76 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
80ee04b9
TK
77
78 if (extent[n] < 0)
79 extent[n] = 0;
6de9cd9a
DN
80 }
81
6c167c45
VL
82 if (retarray->data == NULL)
83 {
dfb55fdc 84 size_t alloc_size, str;
80ee04b9 85
6c167c45
VL
86 for (n = 0; n < rank; n++)
87 {
6c167c45 88 if (n == 0)
dfb55fdc 89 str = 1;
6c167c45 90 else
dfb55fdc
TK
91 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
92
93 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
94
6c167c45
VL
95 }
96
efd4dc1a 97 retarray->offset = 0;
50dd63a9 98 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
80ee04b9 99
dfb55fdc 100 alloc_size = sizeof (GFC_REAL_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
80ee04b9
TK
101 * extent[rank-1];
102
103 if (alloc_size == 0)
104 {
105 /* Make sure we have a zero-sized array. */
dfb55fdc 106 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
80ee04b9 107 return;
dfb55fdc 108
80ee04b9
TK
109 }
110 else
111 retarray->data = internal_malloc_size (alloc_size);
6c167c45 112 }
50dd63a9
TK
113 else
114 {
50dd63a9 115 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8 116 runtime_error ("rank of return array incorrect in"
ccacefc7
TK
117 " SUM intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
119 (long int) rank);
fd6590f8 120
9731c4a3 121 if (unlikely (compile_options.bounds_check))
fd6590f8
TK
122 {
123 for (n=0; n < rank; n++)
124 {
125 index_type ret_extent;
126
dfb55fdc 127 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
fd6590f8
TK
128 if (extent[n] != ret_extent)
129 runtime_error ("Incorrect extent in return value of"
ccacefc7
TK
130 " SUM intrinsic in dimension %ld:"
131 " is %ld, should be %ld", (long int) n + 1,
fd6590f8
TK
132 (long int) ret_extent, (long int) extent[n]);
133 }
134 }
50dd63a9
TK
135 }
136
6de9cd9a
DN
137 for (n = 0; n < rank; n++)
138 {
139 count[n] = 0;
dfb55fdc 140 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
6de9cd9a
DN
141 if (extent[n] <= 0)
142 len = 0;
143 }
144
145 base = array->data;
146 dest = retarray->data;
147
da96f5ab
TK
148 continue_loop = 1;
149 while (continue_loop)
6de9cd9a 150 {
64acfd99 151 const GFC_REAL_8 * restrict src;
6de9cd9a
DN
152 GFC_REAL_8 result;
153 src = base;
154 {
155
156 result = 0;
157 if (len <= 0)
158 *dest = 0;
159 else
160 {
161 for (n = 0; n < len; n++, src += delta)
162 {
163
164 result += *src;
165 }
166 *dest = result;
167 }
168 }
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])
175 {
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
5d7adf7a 180 frequently used path so probably not worth it. */
6de9cd9a
DN
181 base -= sstride[n] * extent[n];
182 dest -= dstride[n] * extent[n];
183 n++;
184 if (n == rank)
185 {
186 /* Break out of the look. */
da96f5ab
TK
187 continue_loop = 0;
188 break;
6de9cd9a
DN
189 }
190 else
191 {
192 count[n]++;
193 base += sstride[n];
194 dest += dstride[n];
195 }
196 }
197 }
198}
199
7d7b8bfe 200
64acfd99
JB
201extern void msum_r8 (gfc_array_r8 * const restrict,
202 gfc_array_r8 * const restrict, const index_type * const restrict,
28dc6b33 203 gfc_array_l1 * const restrict);
7f68c75f 204export_proto(msum_r8);
7d7b8bfe 205
6de9cd9a 206void
64acfd99
JB
207msum_r8 (gfc_array_r8 * const restrict retarray,
208 gfc_array_r8 * const restrict array,
209 const index_type * const restrict pdim,
28dc6b33 210 gfc_array_l1 * const restrict mask)
6de9cd9a 211{
e33e218b
TK
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];
64acfd99
JB
217 GFC_REAL_8 * restrict dest;
218 const GFC_REAL_8 * restrict base;
28dc6b33 219 const GFC_LOGICAL_1 * restrict mbase;
6de9cd9a
DN
220 int rank;
221 int dim;
222 index_type n;
223 index_type len;
224 index_type delta;
225 index_type mdelta;
28dc6b33 226 int mask_kind;
6de9cd9a
DN
227
228 dim = (*pdim) - 1;
229 rank = GFC_DESCRIPTOR_RANK (array) - 1;
e33e218b 230
dfb55fdc 231 len = GFC_DESCRIPTOR_EXTENT(array,dim);
6de9cd9a
DN
232 if (len <= 0)
233 return;
28dc6b33
TK
234
235 mbase = mask->data;
236
237 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
238
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
243 )
244 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
245 else
246 runtime_error ("Funny sized logical array");
247
dfb55fdc
TK
248 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
249 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
6de9cd9a
DN
250
251 for (n = 0; n < dim; n++)
252 {
dfb55fdc
TK
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);
80ee04b9
TK
256
257 if (extent[n] < 0)
258 extent[n] = 0;
259
6de9cd9a
DN
260 }
261 for (n = dim; n < rank; n++)
262 {
dfb55fdc
TK
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);
80ee04b9
TK
266
267 if (extent[n] < 0)
268 extent[n] = 0;
6de9cd9a
DN
269 }
270
50dd63a9
TK
271 if (retarray->data == NULL)
272 {
dfb55fdc 273 size_t alloc_size, str;
80ee04b9 274
50dd63a9
TK
275 for (n = 0; n < rank; n++)
276 {
50dd63a9 277 if (n == 0)
dfb55fdc 278 str = 1;
50dd63a9 279 else
dfb55fdc
TK
280 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
281
282 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
283
50dd63a9
TK
284 }
285
dfb55fdc 286 alloc_size = sizeof (GFC_REAL_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
80ee04b9
TK
287 * extent[rank-1];
288
efd4dc1a 289 retarray->offset = 0;
50dd63a9 290 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
80ee04b9
TK
291
292 if (alloc_size == 0)
293 {
294 /* Make sure we have a zero-sized array. */
dfb55fdc 295 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
80ee04b9
TK
296 return;
297 }
298 else
299 retarray->data = internal_malloc_size (alloc_size);
300
50dd63a9
TK
301 }
302 else
303 {
50dd63a9 304 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8
TK
305 runtime_error ("rank of return array incorrect in SUM intrinsic");
306
9731c4a3 307 if (unlikely (compile_options.bounds_check))
fd6590f8
TK
308 {
309 for (n=0; n < rank; n++)
310 {
311 index_type ret_extent;
312
dfb55fdc 313 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
fd6590f8
TK
314 if (extent[n] != ret_extent)
315 runtime_error ("Incorrect extent in return value of"
ccacefc7
TK
316 " SUM intrinsic in dimension %ld:"
317 " is %ld, should be %ld", (long int) n + 1,
fd6590f8
TK
318 (long int) ret_extent, (long int) extent[n]);
319 }
320 for (n=0; n<= rank; n++)
321 {
322 index_type mask_extent, array_extent;
323
dfb55fdc
TK
324 array_extent = GFC_DESCRIPTOR_EXTENT(array,n);
325 mask_extent = GFC_DESCRIPTOR_EXTENT(mask,n);
fd6590f8
TK
326 if (array_extent != mask_extent)
327 runtime_error ("Incorrect extent in MASK argument of"
ccacefc7
TK
328 " SUM intrinsic in dimension %ld:"
329 " is %ld, should be %ld", (long int) n + 1,
fd6590f8
TK
330 (long int) mask_extent, (long int) array_extent);
331 }
332 }
50dd63a9
TK
333 }
334
6de9cd9a
DN
335 for (n = 0; n < rank; n++)
336 {
337 count[n] = 0;
dfb55fdc 338 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
6de9cd9a
DN
339 if (extent[n] <= 0)
340 return;
341 }
342
343 dest = retarray->data;
344 base = array->data;
6de9cd9a
DN
345
346 while (base)
347 {
64acfd99 348 const GFC_REAL_8 * restrict src;
28dc6b33 349 const GFC_LOGICAL_1 * restrict msrc;
6de9cd9a
DN
350 GFC_REAL_8 result;
351 src = base;
352 msrc = mbase;
353 {
354
355 result = 0;
356 if (len <= 0)
357 *dest = 0;
358 else
359 {
360 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
361 {
362
363 if (*msrc)
364 result += *src;
365 }
366 *dest = result;
367 }
368 }
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])
376 {
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
5d7adf7a 381 frequently used path so probably not worth it. */
6de9cd9a
DN
382 base -= sstride[n] * extent[n];
383 mbase -= mstride[n] * extent[n];
384 dest -= dstride[n] * extent[n];
385 n++;
386 if (n == rank)
387 {
388 /* Break out of the look. */
389 base = NULL;
390 break;
391 }
392 else
393 {
394 count[n]++;
395 base += sstride[n];
396 mbase += mstride[n];
397 dest += dstride[n];
398 }
399 }
400 }
401}
644cb69f 402
97a62038
TK
403
404extern void ssum_r8 (gfc_array_r8 * const restrict,
405 gfc_array_r8 * const restrict, const index_type * const restrict,
406 GFC_LOGICAL_4 *);
407export_proto(ssum_r8);
408
409void
410ssum_r8 (gfc_array_r8 * const restrict retarray,
411 gfc_array_r8 * const restrict array,
412 const index_type * const restrict pdim,
413 GFC_LOGICAL_4 * mask)
414{
802367d7
TK
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_REAL_8 * restrict dest;
97a62038
TK
420 index_type rank;
421 index_type n;
802367d7
TK
422 index_type dim;
423
97a62038
TK
424
425 if (*mask)
426 {
427 sum_r8 (retarray, array, pdim);
428 return;
429 }
802367d7
TK
430 /* Make dim zero based to avoid confusion. */
431 dim = (*pdim) - 1;
432 rank = GFC_DESCRIPTOR_RANK (array) - 1;
433
434 for (n = 0; n < dim; n++)
435 {
dfb55fdc
TK
436 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
437 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
802367d7
TK
438
439 if (extent[n] <= 0)
440 extent[n] = 0;
441 }
442
443 for (n = dim; n < rank; n++)
444 {
dfb55fdc 445 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
802367d7 446 extent[n] =
dfb55fdc 447 GFC_DESCRIPTOR_EXTENT(array,n + 1);
802367d7
TK
448
449 if (extent[n] <= 0)
450 extent[n] = 0;
451 }
97a62038
TK
452
453 if (retarray->data == NULL)
454 {
dfb55fdc 455 size_t alloc_size, str;
802367d7
TK
456
457 for (n = 0; n < rank; n++)
458 {
802367d7 459 if (n == 0)
dfb55fdc 460 str = 1;
802367d7 461 else
dfb55fdc
TK
462 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
463
464 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
465
802367d7
TK
466 }
467
97a62038 468 retarray->offset = 0;
802367d7
TK
469 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
470
dfb55fdc 471 alloc_size = sizeof (GFC_REAL_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
802367d7
TK
472 * extent[rank-1];
473
474 if (alloc_size == 0)
475 {
476 /* Make sure we have a zero-sized array. */
dfb55fdc 477 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
802367d7
TK
478 return;
479 }
480 else
481 retarray->data = internal_malloc_size (alloc_size);
97a62038
TK
482 }
483 else
484 {
802367d7
TK
485 if (rank != GFC_DESCRIPTOR_RANK (retarray))
486 runtime_error ("rank of return array incorrect in"
487 " SUM intrinsic: is %ld, should be %ld",
488 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
489 (long int) rank);
490
9731c4a3 491 if (unlikely (compile_options.bounds_check))
fd6590f8 492 {
802367d7
TK
493 for (n=0; n < rank; n++)
494 {
495 index_type ret_extent;
97a62038 496
dfb55fdc 497 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
802367d7
TK
498 if (extent[n] != ret_extent)
499 runtime_error ("Incorrect extent in return value of"
500 " SUM intrinsic in dimension %ld:"
501 " is %ld, should be %ld", (long int) n + 1,
502 (long int) ret_extent, (long int) extent[n]);
503 }
fd6590f8
TK
504 }
505 }
97a62038 506
802367d7
TK
507 for (n = 0; n < rank; n++)
508 {
509 count[n] = 0;
dfb55fdc 510 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
802367d7
TK
511 }
512
513 dest = retarray->data;
514
515 while(1)
516 {
517 *dest = 0;
518 count[0]++;
519 dest += dstride[0];
520 n = 0;
521 while (count[n] == extent[n])
522 {
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
533 {
534 count[n]++;
535 dest += dstride[n];
536 }
537 }
538 }
97a62038
TK
539}
540
644cb69f 541#endif