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