1 /* Implementation of the CSHIFT intrinsic
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
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"
30 #if defined (HAVE_GFC_INTEGER_4)
33 cshift1 (gfc_array_char
* const restrict ret
,
34 const gfc_array_char
* const restrict array
,
35 const gfc_array_i4
* const restrict h
,
36 const GFC_INTEGER_4
* const restrict pwhich
)
38 /* r.* indicates the return array. */
39 index_type rstride
[GFC_MAX_DIMENSIONS
];
44 /* s.* indicates the source array. */
45 index_type sstride
[GFC_MAX_DIMENSIONS
];
50 /* h.* indicates the shift array. */
51 index_type hstride
[GFC_MAX_DIMENSIONS
];
53 const GFC_INTEGER_4
*hptr
;
55 index_type count
[GFC_MAX_DIMENSIONS
];
56 index_type extent
[GFC_MAX_DIMENSIONS
];
71 if (which
< 0 || (which
+ 1) > GFC_DESCRIPTOR_RANK (array
))
72 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
74 size
= GFC_DESCRIPTOR_SIZE(array
);
76 arraysize
= size0 ((array_t
*)array
);
78 if (ret
->base_addr
== NULL
)
82 ret
->base_addr
= xmallocarray (arraysize
, size
);
84 ret
->dtype
= array
->dtype
;
85 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
89 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
94 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1) *
95 GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
97 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
100 else if (unlikely (compile_options
.bounds_check
))
102 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
103 "return value", "CSHIFT");
106 if (unlikely (compile_options
.bounds_check
))
108 bounds_reduced_extents ((array_t
*) h
, (array_t
*) array
, which
,
109 "SHIFT argument", "CSHIFT");
115 /* See if we should dispatch to a helper function. */
117 type_size
= GFC_DTYPE_TYPE_SIZE (array
);
121 case GFC_DTYPE_LOGICAL_1
:
122 case GFC_DTYPE_INTEGER_1
:
123 case GFC_DTYPE_DERIVED_1
:
124 cshift1_4_i1 ((gfc_array_i1
*)ret
, (gfc_array_i1
*) array
,
128 case GFC_DTYPE_LOGICAL_2
:
129 case GFC_DTYPE_INTEGER_2
:
130 cshift1_4_i2 ((gfc_array_i2
*)ret
, (gfc_array_i2
*) array
,
134 case GFC_DTYPE_LOGICAL_4
:
135 case GFC_DTYPE_INTEGER_4
:
136 cshift1_4_i4 ((gfc_array_i4
*)ret
, (gfc_array_i4
*) array
,
140 case GFC_DTYPE_LOGICAL_8
:
141 case GFC_DTYPE_INTEGER_8
:
142 cshift1_4_i8 ((gfc_array_i8
*)ret
, (gfc_array_i8
*) array
,
146 #if defined (HAVE_INTEGER_16)
147 case GFC_DTYPE_LOGICAL_16
:
148 case GFC_DTYPE_INTEGER_16
:
149 cshift1_4_i16 ((gfc_array_i16
*)ret
, (gfc_array_i16
*) array
,
154 case GFC_DTYPE_REAL_4
:
155 cshift1_4_r4 ((gfc_array_r4
*)ret
, (gfc_array_r4
*) array
,
159 case GFC_DTYPE_REAL_8
:
160 cshift1_4_r8 ((gfc_array_r8
*)ret
, (gfc_array_r8
*) array
,
164 #if defined (HAVE_REAL_10)
165 case GFC_DTYPE_REAL_10
:
166 cshift1_4_r10 ((gfc_array_r10
*)ret
, (gfc_array_r10
*) array
,
171 #if defined (HAVE_REAL_16)
172 case GFC_DTYPE_REAL_16
:
173 cshift1_4_r16 ((gfc_array_r16
*)ret
, (gfc_array_r16
*) array
,
178 case GFC_DTYPE_COMPLEX_4
:
179 cshift1_4_c4 ((gfc_array_c4
*)ret
, (gfc_array_c4
*) array
,
183 case GFC_DTYPE_COMPLEX_8
:
184 cshift1_4_c8 ((gfc_array_c8
*)ret
, (gfc_array_c8
*) array
,
188 #if defined (HAVE_COMPLEX_10)
189 case GFC_DTYPE_COMPLEX_10
:
190 cshift1_4_c10 ((gfc_array_c10
*)ret
, (gfc_array_c10
*) array
,
195 #if defined (HAVE_COMPLEX_16)
196 case GFC_DTYPE_COMPLEX_16
:
197 cshift1_4_c16 ((gfc_array_c16
*)ret
, (gfc_array_c16
*) array
,
211 /* Initialized for avoiding compiler warnings. */
216 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
220 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
223 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
226 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
231 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
232 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
233 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
235 hstride
[n
] = GFC_DESCRIPTOR_STRIDE(h
,n
);
246 dim
= GFC_DESCRIPTOR_RANK (array
);
247 rstride0
= rstride
[0];
248 sstride0
= sstride
[0];
249 hstride0
= hstride
[0];
250 rptr
= ret
->base_addr
;
251 sptr
= array
->base_addr
;
256 /* Do the shift for this dimension. */
258 /* Normal case should be -len < sh < len; try to
259 avoid the expensive remainder operation if possible. */
262 if (unlikely (sh
>= len
|| sh
< 0))
269 src
= &sptr
[sh
* soffset
];
271 if (soffset
== size
&& roffset
== size
)
273 size_t len1
= sh
* size
;
274 size_t len2
= (len
- sh
) * size
;
275 memcpy (rptr
, sptr
+ len1
, len2
);
276 memcpy (rptr
+ len2
, sptr
, len1
);
280 for (n
= 0; n
< len
- sh
; n
++)
282 memcpy (dest
, src
, size
);
286 for (src
= sptr
, n
= 0; n
< sh
; n
++)
288 memcpy (dest
, src
, size
);
294 /* Advance to the next section. */
300 while (count
[n
] == extent
[n
])
302 /* When we get to the end of a dimension, reset it and increment
303 the next dimension. */
305 /* We could precalculate these products, but this is a less
306 frequently used path so probably not worth it. */
307 rptr
-= rstride
[n
] * extent
[n
];
308 sptr
-= sstride
[n
] * extent
[n
];
309 hptr
-= hstride
[n
] * extent
[n
];
313 /* Break out of the loop. */
328 void cshift1_4 (gfc_array_char
* const restrict
,
329 const gfc_array_char
* const restrict
,
330 const gfc_array_i4
* const restrict
,
331 const GFC_INTEGER_4
* const restrict
);
332 export_proto(cshift1_4
);
335 cshift1_4 (gfc_array_char
* const restrict ret
,
336 const gfc_array_char
* const restrict array
,
337 const gfc_array_i4
* const restrict h
,
338 const GFC_INTEGER_4
* const restrict pwhich
)
340 cshift1 (ret
, array
, h
, pwhich
);
344 void cshift1_4_char (gfc_array_char
* const restrict ret
,
346 const gfc_array_char
* const restrict array
,
347 const gfc_array_i4
* const restrict h
,
348 const GFC_INTEGER_4
* const restrict pwhich
,
350 export_proto(cshift1_4_char
);
353 cshift1_4_char (gfc_array_char
* const restrict ret
,
354 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
355 const gfc_array_char
* const restrict array
,
356 const gfc_array_i4
* const restrict h
,
357 const GFC_INTEGER_4
* const restrict pwhich
,
358 GFC_INTEGER_4 array_length
__attribute__((unused
)))
360 cshift1 (ret
, array
, h
, pwhich
);
364 void cshift1_4_char4 (gfc_array_char
* const restrict ret
,
366 const gfc_array_char
* const restrict array
,
367 const gfc_array_i4
* const restrict h
,
368 const GFC_INTEGER_4
* const restrict pwhich
,
370 export_proto(cshift1_4_char4
);
373 cshift1_4_char4 (gfc_array_char
* const restrict ret
,
374 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
375 const gfc_array_char
* const restrict array
,
376 const gfc_array_i4
* const restrict h
,
377 const GFC_INTEGER_4
* const restrict pwhich
,
378 GFC_INTEGER_4 array_length
__attribute__((unused
)))
380 cshift1 (ret
, array
, h
, pwhich
);