Makefile.am (gfor_helper_src): Split selected_kind.f90.
[gcc.git] / libgfortran / intrinsics / cshift0.c
1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
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 Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Ligbfor 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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
27
28 /* TODO: make this work for large shifts when
29 sizeof(int) < sizeof (index_type). */
30
31 static void
32 __cshift0 (gfc_array_char * ret, const gfc_array_char * array,
33 int shift, int which)
34 {
35 /* r.* indicates the return array. */
36 index_type rstride[GFC_MAX_DIMENSIONS - 1];
37 index_type rstride0;
38 index_type roffset;
39 char *rptr;
40 char *dest;
41 /* s.* indicates the source array. */
42 index_type sstride[GFC_MAX_DIMENSIONS - 1];
43 index_type sstride0;
44 index_type soffset;
45 const char *sptr;
46 const char *src;
47
48 index_type count[GFC_MAX_DIMENSIONS - 1];
49 index_type extent[GFC_MAX_DIMENSIONS - 1];
50 index_type dim;
51 index_type size;
52 index_type len;
53 index_type n;
54
55 if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
56 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
57
58 size = GFC_DESCRIPTOR_SIZE (ret);
59
60 which = which - 1;
61
62 extent[0] = 1;
63 count[0] = 0;
64 size = GFC_DESCRIPTOR_SIZE (array);
65 n = 0;
66
67 /* Initialized for avoiding compiler warnings. */
68 roffset = size;
69 soffset = size;
70 len = 0;
71
72 if (ret->data == NULL)
73 {
74 int i;
75
76 ret->data = internal_malloc (size * size0 ((array_t *)array));
77 ret->base = 0;
78 ret->dtype = array->dtype;
79 for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
80 {
81 ret->dim[i].lbound = 0;
82 ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
83
84 if (i == 0)
85 ret->dim[i].stride = 1;
86 else
87 ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
88 }
89 }
90
91 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
92 {
93 if (dim == which)
94 {
95 roffset = ret->dim[dim].stride * size;
96 if (roffset == 0)
97 roffset = size;
98 soffset = array->dim[dim].stride * size;
99 if (soffset == 0)
100 soffset = size;
101 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
102 }
103 else
104 {
105 count[n] = 0;
106 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
107 rstride[n] = ret->dim[dim].stride * size;
108 sstride[n] = array->dim[dim].stride * size;
109 n++;
110 }
111 }
112 if (sstride[0] == 0)
113 sstride[0] = size;
114 if (rstride[0] == 0)
115 rstride[0] = size;
116
117 dim = GFC_DESCRIPTOR_RANK (array);
118 rstride0 = rstride[0];
119 sstride0 = sstride[0];
120 rptr = ret->data;
121 sptr = array->data;
122
123 shift = (div (shift, len)).rem;
124 if (shift < 0)
125 shift += len;
126
127 while (rptr)
128 {
129 /* Do the shift for this dimension. */
130 src = &sptr[shift * soffset];
131 dest = rptr;
132 for (n = 0; n < len; n++)
133 {
134 memcpy (dest, src, size);
135 dest += roffset;
136 if (n == len - shift - 1)
137 src = sptr;
138 else
139 src += soffset;
140 }
141
142 /* Advance to the next section. */
143 rptr += rstride0;
144 sptr += sstride0;
145 count[0]++;
146 n = 0;
147 while (count[n] == extent[n])
148 {
149 /* When we get to the end of a dimension, reset it and increment
150 the next dimension. */
151 count[n] = 0;
152 /* We could precalculate these products, but this is a less
153 frequently used path so proabably not worth it. */
154 rptr -= rstride[n] * extent[n];
155 sptr -= sstride[n] * extent[n];
156 n++;
157 if (n >= dim - 1)
158 {
159 /* Break out of the loop. */
160 rptr = NULL;
161 break;
162 }
163 else
164 {
165 count[n]++;
166 rptr += rstride[n];
167 sptr += sstride[n];
168 }
169 }
170 }
171 }
172
173
174 void
175 __cshift0_4 (gfc_array_char * ret, const gfc_array_char * array,
176 const GFC_INTEGER_4 * pshift, const GFC_INTEGER_4 * pdim)
177 {
178 __cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
179 }
180
181
182 void
183 __cshift0_8 (gfc_array_char * ret, const gfc_array_char * array,
184 const GFC_INTEGER_8 * pshift, const GFC_INTEGER_8 * pdim)
185 {
186 __cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
187 }
188