]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/unpack_generic.c
re PR libfortran/19280 (Inconsistent licensing of libgfortran)
[thirdparty/gcc.git] / libgfortran / intrinsics / unpack_generic.c
1 /* Generic implementation of the RESHAPE intrinsic
2 Copyright 2002 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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37 extern void unpack1 (const gfc_array_char *, const gfc_array_char *,
38 const gfc_array_l4 *, const gfc_array_char *);
39 iexport_proto(unpack1);
40
41 void
42 unpack1 (const gfc_array_char *ret, const gfc_array_char *vector,
43 const gfc_array_l4 *mask, const gfc_array_char *field)
44 {
45 /* r.* indicates the return array. */
46 index_type rstride[GFC_MAX_DIMENSIONS];
47 index_type rstride0;
48 char *rptr;
49 /* v.* indicates the vector array. */
50 index_type vstride0;
51 char *vptr;
52 /* f.* indicates the field array. */
53 index_type fstride[GFC_MAX_DIMENSIONS];
54 index_type fstride0;
55 const char *fptr;
56 /* m.* indicates the mask array. */
57 index_type mstride[GFC_MAX_DIMENSIONS];
58 index_type mstride0;
59 const GFC_LOGICAL_4 *mptr;
60
61 index_type count[GFC_MAX_DIMENSIONS];
62 index_type extent[GFC_MAX_DIMENSIONS];
63 index_type n;
64 index_type dim;
65 index_type size;
66 index_type fsize;
67
68 size = GFC_DESCRIPTOR_SIZE (ret);
69 /* A field element size of 0 actually means this is a scalar. */
70 fsize = GFC_DESCRIPTOR_SIZE (field);
71 dim = GFC_DESCRIPTOR_RANK (ret);
72 for (n = 0; n < dim; n++)
73 {
74 count[n] = 0;
75 extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
76 rstride[n] = ret->dim[n].stride * size;
77 fstride[n] = field->dim[n].stride * fsize;
78 mstride[n] = mask->dim[n].stride;
79 }
80 if (rstride[0] == 0)
81 rstride[0] = size;
82 if (fstride[0] == 0)
83 fstride[0] = fsize;
84 if (mstride[0] == 0)
85 mstride[0] = 1;
86
87 vstride0 = vector->dim[0].stride * size;
88 if (vstride0 == 0)
89 vstride0 = size;
90 rstride0 = rstride[0];
91 fstride0 = fstride[0];
92 mstride0 = mstride[0];
93 rptr = ret->data;
94 fptr = field->data;
95 mptr = mask->data;
96 vptr = vector->data;
97
98 /* Use the same loop for both logical types. */
99 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
100 {
101 if (GFC_DESCRIPTOR_SIZE (mask) != 8)
102 runtime_error ("Funny sized logical array");
103 for (n = 0; n < dim; n++)
104 mstride[n] <<= 1;
105 mstride0 <<= 1;
106 mptr = GFOR_POINTER_L8_TO_L4 (mptr);
107 }
108
109 while (rptr)
110 {
111 if (*mptr)
112 {
113 /* From vector. */
114 memcpy (rptr, vptr, size);
115 vptr += vstride0;
116 }
117 else
118 {
119 /* From field. */
120 memcpy (rptr, fptr, size);
121 }
122 /* Advance to the next element. */
123 rptr += rstride0;
124 fptr += fstride0;
125 mptr += mstride0;
126 count[0]++;
127 n = 0;
128 while (count[n] == extent[n])
129 {
130 /* When we get to the end of a dimension, reset it and increment
131 the next dimension. */
132 count[n] = 0;
133 /* We could precalculate these products, but this is a less
134 frequently used path so proabably not worth it. */
135 rptr -= rstride[n] * extent[n];
136 fptr -= fstride[n] * extent[n];
137 mptr -= mstride[n] * extent[n];
138 n++;
139 if (n >= dim)
140 {
141 /* Break out of the loop. */
142 rptr = NULL;
143 break;
144 }
145 else
146 {
147 count[n]++;
148 rptr += rstride[n];
149 fptr += fstride[n];
150 mptr += mstride[n];
151 }
152 }
153 }
154 }
155 iexport(unpack1);
156
157 extern void unpack0 (const gfc_array_char *, const gfc_array_char *,
158 const gfc_array_l4 *, char *);
159 export_proto(unpack0);
160
161 void
162 unpack0 (const gfc_array_char *ret, const gfc_array_char *vector,
163 const gfc_array_l4 *mask, char *field)
164 {
165 gfc_array_char tmp;
166
167 tmp.dtype = 0;
168 tmp.data = field;
169 unpack1 (ret, vector, mask, &tmp);
170 }