Convert diagnostics to use quoting flag q 4/n
[official-gcc.git] / libgfortran / intrinsics / pack_generic.c
blob08c022e4e746f4b1099879fac8fb88626359b494
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 __pack (const gfc_array_char * ret, const gfc_array_char * array,
30 const gfc_array_l4 * mask, const gfc_array_char * vector)
32 /* r.* indicates the return array. */
33 index_type rstride0;
34 char *rptr;
35 /* s.* indicates the source array. */
36 index_type sstride[GFC_MAX_DIMENSIONS];
37 index_type sstride0;
38 const char *sptr;
39 /* m.* indicates the mask array. */
40 index_type mstride[GFC_MAX_DIMENSIONS];
41 index_type mstride0;
42 const GFC_LOGICAL_4 *mptr;
44 index_type count[GFC_MAX_DIMENSIONS];
45 index_type extent[GFC_MAX_DIMENSIONS];
46 index_type n;
47 index_type dim;
48 index_type size;
49 index_type nelem;
51 size = GFC_DESCRIPTOR_SIZE (array);
52 dim = GFC_DESCRIPTOR_RANK (array);
53 for (n = 0; n < dim; n++)
55 count[n] = 0;
56 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
57 sstride[n] = array->dim[n].stride * size;
58 mstride[n] = mask->dim[n].stride;
60 if (sstride[0] == 0)
61 sstride[0] = size;
62 if (mstride[0] == 0)
63 mstride[0] = 1;
65 rstride0 = ret->dim[0].stride * size;
66 if (rstride0 == 0)
67 rstride0 = size;
68 sstride0 = sstride[0];
69 mstride0 = mstride[0];
70 rptr = ret->data;
71 sptr = array->data;
72 mptr = mask->data;
74 /* Use the same loop for both logical types. */
75 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
77 if (GFC_DESCRIPTOR_SIZE (mask) != 8)
78 runtime_error ("Funny sized logical array");
79 for (n = 0; n < dim; n++)
80 mstride[n] <<= 1;
81 mstride0 <<= 1;
82 mptr = GFOR_POINTER_L8_TO_L4 (mptr);
85 while (sptr)
87 /* Test this element. */
88 if (*mptr)
90 /* Add it. */
91 memcpy (rptr, sptr, size);
92 rptr += rstride0;
94 /* Advance to the next element. */
95 sptr += sstride0;
96 mptr += mstride0;
97 count[0]++;
98 n = 0;
99 while (count[n] == extent[n])
101 /* When we get to the end of a dimension, reset it and increment
102 the next dimension. */
103 count[n] = 0;
104 /* We could precalculate these products, but this is a less
105 frequently used path so proabably not worth it. */
106 sptr -= sstride[n] * extent[n];
107 mptr -= mstride[n] * extent[n];
108 n++;
109 if (n >= dim)
111 /* Break out of the loop. */
112 sptr = NULL;
113 break;
115 else
117 count[n]++;
118 sptr += sstride[n];
119 mptr += mstride[n];
124 /* Add any remaining elements from VECTOR. */
125 if (vector)
127 n = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
128 nelem = ((rptr - ret->data) / rstride0);
129 if (n > nelem)
131 sstride0 = vector->dim[0].stride * size;
132 if (sstride0 == 0)
133 sstride0 = size;
135 sptr = vector->data + sstride0 * nelem;
136 n -= nelem;
137 while (n--)
139 memcpy (rptr, sptr, size);
140 rptr += rstride0;
141 sptr += sstride0;