]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/count_16_l.c
libgfortran: Don't skip allocation if size is zero [PR112412]
[thirdparty/gcc.git] / libgfortran / generated / count_16_l.c
CommitLineData
644cb69f 1/* Implementation of the COUNT intrinsic
83ffe9cd 2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
644cb69f
FXC
3 Contributed by Paul Brook <paul@nowt.org>
4
0cd0559e 5This file is part of the GNU Fortran runtime library (libgfortran).
644cb69f
FXC
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
28
90469382 29#if defined (HAVE_GFC_INTEGER_16)
644cb69f
FXC
30
31
90469382
TK
32extern void count_16_l (gfc_array_i16 * const restrict,
33 gfc_array_l1 * const restrict, const index_type * const restrict);
34export_proto(count_16_l);
644cb69f
FXC
35
36void
90469382
TK
37count_16_l (gfc_array_i16 * const restrict retarray,
38 gfc_array_l1 * const restrict array,
64acfd99 39 const index_type * const restrict pdim)
644cb69f
FXC
40{
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 45 const GFC_LOGICAL_1 * restrict base;
64acfd99 46 GFC_INTEGER_16 * restrict dest;
644cb69f
FXC
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;
644cb69f
FXC
54
55 /* Make dim zero based to avoid confusion. */
56 dim = (*pdim) - 1;
57 rank = GFC_DESCRIPTOR_RANK (array) - 1;
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);
644cb69f
FXC
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;
644cb69f
FXC
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;
644cb69f
FXC
82 }
83
21d1335b 84 if (retarray->base_addr == NULL)
644cb69f 85 {
dfb55fdc 86 size_t alloc_size, str;
80ee04b9 87
644cb69f
FXC
88 for (n = 0; n < rank; n++)
89 {
644cb69f 90 if (n == 0)
dfb55fdc 91 str = 1;
644cb69f 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
644cb69f
FXC
97 }
98
644cb69f 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_16));
80ee04b9
TK
105 if (alloc_size == 0)
106 {
107 /* Make sure we have a zero-sized array. */
dfb55fdc 108 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
80ee04b9
TK
109 return;
110 }
644cb69f
FXC
111 }
112 else
113 {
644cb69f 114 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8 115 runtime_error ("rank of return array incorrect in"
5095283b
FXC
116 " COUNT intrinsic: is %ld, should be %ld",
117 (long int) GFC_DESCRIPTOR_RANK (retarray),
118 (long int) rank);
fd6590f8 119
9731c4a3 120 if (unlikely (compile_options.bounds_check))
fd6590f8
TK
121 {
122 for (n=0; n < rank; n++)
123 {
124 index_type ret_extent;
125
dfb55fdc 126 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
fd6590f8
TK
127 if (extent[n] != ret_extent)
128 runtime_error ("Incorrect extent in return value of"
90469382 129 " COUNT intrinsic in dimension %d:"
60dcf38a 130 " is %ld, should be %ld", (int) n + 1,
fd6590f8
TK
131 (long int) ret_extent, (long int) extent[n]);
132 }
133 }
644cb69f
FXC
134 }
135
136 for (n = 0; n < rank; n++)
137 {
138 count[n] = 0;
dfb55fdc 139 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
644cb69f 140 if (extent[n] <= 0)
facc1285 141 return;
644cb69f
FXC
142 }
143
21d1335b 144 base = array->base_addr;
90469382
TK
145
146 if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
147#ifdef HAVE_GFC_LOGICAL_16
148 || src_kind == 16
149#endif
150 )
151 {
152 if (base)
153 base = GFOR_POINTER_TO_L1 (base, src_kind);
154 }
155 else
156 internal_error (NULL, "Funny sized logical array in COUNT intrinsic");
157
21d1335b 158 dest = retarray->base_addr;
644cb69f 159
da96f5ab
TK
160 continue_loop = 1;
161 while (continue_loop)
644cb69f 162 {
90469382 163 const GFC_LOGICAL_1 * restrict src;
644cb69f
FXC
164 GFC_INTEGER_16 result;
165 src = base;
166 {
167
168 result = 0;
169 if (len <= 0)
170 *dest = 0;
171 else
172 {
173 for (n = 0; n < len; n++, src += delta)
174 {
175
176 if (*src)
177 result++;
178 }
179 *dest = result;
180 }
181 }
182 /* Advance to the next element. */
183 count[0]++;
184 base += sstride[0];
185 dest += dstride[0];
186 n = 0;
187 while (count[n] == extent[n])
188 {
189 /* When we get to the end of a dimension, reset it and increment
190 the next dimension. */
191 count[n] = 0;
192 /* We could precalculate these products, but this is a less
5d7adf7a 193 frequently used path so probably not worth it. */
644cb69f
FXC
194 base -= sstride[n] * extent[n];
195 dest -= dstride[n] * extent[n];
196 n++;
80dd631f 197 if (n >= rank)
644cb69f 198 {
80dd631f 199 /* Break out of the loop. */
da96f5ab 200 continue_loop = 0;
644cb69f
FXC
201 break;
202 }
203 else
204 {
205 count[n]++;
206 base += sstride[n];
207 dest += dstride[n];
208 }
209 }
210 }
211}
212
213#endif