PR libfortran/18966
[official-gcc.git] / libgfortran / intrinsics / spread_generic.c
blob44c402798aeb5409b24c387d1fd6dc9712dda8ba
1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Ligbfor 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 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
28 extern void spread (const gfc_array_char *, const gfc_array_char *,
29 const index_type *, const index_type *);
30 export_proto(spread);
32 void
33 spread (const gfc_array_char *ret, const gfc_array_char *source,
34 const index_type *along, const index_type *pncopies)
36 /* r.* indicates the return array. */
37 index_type rstride[GFC_MAX_DIMENSIONS - 1];
38 index_type rstride0;
39 index_type rdelta;
40 char *rptr;
41 char *dest;
42 /* s.* indicates the source array. */
43 index_type sstride[GFC_MAX_DIMENSIONS - 1];
44 index_type sstride0;
45 const char *sptr;
47 index_type count[GFC_MAX_DIMENSIONS - 1];
48 index_type extent[GFC_MAX_DIMENSIONS - 1];
49 index_type n;
50 index_type dim;
51 index_type size;
52 index_type ncopies;
54 size = GFC_DESCRIPTOR_SIZE (source);
55 dim = 0;
56 for (n = 0; n < GFC_DESCRIPTOR_RANK (ret); n++)
58 if (n == *along - 1)
60 rdelta = ret->dim[n].stride * size;
62 else
64 count[dim] = 0;
65 extent[dim] = source->dim[dim].ubound + 1 - source->dim[dim].lbound;
66 sstride[dim] = source->dim[dim].stride * size;
67 rstride[dim] = ret->dim[n].stride * size;
68 dim++;
71 dim = GFC_DESCRIPTOR_RANK (source);
72 if (sstride[0] == 0)
73 sstride[0] = size;
74 if (rstride[0] == 0)
75 rstride[0] = size;
77 sstride0 = sstride[0];
78 rstride0 = rstride[0];
79 rptr = ret->data;
80 sptr = source->data;
81 ncopies = *pncopies;
83 while (sptr)
85 /* Spread this element. */
86 dest = rptr;
87 for (n = 0; n < ncopies; n++)
89 memcpy (dest, sptr, size);
90 dest += rdelta;
92 /* Advance to the next element. */
93 sptr += sstride0;
94 rptr += rstride0;
95 count[0]++;
96 n = 0;
97 while (count[n] == extent[n])
99 /* When we get to the end of a dimension, reset it and increment
100 the next dimension. */
101 count[n] = 0;
102 /* We could precalculate these products, but this is a less
103 frequently used path so probably not worth it. */
104 sptr -= sstride[n] * extent[n];
105 rptr -= rstride[n] * extent[n];
106 n++;
107 if (n >= dim)
109 /* Break out of the loop. */
110 sptr = NULL;
111 break;
113 else
115 count[n]++;
116 sptr += sstride[n];
117 rptr += rstride[n];