Rebase.
[official-gcc.git] / libgfortran / generated / all_l2.c
blob3ef4a1dc25c6087a99f864df89b9402c84bf3806
1 /* Implementation of the ALL intrinsic
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
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
15 GNU General Public License for more details.
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.
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/>. */
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <assert.h>
31 #if defined (HAVE_GFC_LOGICAL_2)
34 extern void all_l2 (gfc_array_l2 * const restrict,
35 gfc_array_l1 * const restrict, const index_type * const restrict);
36 export_proto(all_l2);
38 void
39 all_l2 (gfc_array_l2 * const restrict retarray,
40 gfc_array_l1 * const restrict array,
41 const index_type * const restrict pdim)
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];
47 const GFC_LOGICAL_1 * restrict base;
48 GFC_LOGICAL_2 * restrict dest;
49 index_type rank;
50 index_type n;
51 index_type len;
52 index_type delta;
53 index_type dim;
54 int src_kind;
55 int continue_loop;
57 /* Make dim zero based to avoid confusion. */
58 dim = (*pdim) - 1;
59 rank = GFC_DESCRIPTOR_RANK (array) - 1;
61 src_kind = GFC_DESCRIPTOR_SIZE (array);
63 len = GFC_DESCRIPTOR_EXTENT(array,dim);
64 if (len < 0)
65 len = 0;
67 delta = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
69 for (n = 0; n < dim; n++)
71 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
72 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
74 if (extent[n] < 0)
75 extent[n] = 0;
77 for (n = dim; n < rank; n++)
79 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n + 1);
80 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n + 1);
82 if (extent[n] < 0)
83 extent[n] = 0;
86 if (retarray->base_addr == NULL)
88 size_t alloc_size, str;
90 for (n = 0; n < rank; n++)
92 if (n == 0)
93 str = 1;
94 else
95 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
97 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
101 retarray->offset = 0;
102 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
104 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
106 if (alloc_size == 0)
108 /* Make sure we have a zero-sized array. */
109 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
110 return;
112 else
113 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_LOGICAL_2));
115 else
117 if (rank != GFC_DESCRIPTOR_RANK (retarray))
118 runtime_error ("rank of return array incorrect in"
119 " ALL intrinsic: is %ld, should be %ld",
120 (long int) GFC_DESCRIPTOR_RANK (retarray),
121 (long int) rank);
123 if (unlikely (compile_options.bounds_check))
125 for (n=0; n < rank; n++)
127 index_type ret_extent;
129 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
130 if (extent[n] != ret_extent)
131 runtime_error ("Incorrect extent in return value of"
132 " ALL intrinsic in dimension %d:"
133 " is %ld, should be %ld", (int) n + 1,
134 (long int) ret_extent, (long int) extent[n]);
139 for (n = 0; n < rank; n++)
141 count[n] = 0;
142 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
143 if (extent[n] <= 0)
144 return;
147 base = array->base_addr;
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
155 if (base)
156 base = GFOR_POINTER_TO_L1 (base, src_kind);
158 else
159 internal_error (NULL, "Funny sized logical array in ALL intrinsic");
161 dest = retarray->base_addr;
163 continue_loop = 1;
164 while (continue_loop)
166 const GFC_LOGICAL_1 * restrict src;
167 GFC_LOGICAL_2 result;
168 src = base;
171 /* Return true only if all the elements are set. */
172 result = 1;
173 if (len <= 0)
174 *dest = 1;
175 else
177 for (n = 0; n < len; n++, src += delta)
180 if (! *src)
182 result = 0;
183 break;
186 *dest = result;
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])
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
200 frequently used path so probably not worth it. */
201 base -= sstride[n] * extent[n];
202 dest -= dstride[n] * extent[n];
203 n++;
204 if (n == rank)
206 /* Break out of the look. */
207 continue_loop = 0;
208 break;
210 else
212 count[n]++;
213 base += sstride[n];
214 dest += dstride[n];
220 #endif