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_8)
33 cshift1 (gfc_array_char
* const restrict ret
,
34 const gfc_array_char
* const restrict array
,
35 const gfc_array_i8
* const restrict h
,
36 const GFC_INTEGER_8
* 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_8
*hptr
;
55 index_type count
[GFC_MAX_DIMENSIONS
];
56 index_type extent
[GFC_MAX_DIMENSIONS
];
70 if (which
< 0 || (which
+ 1) > GFC_DESCRIPTOR_RANK (array
))
71 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
73 size
= GFC_DESCRIPTOR_SIZE(array
);
75 arraysize
= size0 ((array_t
*)array
);
77 if (ret
->base_addr
== NULL
)
81 ret
->base_addr
= xmallocarray (arraysize
, size
);
83 ret
->dtype
= array
->dtype
;
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", "CSHIFT");
105 if (unlikely (compile_options
.bounds_check
))
107 bounds_reduced_extents ((array_t
*) h
, (array_t
*) array
, which
,
108 "SHIFT argument", "CSHIFT");
118 /* Initialized for avoiding compiler warnings. */
123 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
127 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
130 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
133 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
138 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
139 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
140 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
142 hstride
[n
] = GFC_DESCRIPTOR_STRIDE(h
,n
);
153 dim
= GFC_DESCRIPTOR_RANK (array
);
154 rstride0
= rstride
[0];
155 sstride0
= sstride
[0];
156 hstride0
= hstride
[0];
157 rptr
= ret
->base_addr
;
158 sptr
= array
->base_addr
;
163 /* Do the shift for this dimension. */
165 sh
= (div (sh
, len
)).rem
;
169 src
= &sptr
[sh
* soffset
];
172 for (n
= 0; n
< len
; n
++)
174 memcpy (dest
, src
, size
);
176 if (n
== len
- sh
- 1)
182 /* Advance to the next section. */
188 while (count
[n
] == extent
[n
])
190 /* When we get to the end of a dimension, reset it and increment
191 the next dimension. */
193 /* We could precalculate these products, but this is a less
194 frequently used path so probably not worth it. */
195 rptr
-= rstride
[n
] * extent
[n
];
196 sptr
-= sstride
[n
] * extent
[n
];
197 hptr
-= hstride
[n
] * extent
[n
];
201 /* Break out of the loop. */
216 void cshift1_8 (gfc_array_char
* const restrict
,
217 const gfc_array_char
* const restrict
,
218 const gfc_array_i8
* const restrict
,
219 const GFC_INTEGER_8
* const restrict
);
220 export_proto(cshift1_8
);
223 cshift1_8 (gfc_array_char
* const restrict ret
,
224 const gfc_array_char
* const restrict array
,
225 const gfc_array_i8
* const restrict h
,
226 const GFC_INTEGER_8
* const restrict pwhich
)
228 cshift1 (ret
, array
, h
, pwhich
);
232 void cshift1_8_char (gfc_array_char
* const restrict ret
,
234 const gfc_array_char
* const restrict array
,
235 const gfc_array_i8
* const restrict h
,
236 const GFC_INTEGER_8
* const restrict pwhich
,
238 export_proto(cshift1_8_char
);
241 cshift1_8_char (gfc_array_char
* const restrict ret
,
242 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
243 const gfc_array_char
* const restrict array
,
244 const gfc_array_i8
* const restrict h
,
245 const GFC_INTEGER_8
* const restrict pwhich
,
246 GFC_INTEGER_4 array_length
__attribute__((unused
)))
248 cshift1 (ret
, array
, h
, pwhich
);
252 void cshift1_8_char4 (gfc_array_char
* const restrict ret
,
254 const gfc_array_char
* const restrict array
,
255 const gfc_array_i8
* const restrict h
,
256 const GFC_INTEGER_8
* const restrict pwhich
,
258 export_proto(cshift1_8_char4
);
261 cshift1_8_char4 (gfc_array_char
* const restrict ret
,
262 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
263 const gfc_array_char
* const restrict array
,
264 const gfc_array_i8
* const restrict h
,
265 const GFC_INTEGER_8
* const restrict pwhich
,
266 GFC_INTEGER_4 array_length
__attribute__((unused
)))
268 cshift1 (ret
, array
, h
, pwhich
);