]>
Commit | Line | Data |
---|---|---|
6de9cd9a DN |
1 | /* Implementation of the COUNT intrinsic |
2 | Copyright 2002 Free Software Foundation, Inc. | |
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 |
57dea9f6 TM |
10 | version 2 of the License, or (at your option) any later version. |
11 | ||
12 | In addition to the permissions in the GNU General Public License, the | |
13 | Free Software Foundation gives you unlimited permission to link the | |
14 | compiled version of this file into combinations with other programs, | |
15 | and to distribute those combinations without any restriction coming | |
16 | from the use of this file. (The General Public License restrictions | |
17 | do apply in other respects; for example, they cover modification of | |
18 | the file, and distribution when not linked into a combine | |
19 | executable.) | |
6de9cd9a DN |
20 | |
21 | Libgfortran is distributed in the hope that it will be useful, | |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
57dea9f6 | 24 | GNU General Public License for more details. |
6de9cd9a | 25 | |
57dea9f6 TM |
26 | You should have received a copy of the GNU General Public |
27 | License along with libgfortran; see the file COPYING. If not, | |
fe2ae685 KC |
28 | write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
29 | Boston, MA 02110-1301, USA. */ | |
6de9cd9a DN |
30 | |
31 | #include "config.h" | |
32 | #include <stdlib.h> | |
33 | #include <assert.h> | |
34 | #include "libgfortran.h" | |
35 | ||
7d7b8bfe | 36 | |
644cb69f FXC |
37 | #if defined (HAVE_GFC_LOGICAL_8) && defined (HAVE_GFC_INTEGER_4) |
38 | ||
39 | ||
7f68c75f RH |
40 | extern void count_4_l8 (gfc_array_i4 *, gfc_array_l8 *, index_type *); |
41 | export_proto(count_4_l8); | |
7d7b8bfe | 42 | |
6de9cd9a | 43 | void |
7f68c75f | 44 | count_4_l8 (gfc_array_i4 *retarray, gfc_array_l8 *array, index_type *pdim) |
6de9cd9a | 45 | { |
e33e218b TK |
46 | index_type count[GFC_MAX_DIMENSIONS]; |
47 | index_type extent[GFC_MAX_DIMENSIONS]; | |
48 | index_type sstride[GFC_MAX_DIMENSIONS]; | |
49 | index_type dstride[GFC_MAX_DIMENSIONS]; | |
6de9cd9a DN |
50 | GFC_LOGICAL_8 *base; |
51 | GFC_INTEGER_4 *dest; | |
52 | index_type rank; | |
53 | index_type n; | |
54 | index_type len; | |
55 | index_type delta; | |
56 | index_type dim; | |
57 | ||
58 | /* Make dim zero based to avoid confusion. */ | |
59 | dim = (*pdim) - 1; | |
60 | rank = GFC_DESCRIPTOR_RANK (array) - 1; | |
f6c7d65b PT |
61 | |
62 | /* TODO: It should be a front end job to correctly set the strides. */ | |
63 | ||
6de9cd9a DN |
64 | if (array->dim[0].stride == 0) |
65 | array->dim[0].stride = 1; | |
6de9cd9a DN |
66 | |
67 | len = array->dim[dim].ubound + 1 - array->dim[dim].lbound; | |
68 | delta = array->dim[dim].stride; | |
69 | ||
70 | for (n = 0; n < dim; n++) | |
71 | { | |
72 | sstride[n] = array->dim[n].stride; | |
73 | extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound; | |
74 | } | |
75 | for (n = dim; n < rank; n++) | |
76 | { | |
77 | sstride[n] = array->dim[n + 1].stride; | |
78 | extent[n] = | |
79 | array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound; | |
80 | } | |
81 | ||
6c167c45 VL |
82 | if (retarray->data == NULL) |
83 | { | |
84 | for (n = 0; n < rank; n++) | |
85 | { | |
86 | retarray->dim[n].lbound = 0; | |
87 | retarray->dim[n].ubound = extent[n]-1; | |
88 | if (n == 0) | |
89 | retarray->dim[n].stride = 1; | |
90 | else | |
91 | retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1]; | |
92 | } | |
93 | ||
07d3cebe RH |
94 | retarray->data |
95 | = internal_malloc_size (sizeof (GFC_INTEGER_4) | |
96 | * retarray->dim[rank-1].stride | |
97 | * extent[rank-1]); | |
efd4dc1a | 98 | retarray->offset = 0; |
50dd63a9 | 99 | retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; |
6c167c45 | 100 | } |
50dd63a9 TK |
101 | else |
102 | { | |
103 | if (retarray->dim[0].stride == 0) | |
104 | retarray->dim[0].stride = 1; | |
105 | ||
106 | if (rank != GFC_DESCRIPTOR_RANK (retarray)) | |
107 | runtime_error ("rank of return array incorrect"); | |
108 | } | |
109 | ||
6de9cd9a DN |
110 | for (n = 0; n < rank; n++) |
111 | { | |
112 | count[n] = 0; | |
113 | dstride[n] = retarray->dim[n].stride; | |
114 | if (extent[n] <= 0) | |
115 | len = 0; | |
116 | } | |
117 | ||
118 | base = array->data; | |
119 | dest = retarray->data; | |
120 | ||
121 | while (base) | |
122 | { | |
123 | GFC_LOGICAL_8 *src; | |
124 | GFC_INTEGER_4 result; | |
125 | src = base; | |
126 | { | |
127 | ||
128 | result = 0; | |
129 | if (len <= 0) | |
130 | *dest = 0; | |
131 | else | |
132 | { | |
133 | for (n = 0; n < len; n++, src += delta) | |
134 | { | |
135 | ||
136 | if (*src) | |
137 | result++; | |
138 | } | |
139 | *dest = result; | |
140 | } | |
141 | } | |
142 | /* Advance to the next element. */ | |
143 | count[0]++; | |
144 | base += sstride[0]; | |
145 | dest += dstride[0]; | |
146 | n = 0; | |
147 | while (count[n] == extent[n]) | |
148 | { | |
149 | /* When we get to the end of a dimension, reset it and increment | |
150 | the next dimension. */ | |
151 | count[n] = 0; | |
152 | /* We could precalculate these products, but this is a less | |
153 | frequently used path so proabably not worth it. */ | |
154 | base -= sstride[n] * extent[n]; | |
155 | dest -= dstride[n] * extent[n]; | |
156 | n++; | |
157 | if (n == rank) | |
158 | { | |
159 | /* Break out of the look. */ | |
160 | base = NULL; | |
161 | break; | |
162 | } | |
163 | else | |
164 | { | |
165 | count[n]++; | |
166 | base += sstride[n]; | |
167 | dest += dstride[n]; | |
168 | } | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
644cb69f | 173 | #endif |