1 /* Implementation of the PRODUCT intrinsic
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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"
29 #if defined (HAVE_GFC_INTEGER_2) && defined (HAVE_GFC_INTEGER_2)
32 extern void product_i2 (gfc_array_i2
* const restrict
,
33 gfc_array_i2
* const restrict
, const index_type
* const restrict
);
34 export_proto(product_i2
);
37 product_i2 (gfc_array_i2
* const restrict retarray
,
38 gfc_array_i2
* const restrict array
,
39 const index_type
* const restrict pdim
)
41 index_type count
[GFC_MAX_DIMENSIONS
];
42 index_type extent
[GFC_MAX_DIMENSIONS
];
43 index_type sstride
[GFC_MAX_DIMENSIONS
];
44 index_type dstride
[GFC_MAX_DIMENSIONS
];
45 const GFC_INTEGER_2
* restrict base
;
46 GFC_INTEGER_2
* restrict dest
;
54 /* Make dim zero based to avoid confusion. */
55 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
58 if (unlikely (dim
< 0 || dim
> rank
))
60 runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
61 "is %ld, should be between 1 and %ld",
62 (long int) dim
+ 1, (long int) rank
+ 1);
65 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
68 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
70 for (n
= 0; n
< dim
; n
++)
72 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
73 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
78 for (n
= dim
; n
< rank
; n
++)
80 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
81 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
87 if (retarray
->base_addr
== NULL
)
89 size_t alloc_size
, str
;
91 for (n
= 0; n
< rank
; n
++)
96 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
98 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
102 retarray
->offset
= 0;
103 retarray
->dtype
.rank
= rank
;
105 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
107 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_2
));
113 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
114 runtime_error ("rank of return array incorrect in"
115 " PRODUCT intrinsic: is %ld, should be %ld",
116 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
119 if (unlikely (compile_options
.bounds_check
))
120 bounds_ifunction_return ((array_t
*) retarray
, extent
,
121 "return value", "PRODUCT");
124 for (n
= 0; n
< rank
; n
++)
127 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
132 base
= array
->base_addr
;
133 dest
= retarray
->base_addr
;
136 while (continue_loop
)
138 const GFC_INTEGER_2
* restrict src
;
139 GFC_INTEGER_2 result
;
148 #if ! defined HAVE_BACK_ARG
149 for (n
= 0; n
< len
; n
++, src
+= delta
)
159 /* Advance to the next element. */
164 while (count
[n
] == extent
[n
])
166 /* When we get to the end of a dimension, reset it and increment
167 the next dimension. */
169 /* We could precalculate these products, but this is a less
170 frequently used path so probably not worth it. */
171 base
-= sstride
[n
] * extent
[n
];
172 dest
-= dstride
[n
] * extent
[n
];
176 /* Break out of the loop. */
191 extern void mproduct_i2 (gfc_array_i2
* const restrict
,
192 gfc_array_i2
* const restrict
, const index_type
* const restrict
,
193 gfc_array_l1
* const restrict
);
194 export_proto(mproduct_i2
);
197 mproduct_i2 (gfc_array_i2
* const restrict retarray
,
198 gfc_array_i2
* const restrict array
,
199 const index_type
* const restrict pdim
,
200 gfc_array_l1
* const restrict mask
)
202 index_type count
[GFC_MAX_DIMENSIONS
];
203 index_type extent
[GFC_MAX_DIMENSIONS
];
204 index_type sstride
[GFC_MAX_DIMENSIONS
];
205 index_type dstride
[GFC_MAX_DIMENSIONS
];
206 index_type mstride
[GFC_MAX_DIMENSIONS
];
207 GFC_INTEGER_2
* restrict dest
;
208 const GFC_INTEGER_2
* restrict base
;
209 const GFC_LOGICAL_1
* restrict mbase
;
221 product_i2 (retarray
, array
, pdim
, back
);
223 product_i2 (retarray
, array
, pdim
);
229 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
232 if (unlikely (dim
< 0 || dim
> rank
))
234 runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
235 "is %ld, should be between 1 and %ld",
236 (long int) dim
+ 1, (long int) rank
+ 1);
239 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
243 mbase
= mask
->base_addr
;
245 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
247 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
248 #ifdef HAVE_GFC_LOGICAL_16
252 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
254 runtime_error ("Funny sized logical array");
256 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
257 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
259 for (n
= 0; n
< dim
; n
++)
261 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
262 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
263 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
269 for (n
= dim
; n
< rank
; n
++)
271 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
272 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
273 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
279 if (retarray
->base_addr
== NULL
)
281 size_t alloc_size
, str
;
283 for (n
= 0; n
< rank
; n
++)
288 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
290 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
294 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
296 retarray
->offset
= 0;
297 retarray
->dtype
.rank
= rank
;
299 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_2
));
305 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
306 runtime_error ("rank of return array incorrect in PRODUCT intrinsic");
308 if (unlikely (compile_options
.bounds_check
))
310 bounds_ifunction_return ((array_t
*) retarray
, extent
,
311 "return value", "PRODUCT");
312 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
313 "MASK argument", "PRODUCT");
317 for (n
= 0; n
< rank
; n
++)
320 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
325 dest
= retarray
->base_addr
;
326 base
= array
->base_addr
;
330 const GFC_INTEGER_2
* restrict src
;
331 const GFC_LOGICAL_1
* restrict msrc
;
332 GFC_INTEGER_2 result
;
338 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
346 /* Advance to the next element. */
352 while (count
[n
] == extent
[n
])
354 /* When we get to the end of a dimension, reset it and increment
355 the next dimension. */
357 /* We could precalculate these products, but this is a less
358 frequently used path so probably not worth it. */
359 base
-= sstride
[n
] * extent
[n
];
360 mbase
-= mstride
[n
] * extent
[n
];
361 dest
-= dstride
[n
] * extent
[n
];
365 /* Break out of the loop. */
381 extern void sproduct_i2 (gfc_array_i2
* const restrict
,
382 gfc_array_i2
* const restrict
, const index_type
* const restrict
,
384 export_proto(sproduct_i2
);
387 sproduct_i2 (gfc_array_i2
* const restrict retarray
,
388 gfc_array_i2
* const restrict array
,
389 const index_type
* const restrict pdim
,
390 GFC_LOGICAL_4
* mask
)
392 index_type count
[GFC_MAX_DIMENSIONS
];
393 index_type extent
[GFC_MAX_DIMENSIONS
];
394 index_type dstride
[GFC_MAX_DIMENSIONS
];
395 GFC_INTEGER_2
* restrict dest
;
401 if (mask
== NULL
|| *mask
)
404 product_i2 (retarray
, array
, pdim
, back
);
406 product_i2 (retarray
, array
, pdim
);
410 /* Make dim zero based to avoid confusion. */
412 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
414 if (unlikely (dim
< 0 || dim
> rank
))
416 runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
417 "is %ld, should be between 1 and %ld",
418 (long int) dim
+ 1, (long int) rank
+ 1);
421 for (n
= 0; n
< dim
; n
++)
423 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
429 for (n
= dim
; n
< rank
; n
++)
432 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
438 if (retarray
->base_addr
== NULL
)
440 size_t alloc_size
, str
;
442 for (n
= 0; n
< rank
; n
++)
447 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
449 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
453 retarray
->offset
= 0;
454 retarray
->dtype
.rank
= rank
;
456 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
458 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_2
));
464 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
465 runtime_error ("rank of return array incorrect in"
466 " PRODUCT intrinsic: is %ld, should be %ld",
467 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
470 if (unlikely (compile_options
.bounds_check
))
472 for (n
=0; n
< rank
; n
++)
474 index_type ret_extent
;
476 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
477 if (extent
[n
] != ret_extent
)
478 runtime_error ("Incorrect extent in return value of"
479 " PRODUCT intrinsic in dimension %ld:"
480 " is %ld, should be %ld", (long int) n
+ 1,
481 (long int) ret_extent
, (long int) extent
[n
]);
486 for (n
= 0; n
< rank
; n
++)
489 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
492 dest
= retarray
->base_addr
;
500 while (count
[n
] == extent
[n
])
502 /* When we get to the end of a dimension, reset it and increment
503 the next dimension. */
505 /* We could precalculate these products, but this is a less
506 frequently used path so probably not worth it. */
507 dest
-= dstride
[n
] * extent
[n
];