* config/mips/mips.md (length): Don't use mips_fetch_insns for indexed
[official-gcc.git] / libgfortran / intrinsics / unpack_generic.c
blob3d02a3e6060ec87692f4e672739c866ffa75b2e5
1 /* Generic implementation of the RESHAPE intrinsic
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfor 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. */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
28 void
29 __unpack1 (const gfc_array_char * ret, const gfc_array_char * vector,
30 const gfc_array_l4 * mask, const gfc_array_char * field)
32 /* r.* indicates the return array. */
33 index_type rstride[GFC_MAX_DIMENSIONS];
34 index_type rstride0;
35 char *rptr;
36 /* v.* indicates the vector array. */
37 index_type vstride0;
38 char *vptr;
39 /* f.* indicates the field array. */
40 index_type fstride[GFC_MAX_DIMENSIONS];
41 index_type fstride0;
42 const char *fptr;
43 /* m.* indicates the mask array. */
44 index_type mstride[GFC_MAX_DIMENSIONS];
45 index_type mstride0;
46 const GFC_LOGICAL_4 *mptr;
48 index_type count[GFC_MAX_DIMENSIONS];
49 index_type extent[GFC_MAX_DIMENSIONS];
50 index_type n;
51 index_type dim;
52 index_type size;
53 index_type fsize;
55 size = GFC_DESCRIPTOR_SIZE (ret);
56 /* A field element size of 0 actually means this is a scalar. */
57 fsize = GFC_DESCRIPTOR_SIZE (field);
58 dim = GFC_DESCRIPTOR_RANK (ret);
59 for (n = 0; n < dim; n++)
61 count[n] = 0;
62 extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
63 rstride[n] = ret->dim[n].stride * size;
64 fstride[n] = field->dim[n].stride * fsize;
65 mstride[n] = mask->dim[n].stride;
67 if (rstride[0] == 0)
68 rstride[0] = size;
69 if (fstride[0] == 0)
70 fstride[0] = fsize;
71 if (mstride[0] == 0)
72 mstride[0] = 1;
74 vstride0 = vector->dim[0].stride * size;
75 if (vstride0 == 0)
76 vstride0 = size;
77 rstride0 = rstride[0];
78 fstride0 = fstride[0];
79 mstride0 = mstride[0];
80 rptr = ret->data;
81 fptr = field->data;
82 mptr = mask->data;
83 vptr = vector->data;
86 /* Use the same loop for both logical types. */
87 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
89 if (GFC_DESCRIPTOR_SIZE (mask) != 8)
90 runtime_error ("Funny sized logical array");
91 for (n = 0; n < dim; n++)
92 mstride[n] <<= 1;
93 mstride0 <<= 1;
94 mptr = GFOR_POINTER_L8_TO_L4 (mptr);
97 while (rptr)
99 if (*mptr)
101 /* From vector. */
102 memcpy (rptr, vptr, size);
103 vptr += vstride0;
105 else
107 /* From field. */
108 memcpy (rptr, fptr, size);
110 /* Advance to the next element. */
111 rptr += rstride0;
112 fptr += fstride0;
113 mptr += mstride0;
114 count[0]++;
115 n = 0;
116 while (count[n] == extent[n])
118 /* When we get to the end of a dimension, reset it and increment
119 the next dimension. */
120 count[n] = 0;
121 /* We could precalculate these products, but this is a less
122 frequently used path so proabably not worth it. */
123 rptr -= rstride[n] * extent[n];
124 fptr -= fstride[n] * extent[n];
125 mptr -= mstride[n] * extent[n];
126 n++;
127 if (n >= dim)
129 /* Break out of the loop. */
130 rptr = NULL;
131 break;
133 else
135 count[n]++;
136 rptr += rstride[n];
137 fptr += fstride[n];
138 mptr += mstride[n];
144 void
145 __unpack0 (const gfc_array_char * ret, const gfc_array_char * vector,
146 const gfc_array_l4 * mask, char * field)
148 gfc_array_char tmp;
150 tmp.dtype = 0;
151 tmp.data = field;
152 __unpack1 (ret, vector, mask, &tmp);