2009-04-11 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / libgfortran / generated / all_l2.c
blob00f0886cb364953359d5e5753be6829d49f9a1f5
1 /* Implementation of the ALL intrinsic
2 Copyright 2002, 2007, 2009 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 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 = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
64 if (len < 0)
65 len = 0;
67 delta = array->dim[dim].stride * src_kind;
69 for (n = 0; n < dim; n++)
71 sstride[n] = array->dim[n].stride * src_kind;
72 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
74 if (extent[n] < 0)
75 extent[n] = 0;
77 for (n = dim; n < rank; n++)
79 sstride[n] = array->dim[n + 1].stride * src_kind;
80 extent[n] =
81 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
83 if (extent[n] < 0)
84 extent[n] = 0;
87 if (retarray->data == NULL)
89 size_t alloc_size;
91 for (n = 0; n < rank; n++)
93 retarray->dim[n].lbound = 0;
94 retarray->dim[n].ubound = extent[n]-1;
95 if (n == 0)
96 retarray->dim[n].stride = 1;
97 else
98 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
101 retarray->offset = 0;
102 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
104 alloc_size = sizeof (GFC_LOGICAL_2) * retarray->dim[rank-1].stride
105 * extent[rank-1];
107 if (alloc_size == 0)
109 /* Make sure we have a zero-sized array. */
110 retarray->dim[0].lbound = 0;
111 retarray->dim[0].ubound = -1;
112 return;
114 else
115 retarray->data = internal_malloc_size (alloc_size);
117 else
119 if (rank != GFC_DESCRIPTOR_RANK (retarray))
120 runtime_error ("rank of return array incorrect in"
121 " ALL intrinsic: is %ld, should be %ld",
122 (long int) GFC_DESCRIPTOR_RANK (retarray),
123 (long int) rank);
125 if (unlikely (compile_options.bounds_check))
127 for (n=0; n < rank; n++)
129 index_type ret_extent;
131 ret_extent = retarray->dim[n].ubound + 1
132 - retarray->dim[n].lbound;
133 if (extent[n] != ret_extent)
134 runtime_error ("Incorrect extent in return value of"
135 " ALL intrinsic in dimension %d:"
136 " is %ld, should be %ld", (int) n + 1,
137 (long int) ret_extent, (long int) extent[n]);
142 for (n = 0; n < rank; n++)
144 count[n] = 0;
145 dstride[n] = retarray->dim[n].stride;
146 if (extent[n] <= 0)
147 len = 0;
150 base = array->data;
152 if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
153 #ifdef HAVE_GFC_LOGICAL_16
154 || src_kind == 16
155 #endif
158 if (base)
159 base = GFOR_POINTER_TO_L1 (base, src_kind);
161 else
162 internal_error (NULL, "Funny sized logical array in ALL intrinsic");
164 dest = retarray->data;
166 continue_loop = 1;
167 while (continue_loop)
169 const GFC_LOGICAL_1 * restrict src;
170 GFC_LOGICAL_2 result;
171 src = base;
174 /* Return true only if all the elements are set. */
175 result = 1;
176 if (len <= 0)
177 *dest = 1;
178 else
180 for (n = 0; n < len; n++, src += delta)
183 if (! *src)
185 result = 0;
186 break;
189 *dest = result;
192 /* Advance to the next element. */
193 count[0]++;
194 base += sstride[0];
195 dest += dstride[0];
196 n = 0;
197 while (count[n] == extent[n])
199 /* When we get to the end of a dimension, reset it and increment
200 the next dimension. */
201 count[n] = 0;
202 /* We could precalculate these products, but this is a less
203 frequently used path so probably not worth it. */
204 base -= sstride[n] * extent[n];
205 dest -= dstride[n] * extent[n];
206 n++;
207 if (n == rank)
209 /* Break out of the look. */
210 continue_loop = 0;
211 break;
213 else
215 count[n]++;
216 base += sstride[n];
217 dest += dstride[n];
223 #endif