]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/spread_generic.c
1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002, 2005 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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
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.
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., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
35 #include "libgfortran.h"
37 extern void spread (gfc_array_char
*, const gfc_array_char
*,
38 const index_type
*, const index_type
*);
42 spread (gfc_array_char
*ret
, const gfc_array_char
*source
,
43 const index_type
*along
, const index_type
*pncopies
)
45 /* r.* indicates the return array. */
46 index_type rstride
[GFC_MAX_DIMENSIONS
];
48 index_type rdelta
= 0;
53 /* s.* indicates the source array. */
54 index_type sstride
[GFC_MAX_DIMENSIONS
];
59 index_type count
[GFC_MAX_DIMENSIONS
];
60 index_type extent
[GFC_MAX_DIMENSIONS
];
66 srank
= GFC_DESCRIPTOR_RANK(source
);
69 if (rrank
> GFC_MAX_DIMENSIONS
)
70 runtime_error ("return rank too large in spread()");
73 runtime_error ("dim outside of rank in spread()");
77 size
= GFC_DESCRIPTOR_SIZE (source
);
78 if (ret
->data
== NULL
)
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
;
85 for (n
= 0; n
< rrank
; n
++)
87 ret
->dim
[n
].stride
= rs
;
88 ret
->dim
[n
].lbound
= 0;
91 ret
->dim
[n
].ubound
= ncopies
- 1;
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
;
103 ret
->dim
[n
].ubound
= extent
[dim
]-1;
109 ret
->data
= internal_malloc_size (rs
* size
);
114 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
115 runtime_error ("rank mismatch in spread()");
117 if (ret
->dim
[0].stride
== 0)
118 ret
->dim
[0].stride
= 1;
120 for (n
= 0; n
< rrank
; n
++)
124 rdelta
= ret
->dim
[n
].stride
* size
;
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
;
139 sstride0
= sstride
[0];
140 rstride0
= rstride
[0];
146 /* Spread this element. */
148 for (n
= 0; n
< ncopies
; n
++)
150 memcpy (dest
, sptr
, size
);
153 /* Advance to the next element. */
158 while (count
[n
] == extent
[n
])
160 /* When we get to the end of a dimension, reset it and increment
161 the next dimension. */
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
];
170 /* Break out of the loop. */