1 /* Implementation of the PRODUCT intrinsic
2 Copyright 2002, 2007, 2009 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 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"
31 #if defined (HAVE_GFC_REAL_16) && defined (HAVE_GFC_REAL_16)
34 extern void product_r16 (gfc_array_r16
* const restrict
,
35 gfc_array_r16
* const restrict
, const index_type
* const restrict
);
36 export_proto(product_r16
);
39 product_r16 (gfc_array_r16
* const restrict retarray
,
40 gfc_array_r16
* const restrict array
,
41 const index_type
* const restrict pdim
)
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_REAL_16
* restrict base
;
48 GFC_REAL_16
* restrict dest
;
56 /* Make dim zero based to avoid confusion. */
58 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
60 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
63 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
65 for (n
= 0; n
< dim
; n
++)
67 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
68 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
73 for (n
= dim
; n
< rank
; n
++)
75 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
76 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
82 if (retarray
->data
== NULL
)
84 size_t alloc_size
, str
;
86 for (n
= 0; n
< rank
; n
++)
91 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
93 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
98 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
100 alloc_size
= sizeof (GFC_REAL_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
105 /* Make sure we have a zero-sized array. */
106 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
111 retarray
->data
= internal_malloc_size (alloc_size
);
115 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
116 runtime_error ("rank of return array incorrect in"
117 " PRODUCT intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
121 if (unlikely (compile_options
.bounds_check
))
123 for (n
=0; n
< rank
; n
++)
125 index_type ret_extent
;
127 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
128 if (extent
[n
] != ret_extent
)
129 runtime_error ("Incorrect extent in return value of"
130 " PRODUCT intrinsic in dimension %ld:"
131 " is %ld, should be %ld", (long int) n
+ 1,
132 (long int) ret_extent
, (long int) extent
[n
]);
137 for (n
= 0; n
< rank
; n
++)
140 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
146 dest
= retarray
->data
;
149 while (continue_loop
)
151 const GFC_REAL_16
* restrict src
;
161 for (n
= 0; n
< len
; n
++, src
+= delta
)
169 /* Advance to the next element. */
174 while (count
[n
] == extent
[n
])
176 /* When we get to the end of a dimension, reset it and increment
177 the next dimension. */
179 /* We could precalculate these products, but this is a less
180 frequently used path so probably not worth it. */
181 base
-= sstride
[n
] * extent
[n
];
182 dest
-= dstride
[n
] * extent
[n
];
186 /* Break out of the look. */
201 extern void mproduct_r16 (gfc_array_r16
* const restrict
,
202 gfc_array_r16
* const restrict
, const index_type
* const restrict
,
203 gfc_array_l1
* const restrict
);
204 export_proto(mproduct_r16
);
207 mproduct_r16 (gfc_array_r16
* const restrict retarray
,
208 gfc_array_r16
* const restrict array
,
209 const index_type
* const restrict pdim
,
210 gfc_array_l1
* const restrict mask
)
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
];
217 GFC_REAL_16
* restrict dest
;
218 const GFC_REAL_16
* restrict base
;
219 const GFC_LOGICAL_1
* restrict mbase
;
229 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
231 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
237 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
239 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
240 #ifdef HAVE_GFC_LOGICAL_16
244 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
246 runtime_error ("Funny sized logical array");
248 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
249 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
251 for (n
= 0; n
< dim
; n
++)
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
);
261 for (n
= dim
; n
< rank
; n
++)
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);
271 if (retarray
->data
== NULL
)
273 size_t alloc_size
, str
;
275 for (n
= 0; n
< rank
; n
++)
280 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
282 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
286 alloc_size
= sizeof (GFC_REAL_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
289 retarray
->offset
= 0;
290 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
294 /* Make sure we have a zero-sized array. */
295 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
299 retarray
->data
= internal_malloc_size (alloc_size
);
304 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
305 runtime_error ("rank of return array incorrect in PRODUCT intrinsic");
307 if (unlikely (compile_options
.bounds_check
))
309 for (n
=0; n
< rank
; n
++)
311 index_type ret_extent
;
313 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
314 if (extent
[n
] != ret_extent
)
315 runtime_error ("Incorrect extent in return value of"
316 " PRODUCT intrinsic in dimension %ld:"
317 " is %ld, should be %ld", (long int) n
+ 1,
318 (long int) ret_extent
, (long int) extent
[n
]);
320 for (n
=0; n
<= rank
; n
++)
322 index_type mask_extent
, array_extent
;
324 array_extent
= GFC_DESCRIPTOR_EXTENT(array
,n
);
325 mask_extent
= GFC_DESCRIPTOR_EXTENT(mask
,n
);
326 if (array_extent
!= mask_extent
)
327 runtime_error ("Incorrect extent in MASK argument of"
328 " PRODUCT intrinsic in dimension %ld:"
329 " is %ld, should be %ld", (long int) n
+ 1,
330 (long int) mask_extent
, (long int) array_extent
);
335 for (n
= 0; n
< rank
; n
++)
338 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
343 dest
= retarray
->data
;
348 const GFC_REAL_16
* restrict src
;
349 const GFC_LOGICAL_1
* restrict msrc
;
360 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
369 /* Advance to the next element. */
375 while (count
[n
] == extent
[n
])
377 /* When we get to the end of a dimension, reset it and increment
378 the next dimension. */
380 /* We could precalculate these products, but this is a less
381 frequently used path so probably not worth it. */
382 base
-= sstride
[n
] * extent
[n
];
383 mbase
-= mstride
[n
] * extent
[n
];
384 dest
-= dstride
[n
] * extent
[n
];
388 /* Break out of the look. */
404 extern void sproduct_r16 (gfc_array_r16
* const restrict
,
405 gfc_array_r16
* const restrict
, const index_type
* const restrict
,
407 export_proto(sproduct_r16
);
410 sproduct_r16 (gfc_array_r16
* const restrict retarray
,
411 gfc_array_r16
* const restrict array
,
412 const index_type
* const restrict pdim
,
413 GFC_LOGICAL_4
* mask
)
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_16
* restrict dest
;
427 product_r16 (retarray
, array
, pdim
);
430 /* Make dim zero based to avoid confusion. */
432 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
434 for (n
= 0; n
< dim
; n
++)
436 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
437 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
443 for (n
= dim
; n
< rank
; n
++)
445 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
447 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
453 if (retarray
->data
== NULL
)
455 size_t alloc_size
, str
;
457 for (n
= 0; n
< rank
; n
++)
462 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
464 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
468 retarray
->offset
= 0;
469 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
471 alloc_size
= sizeof (GFC_REAL_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
476 /* Make sure we have a zero-sized array. */
477 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
481 retarray
->data
= internal_malloc_size (alloc_size
);
485 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
486 runtime_error ("rank of return array incorrect in"
487 " PRODUCT intrinsic: is %ld, should be %ld",
488 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
491 if (unlikely (compile_options
.bounds_check
))
493 for (n
=0; n
< rank
; n
++)
495 index_type ret_extent
;
497 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
498 if (extent
[n
] != ret_extent
)
499 runtime_error ("Incorrect extent in return value of"
500 " PRODUCT intrinsic in dimension %ld:"
501 " is %ld, should be %ld", (long int) n
+ 1,
502 (long int) ret_extent
, (long int) extent
[n
]);
507 for (n
= 0; n
< rank
; n
++)
510 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
513 dest
= retarray
->data
;
521 while (count
[n
] == extent
[n
])
523 /* When we get to the end of a dimension, reset it and increment
524 the next dimension. */
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
];