]>
Commit | Line | Data |
---|---|---|
7f68c75f | 1 | /* Generic implementation of the SPREAD intrinsic |
6de9cd9a DN |
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 (libgfor). | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include <stdlib.h> | |
24 | #include <assert.h> | |
25 | #include <string.h> | |
26 | #include "libgfortran.h" | |
27 | ||
7f68c75f RH |
28 | extern void spread (const gfc_array_char *, const gfc_array_char *, |
29 | const index_type *, const index_type *); | |
30 | export_proto(spread); | |
7d7b8bfe | 31 | |
6de9cd9a | 32 | void |
7f68c75f RH |
33 | spread (const gfc_array_char *ret, const gfc_array_char *source, |
34 | const index_type *along, const index_type *pncopies) | |
6de9cd9a DN |
35 | { |
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; | |
46 | ||
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; | |
53 | ||
54 | size = GFC_DESCRIPTOR_SIZE (source); | |
55 | dim = 0; | |
56 | for (n = 0; n < GFC_DESCRIPTOR_RANK (ret); n++) | |
57 | { | |
58 | if (n == *along - 1) | |
59 | { | |
60 | rdelta = ret->dim[n].stride * size; | |
61 | } | |
62 | else | |
63 | { | |
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++; | |
69 | } | |
70 | } | |
71 | dim = GFC_DESCRIPTOR_RANK (source); | |
72 | if (sstride[0] == 0) | |
73 | sstride[0] = size; | |
74 | if (rstride[0] == 0) | |
75 | rstride[0] = size; | |
76 | ||
77 | sstride0 = sstride[0]; | |
78 | rstride0 = rstride[0]; | |
79 | rptr = ret->data; | |
80 | sptr = source->data; | |
81 | ncopies = *pncopies; | |
82 | ||
83 | while (sptr) | |
84 | { | |
85 | /* Spread this element. */ | |
86 | dest = rptr; | |
87 | for (n = 0; n < ncopies; n++) | |
88 | { | |
89 | memcpy (dest, sptr, size); | |
90 | dest += rdelta; | |
91 | } | |
92 | /* Advance to the next element. */ | |
93 | sptr += sstride0; | |
94 | rptr += rstride0; | |
95 | count[0]++; | |
96 | n = 0; | |
97 | while (count[n] == extent[n]) | |
98 | { | |
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) | |
108 | { | |
109 | /* Break out of the loop. */ | |
110 | sptr = NULL; | |
111 | break; | |
112 | } | |
113 | else | |
114 | { | |
115 | count[n]++; | |
116 | sptr += sstride[n]; | |
117 | rptr += rstride[n]; | |
118 | } | |
119 | } | |
120 | } | |
121 | } |