/* Special implementation of the SPREAD intrinsic
- Copyright 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008-2020 Free Software Foundation, Inc.
Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
spread_generic.c written by Paul Brook <paul@nowt.org>
-This file is part of the GNU Fortran 95 runtime library (libgfortran).
+This file is part of the GNU Fortran runtime library (libgfortran).
Libgfortran is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
-version 2 of the License, or (at your option) any later version.
-
-In addition to the permissions in the GNU General Public License, the
-Free Software Foundation gives you unlimited permission to link the
-compiled version of this file into combinations with other programs,
-and to distribute those combinations without any restriction coming
-from the use of this file. (The General Public License restrictions
-do apply in other respects; for example, they cover modification of
-the file, and distribution when not linked into a combine
-executable.)
+version 3 of the License, or (at your option) any later version.
Ligbfortran is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-You should have received a copy of the GNU General Public
-License along with libgfortran; see the file COPYING. If not,
-write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA. */
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+<http://www.gnu.org/licenses/>. */
#include "libgfortran.h"
-#include <stdlib.h>
-#include <assert.h>
#include <string.h>
index_type rrank;
index_type rs;
GFC_INTEGER_16 *rptr;
- GFC_INTEGER_16 *dest;
+ GFC_INTEGER_16 * restrict dest;
/* s.* indicates the source array. */
index_type sstride[GFC_MAX_DIMENSIONS];
index_type sstride0;
ncopies = pncopies;
- if (ret->data == NULL)
+ if (ret->base_addr == NULL)
{
+
+ size_t ub, stride;
+
/* The front end has signalled that we need to populate the
return array descriptor. */
- ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rrank;
+ ret->dtype.rank = rrank;
+
dim = 0;
rs = 1;
for (n = 0; n < rrank; n++)
{
- ret->dim[n].stride = rs;
- ret->dim[n].lbound = 0;
+ stride = rs;
if (n == along - 1)
{
- ret->dim[n].ubound = ncopies - 1;
+ ub = ncopies - 1;
rdelta = rs;
rs *= ncopies;
}
else
{
count[dim] = 0;
- extent[dim] = source->dim[dim].ubound + 1
- - source->dim[dim].lbound;
- sstride[dim] = source->dim[dim].stride;
+ extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
+ sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
rstride[dim] = rs;
- ret->dim[n].ubound = extent[dim]-1;
+ ub = extent[dim] - 1;
rs *= extent[dim];
dim++;
}
+ GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
}
ret->offset = 0;
- if (rs > 0)
- ret->data = internal_malloc_size (rs * sizeof(GFC_INTEGER_16));
- else
- {
- ret->data = internal_malloc_size (1);
- return;
- }
+
+ /* xmallocarray allocates a single byte for zero size. */
+ ret->base_addr = xmallocarray (rs, sizeof(GFC_INTEGER_16));
+ if (rs <= 0)
+ return;
}
else
{
if (GFC_DESCRIPTOR_RANK(ret) != rrank)
runtime_error ("rank mismatch in spread()");
- if (compile_options.bounds_check)
+ if (unlikely (compile_options.bounds_check))
{
for (n = 0; n < rrank; n++)
{
index_type ret_extent;
- ret_extent = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
+ ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
if (n == along - 1)
{
- rdelta = ret->dim[n].stride;
+ rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
if (ret_extent != ncopies)
runtime_error("Incorrect extent in return value of SPREAD"
else
{
count[dim] = 0;
- extent[dim] = source->dim[dim].ubound + 1
- - source->dim[dim].lbound;
+ extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
if (ret_extent != extent[dim])
runtime_error("Incorrect extent in return value of SPREAD"
" intrinsic in dimension %ld: is %ld,"
if (extent[dim] <= 0)
zero_sized = 1;
- sstride[dim] = source->dim[dim].stride;
- rstride[dim] = ret->dim[n].stride;
+ sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
+ rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
dim++;
}
}
{
if (n == along - 1)
{
- rdelta = ret->dim[n].stride;
+ rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
}
else
{
count[dim] = 0;
- extent[dim] = source->dim[dim].ubound + 1
- - source->dim[dim].lbound;
+ extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
if (extent[dim] <= 0)
zero_sized = 1;
- sstride[dim] = source->dim[dim].stride;
- rstride[dim] = ret->dim[n].stride;
+ sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
+ rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
dim++;
}
}
}
sstride0 = sstride[0];
rstride0 = rstride[0];
- rptr = ret->data;
- sptr = source->data;
+ rptr = ret->base_addr;
+ sptr = source->base_addr;
while (sptr)
{
void
spread_scalar_i16 (gfc_array_i16 *ret, const GFC_INTEGER_16 *source,
- const index_type along, const index_type pncopies)
+ const index_type along, const index_type ncopies)
{
- int n;
- int ncopies = pncopies;
- GFC_INTEGER_16 * dest;
+ GFC_INTEGER_16 * restrict dest;
index_type stride;
if (GFC_DESCRIPTOR_RANK (ret) != 1)
if (along > 1)
runtime_error ("dim outside of rank in spread()");
- if (ret->data == NULL)
+ if (ret->base_addr == NULL)
{
- ret->data = internal_malloc_size (ncopies * sizeof (GFC_INTEGER_16));
+ ret->base_addr = xmallocarray (ncopies, sizeof (GFC_INTEGER_16));
ret->offset = 0;
- ret->dim[0].stride = 1;
- ret->dim[0].lbound = 0;
- ret->dim[0].ubound = ncopies - 1;
+ GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1);
}
else
{
- if (ncopies - 1 > (ret->dim[0].ubound - ret->dim[0].lbound)
- / ret->dim[0].stride)
+ if (ncopies - 1 > (GFC_DESCRIPTOR_EXTENT(ret,0) - 1)
+ / GFC_DESCRIPTOR_STRIDE(ret,0))
runtime_error ("dim too large in spread()");
}
- dest = ret->data;
- stride = ret->dim[0].stride;
+ dest = ret->base_addr;
+ stride = GFC_DESCRIPTOR_STRIDE(ret,0);
- for (n = 0; n < ncopies; n++)
+ for (index_type n = 0; n < ncopies; n++)
{
*dest = *source;
dest += stride;
}
#endif
+