]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/count_1_l.c
Update copyright years.
[thirdparty/gcc.git] / libgfortran / generated / count_1_l.c
CommitLineData
6de9cd9a 1/* Implementation of the COUNT intrinsic
a945c346 2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook <paul@nowt.org>
4
0cd0559e 5This file is part of the GNU Fortran runtime library (libgfortran).
6de9cd9a
DN
6
7Libgfortran is free software; you can redistribute it and/or
57dea9f6 8modify it under the terms of the GNU General Public
6de9cd9a 9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
6de9cd9a
DN
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
57dea9f6 15GNU General Public License for more details.
6de9cd9a 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/>. */
6de9cd9a 25
36ae8a61 26#include "libgfortran.h"
6de9cd9a 27
7d7b8bfe 28
90469382 29#if defined (HAVE_GFC_INTEGER_1)
644cb69f
FXC
30
31
90469382
TK
32extern void count_1_l (gfc_array_i1 * const restrict,
33 gfc_array_l1 * const restrict, const index_type * const restrict);
34export_proto(count_1_l);
7d7b8bfe 35
6de9cd9a 36void
90469382
TK
37count_1_l (gfc_array_i1 * const restrict retarray,
38 gfc_array_l1 * const restrict array,
64acfd99 39 const index_type * const restrict pdim)
6de9cd9a 40{
e33e218b
TK
41 index_type count[GFC_MAX_DIMENSIONS];
42 index_type extent[GFC_MAX_DIMENSIONS];
43 index_type sstride[GFC_MAX_DIMENSIONS];
44 index_type dstride[GFC_MAX_DIMENSIONS];
90469382
TK
45 const GFC_LOGICAL_1 * restrict base;
46 GFC_INTEGER_1 * restrict dest;
6de9cd9a
DN
47 index_type rank;
48 index_type n;
49 index_type len;
50 index_type delta;
51 index_type dim;
90469382 52 int src_kind;
da96f5ab 53 int continue_loop;
6de9cd9a
DN
54
55 /* Make dim zero based to avoid confusion. */
56 dim = (*pdim) - 1;
57 rank = GFC_DESCRIPTOR_RANK (array) - 1;
f6c7d65b 58
90469382
TK
59 src_kind = GFC_DESCRIPTOR_SIZE (array);
60
dfb55fdc 61 len = GFC_DESCRIPTOR_EXTENT(array,dim);
da96f5ab
TK
62 if (len < 0)
63 len = 0;
64
dfb55fdc 65 delta = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
6de9cd9a
DN
66
67 for (n = 0; n < dim; n++)
68 {
dfb55fdc
TK
69 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
70 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
80ee04b9
TK
71
72 if (extent[n] < 0)
73 extent[n] = 0;
6de9cd9a
DN
74 }
75 for (n = dim; n < rank; n++)
76 {
dfb55fdc
TK
77 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n + 1);
78 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n + 1);
80ee04b9
TK
79
80 if (extent[n] < 0)
81 extent[n] = 0;
6de9cd9a
DN
82 }
83
21d1335b 84 if (retarray->base_addr == NULL)
6c167c45 85 {
dfb55fdc 86 size_t alloc_size, str;
80ee04b9 87
6c167c45
VL
88 for (n = 0; n < rank; n++)
89 {
6c167c45 90 if (n == 0)
dfb55fdc 91 str = 1;
6c167c45 92 else
dfb55fdc
TK
93 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
94
95 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
96
6c167c45
VL
97 }
98
efd4dc1a 99 retarray->offset = 0;
ca708a2b 100 retarray->dtype.rank = rank;
80ee04b9 101
92e6f3a4 102 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
80ee04b9 103
d56bf419 104 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_1));
80ee04b9 105 if (alloc_size == 0)
62715bf8 106 return;
6c167c45 107 }
50dd63a9
TK
108 else
109 {
50dd63a9 110 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8 111 runtime_error ("rank of return array incorrect in"
5095283b
FXC
112 " COUNT intrinsic: is %ld, should be %ld",
113 (long int) GFC_DESCRIPTOR_RANK (retarray),
114 (long int) rank);
fd6590f8 115
9731c4a3 116 if (unlikely (compile_options.bounds_check))
fd6590f8
TK
117 {
118 for (n=0; n < rank; n++)
119 {
120 index_type ret_extent;
121
dfb55fdc 122 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
fd6590f8
TK
123 if (extent[n] != ret_extent)
124 runtime_error ("Incorrect extent in return value of"
90469382 125 " COUNT intrinsic in dimension %d:"
60dcf38a 126 " is %ld, should be %ld", (int) n + 1,
fd6590f8
TK
127 (long int) ret_extent, (long int) extent[n]);
128 }
129 }
50dd63a9
TK
130 }
131
6de9cd9a
DN
132 for (n = 0; n < rank; n++)
133 {
134 count[n] = 0;
dfb55fdc 135 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
6de9cd9a 136 if (extent[n] <= 0)
facc1285 137 return;
6de9cd9a
DN
138 }
139
21d1335b 140 base = array->base_addr;
90469382
TK
141
142 if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
143#ifdef HAVE_GFC_LOGICAL_16
144 || src_kind == 16
145#endif
146 )
147 {
148 if (base)
149 base = GFOR_POINTER_TO_L1 (base, src_kind);
150 }
151 else
152 internal_error (NULL, "Funny sized logical array in COUNT intrinsic");
153
21d1335b 154 dest = retarray->base_addr;
6de9cd9a 155
da96f5ab
TK
156 continue_loop = 1;
157 while (continue_loop)
6de9cd9a 158 {
90469382
TK
159 const GFC_LOGICAL_1 * restrict src;
160 GFC_INTEGER_1 result;
6de9cd9a
DN
161 src = base;
162 {
163
164 result = 0;
165 if (len <= 0)
166 *dest = 0;
167 else
168 {
169 for (n = 0; n < len; n++, src += delta)
170 {
171
172 if (*src)
173 result++;
174 }
175 *dest = result;
176 }
177 }
178 /* Advance to the next element. */
179 count[0]++;
180 base += sstride[0];
181 dest += dstride[0];
182 n = 0;
183 while (count[n] == extent[n])
184 {
185 /* When we get to the end of a dimension, reset it and increment
186 the next dimension. */
187 count[n] = 0;
188 /* We could precalculate these products, but this is a less
5d7adf7a 189 frequently used path so probably not worth it. */
6de9cd9a
DN
190 base -= sstride[n] * extent[n];
191 dest -= dstride[n] * extent[n];
192 n++;
80dd631f 193 if (n >= rank)
6de9cd9a 194 {
80dd631f 195 /* Break out of the loop. */
da96f5ab 196 continue_loop = 0;
6de9cd9a
DN
197 break;
198 }
199 else
200 {
201 count[n]++;
202 base += sstride[n];
203 dest += dstride[n];
204 }
205 }
206 }
207}
208
644cb69f 209#endif