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_INTEGER_2)
34 spread_i2 (gfc_array_i2
*ret
, const gfc_array_i2
*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_INTEGER_2
* restrict dest
;
45 /* s.* indicates the source array. */
46 index_type sstride
[GFC_MAX_DIMENSIONS
];
49 const GFC_INTEGER_2
*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 GFC_DTYPE_COPY_SETRANK(ret
,source
,rrank
);
78 for (n
= 0; n
< rrank
; n
++)
90 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
91 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
98 GFC_DIMENSION_SET(ret
->dim
[n
], 0, ub
, stride
);
102 /* xmallocarray allocates a single byte for zero size. */
103 ret
->base_addr
= xmallocarray (rs
, sizeof(GFC_INTEGER_2
));
114 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
115 runtime_error ("rank mismatch in spread()");
117 if (unlikely (compile_options
.bounds_check
))
119 for (n
= 0; n
< rrank
; n
++)
121 index_type ret_extent
;
123 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
126 rdelta
= GFC_DESCRIPTOR_STRIDE(ret
,n
);
128 if (ret_extent
!= ncopies
)
129 runtime_error("Incorrect extent in return value of SPREAD"
130 " intrinsic in dimension %ld: is %ld,"
131 " should be %ld", (long int) n
+1,
132 (long int) ret_extent
, (long int) ncopies
);
137 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
138 if (ret_extent
!= extent
[dim
])
139 runtime_error("Incorrect extent in return value of SPREAD"
140 " intrinsic in dimension %ld: is %ld,"
141 " should be %ld", (long int) n
+1,
142 (long int) ret_extent
,
143 (long int) extent
[dim
]);
145 if (extent
[dim
] <= 0)
147 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
148 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE(ret
,n
);
155 for (n
= 0; n
< rrank
; n
++)
159 rdelta
= GFC_DESCRIPTOR_STRIDE(ret
,n
);
164 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
165 if (extent
[dim
] <= 0)
167 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
168 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE(ret
,n
);
180 sstride0
= sstride
[0];
181 rstride0
= rstride
[0];
182 rptr
= ret
->base_addr
;
183 sptr
= source
->base_addr
;
187 /* Spread this element. */
189 for (n
= 0; n
< ncopies
; n
++)
194 /* Advance to the next element. */
199 while (count
[n
] == extent
[n
])
201 /* When we get to the end of a dimension, reset it and increment
202 the next dimension. */
204 /* We could precalculate these products, but this is a less
205 frequently used path so probably not worth it. */
206 sptr
-= sstride
[n
] * extent
[n
];
207 rptr
-= rstride
[n
] * extent
[n
];
211 /* Break out of the loop. */
225 /* This version of spread_internal treats the special case of a scalar
226 source. This is much simpler than the more general case above. */
229 spread_scalar_i2 (gfc_array_i2
*ret
, const GFC_INTEGER_2
*source
,
230 const index_type along
, const index_type ncopies
)
232 GFC_INTEGER_2
* restrict dest
;
235 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
236 runtime_error ("incorrect destination rank in spread()");
239 runtime_error ("dim outside of rank in spread()");
241 if (ret
->base_addr
== NULL
)
243 ret
->base_addr
= xmallocarray (ncopies
, sizeof (GFC_INTEGER_2
));
245 GFC_DIMENSION_SET(ret
->dim
[0], 0, ncopies
- 1, 1);
249 if (ncopies
- 1 > (GFC_DESCRIPTOR_EXTENT(ret
,0) - 1)
250 / GFC_DESCRIPTOR_STRIDE(ret
,0))
251 runtime_error ("dim too large in spread()");
254 dest
= ret
->base_addr
;
255 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
257 for (index_type n
= 0; n
< ncopies
; n
++)