1 /* Special implementation of the SPREAD intrinsic
2 Copyright 2008, 2009 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 95 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"
33 #if defined (HAVE_GFC_INTEGER_16)
36 spread_i16 (gfc_array_i16
*ret
, const gfc_array_i16
*source
,
37 const index_type along
, const index_type pncopies
)
39 /* r.* indicates the return array. */
40 index_type rstride
[GFC_MAX_DIMENSIONS
];
42 index_type rdelta
= 0;
46 GFC_INTEGER_16
* restrict dest
;
47 /* s.* indicates the source array. */
48 index_type sstride
[GFC_MAX_DIMENSIONS
];
51 const GFC_INTEGER_16
*sptr
;
53 index_type count
[GFC_MAX_DIMENSIONS
];
54 index_type extent
[GFC_MAX_DIMENSIONS
];
59 srank
= GFC_DESCRIPTOR_RANK(source
);
62 if (rrank
> GFC_MAX_DIMENSIONS
)
63 runtime_error ("return rank too large in spread()");
66 runtime_error ("dim outside of rank in spread()");
70 if (ret
->data
== NULL
)
75 /* The front end has signalled that we need to populate the
76 return array descriptor. */
77 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rrank
;
80 for (n
= 0; n
< rrank
; n
++)
92 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
93 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
100 GFC_DIMENSION_SET(ret
->dim
[n
], 0, ub
, stride
);
104 ret
->data
= internal_malloc_size (rs
* sizeof(GFC_INTEGER_16
));
107 ret
->data
= internal_malloc_size (1);
118 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
119 runtime_error ("rank mismatch in spread()");
121 if (unlikely (compile_options
.bounds_check
))
123 for (n
= 0; n
< rrank
; n
++)
125 index_type ret_extent
;
127 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
130 rdelta
= GFC_DESCRIPTOR_STRIDE(ret
,n
);
132 if (ret_extent
!= ncopies
)
133 runtime_error("Incorrect extent in return value of SPREAD"
134 " intrinsic in dimension %ld: is %ld,"
135 " should be %ld", (long int) n
+1,
136 (long int) ret_extent
, (long int) ncopies
);
141 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
142 if (ret_extent
!= extent
[dim
])
143 runtime_error("Incorrect extent in return value of SPREAD"
144 " intrinsic in dimension %ld: is %ld,"
145 " should be %ld", (long int) n
+1,
146 (long int) ret_extent
,
147 (long int) extent
[dim
]);
149 if (extent
[dim
] <= 0)
151 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
152 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE(ret
,n
);
159 for (n
= 0; n
< rrank
; n
++)
163 rdelta
= GFC_DESCRIPTOR_STRIDE(ret
,n
);
168 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
169 if (extent
[dim
] <= 0)
171 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE(source
,dim
);
172 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE(ret
,n
);
184 sstride0
= sstride
[0];
185 rstride0
= rstride
[0];
191 /* Spread this element. */
193 for (n
= 0; n
< ncopies
; n
++)
198 /* Advance to the next element. */
203 while (count
[n
] == extent
[n
])
205 /* When we get to the end of a dimension, reset it and increment
206 the next dimension. */
208 /* We could precalculate these products, but this is a less
209 frequently used path so probably not worth it. */
210 sptr
-= sstride
[n
] * extent
[n
];
211 rptr
-= rstride
[n
] * extent
[n
];
215 /* Break out of the loop. */
229 /* This version of spread_internal treats the special case of a scalar
230 source. This is much simpler than the more general case above. */
233 spread_scalar_i16 (gfc_array_i16
*ret
, const GFC_INTEGER_16
*source
,
234 const index_type along
, const index_type pncopies
)
237 int ncopies
= pncopies
;
238 GFC_INTEGER_16
* restrict dest
;
241 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
242 runtime_error ("incorrect destination rank in spread()");
245 runtime_error ("dim outside of rank in spread()");
247 if (ret
->data
== NULL
)
249 ret
->data
= internal_malloc_size (ncopies
* sizeof (GFC_INTEGER_16
));
251 GFC_DIMENSION_SET(ret
->dim
[0], 0, ncopies
- 1, 1);
255 if (ncopies
- 1 > (GFC_DESCRIPTOR_EXTENT(ret
,0) - 1)
256 / GFC_DESCRIPTOR_STRIDE(ret
,0))
257 runtime_error ("dim too large in spread()");
261 stride
= GFC_DESCRIPTOR_STRIDE(ret
,0);
263 for (n
= 0; n
< ncopies
; n
++)