]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Implementation of the SUM intrinsic |
e3c063ce | 2 | Copyright (C) 2002-2013 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Paul Brook <paul@nowt.org> |
4 | ||
57dea9f6 | 5 | This file is part of the GNU Fortran 95 runtime library (libgfortran). |
6de9cd9a DN |
6 | |
7 | Libgfortran is free software; you can redistribute it and/or | |
57dea9f6 | 8 | modify it under the terms of the GNU General Public |
6de9cd9a | 9 | License as published by the Free Software Foundation; either |
748086b7 | 10 | version 3 of the License, or (at your option) any later version. |
6de9cd9a DN |
11 | |
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 | |
57dea9f6 | 15 | GNU General Public License for more details. |
6de9cd9a | 16 | |
748086b7 JJ |
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. | |
20 | ||
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/>. */ | |
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_INTEGER_4) && defined (HAVE_GFC_INTEGER_4) |
32 | ||
33 | ||
64acfd99 JB |
34 | extern void sum_i4 (gfc_array_i4 * const restrict, |
35 | gfc_array_i4 * const restrict, const index_type * const restrict); | |
7f68c75f | 36 | export_proto(sum_i4); |
7d7b8bfe | 37 | |
6de9cd9a | 38 | void |
64acfd99 JB |
39 | sum_i4 (gfc_array_i4 * const restrict retarray, |
40 | gfc_array_i4 * 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_INTEGER_4 * restrict base; |
48 | GFC_INTEGER_4 * 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 | ||
21d1335b | 82 | if (retarray->base_addr == NULL) |
6c167c45 | 83 | { |
dfb55fdc | 84 | size_t alloc_size, str; |
80ee04b9 | 85 | |
6c167c45 | 86 | for (n = 0; n < rank; n++) |
80927a56 JJ |
87 | { |
88 | if (n == 0) | |
dfb55fdc | 89 | str = 1; |
80927a56 JJ |
90 | else |
91 | str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; | |
dfb55fdc TK |
92 | |
93 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
94 | ||
80927a56 | 95 | } |
6c167c45 | 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_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
80ee04b9 TK |
101 | * extent[rank-1]; |
102 | ||
1a0fd3d3 | 103 | retarray->base_addr = xmalloc (alloc_size); |
80ee04b9 TK |
104 | if (alloc_size == 0) |
105 | { | |
106 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 107 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
80ee04b9 | 108 | return; |
dfb55fdc | 109 | |
80ee04b9 | 110 | } |
6c167c45 | 111 | } |
50dd63a9 TK |
112 | else |
113 | { | |
50dd63a9 | 114 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
fd6590f8 | 115 | runtime_error ("rank of return array incorrect in" |
ccacefc7 TK |
116 | " SUM intrinsic: is %ld, should be %ld", |
117 | (long int) (GFC_DESCRIPTOR_RANK (retarray)), | |
118 | (long int) rank); | |
fd6590f8 | 119 | |
9731c4a3 | 120 | if (unlikely (compile_options.bounds_check)) |
16bff921 TK |
121 | bounds_ifunction_return ((array_t *) retarray, extent, |
122 | "return value", "SUM"); | |
50dd63a9 TK |
123 | } |
124 | ||
6de9cd9a DN |
125 | for (n = 0; n < rank; n++) |
126 | { | |
127 | count[n] = 0; | |
dfb55fdc | 128 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
6de9cd9a | 129 | if (extent[n] <= 0) |
facc1285 | 130 | return; |
6de9cd9a DN |
131 | } |
132 | ||
21d1335b TB |
133 | base = array->base_addr; |
134 | dest = retarray->base_addr; | |
6de9cd9a | 135 | |
da96f5ab TK |
136 | continue_loop = 1; |
137 | while (continue_loop) | |
6de9cd9a | 138 | { |
64acfd99 | 139 | const GFC_INTEGER_4 * restrict src; |
6de9cd9a DN |
140 | GFC_INTEGER_4 result; |
141 | src = base; | |
142 | { | |
143 | ||
144 | result = 0; | |
80927a56 | 145 | if (len <= 0) |
6de9cd9a DN |
146 | *dest = 0; |
147 | else | |
148 | { | |
149 | for (n = 0; n < len; n++, src += delta) | |
150 | { | |
151 | ||
152 | result += *src; | |
80927a56 | 153 | } |
0cd0559e | 154 | |
6de9cd9a DN |
155 | *dest = result; |
156 | } | |
157 | } | |
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]) | |
80927a56 JJ |
164 | { |
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) | |
174 | { | |
175 | /* Break out of the look. */ | |
da96f5ab TK |
176 | continue_loop = 0; |
177 | break; | |
80927a56 JJ |
178 | } |
179 | else | |
180 | { | |
181 | count[n]++; | |
182 | base += sstride[n]; | |
183 | dest += dstride[n]; | |
184 | } | |
185 | } | |
6de9cd9a DN |
186 | } |
187 | } | |
188 | ||
7d7b8bfe | 189 | |
64acfd99 JB |
190 | extern void msum_i4 (gfc_array_i4 * const restrict, |
191 | gfc_array_i4 * const restrict, const index_type * const restrict, | |
28dc6b33 | 192 | gfc_array_l1 * const restrict); |
7f68c75f | 193 | export_proto(msum_i4); |
7d7b8bfe | 194 | |
6de9cd9a | 195 | void |
64acfd99 JB |
196 | msum_i4 (gfc_array_i4 * const restrict retarray, |
197 | gfc_array_i4 * const restrict array, | |
198 | const index_type * const restrict pdim, | |
28dc6b33 | 199 | gfc_array_l1 * const restrict mask) |
6de9cd9a | 200 | { |
e33e218b TK |
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]; | |
64acfd99 JB |
206 | GFC_INTEGER_4 * restrict dest; |
207 | const GFC_INTEGER_4 * restrict base; | |
28dc6b33 | 208 | const GFC_LOGICAL_1 * restrict mbase; |
6de9cd9a DN |
209 | int rank; |
210 | int dim; | |
211 | index_type n; | |
212 | index_type len; | |
213 | index_type delta; | |
214 | index_type mdelta; | |
28dc6b33 | 215 | int mask_kind; |
6de9cd9a DN |
216 | |
217 | dim = (*pdim) - 1; | |
218 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
e33e218b | 219 | |
dfb55fdc | 220 | len = GFC_DESCRIPTOR_EXTENT(array,dim); |
6de9cd9a DN |
221 | if (len <= 0) |
222 | return; | |
28dc6b33 | 223 | |
21d1335b | 224 | mbase = mask->base_addr; |
28dc6b33 TK |
225 | |
226 | mask_kind = GFC_DESCRIPTOR_SIZE (mask); | |
227 | ||
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 | |
232 | ) | |
233 | mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind); | |
234 | else | |
235 | runtime_error ("Funny sized logical array"); | |
236 | ||
dfb55fdc TK |
237 | delta = GFC_DESCRIPTOR_STRIDE(array,dim); |
238 | mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim); | |
6de9cd9a DN |
239 | |
240 | for (n = 0; n < dim; n++) | |
241 | { | |
dfb55fdc TK |
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); | |
80ee04b9 TK |
245 | |
246 | if (extent[n] < 0) | |
247 | extent[n] = 0; | |
248 | ||
6de9cd9a DN |
249 | } |
250 | for (n = dim; n < rank; n++) | |
251 | { | |
dfb55fdc TK |
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); | |
80ee04b9 TK |
255 | |
256 | if (extent[n] < 0) | |
257 | extent[n] = 0; | |
6de9cd9a DN |
258 | } |
259 | ||
21d1335b | 260 | if (retarray->base_addr == NULL) |
50dd63a9 | 261 | { |
dfb55fdc | 262 | size_t alloc_size, str; |
80ee04b9 | 263 | |
50dd63a9 | 264 | for (n = 0; n < rank; n++) |
80927a56 JJ |
265 | { |
266 | if (n == 0) | |
267 | str = 1; | |
268 | else | |
269 | str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; | |
dfb55fdc TK |
270 | |
271 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
272 | ||
80927a56 | 273 | } |
50dd63a9 | 274 | |
dfb55fdc | 275 | alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
80ee04b9 TK |
276 | * extent[rank-1]; |
277 | ||
efd4dc1a | 278 | retarray->offset = 0; |
50dd63a9 | 279 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
80ee04b9 TK |
280 | |
281 | if (alloc_size == 0) | |
282 | { | |
283 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 284 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
80ee04b9 TK |
285 | return; |
286 | } | |
287 | else | |
1a0fd3d3 | 288 | retarray->base_addr = xmalloc (alloc_size); |
80ee04b9 | 289 | |
50dd63a9 TK |
290 | } |
291 | else | |
292 | { | |
50dd63a9 | 293 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
fd6590f8 TK |
294 | runtime_error ("rank of return array incorrect in SUM intrinsic"); |
295 | ||
9731c4a3 | 296 | if (unlikely (compile_options.bounds_check)) |
fd6590f8 | 297 | { |
16bff921 TK |
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"); | |
fd6590f8 | 302 | } |
50dd63a9 TK |
303 | } |
304 | ||
6de9cd9a DN |
305 | for (n = 0; n < rank; n++) |
306 | { | |
307 | count[n] = 0; | |
dfb55fdc | 308 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
6de9cd9a | 309 | if (extent[n] <= 0) |
80927a56 | 310 | return; |
6de9cd9a DN |
311 | } |
312 | ||
21d1335b TB |
313 | dest = retarray->base_addr; |
314 | base = array->base_addr; | |
6de9cd9a DN |
315 | |
316 | while (base) | |
317 | { | |
64acfd99 | 318 | const GFC_INTEGER_4 * restrict src; |
28dc6b33 | 319 | const GFC_LOGICAL_1 * restrict msrc; |
6de9cd9a DN |
320 | GFC_INTEGER_4 result; |
321 | src = base; | |
322 | msrc = mbase; | |
323 | { | |
324 | ||
325 | result = 0; | |
036e1775 | 326 | for (n = 0; n < len; n++, src += delta, msrc += mdelta) |
6de9cd9a | 327 | { |
6de9cd9a DN |
328 | |
329 | if (*msrc) | |
330 | result += *src; | |
6de9cd9a | 331 | } |
036e1775 | 332 | *dest = result; |
6de9cd9a DN |
333 | } |
334 | /* Advance to the next element. */ | |
335 | count[0]++; | |
336 | base += sstride[0]; | |
337 | mbase += mstride[0]; | |
338 | dest += dstride[0]; | |
339 | n = 0; | |
340 | while (count[n] == extent[n]) | |
80927a56 JJ |
341 | { |
342 | /* When we get to the end of a dimension, reset it and increment | |
343 | the next dimension. */ | |
344 | count[n] = 0; | |
345 | /* We could precalculate these products, but this is a less | |
346 | frequently used path so probably not worth it. */ | |
347 | base -= sstride[n] * extent[n]; | |
348 | mbase -= mstride[n] * extent[n]; | |
349 | dest -= dstride[n] * extent[n]; | |
350 | n++; | |
351 | if (n == rank) | |
352 | { | |
353 | /* Break out of the look. */ | |
354 | base = NULL; | |
355 | break; | |
356 | } | |
357 | else | |
358 | { | |
359 | count[n]++; | |
360 | base += sstride[n]; | |
361 | mbase += mstride[n]; | |
362 | dest += dstride[n]; | |
363 | } | |
364 | } | |
6de9cd9a DN |
365 | } |
366 | } | |
644cb69f | 367 | |
97a62038 TK |
368 | |
369 | extern void ssum_i4 (gfc_array_i4 * const restrict, | |
370 | gfc_array_i4 * const restrict, const index_type * const restrict, | |
371 | GFC_LOGICAL_4 *); | |
372 | export_proto(ssum_i4); | |
373 | ||
374 | void | |
375 | ssum_i4 (gfc_array_i4 * const restrict retarray, | |
376 | gfc_array_i4 * const restrict array, | |
377 | const index_type * const restrict pdim, | |
378 | GFC_LOGICAL_4 * mask) | |
379 | { | |
802367d7 TK |
380 | index_type count[GFC_MAX_DIMENSIONS]; |
381 | index_type extent[GFC_MAX_DIMENSIONS]; | |
802367d7 TK |
382 | index_type dstride[GFC_MAX_DIMENSIONS]; |
383 | GFC_INTEGER_4 * restrict dest; | |
97a62038 TK |
384 | index_type rank; |
385 | index_type n; | |
802367d7 TK |
386 | index_type dim; |
387 | ||
97a62038 TK |
388 | |
389 | if (*mask) | |
390 | { | |
391 | sum_i4 (retarray, array, pdim); | |
392 | return; | |
393 | } | |
802367d7 TK |
394 | /* Make dim zero based to avoid confusion. */ |
395 | dim = (*pdim) - 1; | |
396 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
397 | ||
398 | for (n = 0; n < dim; n++) | |
399 | { | |
dfb55fdc | 400 | extent[n] = GFC_DESCRIPTOR_EXTENT(array,n); |
802367d7 TK |
401 | |
402 | if (extent[n] <= 0) | |
403 | extent[n] = 0; | |
404 | } | |
405 | ||
406 | for (n = dim; n < rank; n++) | |
407 | { | |
802367d7 | 408 | extent[n] = |
80927a56 | 409 | GFC_DESCRIPTOR_EXTENT(array,n + 1); |
802367d7 TK |
410 | |
411 | if (extent[n] <= 0) | |
80927a56 | 412 | extent[n] = 0; |
802367d7 | 413 | } |
97a62038 | 414 | |
21d1335b | 415 | if (retarray->base_addr == NULL) |
97a62038 | 416 | { |
dfb55fdc | 417 | size_t alloc_size, str; |
802367d7 TK |
418 | |
419 | for (n = 0; n < rank; n++) | |
80927a56 JJ |
420 | { |
421 | if (n == 0) | |
422 | str = 1; | |
423 | else | |
424 | str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; | |
dfb55fdc TK |
425 | |
426 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
427 | ||
80927a56 | 428 | } |
802367d7 | 429 | |
97a62038 | 430 | retarray->offset = 0; |
802367d7 TK |
431 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
432 | ||
dfb55fdc | 433 | alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
802367d7 TK |
434 | * extent[rank-1]; |
435 | ||
436 | if (alloc_size == 0) | |
437 | { | |
438 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 439 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
802367d7 TK |
440 | return; |
441 | } | |
442 | else | |
1a0fd3d3 | 443 | retarray->base_addr = xmalloc (alloc_size); |
97a62038 TK |
444 | } |
445 | else | |
446 | { | |
802367d7 TK |
447 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
448 | runtime_error ("rank of return array incorrect in" | |
449 | " SUM intrinsic: is %ld, should be %ld", | |
450 | (long int) (GFC_DESCRIPTOR_RANK (retarray)), | |
451 | (long int) rank); | |
452 | ||
9731c4a3 | 453 | if (unlikely (compile_options.bounds_check)) |
fd6590f8 | 454 | { |
802367d7 TK |
455 | for (n=0; n < rank; n++) |
456 | { | |
457 | index_type ret_extent; | |
97a62038 | 458 | |
dfb55fdc | 459 | ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n); |
802367d7 TK |
460 | if (extent[n] != ret_extent) |
461 | runtime_error ("Incorrect extent in return value of" | |
462 | " SUM intrinsic in dimension %ld:" | |
463 | " is %ld, should be %ld", (long int) n + 1, | |
464 | (long int) ret_extent, (long int) extent[n]); | |
465 | } | |
fd6590f8 TK |
466 | } |
467 | } | |
97a62038 | 468 | |
802367d7 TK |
469 | for (n = 0; n < rank; n++) |
470 | { | |
471 | count[n] = 0; | |
dfb55fdc | 472 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
802367d7 TK |
473 | } |
474 | ||
21d1335b | 475 | dest = retarray->base_addr; |
802367d7 TK |
476 | |
477 | while(1) | |
478 | { | |
479 | *dest = 0; | |
480 | count[0]++; | |
481 | dest += dstride[0]; | |
482 | n = 0; | |
483 | while (count[n] == extent[n]) | |
80927a56 | 484 | { |
802367d7 | 485 | /* When we get to the end of a dimension, reset it and increment |
80927a56 JJ |
486 | the next dimension. */ |
487 | count[n] = 0; | |
488 | /* We could precalculate these products, but this is a less | |
489 | frequently used path so probably not worth it. */ | |
490 | dest -= dstride[n] * extent[n]; | |
491 | n++; | |
492 | if (n == rank) | |
802367d7 | 493 | return; |
80927a56 JJ |
494 | else |
495 | { | |
496 | count[n]++; | |
497 | dest += dstride[n]; | |
498 | } | |
802367d7 TK |
499 | } |
500 | } | |
97a62038 TK |
501 | } |
502 | ||
644cb69f | 503 | #endif |