1 /* Special implementation of the SPREAD intrinsic
2 Copyright (C) 2008-2018 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
4 spread_generic.c written by Paul Brook <paul@nowt.org>
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 Ligbfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
31 #if defined (HAVE_GFC_REAL_10)
34 spread_r10 (gfc_array_r10
*ret
, const gfc_array_r10
*source
,
35 const index_type along
, const index_type pncopies
)
37 /* r.* indicates the return array. */
38 index_type rstride
[GFC_MAX_DIMENSIONS
];
40 index_type rdelta
= 0;
44 GFC_REAL_10
* restrict dest
;
45 /* s.* indicates the source array. */
46 index_type sstride
[GFC_MAX_DIMENSIONS
];
49 const GFC_REAL_10
*sptr
;
51 index_type count
[GFC_MAX_DIMENSIONS
];
52 index_type extent
[GFC_MAX_DIMENSIONS
];
57 srank
= GFC_DESCRIPTOR_RANK(source
);
60 if (rrank
> GFC_MAX_DIMENSIONS
)
61 runtime_error ("return rank too large in spread()");
64 runtime_error ("dim outside of rank in spread()");
68 if (ret
->base_addr
== NULL
)
73 /* The front end has signalled that we need to populate the
74 return array descriptor. */
75 ret
->dtype
.rank
= rrank
;
79 for (n
= 0; n
< rrank
; n
++)
91 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
92 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
99 GFC_DIMENSION_SET(ret
->dim
[n
], 0, ub
, stride
);
103 /* xmallocarray allocates a single byte for zero size. */
104 ret
->base_addr
= xmallocarray (rs
, sizeof(GFC_REAL_10
));
115 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
116 runtime_error ("rank mismatch in spread()");
118 if (unlikely (compile_options
.bounds_check
))
120 for (n
= 0; n
< rrank
; n
++)
122 index_type ret_extent
;
124 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
127 rdelta
= GFC_DESCRIPTOR_STRIDE(ret
,n
);
129 if (ret_extent
!= ncopies
)
130 runtime_error("Incorrect extent in return value of SPREAD"
131 " intrinsic in dimension %ld: is %ld,"
132 " should be %ld", (long int) n
+1,
133 (long int) ret_extent
, (long int) ncopies
);
138 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
139 if (ret_extent
!= extent
[dim
])
140 runtime_error("Incorrect extent in return value of SPREAD"
141 " intrinsic in dimension %ld: is %ld,"
142 " should be %ld", (long int) n
+1,
143 (long int) ret_extent
,
144 (long int) extent
[dim
]);
146 if (extent
[dim
] <= 0)
148 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
149 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE(ret
,n
);
156 for (n
= 0; n
< rrank
; n
++)
160 rdelta
= GFC_DESCRIPTOR_STRIDE(ret
,n
);
165 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
166 if (extent
[dim
] <= 0)
168 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
169 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE(ret
,n
);
181 sstride0
= sstride
[0];
182 rstride0
= rstride
[0];
183 rptr
= ret
->base_addr
;
184 sptr
= source
->base_addr
;
188 /* Spread this element. */
190 for (n
= 0; n
< ncopies
; n
++)
195 /* Advance to the next element. */
200 while (count
[n
] == extent
[n
])
202 /* When we get to the end of a dimension, reset it and increment
203 the next dimension. */
205 /* We could precalculate these products, but this is a less
206 frequently used path so probably not worth it. */
207 sptr
-= sstride
[n
] * extent
[n
];
208 rptr
-= rstride
[n
] * extent
[n
];
212 /* Break out of the loop. */
226 /* This version of spread_internal treats the special case of a scalar
227 source. This is much simpler than the more general case above. */
230 spread_scalar_r10 (gfc_array_r10
*ret
, const GFC_REAL_10
*source
,
231 const index_type along
, const index_type ncopies
)
233 GFC_REAL_10
* restrict dest
;
236 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
237 runtime_error ("incorrect destination rank in spread()");
240 runtime_error ("dim outside of rank in spread()");
242 if (ret
->base_addr
== NULL
)
244 ret
->base_addr
= xmallocarray (ncopies
, sizeof (GFC_REAL_10
));
246 GFC_DIMENSION_SET(ret
->dim
[0], 0, ncopies
- 1, 1);
250 if (ncopies
- 1 > (GFC_DESCRIPTOR_EXTENT(ret
,0) - 1)
251 / GFC_DESCRIPTOR_STRIDE(ret
,0))
252 runtime_error ("dim too large in spread()");
255 dest
= ret
->base_addr
;
256 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
258 for (index_type n
= 0; n
< ncopies
; n
++)