1 /* Generic implementation of the EOSHIFT intrinsic
2 Copyright 2002, 2005, 2007 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. */
31 #include "libgfortran.h"
36 /* TODO: make this work for large shifts when
37 sizeof(int) < sizeof (index_type). */
40 eoshift2 (gfc_array_char
*ret
, const gfc_array_char
*array
,
41 int shift
, const gfc_array_char
*bound
, int which
,
42 index_type size
, const char *filler
, index_type filler_len
)
44 /* r.* indicates the return array. */
45 index_type rstride
[GFC_MAX_DIMENSIONS
];
50 /* s.* indicates the source array. */
51 index_type sstride
[GFC_MAX_DIMENSIONS
];
56 /* b.* indicates the bound array. */
57 index_type bstride
[GFC_MAX_DIMENSIONS
];
61 index_type count
[GFC_MAX_DIMENSIONS
];
62 index_type extent
[GFC_MAX_DIMENSIONS
];
68 /* The compiler cannot figure out that these are set, initialize
69 them to avoid warnings. */
74 arraysize
= size0 ((array_t
*) array
);
76 if (ret
->data
== NULL
)
80 ret
->data
= internal_malloc_size (size
* arraysize
);
82 ret
->dtype
= array
->dtype
;
83 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
85 ret
->dim
[i
].lbound
= 0;
86 ret
->dim
[i
].ubound
= array
->dim
[i
].ubound
- array
->dim
[i
].lbound
;
89 ret
->dim
[i
].stride
= 1;
91 ret
->dim
[i
].stride
= (ret
->dim
[i
-1].ubound
+ 1) * ret
->dim
[i
-1].stride
;
96 if (size0 ((array_t
*) ret
) == 0)
100 if (arraysize
== 0 && filler
== NULL
)
111 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
115 roffset
= ret
->dim
[dim
].stride
* size
;
118 soffset
= array
->dim
[dim
].stride
* size
;
121 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
126 extent
[n
] = array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
127 rstride
[n
] = ret
->dim
[dim
].stride
* size
;
128 sstride
[n
] = array
->dim
[dim
].stride
* size
;
130 bstride
[n
] = bound
->dim
[n
].stride
* size
;
140 if (bound
&& bstride
[0] == 0)
143 dim
= GFC_DESCRIPTOR_RANK (array
);
144 rstride0
= rstride
[0];
145 sstride0
= sstride
[0];
146 bstride0
= bstride
[0];
150 if ((shift
>= 0 ? shift
: -shift
) > len
)
170 /* Do the shift for this dimension. */
173 src
= &sptr
[shift
* soffset
];
179 dest
= &rptr
[-shift
* roffset
];
181 for (n
= 0; n
< len
; n
++)
183 memcpy (dest
, src
, size
);
200 memcpy (dest
, bptr
, size
);
209 memset (dest
, filler
[0], size
);
211 for (i
= 0; i
< size
; i
+= filler_len
)
212 memcpy (&dest
[i
], filler
, filler_len
);
217 /* Advance to the next section. */
223 while (count
[n
] == extent
[n
])
225 /* When we get to the end of a dimension, reset it and increment
226 the next dimension. */
228 /* We could precalculate these products, but this is a less
229 frequently used path so probably not worth it. */
230 rptr
-= rstride
[n
] * extent
[n
];
231 sptr
-= sstride
[n
] * extent
[n
];
232 bptr
-= bstride
[n
] * extent
[n
];
236 /* Break out of the loop. */
252 #define DEFINE_EOSHIFT(N) \
253 extern void eoshift2_##N (gfc_array_char *, const gfc_array_char *, \
254 const GFC_INTEGER_##N *, const gfc_array_char *, \
255 const GFC_INTEGER_##N *); \
256 export_proto(eoshift2_##N); \
259 eoshift2_##N (gfc_array_char *ret, const gfc_array_char *array, \
260 const GFC_INTEGER_##N *pshift, const gfc_array_char *pbound, \
261 const GFC_INTEGER_##N *pdim) \
263 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
264 GFC_DESCRIPTOR_SIZE (array), "\0", 1); \
267 extern void eoshift2_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
268 const gfc_array_char *, \
269 const GFC_INTEGER_##N *, \
270 const gfc_array_char *, \
271 const GFC_INTEGER_##N *, \
272 GFC_INTEGER_4, GFC_INTEGER_4); \
273 export_proto(eoshift2_##N##_char); \
276 eoshift2_##N##_char (gfc_array_char *ret, \
277 GFC_INTEGER_4 ret_length __attribute__((unused)), \
278 const gfc_array_char *array, \
279 const GFC_INTEGER_##N *pshift, \
280 const gfc_array_char *pbound, \
281 const GFC_INTEGER_##N *pdim, \
282 GFC_INTEGER_4 array_length, \
283 GFC_INTEGER_4 bound_length __attribute__((unused))) \
285 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
286 array_length, " ", 1); \
289 extern void eoshift2_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
290 const gfc_array_char *, \
291 const GFC_INTEGER_##N *, \
292 const gfc_array_char *, \
293 const GFC_INTEGER_##N *, \
294 GFC_INTEGER_4, GFC_INTEGER_4); \
295 export_proto(eoshift2_##N##_char4); \
298 eoshift2_##N##_char4 (gfc_array_char *ret, \
299 GFC_INTEGER_4 ret_length __attribute__((unused)), \
300 const gfc_array_char *array, \
301 const GFC_INTEGER_##N *pshift, \
302 const gfc_array_char *pbound, \
303 const GFC_INTEGER_##N *pdim, \
304 GFC_INTEGER_4 array_length, \
305 GFC_INTEGER_4 bound_length __attribute__((unused))) \
307 static const gfc_char4_t space = (unsigned char) ' '; \
308 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
309 array_length * sizeof (gfc_char4_t), (const char *) &space, \
310 sizeof (gfc_char4_t)); \
317 #ifdef HAVE_GFC_INTEGER_16