1 /* Generic implementation of the EOSHIFT intrinsic
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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 3 of the License, or (at your option) any later version.
12 Ligbfortran 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 General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
31 /* TODO: make this work for large shifts when
32 sizeof(int) < sizeof (index_type). */
35 eoshift2 (gfc_array_char
*ret
, const gfc_array_char
*array
,
36 int shift
, const gfc_array_char
*bound
, int which
,
37 const char *filler
, index_type filler_len
)
39 /* r.* indicates the return array. */
40 index_type rstride
[GFC_MAX_DIMENSIONS
];
45 /* s.* indicates the source array. */
46 index_type sstride
[GFC_MAX_DIMENSIONS
];
51 /* b.* indicates the bound array. */
52 index_type bstride
[GFC_MAX_DIMENSIONS
];
56 index_type count
[GFC_MAX_DIMENSIONS
];
57 index_type extent
[GFC_MAX_DIMENSIONS
];
64 /* The compiler cannot figure out that these are set, initialize
65 them to avoid warnings. */
70 size
= GFC_DESCRIPTOR_SIZE (array
);
72 arraysize
= size0 ((array_t
*) array
);
74 if (ret
->base_addr
== NULL
)
79 ret
->dtype
= array
->dtype
;
81 /* xmalloc allocates a single byte for zero size. */
82 ret
->base_addr
= xmalloc (size
* arraysize
);
84 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
88 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
93 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1)
94 * GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
96 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
99 else if (unlikely (compile_options
.bounds_check
))
101 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
102 "return value", "EOSHIFT");
116 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
120 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
123 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
126 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
131 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
132 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
133 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
135 bstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(bound
,n
);
145 if (bound
&& bstride
[0] == 0)
148 dim
= GFC_DESCRIPTOR_RANK (array
);
149 rstride0
= rstride
[0];
150 sstride0
= sstride
[0];
151 bstride0
= bstride
[0];
152 rptr
= ret
->base_addr
;
153 sptr
= array
->base_addr
;
155 if ((shift
>= 0 ? shift
: -shift
) > len
)
169 bptr
= bound
->base_addr
;
175 /* Do the shift for this dimension. */
178 src
= &sptr
[shift
* soffset
];
184 dest
= &rptr
[-shift
* roffset
];
186 for (n
= 0; n
< len
; n
++)
188 memcpy (dest
, src
, size
);
205 memcpy (dest
, bptr
, size
);
214 memset (dest
, filler
[0], size
);
216 for (i
= 0; i
< size
; i
+= filler_len
)
217 memcpy (&dest
[i
], filler
, filler_len
);
222 /* Advance to the next section. */
228 while (count
[n
] == extent
[n
])
230 /* When we get to the end of a dimension, reset it and increment
231 the next dimension. */
233 /* We could precalculate these products, but this is a less
234 frequently used path so probably not worth it. */
235 rptr
-= rstride
[n
] * extent
[n
];
236 sptr
-= sstride
[n
] * extent
[n
];
237 bptr
-= bstride
[n
] * extent
[n
];
241 /* Break out of the loop. */
257 #define DEFINE_EOSHIFT(N) \
258 extern void eoshift2_##N (gfc_array_char *, const gfc_array_char *, \
259 const GFC_INTEGER_##N *, const gfc_array_char *, \
260 const GFC_INTEGER_##N *); \
261 export_proto(eoshift2_##N); \
264 eoshift2_##N (gfc_array_char *ret, const gfc_array_char *array, \
265 const GFC_INTEGER_##N *pshift, const gfc_array_char *pbound, \
266 const GFC_INTEGER_##N *pdim) \
268 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
272 extern void eoshift2_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
273 const gfc_array_char *, \
274 const GFC_INTEGER_##N *, \
275 const gfc_array_char *, \
276 const GFC_INTEGER_##N *, \
277 GFC_INTEGER_4, GFC_INTEGER_4); \
278 export_proto(eoshift2_##N##_char); \
281 eoshift2_##N##_char (gfc_array_char *ret, \
282 GFC_INTEGER_4 ret_length __attribute__((unused)), \
283 const gfc_array_char *array, \
284 const GFC_INTEGER_##N *pshift, \
285 const gfc_array_char *pbound, \
286 const GFC_INTEGER_##N *pdim, \
287 GFC_INTEGER_4 array_length __attribute__((unused)), \
288 GFC_INTEGER_4 bound_length __attribute__((unused))) \
290 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
294 extern void eoshift2_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
295 const gfc_array_char *, \
296 const GFC_INTEGER_##N *, \
297 const gfc_array_char *, \
298 const GFC_INTEGER_##N *, \
299 GFC_INTEGER_4, GFC_INTEGER_4); \
300 export_proto(eoshift2_##N##_char4); \
303 eoshift2_##N##_char4 (gfc_array_char *ret, \
304 GFC_INTEGER_4 ret_length __attribute__((unused)), \
305 const gfc_array_char *array, \
306 const GFC_INTEGER_##N *pshift, \
307 const gfc_array_char *pbound, \
308 const GFC_INTEGER_##N *pdim, \
309 GFC_INTEGER_4 array_length __attribute__((unused)), \
310 GFC_INTEGER_4 bound_length __attribute__((unused))) \
312 static const gfc_char4_t space = (unsigned char) ' '; \
313 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
314 (const char *) &space, \
315 sizeof (gfc_char4_t)); \
322 #ifdef HAVE_GFC_INTEGER_16