]>
Commit | Line | Data |
---|---|---|
567c915b | 1 | /* Implementation of the PRODUCT intrinsic |
0cd0559e | 2 | Copyright 2002, 2007, 2009, 2010 Free Software Foundation, Inc. |
567c915b TK |
3 | Contributed by Paul Brook <paul@nowt.org> |
4 | ||
0cd0559e | 5 | This file is part of the GNU Fortran runtime library (libgfortran). |
567c915b TK |
6 | |
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 | |
748086b7 | 10 | version 3 of the License, or (at your option) any later version. |
567c915b TK |
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 | |
15 | GNU General Public License for more details. | |
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/>. */ | |
567c915b | 25 | |
36ae8a61 | 26 | #include "libgfortran.h" |
567c915b TK |
27 | #include <stdlib.h> |
28 | #include <assert.h> | |
567c915b TK |
29 | |
30 | ||
31 | #if defined (HAVE_GFC_INTEGER_2) && defined (HAVE_GFC_INTEGER_2) | |
32 | ||
33 | ||
34 | extern void product_i2 (gfc_array_i2 * const restrict, | |
35 | gfc_array_i2 * const restrict, const index_type * const restrict); | |
36 | export_proto(product_i2); | |
37 | ||
38 | void | |
39 | product_i2 (gfc_array_i2 * const restrict retarray, | |
40 | gfc_array_i2 * const restrict array, | |
41 | const index_type * const restrict pdim) | |
42 | { | |
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; | |
da96f5ab | 54 | int continue_loop; |
567c915b TK |
55 | |
56 | /* Make dim zero based to avoid confusion. */ | |
57 | dim = (*pdim) - 1; | |
58 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
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); |
567c915b TK |
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); | |
567c915b TK |
69 | |
70 | if (extent[n] < 0) | |
71 | extent[n] = 0; | |
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); | |
567c915b TK |
77 | |
78 | if (extent[n] < 0) | |
79 | extent[n] = 0; | |
80 | } | |
81 | ||
82 | if (retarray->data == NULL) | |
83 | { | |
dfb55fdc | 84 | size_t alloc_size, str; |
567c915b TK |
85 | |
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 | } |
567c915b TK |
96 | |
97 | retarray->offset = 0; | |
98 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; | |
99 | ||
dfb55fdc | 100 | alloc_size = sizeof (GFC_INTEGER_2) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
567c915b TK |
101 | * extent[rank-1]; |
102 | ||
95928a13 | 103 | retarray->data = internal_malloc_size (alloc_size); |
567c915b 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); |
567c915b | 108 | return; |
dfb55fdc | 109 | |
567c915b | 110 | } |
567c915b TK |
111 | } |
112 | else | |
113 | { | |
114 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) | |
fd6590f8 | 115 | runtime_error ("rank of return array incorrect in" |
ccacefc7 TK |
116 | " PRODUCT 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", "PRODUCT"); | |
567c915b TK |
123 | } |
124 | ||
125 | for (n = 0; n < rank; n++) | |
126 | { | |
127 | count[n] = 0; | |
dfb55fdc | 128 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
567c915b | 129 | if (extent[n] <= 0) |
facc1285 | 130 | return; |
567c915b TK |
131 | } |
132 | ||
133 | base = array->data; | |
134 | dest = retarray->data; | |
135 | ||
da96f5ab TK |
136 | continue_loop = 1; |
137 | while (continue_loop) | |
567c915b TK |
138 | { |
139 | const GFC_INTEGER_2 * restrict src; | |
140 | GFC_INTEGER_2 result; | |
141 | src = base; | |
142 | { | |
143 | ||
144 | result = 1; | |
80927a56 | 145 | if (len <= 0) |
567c915b TK |
146 | *dest = 1; |
147 | else | |
148 | { | |
149 | for (n = 0; n < len; n++, src += delta) | |
150 | { | |
151 | ||
152 | result *= *src; | |
80927a56 | 153 | } |
0cd0559e | 154 | |
567c915b TK |
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 | } | |
567c915b TK |
186 | } |
187 | } | |
188 | ||
189 | ||
190 | extern void mproduct_i2 (gfc_array_i2 * const restrict, | |
191 | gfc_array_i2 * const restrict, const index_type * const restrict, | |
28dc6b33 | 192 | gfc_array_l1 * const restrict); |
567c915b TK |
193 | export_proto(mproduct_i2); |
194 | ||
195 | void | |
196 | mproduct_i2 (gfc_array_i2 * const restrict retarray, | |
197 | gfc_array_i2 * const restrict array, | |
198 | const index_type * const restrict pdim, | |
28dc6b33 | 199 | gfc_array_l1 * const restrict mask) |
567c915b TK |
200 | { |
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; | |
28dc6b33 | 208 | const GFC_LOGICAL_1 * restrict mbase; |
567c915b TK |
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; |
567c915b TK |
216 | |
217 | dim = (*pdim) - 1; | |
218 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
219 | ||
dfb55fdc | 220 | len = GFC_DESCRIPTOR_EXTENT(array,dim); |
567c915b TK |
221 | if (len <= 0) |
222 | return; | |
28dc6b33 TK |
223 | |
224 | mbase = mask->data; | |
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); | |
567c915b TK |
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); | |
567c915b TK |
245 | |
246 | if (extent[n] < 0) | |
247 | extent[n] = 0; | |
248 | ||
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); | |
567c915b TK |
255 | |
256 | if (extent[n] < 0) | |
257 | extent[n] = 0; | |
258 | } | |
259 | ||
260 | if (retarray->data == NULL) | |
261 | { | |
dfb55fdc | 262 | size_t alloc_size, str; |
567c915b TK |
263 | |
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 | } |
567c915b | 274 | |
dfb55fdc | 275 | alloc_size = sizeof (GFC_INTEGER_2) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
567c915b TK |
276 | * extent[rank-1]; |
277 | ||
278 | retarray->offset = 0; | |
279 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; | |
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); |
567c915b TK |
285 | return; |
286 | } | |
287 | else | |
288 | retarray->data = internal_malloc_size (alloc_size); | |
289 | ||
290 | } | |
291 | else | |
292 | { | |
293 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) | |
fd6590f8 TK |
294 | runtime_error ("rank of return array incorrect in PRODUCT 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", "PRODUCT"); | |
300 | bounds_equal_extents ((array_t *) mask, (array_t *) array, | |
301 | "MASK argument", "PRODUCT"); | |
fd6590f8 | 302 | } |
567c915b TK |
303 | } |
304 | ||
305 | for (n = 0; n < rank; n++) | |
306 | { | |
307 | count[n] = 0; | |
dfb55fdc | 308 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
567c915b | 309 | if (extent[n] <= 0) |
80927a56 | 310 | return; |
567c915b TK |
311 | } |
312 | ||
313 | dest = retarray->data; | |
314 | base = array->data; | |
567c915b TK |
315 | |
316 | while (base) | |
317 | { | |
318 | const GFC_INTEGER_2 * restrict src; | |
28dc6b33 | 319 | const GFC_LOGICAL_1 * restrict msrc; |
567c915b TK |
320 | GFC_INTEGER_2 result; |
321 | src = base; | |
322 | msrc = mbase; | |
323 | { | |
324 | ||
325 | result = 1; | |
80927a56 | 326 | if (len <= 0) |
567c915b TK |
327 | *dest = 1; |
328 | else | |
329 | { | |
330 | for (n = 0; n < len; n++, src += delta, msrc += mdelta) | |
331 | { | |
332 | ||
333 | if (*msrc) | |
334 | result *= *src; | |
80927a56 | 335 | } |
567c915b TK |
336 | *dest = result; |
337 | } | |
338 | } | |
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]) | |
80927a56 JJ |
346 | { |
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) | |
357 | { | |
358 | /* Break out of the look. */ | |
359 | base = NULL; | |
360 | break; | |
361 | } | |
362 | else | |
363 | { | |
364 | count[n]++; | |
365 | base += sstride[n]; | |
366 | mbase += mstride[n]; | |
367 | dest += dstride[n]; | |
368 | } | |
369 | } | |
567c915b TK |
370 | } |
371 | } | |
372 | ||
373 | ||
374 | extern void sproduct_i2 (gfc_array_i2 * const restrict, | |
375 | gfc_array_i2 * const restrict, const index_type * const restrict, | |
376 | GFC_LOGICAL_4 *); | |
377 | export_proto(sproduct_i2); | |
378 | ||
379 | void | |
380 | sproduct_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) | |
384 | { | |
802367d7 TK |
385 | index_type count[GFC_MAX_DIMENSIONS]; |
386 | index_type extent[GFC_MAX_DIMENSIONS]; | |
802367d7 TK |
387 | index_type dstride[GFC_MAX_DIMENSIONS]; |
388 | GFC_INTEGER_2 * restrict dest; | |
567c915b TK |
389 | index_type rank; |
390 | index_type n; | |
802367d7 TK |
391 | index_type dim; |
392 | ||
567c915b TK |
393 | |
394 | if (*mask) | |
395 | { | |
396 | product_i2 (retarray, array, pdim); | |
397 | return; | |
398 | } | |
802367d7 TK |
399 | /* Make dim zero based to avoid confusion. */ |
400 | dim = (*pdim) - 1; | |
401 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
402 | ||
403 | for (n = 0; n < dim; n++) | |
404 | { | |
dfb55fdc | 405 | extent[n] = GFC_DESCRIPTOR_EXTENT(array,n); |
802367d7 TK |
406 | |
407 | if (extent[n] <= 0) | |
408 | extent[n] = 0; | |
409 | } | |
410 | ||
411 | for (n = dim; n < rank; n++) | |
412 | { | |
802367d7 | 413 | extent[n] = |
80927a56 | 414 | GFC_DESCRIPTOR_EXTENT(array,n + 1); |
802367d7 TK |
415 | |
416 | if (extent[n] <= 0) | |
80927a56 | 417 | extent[n] = 0; |
802367d7 | 418 | } |
567c915b TK |
419 | |
420 | if (retarray->data == NULL) | |
421 | { | |
dfb55fdc | 422 | size_t alloc_size, str; |
802367d7 TK |
423 | |
424 | for (n = 0; n < rank; n++) | |
80927a56 JJ |
425 | { |
426 | if (n == 0) | |
427 | str = 1; | |
428 | else | |
429 | str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1]; | |
dfb55fdc TK |
430 | |
431 | GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str); | |
432 | ||
80927a56 | 433 | } |
802367d7 | 434 | |
567c915b | 435 | retarray->offset = 0; |
802367d7 TK |
436 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
437 | ||
dfb55fdc | 438 | alloc_size = sizeof (GFC_INTEGER_2) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
802367d7 TK |
439 | * extent[rank-1]; |
440 | ||
441 | if (alloc_size == 0) | |
442 | { | |
443 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 444 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
802367d7 TK |
445 | return; |
446 | } | |
447 | else | |
448 | retarray->data = internal_malloc_size (alloc_size); | |
567c915b TK |
449 | } |
450 | else | |
451 | { | |
802367d7 TK |
452 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
453 | runtime_error ("rank of return array incorrect in" | |
454 | " PRODUCT intrinsic: is %ld, should be %ld", | |
455 | (long int) (GFC_DESCRIPTOR_RANK (retarray)), | |
456 | (long int) rank); | |
457 | ||
9731c4a3 | 458 | if (unlikely (compile_options.bounds_check)) |
fd6590f8 | 459 | { |
802367d7 TK |
460 | for (n=0; n < rank; n++) |
461 | { | |
462 | index_type ret_extent; | |
567c915b | 463 | |
dfb55fdc | 464 | ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n); |
802367d7 TK |
465 | if (extent[n] != ret_extent) |
466 | runtime_error ("Incorrect extent in return value of" | |
467 | " PRODUCT intrinsic in dimension %ld:" | |
468 | " is %ld, should be %ld", (long int) n + 1, | |
469 | (long int) ret_extent, (long int) extent[n]); | |
470 | } | |
fd6590f8 TK |
471 | } |
472 | } | |
567c915b | 473 | |
802367d7 TK |
474 | for (n = 0; n < rank; n++) |
475 | { | |
476 | count[n] = 0; | |
dfb55fdc | 477 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
802367d7 TK |
478 | } |
479 | ||
480 | dest = retarray->data; | |
481 | ||
482 | while(1) | |
483 | { | |
484 | *dest = 1; | |
485 | count[0]++; | |
486 | dest += dstride[0]; | |
487 | n = 0; | |
488 | while (count[n] == extent[n]) | |
80927a56 | 489 | { |
802367d7 | 490 | /* When we get to the end of a dimension, reset it and increment |
80927a56 JJ |
491 | the next dimension. */ |
492 | count[n] = 0; | |
493 | /* We could precalculate these products, but this is a less | |
494 | frequently used path so probably not worth it. */ | |
495 | dest -= dstride[n] * extent[n]; | |
496 | n++; | |
497 | if (n == rank) | |
802367d7 | 498 | return; |
80927a56 JJ |
499 | else |
500 | { | |
501 | count[n]++; | |
502 | dest += dstride[n]; | |
503 | } | |
802367d7 TK |
504 | } |
505 | } | |
567c915b TK |
506 | } |
507 | ||
508 | #endif |