OSDN Git Service

28dce3a242253bc45bb01604db2163ff596a50fc
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / matmul_l16.c
1 /* Implementation of the MATMUL intrinsic
2    Copyright 2002, 2005 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 #if defined (HAVE_GFC_LOGICAL_16)
37
38 /* Dimensions: retarray(x,y) a(x, count) b(count,y).
39    Either a or b can be rank 1.  In this case x or y is 1.  */
40
41 extern void matmul_l16 (gfc_array_l16 *, gfc_array_l4 *, gfc_array_l4 *);
42 export_proto(matmul_l16);
43
44 void
45 matmul_l16 (gfc_array_l16 * retarray, gfc_array_l4 * a, gfc_array_l4 * b)
46 {
47   GFC_INTEGER_4 *abase;
48   GFC_INTEGER_4 *bbase;
49   GFC_LOGICAL_16 *dest;
50   index_type rxstride;
51   index_type rystride;
52   index_type xcount;
53   index_type ycount;
54   index_type xstride;
55   index_type ystride;
56   index_type x;
57   index_type y;
58
59   GFC_INTEGER_4 *pa;
60   GFC_INTEGER_4 *pb;
61   index_type astride;
62   index_type bstride;
63   index_type count;
64   index_type n;
65
66   assert (GFC_DESCRIPTOR_RANK (a) == 2
67           || GFC_DESCRIPTOR_RANK (b) == 2);
68
69   if (retarray->data == NULL)
70     {
71       if (GFC_DESCRIPTOR_RANK (a) == 1)
72         {
73           retarray->dim[0].lbound = 0;
74           retarray->dim[0].ubound = b->dim[1].ubound - b->dim[1].lbound;
75           retarray->dim[0].stride = 1;
76         }
77       else if (GFC_DESCRIPTOR_RANK (b) == 1)
78         {
79           retarray->dim[0].lbound = 0;
80           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
81           retarray->dim[0].stride = 1;
82         }
83       else
84         {
85           retarray->dim[0].lbound = 0;
86           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
87           retarray->dim[0].stride = 1;
88           
89           retarray->dim[1].lbound = 0;
90           retarray->dim[1].ubound = b->dim[1].ubound - b->dim[1].lbound;
91           retarray->dim[1].stride = retarray->dim[0].ubound+1;
92         }
93           
94       retarray->data
95         = internal_malloc_size (sizeof (GFC_LOGICAL_16) * size0 ((array_t *) retarray));
96       retarray->offset = 0;
97     }
98
99   abase = a->data;
100   if (GFC_DESCRIPTOR_SIZE (a) != 4)
101     {
102       assert (GFC_DESCRIPTOR_SIZE (a) == 8);
103       abase = GFOR_POINTER_L8_TO_L4 (abase);
104     }
105   bbase = b->data;
106   if (GFC_DESCRIPTOR_SIZE (b) != 4)
107     {
108       assert (GFC_DESCRIPTOR_SIZE (b) == 8);
109       bbase = GFOR_POINTER_L8_TO_L4 (bbase);
110     }
111   dest = retarray->data;
112
113   if (retarray->dim[0].stride == 0)
114     retarray->dim[0].stride = 1;
115   if (a->dim[0].stride == 0)
116     a->dim[0].stride = 1;
117   if (b->dim[0].stride == 0)
118     b->dim[0].stride = 1;
119
120
121   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
122     {
123       rxstride = retarray->dim[0].stride;
124       rystride = rxstride;
125     }
126   else
127     {
128       rxstride = retarray->dim[0].stride;
129       rystride = retarray->dim[1].stride;
130     }
131
132   /* If we have rank 1 parameters, zero the absent stride, and set the size to
133      one.  */
134   if (GFC_DESCRIPTOR_RANK (a) == 1)
135     {
136       astride = a->dim[0].stride;
137       count = a->dim[0].ubound + 1 - a->dim[0].lbound;
138       xstride = 0;
139       rxstride = 0;
140       xcount = 1;
141     }
142   else
143     {
144       astride = a->dim[1].stride;
145       count = a->dim[1].ubound + 1 - a->dim[1].lbound;
146       xstride = a->dim[0].stride;
147       xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
148     }
149   if (GFC_DESCRIPTOR_RANK (b) == 1)
150     {
151       bstride = b->dim[0].stride;
152       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
153       ystride = 0;
154       rystride = 0;
155       ycount = 1;
156     }
157   else
158     {
159       bstride = b->dim[0].stride;
160       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
161       ystride = b->dim[1].stride;
162       ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
163     }
164
165   for (y = 0; y < ycount; y++)
166     {
167       for (x = 0; x < xcount; x++)
168         {
169           /* Do the summation for this element.  For real and integer types
170              this is the same as DOT_PRODUCT.  For complex types we use do
171              a*b, not conjg(a)*b.  */
172           pa = abase;
173           pb = bbase;
174           *dest = 0;
175
176           for (n = 0; n < count; n++)
177             {
178               if (*pa && *pb)
179                 {
180                   *dest = 1;
181                   break;
182                 }
183               pa += astride;
184               pb += bstride;
185             }
186
187           dest += rxstride;
188           abase += xstride;
189         }
190       abase -= xstride * xcount;
191       bbase += ystride;
192       dest += rystride - (rxstride * xcount);
193     }
194 }
195
196 #endif