1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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.
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. */
26 #include "libgfortran.h"
28 /* TODO: make this work for large shifts when
29 sizeof(int) < sizeof (index_type). */
32 __cshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
35 /* r.* indicates the return array. */
36 index_type rstride
[GFC_MAX_DIMENSIONS
- 1];
41 /* s.* indicates the source array. */
42 index_type sstride
[GFC_MAX_DIMENSIONS
- 1];
48 index_type count
[GFC_MAX_DIMENSIONS
- 1];
49 index_type extent
[GFC_MAX_DIMENSIONS
- 1];
55 if (which
< 1 || which
> GFC_DESCRIPTOR_RANK (array
))
56 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
58 size
= GFC_DESCRIPTOR_SIZE (ret
);
64 size
= GFC_DESCRIPTOR_SIZE (array
);
67 /* Initialized for avoiding compiler warnings. */
72 if (ret
->data
== NULL
)
76 ret
->data
= internal_malloc (size
* size0 ((array_t
*)array
));
78 ret
->dtype
= array
->dtype
;
79 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
81 ret
->dim
[i
].lbound
= 0;
82 ret
->dim
[i
].ubound
= array
->dim
[i
].ubound
- array
->dim
[i
].lbound
;
85 ret
->dim
[i
].stride
= 1;
87 ret
->dim
[i
].stride
= (ret
->dim
[i
-1].ubound
+ 1) * ret
->dim
[i
-1].stride
;
91 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
95 roffset
= ret
->dim
[dim
].stride
* size
;
98 soffset
= array
->dim
[dim
].stride
* size
;
101 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
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
;
117 dim
= GFC_DESCRIPTOR_RANK (array
);
118 rstride0
= rstride
[0];
119 sstride0
= sstride
[0];
123 shift
= (div (shift
, len
)).rem
;
129 /* Do the shift for this dimension. */
130 src
= &sptr
[shift
* soffset
];
132 for (n
= 0; n
< len
; n
++)
134 memcpy (dest
, src
, size
);
136 if (n
== len
- shift
- 1)
142 /* Advance to the next section. */
147 while (count
[n
] == extent
[n
])
149 /* When we get to the end of a dimension, reset it and increment
150 the next dimension. */
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
];
159 /* Break out of the loop. */
175 __cshift0_4 (gfc_array_char
* ret
, const gfc_array_char
* array
,
176 const GFC_INTEGER_4
* pshift
, const GFC_INTEGER_4
* pdim
)
178 __cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);
183 __cshift0_8 (gfc_array_char
* ret
, const gfc_array_char
* array
,
184 const GFC_INTEGER_8
* pshift
, const GFC_INTEGER_8
* pdim
)
186 __cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);