xref: /netbsd-src/external/gpl3/gcc/dist/libgfortran/generated/pack_c4.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1181254a7Smrg /* Specific implementation of the PACK intrinsic
2*b1e83836Smrg    Copyright (C) 2002-2022 Free Software Foundation, Inc.
3181254a7Smrg    Contributed by Paul Brook <paul@nowt.org>
4181254a7Smrg 
5181254a7Smrg This file is part of the GNU Fortran runtime library (libgfortran).
6181254a7Smrg 
7181254a7Smrg Libgfortran is free software; you can redistribute it and/or
8181254a7Smrg modify it under the terms of the GNU General Public
9181254a7Smrg License as published by the Free Software Foundation; either
10181254a7Smrg version 3 of the License, or (at your option) any later version.
11181254a7Smrg 
12181254a7Smrg Ligbfortran is distributed in the hope that it will be useful,
13181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15181254a7Smrg GNU General Public License for more details.
16181254a7Smrg 
17181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
18181254a7Smrg permissions described in the GCC Runtime Library Exception, version
19181254a7Smrg 3.1, as published by the Free Software Foundation.
20181254a7Smrg 
21181254a7Smrg You should have received a copy of the GNU General Public License and
22181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
23181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24181254a7Smrg <http://www.gnu.org/licenses/>.  */
25181254a7Smrg 
26181254a7Smrg #include "libgfortran.h"
27181254a7Smrg #include <string.h>
28181254a7Smrg 
29181254a7Smrg 
30181254a7Smrg #if defined (HAVE_GFC_COMPLEX_4)
31181254a7Smrg 
32181254a7Smrg /* PACK is specified as follows:
33181254a7Smrg 
34181254a7Smrg    13.14.80 PACK (ARRAY, MASK, [VECTOR])
35181254a7Smrg 
36181254a7Smrg    Description: Pack an array into an array of rank one under the
37181254a7Smrg    control of a mask.
38181254a7Smrg 
39181254a7Smrg    Class: Transformational function.
40181254a7Smrg 
41181254a7Smrg    Arguments:
42181254a7Smrg       ARRAY   may be of any type. It shall not be scalar.
43181254a7Smrg       MASK    shall be of type LOGICAL. It shall be conformable with ARRAY.
44181254a7Smrg       VECTOR  (optional) shall be of the same type and type parameters
45181254a7Smrg               as ARRAY. VECTOR shall have at least as many elements as
46181254a7Smrg               there are true elements in MASK. If MASK is a scalar
47181254a7Smrg               with the value true, VECTOR shall have at least as many
48181254a7Smrg               elements as there are in ARRAY.
49181254a7Smrg 
50181254a7Smrg    Result Characteristics: The result is an array of rank one with the
51181254a7Smrg    same type and type parameters as ARRAY. If VECTOR is present, the
52181254a7Smrg    result size is that of VECTOR; otherwise, the result size is the
53181254a7Smrg    number /t/ of true elements in MASK unless MASK is scalar with the
54181254a7Smrg    value true, in which case the result size is the size of ARRAY.
55181254a7Smrg 
56181254a7Smrg    Result Value: Element /i/ of the result is the element of ARRAY
57181254a7Smrg    that corresponds to the /i/th true element of MASK, taking elements
58181254a7Smrg    in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
59181254a7Smrg    present and has size /n/ > /t/, element /i/ of the result has the
60181254a7Smrg    value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
61181254a7Smrg 
62181254a7Smrg    Examples: The nonzero elements of an array M with the value
63181254a7Smrg    | 0 0 0 |
64181254a7Smrg    | 9 0 0 | may be "gathered" by the function PACK. The result of
65181254a7Smrg    | 0 0 7 |
66181254a7Smrg    PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
67181254a7Smrg    VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
68181254a7Smrg 
69181254a7Smrg There are two variants of the PACK intrinsic: one, where MASK is
70181254a7Smrg array valued, and the other one where MASK is scalar.  */
71181254a7Smrg 
72181254a7Smrg void
pack_c4(gfc_array_c4 * ret,const gfc_array_c4 * array,const gfc_array_l1 * mask,const gfc_array_c4 * vector)73181254a7Smrg pack_c4 (gfc_array_c4 *ret, const gfc_array_c4 *array,
74181254a7Smrg 	       const gfc_array_l1 *mask, const gfc_array_c4 *vector)
75181254a7Smrg {
76181254a7Smrg   /* r.* indicates the return array.  */
77181254a7Smrg   index_type rstride0;
78181254a7Smrg   GFC_COMPLEX_4 * restrict rptr;
79181254a7Smrg   /* s.* indicates the source array.  */
80181254a7Smrg   index_type sstride[GFC_MAX_DIMENSIONS];
81181254a7Smrg   index_type sstride0;
82181254a7Smrg   const GFC_COMPLEX_4 *sptr;
83181254a7Smrg   /* m.* indicates the mask array.  */
84181254a7Smrg   index_type mstride[GFC_MAX_DIMENSIONS];
85181254a7Smrg   index_type mstride0;
86181254a7Smrg   const GFC_LOGICAL_1 *mptr;
87181254a7Smrg 
88181254a7Smrg   index_type count[GFC_MAX_DIMENSIONS];
89181254a7Smrg   index_type extent[GFC_MAX_DIMENSIONS];
90181254a7Smrg   int zero_sized;
91181254a7Smrg   index_type n;
92181254a7Smrg   index_type dim;
93181254a7Smrg   index_type nelem;
94181254a7Smrg   index_type total;
95181254a7Smrg   int mask_kind;
96181254a7Smrg 
97181254a7Smrg   dim = GFC_DESCRIPTOR_RANK (array);
98181254a7Smrg 
99181254a7Smrg   mptr = mask->base_addr;
100181254a7Smrg 
101181254a7Smrg   /* Use the same loop for all logical types, by using GFC_LOGICAL_1
102181254a7Smrg      and using shifting to address size and endian issues.  */
103181254a7Smrg 
104181254a7Smrg   mask_kind = GFC_DESCRIPTOR_SIZE (mask);
105181254a7Smrg 
106181254a7Smrg   if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
107181254a7Smrg #ifdef HAVE_GFC_LOGICAL_16
108181254a7Smrg       || mask_kind == 16
109181254a7Smrg #endif
110181254a7Smrg       )
111181254a7Smrg     {
112181254a7Smrg       /*  Do not convert a NULL pointer as we use test for NULL below.  */
113181254a7Smrg       if (mptr)
114181254a7Smrg 	mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
115181254a7Smrg     }
116181254a7Smrg   else
117181254a7Smrg     runtime_error ("Funny sized logical array");
118181254a7Smrg 
119181254a7Smrg   zero_sized = 0;
120181254a7Smrg   for (n = 0; n < dim; n++)
121181254a7Smrg     {
122181254a7Smrg       count[n] = 0;
123181254a7Smrg       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
124181254a7Smrg       if (extent[n] <= 0)
125181254a7Smrg        zero_sized = 1;
126181254a7Smrg       sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
127181254a7Smrg       mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
128181254a7Smrg     }
129181254a7Smrg   if (sstride[0] == 0)
130181254a7Smrg     sstride[0] = 1;
131181254a7Smrg   if (mstride[0] == 0)
132181254a7Smrg     mstride[0] = mask_kind;
133181254a7Smrg 
134181254a7Smrg   if (zero_sized)
135181254a7Smrg     sptr = NULL;
136181254a7Smrg   else
137181254a7Smrg     sptr = array->base_addr;
138181254a7Smrg 
139181254a7Smrg   if (ret->base_addr == NULL || unlikely (compile_options.bounds_check))
140181254a7Smrg     {
141181254a7Smrg       /* Count the elements, either for allocating memory or
142181254a7Smrg 	 for bounds checking.  */
143181254a7Smrg 
144181254a7Smrg       if (vector != NULL)
145181254a7Smrg 	{
146181254a7Smrg 	  /* The return array will have as many
147181254a7Smrg 	     elements as there are in VECTOR.  */
148181254a7Smrg 	  total = GFC_DESCRIPTOR_EXTENT(vector,0);
149181254a7Smrg 	  if (total < 0)
150181254a7Smrg 	    {
151181254a7Smrg 	      total = 0;
152181254a7Smrg 	      vector = NULL;
153181254a7Smrg 	    }
154181254a7Smrg 	}
155181254a7Smrg       else
156181254a7Smrg         {
157181254a7Smrg       	  /* We have to count the true elements in MASK.  */
158181254a7Smrg 	  total = count_0 (mask);
159181254a7Smrg         }
160181254a7Smrg 
161181254a7Smrg       if (ret->base_addr == NULL)
162181254a7Smrg 	{
163181254a7Smrg 	  /* Setup the array descriptor.  */
164181254a7Smrg 	  GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
165181254a7Smrg 
166181254a7Smrg 	  ret->offset = 0;
167181254a7Smrg 
168181254a7Smrg 	  /* xmallocarray allocates a single byte for zero size.  */
169181254a7Smrg 	  ret->base_addr = xmallocarray (total, sizeof (GFC_COMPLEX_4));
170181254a7Smrg 
171181254a7Smrg 	  if (total == 0)
172181254a7Smrg 	    return;
173181254a7Smrg 	}
174181254a7Smrg       else
175181254a7Smrg 	{
176181254a7Smrg 	  /* We come here because of range checking.  */
177181254a7Smrg 	  index_type ret_extent;
178181254a7Smrg 
179181254a7Smrg 	  ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
180181254a7Smrg 	  if (total != ret_extent)
181181254a7Smrg 	    runtime_error ("Incorrect extent in return value of PACK intrinsic;"
182181254a7Smrg 			   " is %ld, should be %ld", (long int) total,
183181254a7Smrg 			   (long int) ret_extent);
184181254a7Smrg 	}
185181254a7Smrg     }
186181254a7Smrg 
187181254a7Smrg   rstride0 = GFC_DESCRIPTOR_STRIDE(ret,0);
188181254a7Smrg   if (rstride0 == 0)
189181254a7Smrg     rstride0 = 1;
190181254a7Smrg   sstride0 = sstride[0];
191181254a7Smrg   mstride0 = mstride[0];
192181254a7Smrg   rptr = ret->base_addr;
193181254a7Smrg 
194181254a7Smrg   while (sptr && mptr)
195181254a7Smrg     {
196181254a7Smrg       /* Test this element.  */
197181254a7Smrg       if (*mptr)
198181254a7Smrg         {
199181254a7Smrg           /* Add it.  */
200181254a7Smrg 	  *rptr = *sptr;
201181254a7Smrg           rptr += rstride0;
202181254a7Smrg         }
203181254a7Smrg       /* Advance to the next element.  */
204181254a7Smrg       sptr += sstride0;
205181254a7Smrg       mptr += mstride0;
206181254a7Smrg       count[0]++;
207181254a7Smrg       n = 0;
208181254a7Smrg       while (count[n] == extent[n])
209181254a7Smrg         {
210181254a7Smrg           /* When we get to the end of a dimension, reset it and increment
211181254a7Smrg              the next dimension.  */
212181254a7Smrg           count[n] = 0;
213181254a7Smrg           /* We could precalculate these products, but this is a less
214181254a7Smrg              frequently used path so probably not worth it.  */
215181254a7Smrg           sptr -= sstride[n] * extent[n];
216181254a7Smrg           mptr -= mstride[n] * extent[n];
217181254a7Smrg           n++;
218181254a7Smrg           if (n >= dim)
219181254a7Smrg             {
220181254a7Smrg               /* Break out of the loop.  */
221181254a7Smrg               sptr = NULL;
222181254a7Smrg               break;
223181254a7Smrg             }
224181254a7Smrg           else
225181254a7Smrg             {
226181254a7Smrg               count[n]++;
227181254a7Smrg               sptr += sstride[n];
228181254a7Smrg               mptr += mstride[n];
229181254a7Smrg             }
230181254a7Smrg         }
231181254a7Smrg     }
232181254a7Smrg 
233181254a7Smrg   /* Add any remaining elements from VECTOR.  */
234181254a7Smrg   if (vector)
235181254a7Smrg     {
236181254a7Smrg       n = GFC_DESCRIPTOR_EXTENT(vector,0);
237181254a7Smrg       nelem = ((rptr - ret->base_addr) / rstride0);
238181254a7Smrg       if (n > nelem)
239181254a7Smrg         {
240181254a7Smrg           sstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
241181254a7Smrg           if (sstride0 == 0)
242181254a7Smrg             sstride0 = 1;
243181254a7Smrg 
244181254a7Smrg           sptr = vector->base_addr + sstride0 * nelem;
245181254a7Smrg           n -= nelem;
246181254a7Smrg           while (n--)
247181254a7Smrg             {
248181254a7Smrg 	      *rptr = *sptr;
249181254a7Smrg               rptr += rstride0;
250181254a7Smrg               sptr += sstride0;
251181254a7Smrg             }
252181254a7Smrg         }
253181254a7Smrg     }
254181254a7Smrg }
255181254a7Smrg 
256181254a7Smrg #endif
257181254a7Smrg 
258