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