]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/count_16_l.c
dump-noaddr.x (dump_compare): Use --dumpbase to put dump files in a subdirectory.
[thirdparty/gcc.git] / libgfortran / generated / count_16_l.c
CommitLineData
644cb69f 1/* Implementation of the COUNT intrinsic
748086b7 2 Copyright 2002, 2007, 2009 Free Software Foundation, Inc.
644cb69f
FXC
3 Contributed by Paul Brook <paul@nowt.org>
4
5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
644cb69f
FXC
11
12Libgfortran is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
644cb69f 25
36ae8a61 26#include "libgfortran.h"
644cb69f
FXC
27#include <stdlib.h>
28#include <assert.h>
644cb69f
FXC
29
30
90469382 31#if defined (HAVE_GFC_INTEGER_16)
644cb69f
FXC
32
33
90469382
TK
34extern void count_16_l (gfc_array_i16 * const restrict,
35 gfc_array_l1 * const restrict, const index_type * const restrict);
36export_proto(count_16_l);
644cb69f
FXC
37
38void
90469382
TK
39count_16_l (gfc_array_i16 * const restrict retarray,
40 gfc_array_l1 * const restrict array,
64acfd99 41 const index_type * const restrict pdim)
644cb69f
FXC
42{
43 index_type count[GFC_MAX_DIMENSIONS];
44 index_type extent[GFC_MAX_DIMENSIONS];
45 index_type sstride[GFC_MAX_DIMENSIONS];
46 index_type dstride[GFC_MAX_DIMENSIONS];
90469382 47 const GFC_LOGICAL_1 * restrict base;
64acfd99 48 GFC_INTEGER_16 * restrict dest;
644cb69f
FXC
49 index_type rank;
50 index_type n;
51 index_type len;
52 index_type delta;
53 index_type dim;
90469382 54 int src_kind;
da96f5ab 55 int continue_loop;
644cb69f
FXC
56
57 /* Make dim zero based to avoid confusion. */
58 dim = (*pdim) - 1;
59 rank = GFC_DESCRIPTOR_RANK (array) - 1;
60
90469382
TK
61 src_kind = GFC_DESCRIPTOR_SIZE (array);
62
644cb69f 63 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
da96f5ab
TK
64 if (len < 0)
65 len = 0;
66
90469382 67 delta = array->dim[dim].stride * src_kind;
644cb69f
FXC
68
69 for (n = 0; n < dim; n++)
70 {
90469382 71 sstride[n] = array->dim[n].stride * src_kind;
644cb69f 72 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
80ee04b9
TK
73
74 if (extent[n] < 0)
75 extent[n] = 0;
644cb69f
FXC
76 }
77 for (n = dim; n < rank; n++)
78 {
90469382 79 sstride[n] = array->dim[n + 1].stride * src_kind;
644cb69f
FXC
80 extent[n] =
81 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
80ee04b9
TK
82
83 if (extent[n] < 0)
84 extent[n] = 0;
644cb69f
FXC
85 }
86
87 if (retarray->data == NULL)
88 {
80ee04b9
TK
89 size_t alloc_size;
90
644cb69f
FXC
91 for (n = 0; n < rank; n++)
92 {
93 retarray->dim[n].lbound = 0;
94 retarray->dim[n].ubound = extent[n]-1;
95 if (n == 0)
96 retarray->dim[n].stride = 1;
97 else
98 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
99 }
100
644cb69f
FXC
101 retarray->offset = 0;
102 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
80ee04b9
TK
103
104 alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
105 * extent[rank-1];
106
107 if (alloc_size == 0)
108 {
109 /* Make sure we have a zero-sized array. */
110 retarray->dim[0].lbound = 0;
111 retarray->dim[0].ubound = -1;
112 return;
113 }
114 else
115 retarray->data = internal_malloc_size (alloc_size);
644cb69f
FXC
116 }
117 else
118 {
644cb69f 119 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8 120 runtime_error ("rank of return array incorrect in"
5095283b
FXC
121 " COUNT intrinsic: is %ld, should be %ld",
122 (long int) GFC_DESCRIPTOR_RANK (retarray),
123 (long int) rank);
fd6590f8 124
9731c4a3 125 if (unlikely (compile_options.bounds_check))
fd6590f8
TK
126 {
127 for (n=0; n < rank; n++)
128 {
129 index_type ret_extent;
130
131 ret_extent = retarray->dim[n].ubound + 1
132 - retarray->dim[n].lbound;
133 if (extent[n] != ret_extent)
134 runtime_error ("Incorrect extent in return value of"
90469382 135 " COUNT intrinsic in dimension %d:"
60dcf38a 136 " is %ld, should be %ld", (int) n + 1,
fd6590f8
TK
137 (long int) ret_extent, (long int) extent[n]);
138 }
139 }
644cb69f
FXC
140 }
141
142 for (n = 0; n < rank; n++)
143 {
144 count[n] = 0;
145 dstride[n] = retarray->dim[n].stride;
146 if (extent[n] <= 0)
147 len = 0;
148 }
149
150 base = array->data;
90469382
TK
151
152 if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
153#ifdef HAVE_GFC_LOGICAL_16
154 || src_kind == 16
155#endif
156 )
157 {
158 if (base)
159 base = GFOR_POINTER_TO_L1 (base, src_kind);
160 }
161 else
162 internal_error (NULL, "Funny sized logical array in COUNT intrinsic");
163
644cb69f
FXC
164 dest = retarray->data;
165
da96f5ab
TK
166 continue_loop = 1;
167 while (continue_loop)
644cb69f 168 {
90469382 169 const GFC_LOGICAL_1 * restrict src;
644cb69f
FXC
170 GFC_INTEGER_16 result;
171 src = base;
172 {
173
174 result = 0;
175 if (len <= 0)
176 *dest = 0;
177 else
178 {
179 for (n = 0; n < len; n++, src += delta)
180 {
181
182 if (*src)
183 result++;
184 }
185 *dest = result;
186 }
187 }
188 /* Advance to the next element. */
189 count[0]++;
190 base += sstride[0];
191 dest += dstride[0];
192 n = 0;
193 while (count[n] == extent[n])
194 {
195 /* When we get to the end of a dimension, reset it and increment
196 the next dimension. */
197 count[n] = 0;
198 /* We could precalculate these products, but this is a less
5d7adf7a 199 frequently used path so probably not worth it. */
644cb69f
FXC
200 base -= sstride[n] * extent[n];
201 dest -= dstride[n] * extent[n];
202 n++;
203 if (n == rank)
204 {
205 /* Break out of the look. */
da96f5ab 206 continue_loop = 0;
644cb69f
FXC
207 break;
208 }
209 else
210 {
211 count[n]++;
212 base += sstride[n];
213 dest += dstride[n];
214 }
215 }
216 }
217}
218
219#endif