]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/generated/cshift0_r10.c
re PR libfortran/36886 (misaligment for cshift of character)
[thirdparty/gcc.git] / libgfortran / generated / cshift0_r10.c
1 /* Helper function for cshift functions.
2 Copyright 2008 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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
19 executable.)
20
21 Libgfortran 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.
25
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. */
30
31 #include "libgfortran.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35
36
37 #if defined (HAVE_GFC_REAL_10)
38
39 void
40 cshift0_r10 (gfc_array_r10 *ret, const gfc_array_r10 *array, ssize_t shift,
41 int which)
42 {
43 /* r.* indicates the return array. */
44 index_type rstride[GFC_MAX_DIMENSIONS];
45 index_type rstride0;
46 index_type roffset;
47 GFC_REAL_10 *rptr;
48
49 /* s.* indicates the source array. */
50 index_type sstride[GFC_MAX_DIMENSIONS];
51 index_type sstride0;
52 index_type soffset;
53 const GFC_REAL_10 *sptr;
54
55 index_type count[GFC_MAX_DIMENSIONS];
56 index_type extent[GFC_MAX_DIMENSIONS];
57 index_type dim;
58 index_type len;
59 index_type n;
60
61 which = which - 1;
62 sstride[0] = 0;
63 rstride[0] = 0;
64
65 extent[0] = 1;
66 count[0] = 0;
67 n = 0;
68 /* Initialized for avoiding compiler warnings. */
69 roffset = 1;
70 soffset = 1;
71 len = 0;
72
73 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
74 {
75 if (dim == which)
76 {
77 roffset = ret->dim[dim].stride;
78 if (roffset == 0)
79 roffset = 1;
80 soffset = array->dim[dim].stride;
81 if (soffset == 0)
82 soffset = 1;
83 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
84 }
85 else
86 {
87 count[n] = 0;
88 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
89 rstride[n] = ret->dim[dim].stride;
90 sstride[n] = array->dim[dim].stride;
91 n++;
92 }
93 }
94 if (sstride[0] == 0)
95 sstride[0] = 1;
96 if (rstride[0] == 0)
97 rstride[0] = 1;
98
99 dim = GFC_DESCRIPTOR_RANK (array);
100 rstride0 = rstride[0];
101 sstride0 = sstride[0];
102 rptr = ret->data;
103 sptr = array->data;
104
105 shift = len == 0 ? 0 : shift % (ssize_t)len;
106 if (shift < 0)
107 shift += len;
108
109 while (rptr)
110 {
111 /* Do the shift for this dimension. */
112
113 /* If elements are contiguous, perform the operation
114 in two block moves. */
115 if (soffset == 1 && roffset == 1)
116 {
117 size_t len1 = shift * sizeof (GFC_REAL_10);
118 size_t len2 = (len - shift) * sizeof (GFC_REAL_10);
119 memcpy (rptr, sptr + shift, len2);
120 memcpy (rptr + (len - shift), sptr, len1);
121 }
122 else
123 {
124 /* Otherwise, we will have to perform the copy one element at
125 a time. */
126 GFC_REAL_10 *dest = rptr;
127 const GFC_REAL_10 *src = &sptr[shift * soffset];
128
129 for (n = 0; n < len - shift; n++)
130 {
131 *dest = *src;
132 dest += roffset;
133 src += soffset;
134 }
135 for (src = sptr, n = 0; n < shift; n++)
136 {
137 *dest = *src;
138 dest += roffset;
139 src += soffset;
140 }
141 }
142
143 /* Advance to the next section. */
144 rptr += rstride0;
145 sptr += sstride0;
146 count[0]++;
147 n = 0;
148 while (count[n] == extent[n])
149 {
150 /* When we get to the end of a dimension, reset it and increment
151 the next dimension. */
152 count[n] = 0;
153 /* We could precalculate these products, but this is a less
154 frequently used path so probably not worth it. */
155 rptr -= rstride[n] * extent[n];
156 sptr -= sstride[n] * extent[n];
157 n++;
158 if (n >= dim - 1)
159 {
160 /* Break out of the loop. */
161 rptr = NULL;
162 break;
163 }
164 else
165 {
166 count[n]++;
167 rptr += rstride[n];
168 sptr += sstride[n];
169 }
170 }
171 }
172
173 return;
174 }
175
176 #endif