]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/any_l8.c
Introduce xmallocarray, an overflow checking variant of xmalloc.
[thirdparty/gcc.git] / libgfortran / generated / any_l8.c
CommitLineData
6de9cd9a 1/* Implementation of the ANY intrinsic
f0bcf628 2 Copyright (C) 2002-2014 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
DN
27#include <stdlib.h>
28#include <assert.h>
6de9cd9a 29
7d7b8bfe 30
90469382 31#if defined (HAVE_GFC_LOGICAL_8)
644cb69f
FXC
32
33
64acfd99 34extern void any_l8 (gfc_array_l8 * const restrict,
90469382 35 gfc_array_l1 * const restrict, const index_type * const restrict);
7f68c75f 36export_proto(any_l8);
7d7b8bfe 37
6de9cd9a 38void
64acfd99 39any_l8 (gfc_array_l8 * const restrict retarray,
90469382 40 gfc_array_l1 * const restrict array,
64acfd99 41 const index_type * const restrict pdim)
6de9cd9a 42{
e33e218b
TK
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_LOGICAL_8 * restrict dest;
6de9cd9a
DN
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;
6de9cd9a
DN
56
57 /* Make dim zero based to avoid confusion. */
58 dim = (*pdim) - 1;
59 rank = GFC_DESCRIPTOR_RANK (array) - 1;
f6c7d65b 60
90469382
TK
61 src_kind = GFC_DESCRIPTOR_SIZE (array);
62
dfb55fdc 63 len = GFC_DESCRIPTOR_EXTENT(array,dim);
da96f5ab
TK
64 if (len < 0)
65 len = 0;
66
dfb55fdc 67 delta = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
6de9cd9a
DN
68
69 for (n = 0; n < dim; n++)
70 {
dfb55fdc
TK
71 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
72 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
80ee04b9
TK
73
74 if (extent[n] < 0)
75 extent[n] = 0;
6de9cd9a
DN
76 }
77 for (n = dim; n < rank; n++)
78 {
dfb55fdc
TK
79 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n + 1);
80 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n + 1);
80ee04b9
TK
81
82 if (extent[n] < 0)
83 extent[n] = 0;
6de9cd9a
DN
84 }
85
21d1335b 86 if (retarray->base_addr == NULL)
6c167c45 87 {
dfb55fdc 88 size_t alloc_size, str;
80ee04b9 89
6c167c45
VL
90 for (n = 0; n < rank; n++)
91 {
6c167c45 92 if (n == 0)
dfb55fdc 93 str = 1;
6c167c45 94 else
dfb55fdc
TK
95 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
96
97 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
98
6c167c45
VL
99 }
100
efd4dc1a 101 retarray->offset = 0;
50dd63a9 102 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
80ee04b9 103
92e6f3a4 104 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
80ee04b9
TK
105
106 if (alloc_size == 0)
107 {
108 /* Make sure we have a zero-sized array. */
dfb55fdc 109 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
80ee04b9
TK
110 return;
111 }
112 else
92e6f3a4 113 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_LOGICAL_8));
6c167c45 114 }
50dd63a9
TK
115 else
116 {
50dd63a9 117 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8 118 runtime_error ("rank of return array incorrect in"
5095283b
FXC
119 " ANY intrinsic: is %ld, should be %ld",
120 (long int) GFC_DESCRIPTOR_RANK (retarray),
121 (long int) rank);
fd6590f8 122
9731c4a3 123 if (unlikely (compile_options.bounds_check))
fd6590f8
TK
124 {
125 for (n=0; n < rank; n++)
126 {
127 index_type ret_extent;
128
dfb55fdc 129 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
fd6590f8
TK
130 if (extent[n] != ret_extent)
131 runtime_error ("Incorrect extent in return value of"
90469382 132 " ANY intrinsic in dimension %d:"
60dcf38a 133 " is %ld, should be %ld", (int) n + 1,
fd6590f8
TK
134 (long int) ret_extent, (long int) extent[n]);
135 }
136 }
50dd63a9
TK
137 }
138
6de9cd9a
DN
139 for (n = 0; n < rank; n++)
140 {
141 count[n] = 0;
dfb55fdc 142 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
6de9cd9a 143 if (extent[n] <= 0)
facc1285 144 return;
6de9cd9a
DN
145 }
146
21d1335b 147 base = array->base_addr;
90469382
TK
148
149 if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
150#ifdef HAVE_GFC_LOGICAL_16
151 || src_kind == 16
152#endif
153 )
154 {
155 if (base)
156 base = GFOR_POINTER_TO_L1 (base, src_kind);
157 }
158 else
159 internal_error (NULL, "Funny sized logical array in ANY intrinsic");
160
21d1335b 161 dest = retarray->base_addr;
6de9cd9a 162
da96f5ab
TK
163 continue_loop = 1;
164 while (continue_loop)
6de9cd9a 165 {
90469382 166 const GFC_LOGICAL_1 * restrict src;
6de9cd9a
DN
167 GFC_LOGICAL_8 result;
168 src = base;
169 {
170
171 result = 0;
172 if (len <= 0)
173 *dest = 0;
174 else
175 {
176 for (n = 0; n < len; n++, src += delta)
177 {
178
179 /* Return true if any of the elements are set. */
180 if (*src)
181 {
182 result = 1;
183 break;
184 }
185 }
186 *dest = result;
187 }
188 }
189 /* Advance to the next element. */
190 count[0]++;
191 base += sstride[0];
192 dest += dstride[0];
193 n = 0;
194 while (count[n] == extent[n])
195 {
196 /* When we get to the end of a dimension, reset it and increment
197 the next dimension. */
198 count[n] = 0;
199 /* We could precalculate these products, but this is a less
5d7adf7a 200 frequently used path so probably not worth it. */
6de9cd9a
DN
201 base -= sstride[n] * extent[n];
202 dest -= dstride[n] * extent[n];
203 n++;
204 if (n == rank)
205 {
206 /* Break out of the look. */
da96f5ab 207 continue_loop = 0;
6de9cd9a
DN
208 break;
209 }
210 else
211 {
212 count[n]++;
213 base += sstride[n];
214 dest += dstride[n];
215 }
216 }
217 }
218}
219
644cb69f 220#endif