OSDN Git Service

* include/tr1_impl/array (at): Do not use builtin_expect.
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / pack.m4
1 `/* Specific implementation of the PACK intrinsic
2    Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009 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 3 of the License, or (at your option) any later version.
11
12 Ligbfortran 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 General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>'
30
31 include(iparm.m4)dnl
32
33 `#if defined (HAVE_'rtype_name`)
34
35 /* PACK is specified as follows:
36
37    13.14.80 PACK (ARRAY, MASK, [VECTOR])
38
39    Description: Pack an array into an array of rank one under the
40    control of a mask.
41
42    Class: Transformational function.
43
44    Arguments:
45       ARRAY   may be of any type. It shall not be scalar.
46       MASK    shall be of type LOGICAL. It shall be conformable with ARRAY.
47       VECTOR  (optional) shall be of the same type and type parameters
48               as ARRAY. VECTOR shall have at least as many elements as
49               there are true elements in MASK. If MASK is a scalar
50               with the value true, VECTOR shall have at least as many
51               elements as there are in ARRAY.
52
53    Result Characteristics: The result is an array of rank one with the
54    same type and type parameters as ARRAY. If VECTOR is present, the
55    result size is that of VECTOR; otherwise, the result size is the
56    number /t/ of true elements in MASK unless MASK is scalar with the
57    value true, in which case the result size is the size of ARRAY.
58
59    Result Value: Element /i/ of the result is the element of ARRAY
60    that corresponds to the /i/th true element of MASK, taking elements
61    in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
62    present and has size /n/ > /t/, element /i/ of the result has the
63    value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
64
65    Examples: The nonzero elements of an array M with the value
66    | 0 0 0 |
67    | 9 0 0 | may be "gathered" by the function PACK. The result of
68    | 0 0 7 |
69    PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
70    VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
71
72 There are two variants of the PACK intrinsic: one, where MASK is
73 array valued, and the other one where MASK is scalar.  */
74
75 void
76 pack_'rtype_code` ('rtype` *ret, const 'rtype` *array,
77                const gfc_array_l1 *mask, const 'rtype` *vector)
78 {
79   /* r.* indicates the return array.  */
80   index_type rstride0;
81   'rtype_name` * restrict rptr;
82   /* s.* indicates the source array.  */
83   index_type sstride[GFC_MAX_DIMENSIONS];
84   index_type sstride0;
85   const 'rtype_name` *sptr;
86   /* m.* indicates the mask array.  */
87   index_type mstride[GFC_MAX_DIMENSIONS];
88   index_type mstride0;
89   const GFC_LOGICAL_1 *mptr;
90
91   index_type count[GFC_MAX_DIMENSIONS];
92   index_type extent[GFC_MAX_DIMENSIONS];
93   int zero_sized;
94   index_type n;
95   index_type dim;
96   index_type nelem;
97   index_type total;
98   int mask_kind;
99
100   dim = GFC_DESCRIPTOR_RANK (array);
101
102   mptr = mask->data;
103
104   /* Use the same loop for all logical types, by using GFC_LOGICAL_1
105      and using shifting to address size and endian issues.  */
106
107   mask_kind = GFC_DESCRIPTOR_SIZE (mask);
108
109   if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
110 #ifdef HAVE_GFC_LOGICAL_16
111       || mask_kind == 16
112 #endif
113       )
114     {
115       /*  Do not convert a NULL pointer as we use test for NULL below.  */
116       if (mptr)
117         mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
118     }
119   else
120     runtime_error ("Funny sized logical array");
121
122   zero_sized = 0;
123   for (n = 0; n < dim; n++)
124     {
125       count[n] = 0;
126       extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
127       if (extent[n] <= 0)
128        zero_sized = 1;
129       sstride[n] = array->dim[n].stride;
130       mstride[n] = mask->dim[n].stride * mask_kind;
131     }
132   if (sstride[0] == 0)
133     sstride[0] = 1;
134   if (mstride[0] == 0)
135     mstride[0] = mask_kind;
136
137   if (zero_sized)
138     sptr = NULL;
139   else
140     sptr = array->data;
141
142   if (ret->data == NULL || compile_options.bounds_check)
143     {
144       /* Count the elements, either for allocating memory or
145          for bounds checking.  */
146
147       if (vector != NULL)
148         {
149           /* The return array will have as many
150              elements as there are in VECTOR.  */
151           total = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
152           if (total < 0)
153             {
154               total = 0;
155               vector = NULL;
156             }
157         }
158       else
159         {
160           /* We have to count the true elements in MASK.  */
161
162           /* TODO: We could speed up pack easily in the case of only
163              few .TRUE. entries in MASK, by keeping track of where we
164              would be in the source array during the initial traversal
165              of MASK, and caching the pointers to those elements. Then,
166              supposed the number of elements is small enough, we would
167              only have to traverse the list, and copy those elements
168              into the result array. In the case of datatypes which fit
169              in one of the integer types we could also cache the
170              value instead of a pointer to it.
171              This approach might be bad from the point of view of
172              cache behavior in the case where our cache is not big
173              enough to hold all elements that have to be copied.  */
174
175           const GFC_LOGICAL_1 *m = mptr;
176
177           total = 0;
178           if (zero_sized)
179             m = NULL;
180
181           while (m)
182             {
183               /* Test this element.  */
184               if (*m)
185                 total++;
186
187               /* Advance to the next element.  */
188               m += mstride[0];
189               count[0]++;
190               n = 0;
191               while (count[n] == extent[n])
192                 {
193                   /* When we get to the end of a dimension, reset it
194                      and increment the next dimension.  */
195                   count[n] = 0;
196                   /* We could precalculate this product, but this is a
197                      less frequently used path so probably not worth
198                      it.  */
199                   m -= mstride[n] * extent[n];
200                   n++;
201                   if (n >= dim)
202                     {
203                       /* Break out of the loop.  */
204                       m = NULL;
205                       break;
206                     }
207                   else
208                     {
209                       count[n]++;
210                       m += mstride[n];
211                     }
212                 }
213             }
214         }
215
216       if (ret->data == NULL)
217         {
218           /* Setup the array descriptor.  */
219           ret->dim[0].lbound = 0;
220           ret->dim[0].ubound = total - 1;
221           ret->dim[0].stride = 1;
222
223           ret->offset = 0;
224           if (total == 0)
225             {
226               /* In this case, nothing remains to be done.  */
227               ret->data = internal_malloc_size (1);
228               return;
229             }
230           else
231             ret->data = internal_malloc_size (sizeof ('rtype_name`) * total);
232         }
233       else 
234         {
235           /* We come here because of range checking.  */
236           index_type ret_extent;
237
238           ret_extent = ret->dim[0].ubound + 1 - ret->dim[0].lbound;
239           if (total != ret_extent)
240             runtime_error ("Incorrect extent in return value of PACK intrinsic;"
241                            " is %ld, should be %ld", (long int) total,
242                            (long int) ret_extent);
243         }
244     }
245
246   rstride0 = ret->dim[0].stride;
247   if (rstride0 == 0)
248     rstride0 = 1;
249   sstride0 = sstride[0];
250   mstride0 = mstride[0];
251   rptr = ret->data;
252
253   while (sptr && mptr)
254     {
255       /* Test this element.  */
256       if (*mptr)
257         {
258           /* Add it.  */
259           *rptr = *sptr;
260           rptr += rstride0;
261         }
262       /* Advance to the next element.  */
263       sptr += sstride0;
264       mptr += mstride0;
265       count[0]++;
266       n = 0;
267       while (count[n] == extent[n])
268         {
269           /* When we get to the end of a dimension, reset it and increment
270              the next dimension.  */
271           count[n] = 0;
272           /* We could precalculate these products, but this is a less
273              frequently used path so probably not worth it.  */
274           sptr -= sstride[n] * extent[n];
275           mptr -= mstride[n] * extent[n];
276           n++;
277           if (n >= dim)
278             {
279               /* Break out of the loop.  */
280               sptr = NULL;
281               break;
282             }
283           else
284             {
285               count[n]++;
286               sptr += sstride[n];
287               mptr += mstride[n];
288             }
289         }
290     }
291
292   /* Add any remaining elements from VECTOR.  */
293   if (vector)
294     {
295       n = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
296       nelem = ((rptr - ret->data) / rstride0);
297       if (n > nelem)
298         {
299           sstride0 = vector->dim[0].stride;
300           if (sstride0 == 0)
301             sstride0 = 1;
302
303           sptr = vector->data + sstride0 * nelem;
304           n -= nelem;
305           while (n--)
306             {
307               *rptr = *sptr;
308               rptr += rstride0;
309               sptr += sstride0;
310             }
311         }
312     }
313 }
314
315 #endif
316 '