1 /* Specific implementation of the UNPACK intrinsic
2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
4 unpack_generic.c by Paul Brook <paul@nowt.org>.
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 Ligbfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
33 #if defined (HAVE_GFC_COMPLEX_10)
36 unpack0_c10 (gfc_array_c10
*ret
, const gfc_array_c10
*vector
,
37 const gfc_array_l1
*mask
, const GFC_COMPLEX_10
*fptr
)
39 /* r.* indicates the return array. */
40 index_type rstride
[GFC_MAX_DIMENSIONS
];
43 GFC_COMPLEX_10
* restrict rptr
;
44 /* v.* indicates the vector array. */
47 /* Value for field, this is constant. */
48 const GFC_COMPLEX_10 fval
= *fptr
;
49 /* m.* indicates the mask array. */
50 index_type mstride
[GFC_MAX_DIMENSIONS
];
52 const GFC_LOGICAL_1
*mptr
;
54 index_type count
[GFC_MAX_DIMENSIONS
];
55 index_type extent
[GFC_MAX_DIMENSIONS
];
66 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
67 and using shifting to address size and endian issues. */
69 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
71 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
72 #ifdef HAVE_GFC_LOGICAL_16
77 /* Do not convert a NULL pointer as we use test for NULL below. */
79 mptr
= GFOR_POINTER_TO_L1 (mptr
, mask_kind
);
82 runtime_error ("Funny sized logical array");
84 if (ret
->data
== NULL
)
86 /* The front end has signalled that we need to populate the
87 return array descriptor. */
88 dim
= GFC_DESCRIPTOR_RANK (mask
);
90 for (n
= 0; n
< dim
; n
++)
93 ret
->dim
[n
].stride
= rs
;
94 ret
->dim
[n
].lbound
= 0;
95 ret
->dim
[n
].ubound
= mask
->dim
[n
].ubound
- mask
->dim
[n
].lbound
;
96 extent
[n
] = ret
->dim
[n
].ubound
+ 1;
97 empty
= empty
|| extent
[n
] <= 0;
98 rstride
[n
] = ret
->dim
[n
].stride
;
99 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
103 ret
->data
= internal_malloc_size (rs
* sizeof (GFC_COMPLEX_10
));
107 dim
= GFC_DESCRIPTOR_RANK (ret
);
108 for (n
= 0; n
< dim
; n
++)
111 extent
[n
] = ret
->dim
[n
].ubound
+ 1 - ret
->dim
[n
].lbound
;
112 empty
= empty
|| extent
[n
] <= 0;
113 rstride
[n
] = ret
->dim
[n
].stride
;
114 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
126 vstride0
= vector
->dim
[0].stride
;
129 rstride0
= rstride
[0];
130 mstride0
= mstride
[0];
147 /* Advance to the next element. */
152 while (count
[n
] == extent
[n
])
154 /* When we get to the end of a dimension, reset it and increment
155 the next dimension. */
157 /* We could precalculate these products, but this is a less
158 frequently used path so probably not worth it. */
159 rptr
-= rstride
[n
] * extent
[n
];
160 mptr
-= mstride
[n
] * extent
[n
];
164 /* Break out of the loop. */
179 unpack1_c10 (gfc_array_c10
*ret
, const gfc_array_c10
*vector
,
180 const gfc_array_l1
*mask
, const gfc_array_c10
*field
)
182 /* r.* indicates the return array. */
183 index_type rstride
[GFC_MAX_DIMENSIONS
];
186 GFC_COMPLEX_10
* restrict rptr
;
187 /* v.* indicates the vector array. */
189 GFC_COMPLEX_10
*vptr
;
190 /* f.* indicates the field array. */
191 index_type fstride
[GFC_MAX_DIMENSIONS
];
193 const GFC_COMPLEX_10
*fptr
;
194 /* m.* indicates the mask array. */
195 index_type mstride
[GFC_MAX_DIMENSIONS
];
197 const GFC_LOGICAL_1
*mptr
;
199 index_type count
[GFC_MAX_DIMENSIONS
];
200 index_type extent
[GFC_MAX_DIMENSIONS
];
211 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
212 and using shifting to address size and endian issues. */
214 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
216 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
217 #ifdef HAVE_GFC_LOGICAL_16
222 /* Do not convert a NULL pointer as we use test for NULL below. */
224 mptr
= GFOR_POINTER_TO_L1 (mptr
, mask_kind
);
227 runtime_error ("Funny sized logical array");
229 if (ret
->data
== NULL
)
231 /* The front end has signalled that we need to populate the
232 return array descriptor. */
233 dim
= GFC_DESCRIPTOR_RANK (mask
);
235 for (n
= 0; n
< dim
; n
++)
238 ret
->dim
[n
].stride
= rs
;
239 ret
->dim
[n
].lbound
= 0;
240 ret
->dim
[n
].ubound
= mask
->dim
[n
].ubound
- mask
->dim
[n
].lbound
;
241 extent
[n
] = ret
->dim
[n
].ubound
+ 1;
242 empty
= empty
|| extent
[n
] <= 0;
243 rstride
[n
] = ret
->dim
[n
].stride
;
244 fstride
[n
] = field
->dim
[n
].stride
;
245 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
249 ret
->data
= internal_malloc_size (rs
* sizeof (GFC_COMPLEX_10
));
253 dim
= GFC_DESCRIPTOR_RANK (ret
);
254 for (n
= 0; n
< dim
; n
++)
257 extent
[n
] = ret
->dim
[n
].ubound
+ 1 - ret
->dim
[n
].lbound
;
258 empty
= empty
|| extent
[n
] <= 0;
259 rstride
[n
] = ret
->dim
[n
].stride
;
260 fstride
[n
] = field
->dim
[n
].stride
;
261 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
275 vstride0
= vector
->dim
[0].stride
;
278 rstride0
= rstride
[0];
279 fstride0
= fstride
[0];
280 mstride0
= mstride
[0];
298 /* Advance to the next element. */
304 while (count
[n
] == extent
[n
])
306 /* When we get to the end of a dimension, reset it and increment
307 the next dimension. */
309 /* We could precalculate these products, but this is a less
310 frequently used path so probably not worth it. */
311 rptr
-= rstride
[n
] * extent
[n
];
312 fptr
-= fstride
[n
] * extent
[n
];
313 mptr
-= mstride
[n
] * extent
[n
];
317 /* Break out of the loop. */