005-04-17 Thomas Koenig <Thomas.Koenig@online.de>
[gcc.git] / libgfortran / intrinsics / spread_generic.c
1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Ligbfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37 extern void spread (gfc_array_char *, const gfc_array_char *,
38 const index_type *, const index_type *);
39 export_proto(spread);
40
41 void
42 spread (gfc_array_char *ret, const gfc_array_char *source,
43 const index_type *along, const index_type *pncopies)
44 {
45 /* r.* indicates the return array. */
46 index_type rstride[GFC_MAX_DIMENSIONS - 1];
47 index_type rstride0;
48 index_type rdelta;
49 index_type rrank;
50 index_type rs;
51 char *rptr;
52 char *dest;
53 /* s.* indicates the source array. */
54 index_type sstride[GFC_MAX_DIMENSIONS - 1];
55 index_type sstride0;
56 index_type srank;
57 const char *sptr;
58
59 index_type count[GFC_MAX_DIMENSIONS - 1];
60 index_type extent[GFC_MAX_DIMENSIONS - 1];
61 index_type n;
62 index_type dim;
63 index_type size;
64 index_type ncopies;
65
66 srank = GFC_DESCRIPTOR_RANK(source);
67
68 rrank = srank + 1;
69 if (rrank > GFC_MAX_DIMENSIONS)
70 runtime_error ("return rank too large in spread()");
71
72 if (*along > rrank)
73 runtime_error ("dim outside of rank in spread()");
74
75 ncopies = *pncopies;
76
77 size = GFC_DESCRIPTOR_SIZE (source);
78 if (ret->data == NULL)
79 {
80 /* The front end has signalled that we need to populate the
81 return array descriptor. */
82 ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rrank;
83 dim = 0;
84 rs = 1;
85 for (n = 0; n < rrank; n++)
86 {
87 ret->dim[n].stride = rs;
88 ret->dim[n].lbound = 0;
89 if (n == *along - 1)
90 {
91 ret->dim[n].ubound = ncopies - 1;
92 rdelta = rs * size;
93 rs *= ncopies;
94 }
95 else
96 {
97 count[dim] = 0;
98 extent[dim] = source->dim[dim].ubound + 1
99 - source->dim[dim].lbound;
100 sstride[dim] = source->dim[dim].stride * size;
101 rstride[dim] = rs * size;
102
103 ret->dim[n].ubound = extent[dim]-1;
104 rs *= extent[dim];
105 dim++;
106 }
107 }
108 ret->base = 0;
109 ret->data = internal_malloc_size (rs * size);
110 }
111 else
112 {
113 dim = 0;
114 if (GFC_DESCRIPTOR_RANK(ret) != rrank)
115 runtime_error ("rank mismatch in spread()");
116
117 if (ret->dim[0].stride == 0)
118 ret->dim[0].stride = 1;
119
120 for (n = 0; n < rrank; n++)
121 {
122 if (n == *along - 1)
123 {
124 rdelta = ret->dim[n].stride * size;
125 }
126 else
127 {
128 count[dim] = 0;
129 extent[dim] = source->dim[dim].ubound + 1
130 - source->dim[dim].lbound;
131 sstride[dim] = source->dim[dim].stride * size;
132 rstride[dim] = ret->dim[n].stride * size;
133 dim++;
134 }
135 }
136 if (sstride[0] == 0)
137 sstride[0] = size;
138 }
139 sstride0 = sstride[0];
140 rstride0 = rstride[0];
141 rptr = ret->data;
142 sptr = source->data;
143
144 while (sptr)
145 {
146 /* Spread this element. */
147 dest = rptr;
148 for (n = 0; n < ncopies; n++)
149 {
150 memcpy (dest, sptr, size);
151 dest += rdelta;
152 }
153 /* Advance to the next element. */
154 sptr += sstride0;
155 rptr += rstride0;
156 count[0]++;
157 n = 0;
158 while (count[n] == extent[n])
159 {
160 /* When we get to the end of a dimension, reset it and increment
161 the next dimension. */
162 count[n] = 0;
163 /* We could precalculate these products, but this is a less
164 frequently used path so probably not worth it. */
165 sptr -= sstride[n] * extent[n];
166 rptr -= rstride[n] * extent[n];
167 n++;
168 if (n >= srank)
169 {
170 /* Break out of the loop. */
171 sptr = NULL;
172 break;
173 }
174 else
175 {
176 count[n]++;
177 sptr += sstride[n];
178 rptr += rstride[n];
179 }
180 }
181 }
182 }