]>
Commit | Line | Data |
---|---|---|
90469382 | 1 | /* Implementation of the ALL intrinsic |
748086b7 | 2 | Copyright 2002, 2007, 2009 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Paul Brook <paul@nowt.org> |
4 | ||
57dea9f6 | 5 | This file is part of the GNU Fortran 95 runtime library (libgfortran). |
6de9cd9a DN |
6 | |
7 | Libgfortran is free software; you can redistribute it and/or | |
57dea9f6 | 8 | modify it under the terms of the GNU General Public |
6de9cd9a | 9 | License as published by the Free Software Foundation; either |
748086b7 | 10 | version 3 of the License, or (at your option) any later version. |
6de9cd9a DN |
11 | |
12 | Libgfortran 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 | |
57dea9f6 | 15 | GNU General Public License for more details. |
6de9cd9a | 16 | |
748086b7 JJ |
17 | Under Section 7 of GPL version 3, you are granted additional |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see 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_1) |
644cb69f FXC |
32 | |
33 | ||
90469382 TK |
34 | extern void all_l1 (gfc_array_l1 * const restrict, |
35 | gfc_array_l1 * const restrict, const index_type * const restrict); | |
36 | export_proto(all_l1); | |
7d7b8bfe | 37 | |
6de9cd9a | 38 | void |
90469382 TK |
39 | all_l1 (gfc_array_l1 * const restrict retarray, |
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 TK |
47 | const GFC_LOGICAL_1 * restrict base; |
48 | GFC_LOGICAL_1 * 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 | ||
6c167c45 VL |
86 | if (retarray->data == NULL) |
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 | |
dfb55fdc | 104 | alloc_size = sizeof (GFC_LOGICAL_1) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) |
80ee04b9 TK |
105 | * extent[rank-1]; |
106 | ||
107 | if (alloc_size == 0) | |
108 | { | |
109 | /* Make sure we have a zero-sized array. */ | |
dfb55fdc | 110 | GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1); |
80ee04b9 TK |
111 | return; |
112 | } | |
113 | else | |
114 | retarray->data = internal_malloc_size (alloc_size); | |
6c167c45 | 115 | } |
50dd63a9 TK |
116 | else |
117 | { | |
50dd63a9 | 118 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) |
fd6590f8 | 119 | runtime_error ("rank of return array incorrect in" |
5095283b FXC |
120 | " ALL intrinsic: is %ld, should be %ld", |
121 | (long int) GFC_DESCRIPTOR_RANK (retarray), | |
122 | (long int) rank); | |
fd6590f8 | 123 | |
9731c4a3 | 124 | if (unlikely (compile_options.bounds_check)) |
fd6590f8 TK |
125 | { |
126 | for (n=0; n < rank; n++) | |
127 | { | |
128 | index_type ret_extent; | |
129 | ||
dfb55fdc | 130 | ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n); |
fd6590f8 TK |
131 | if (extent[n] != ret_extent) |
132 | runtime_error ("Incorrect extent in return value of" | |
90469382 | 133 | " ALL intrinsic in dimension %d:" |
60dcf38a | 134 | " is %ld, should be %ld", (int) n + 1, |
fd6590f8 TK |
135 | (long int) ret_extent, (long int) extent[n]); |
136 | } | |
137 | } | |
50dd63a9 TK |
138 | } |
139 | ||
6de9cd9a DN |
140 | for (n = 0; n < rank; n++) |
141 | { | |
142 | count[n] = 0; | |
dfb55fdc | 143 | dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); |
6de9cd9a DN |
144 | if (extent[n] <= 0) |
145 | len = 0; | |
146 | } | |
147 | ||
148 | base = array->data; | |
90469382 TK |
149 | |
150 | if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8 | |
151 | #ifdef HAVE_GFC_LOGICAL_16 | |
152 | || src_kind == 16 | |
153 | #endif | |
154 | ) | |
155 | { | |
156 | if (base) | |
157 | base = GFOR_POINTER_TO_L1 (base, src_kind); | |
158 | } | |
159 | else | |
160 | internal_error (NULL, "Funny sized logical array in ALL intrinsic"); | |
161 | ||
6de9cd9a DN |
162 | dest = retarray->data; |
163 | ||
da96f5ab TK |
164 | continue_loop = 1; |
165 | while (continue_loop) | |
6de9cd9a | 166 | { |
90469382 TK |
167 | const GFC_LOGICAL_1 * restrict src; |
168 | GFC_LOGICAL_1 result; | |
6de9cd9a DN |
169 | src = base; |
170 | { | |
171 | ||
90469382 TK |
172 | /* Return true only if all the elements are set. */ |
173 | result = 1; | |
6de9cd9a | 174 | if (len <= 0) |
90469382 | 175 | *dest = 1; |
6de9cd9a DN |
176 | else |
177 | { | |
178 | for (n = 0; n < len; n++, src += delta) | |
179 | { | |
180 | ||
90469382 TK |
181 | if (! *src) |
182 | { | |
183 | result = 0; | |
184 | break; | |
185 | } | |
6de9cd9a DN |
186 | } |
187 | *dest = result; | |
188 | } | |
189 | } | |
190 | /* Advance to the next element. */ | |
191 | count[0]++; | |
192 | base += sstride[0]; | |
193 | dest += dstride[0]; | |
194 | n = 0; | |
195 | while (count[n] == extent[n]) | |
196 | { | |
197 | /* When we get to the end of a dimension, reset it and increment | |
198 | the next dimension. */ | |
199 | count[n] = 0; | |
200 | /* We could precalculate these products, but this is a less | |
5d7adf7a | 201 | frequently used path so probably not worth it. */ |
6de9cd9a DN |
202 | base -= sstride[n] * extent[n]; |
203 | dest -= dstride[n] * extent[n]; | |
204 | n++; | |
205 | if (n == rank) | |
206 | { | |
207 | /* Break out of the look. */ | |
da96f5ab | 208 | continue_loop = 0; |
6de9cd9a DN |
209 | break; |
210 | } | |
211 | else | |
212 | { | |
213 | count[n]++; | |
214 | base += sstride[n]; | |
215 | dest += dstride[n]; | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
644cb69f | 221 | #endif |