OSDN Git Service

cbd1717df2550db3d2738f4ec7a7ab1ca167feb6
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / count_4_l16.c
1 /* Implementation of the COUNT intrinsic
2    Copyright 2002 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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 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.)
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
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include "libgfortran.h"
35
36
37 #if defined (HAVE_GFC_LOGICAL_16) && defined (HAVE_GFC_INTEGER_4)
38
39
40 extern void count_4_l16 (gfc_array_i4 *, gfc_array_l16 *, index_type *);
41 export_proto(count_4_l16);
42
43 void
44 count_4_l16 (gfc_array_i4 *retarray, gfc_array_l16 *array, index_type *pdim)
45 {
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];
50   GFC_LOGICAL_16 *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;
61
62   /* TODO:  It should be a front end job to correctly set the strides.  */
63
64   if (array->dim[0].stride == 0)
65     array->dim[0].stride = 1;
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
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
94       retarray->data
95          = internal_malloc_size (sizeof (GFC_INTEGER_4)
96                                  * retarray->dim[rank-1].stride
97                                  * extent[rank-1]);
98       retarray->offset = 0;
99       retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
100     }
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
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_16 *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
173 #endif