PR target/19424
[official-gcc.git] / libgfortran / m4 / matmul.m4
blobebca6469bf02e7cfb082209bcd283900b3025fdc
1 `/* Implementation of the MATMUL intrinsic
2    Copyright 2002 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 2 of the License, or (at your option) any later version.
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.)
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.
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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA.  */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include "libgfortran.h"'
36 include(iparm.m4)dnl
38 /* This is a C version of the following fortran pseudo-code. The key
39    point is the loop order -- we access all arrays column-first, which
40    improves the performance enough to boost galgel spec score by 50%.
42    DIMENSION A(M,COUNT), B(COUNT,N), C(M,N)
43    C = 0
44    DO J=1,N
45      DO K=1,COUNT
46        DO I=1,M
47          C(I,J) = C(I,J)+A(I,K)*B(K,J)
50 extern void matmul_`'rtype_code (rtype * retarray, rtype * a, rtype * b);
51 export_proto(matmul_`'rtype_code);
53 void
54 matmul_`'rtype_code (rtype * retarray, rtype * a, rtype * b)
56   rtype_name *abase;
57   rtype_name *bbase;
58   rtype_name *dest;
60   index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
61   index_type x, y, n, count, xcount, ycount;
63   assert (GFC_DESCRIPTOR_RANK (a) == 2
64           || GFC_DESCRIPTOR_RANK (b) == 2);
66 /* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
68    Either A or B (but not both) can be rank 1:
70    o One-dimensional argument A is implicitly treated as a row matrix
71      dimensioned [1,count], so xcount=1.
73    o One-dimensional argument B is implicitly treated as a column matrix
74      dimensioned [count, 1], so ycount=1.
75   */
77   if (retarray->data == NULL)
78     {
79       if (GFC_DESCRIPTOR_RANK (a) == 1)
80         {
81           retarray->dim[0].lbound = 0;
82           retarray->dim[0].ubound = b->dim[1].ubound - b->dim[1].lbound;
83           retarray->dim[0].stride = 1;
84         }
85       else if (GFC_DESCRIPTOR_RANK (b) == 1)
86         {
87           retarray->dim[0].lbound = 0;
88           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
89           retarray->dim[0].stride = 1;
90         }
91       else
92         {
93           retarray->dim[0].lbound = 0;
94           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
95           retarray->dim[0].stride = 1;
96           
97           retarray->dim[1].lbound = 0;
98           retarray->dim[1].ubound = b->dim[1].ubound - b->dim[1].lbound;
99           retarray->dim[1].stride = retarray->dim[0].ubound+1;
100         }
101           
102       retarray->data
103         = internal_malloc_size (sizeof (rtype_name) * size0 (retarray));
104       retarray->base = 0;
105     }
107   abase = a->data;
108   bbase = b->data;
109   dest = retarray->data;
111   if (retarray->dim[0].stride == 0)
112     retarray->dim[0].stride = 1;
113   if (a->dim[0].stride == 0)
114     a->dim[0].stride = 1;
115   if (b->dim[0].stride == 0)
116     b->dim[0].stride = 1;
118 sinclude(`matmul_asm_'rtype_code`.m4')dnl
120   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
121     {
122       /* One-dimensional result may be addressed in the code below
123          either as a row or a column matrix. We want both cases to
124          work. */
125       rxstride = rystride = retarray->dim[0].stride;
126     }
127   else
128     {
129       rxstride = retarray->dim[0].stride;
130       rystride = retarray->dim[1].stride;
131     }
134   if (GFC_DESCRIPTOR_RANK (a) == 1)
135     {
136       /* Treat it as a a row matrix A[1,count]. */
137       axstride = a->dim[0].stride;
138       aystride = 1;
140       xcount = 1;
141       count = a->dim[0].ubound + 1 - a->dim[0].lbound;
142     }
143   else
144     {
145       axstride = a->dim[0].stride;
146       aystride = a->dim[1].stride;
148       count = a->dim[1].ubound + 1 - a->dim[1].lbound;
149       xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
150     }
152   assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
154   if (GFC_DESCRIPTOR_RANK (b) == 1)
155     {
156       /* Treat it as a column matrix B[count,1] */
157       bxstride = b->dim[0].stride;
159       /* bystride should never be used for 1-dimensional b.
160          in case it is we want it to cause a segfault, rather than
161          an incorrect result. */
162       bystride = 0xDEADBEEF; 
163       ycount = 1;
164     }
165   else
166     {
167       bxstride = b->dim[0].stride;
168       bystride = b->dim[1].stride;
169       ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
170     }
172   assert (a->base == 0);
173   assert (b->base == 0);
174   assert (retarray->base == 0);
176   abase = a->data;
177   bbase = b->data;
178   dest = retarray->data;
180   if (rxstride == 1 && axstride == 1 && bxstride == 1)
181     {
182       rtype_name *bbase_y;
183       rtype_name *dest_y;
184       rtype_name *abase_n;
185       rtype_name bbase_yn;
187       memset (dest, 0, (sizeof (rtype_name) * size0(retarray)));
189       for (y = 0; y < ycount; y++)
190         {
191           bbase_y = bbase + y*bystride;
192           dest_y = dest + y*rystride;
193           for (n = 0; n < count; n++)
194             {
195               abase_n = abase + n*aystride;
196               bbase_yn = bbase_y[n];
197               for (x = 0; x < xcount; x++)
198                 {
199                   dest_y[x] += abase_n[x] * bbase_yn;
200                 }
201             }
202         }
203     }
204   else
205     {
206       for (y = 0; y < ycount; y++)
207         for (x = 0; x < xcount; x++)
208           dest[x*rxstride + y*rystride] = (rtype_name)0;
210       for (y = 0; y < ycount; y++)
211         for (n = 0; n < count; n++)
212           for (x = 0; x < xcount; x++)
213             /* dest[x,y] += a[x,n] * b[n,y] */
214             dest[x*rxstride + y*rystride] += abase[x*axstride + n*aystride] * bbase[n*bxstride + y*bystride];
215     }