OSDN Git Service

2007-08-24 Thomas Koenig <tkoenig@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / unpack_generic.c
1 /* Generic implementation of the UNPACK intrinsic
2    Copyright 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.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 Ligbfortran 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 "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37 static void
38 unpack_internal (gfc_array_char *ret, const gfc_array_char *vector,
39                  const gfc_array_l1 *mask, const gfc_array_char *field,
40                  index_type size, index_type fsize)
41 {
42   /* r.* indicates the return array.  */
43   index_type rstride[GFC_MAX_DIMENSIONS];
44   index_type rstride0;
45   index_type rs;
46   char *rptr;
47   /* v.* indicates the vector array.  */
48   index_type vstride0;
49   char *vptr;
50   /* f.* indicates the field array.  */
51   index_type fstride[GFC_MAX_DIMENSIONS];
52   index_type fstride0;
53   const char *fptr;
54   /* m.* indicates the mask array.  */
55   index_type mstride[GFC_MAX_DIMENSIONS];
56   index_type mstride0;
57   const GFC_LOGICAL_1 *mptr;
58
59   index_type count[GFC_MAX_DIMENSIONS];
60   index_type extent[GFC_MAX_DIMENSIONS];
61   index_type n;
62   index_type dim;
63
64   int empty;
65   int mask_kind;
66
67   empty = 0;
68
69   mptr = mask->data;
70
71   /* Use the same loop for all logical types, by using GFC_LOGICAL_1
72      and using shifting to address size and endian issues.  */
73
74   mask_kind = GFC_DESCRIPTOR_SIZE (mask);
75
76   if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
77 #ifdef HAVE_GFC_LOGICAL_16
78       || mask_kind == 16
79 #endif
80       )
81     {
82       /*  Don't convert a NULL pointer as we use test for NULL below.  */
83       if (mptr)
84         mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
85     }
86   else
87     runtime_error ("Funny sized logical array");
88
89   if (ret->data == NULL)
90     {
91       /* The front end has signalled that we need to populate the
92          return array descriptor.  */
93       dim = GFC_DESCRIPTOR_RANK (mask);
94       rs = 1;
95       for (n = 0; n < dim; n++)
96         {
97           count[n] = 0;
98           ret->dim[n].stride = rs;
99           ret->dim[n].lbound = 0;
100           ret->dim[n].ubound = mask->dim[n].ubound - mask->dim[n].lbound;
101           extent[n] = ret->dim[n].ubound + 1;
102           empty = empty || extent[n] <= 0;
103           rstride[n] = ret->dim[n].stride * size;
104           fstride[n] = field->dim[n].stride * fsize;
105           mstride[n] = mask->dim[n].stride * mask_kind;
106           rs *= extent[n];
107         }
108       ret->offset = 0;
109       ret->data = internal_malloc_size (rs * size);
110     }
111   else
112     {
113       dim = GFC_DESCRIPTOR_RANK (ret);
114       for (n = 0; n < dim; n++)
115         {
116           count[n] = 0;
117           extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
118           empty = empty || extent[n] <= 0;
119           rstride[n] = ret->dim[n].stride * size;
120           fstride[n] = field->dim[n].stride * fsize;
121           mstride[n] = mask->dim[n].stride * mask_kind;
122         }
123       if (rstride[0] == 0)
124         rstride[0] = size;
125     }
126
127   if (empty)
128     return;
129
130   if (fstride[0] == 0)
131     fstride[0] = fsize;
132   if (mstride[0] == 0)
133     mstride[0] = 1;
134
135   vstride0 = vector->dim[0].stride * size;
136   if (vstride0 == 0)
137     vstride0 = size;
138   rstride0 = rstride[0];
139   fstride0 = fstride[0];
140   mstride0 = mstride[0];
141   rptr = ret->data;
142   fptr = field->data;
143   vptr = vector->data;
144
145   while (rptr)
146     {
147       if (*mptr)
148         {
149           /* From vector.  */
150           memcpy (rptr, vptr, size);
151           vptr += vstride0;
152         }
153       else
154         {
155           /* From field.  */
156           memcpy (rptr, fptr, size);
157         }
158       /* Advance to the next element.  */
159       rptr += rstride0;
160       fptr += fstride0;
161       mptr += mstride0;
162       count[0]++;
163       n = 0;
164       while (count[n] == extent[n])
165         {
166           /* When we get to the end of a dimension, reset it and increment
167              the next dimension.  */
168           count[n] = 0;
169           /* We could precalculate these products, but this is a less
170              frequently used path so probably not worth it.  */
171           rptr -= rstride[n] * extent[n];
172           fptr -= fstride[n] * extent[n];
173           mptr -= mstride[n] * extent[n];
174           n++;
175           if (n >= dim)
176             {
177               /* Break out of the loop.  */
178               rptr = NULL;
179               break;
180             }
181           else
182             {
183               count[n]++;
184               rptr += rstride[n];
185               fptr += fstride[n];
186               mptr += mstride[n];
187             }
188         }
189     }
190 }
191
192 extern void unpack1 (gfc_array_char *, const gfc_array_char *,
193                      const gfc_array_l4 *, const gfc_array_char *);
194 export_proto(unpack1);
195
196 void
197 unpack1 (gfc_array_char *ret, const gfc_array_char *vector,
198          const gfc_array_l4 *mask, const gfc_array_char *field)
199 {
200   unpack_internal (ret, vector, mask, field,
201                    GFC_DESCRIPTOR_SIZE (vector),
202                    GFC_DESCRIPTOR_SIZE (field));
203 }
204
205 extern void unpack1_char (gfc_array_char *, GFC_INTEGER_4,
206                           const gfc_array_char *, const gfc_array_l4 *,
207                           const gfc_array_char *, GFC_INTEGER_4,
208                           GFC_INTEGER_4);
209 export_proto(unpack1_char);
210
211 void
212 unpack1_char (gfc_array_char *ret,
213               GFC_INTEGER_4 ret_length __attribute__((unused)),
214               const gfc_array_char *vector, const gfc_array_l4 *mask,
215               const gfc_array_char *field, GFC_INTEGER_4 vector_length,
216               GFC_INTEGER_4 field_length)
217 {
218   unpack_internal (ret, vector, mask, field, vector_length, field_length);
219 }
220
221 extern void unpack0 (gfc_array_char *, const gfc_array_char *,
222                      const gfc_array_l4 *, char *);
223 export_proto(unpack0);
224
225 void
226 unpack0 (gfc_array_char *ret, const gfc_array_char *vector,
227          const gfc_array_l4 *mask, char *field)
228 {
229   gfc_array_char tmp;
230
231   memset (&tmp, 0, sizeof (tmp));
232   tmp.dtype = 0;
233   tmp.data = field;
234   unpack_internal (ret, vector, mask, &tmp, GFC_DESCRIPTOR_SIZE (vector), 0);
235 }
236
237 extern void unpack0_char (gfc_array_char *, GFC_INTEGER_4,
238                           const gfc_array_char *, const gfc_array_l4 *,
239                           char *, GFC_INTEGER_4, GFC_INTEGER_4);
240 export_proto(unpack0_char);
241
242 void
243 unpack0_char (gfc_array_char *ret,
244               GFC_INTEGER_4 ret_length __attribute__((unused)),
245               const gfc_array_char *vector, const gfc_array_l4 *mask,
246               char *field, GFC_INTEGER_4 vector_length,
247               GFC_INTEGER_4 field_length __attribute__((unused)))
248 {
249   gfc_array_char tmp;
250
251   memset (&tmp, 0, sizeof (tmp));
252   tmp.dtype = 0;
253   tmp.data = field;
254   unpack_internal (ret, vector, mask, &tmp, vector_length, 0);
255 }