]> gcc.gnu.org Git - gcc.git/blame - libgfortran/generated/matmul_i4.c
All files: Update FSF address.
[gcc.git] / libgfortran / generated / matmul_i4.c
CommitLineData
6de9cd9a 1/* Implementation of the MATMUL intrinsic
4b6903ec 2 Copyright 2002, 2005 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook <paul@nowt.org>
4
883c9d4d 5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6de9cd9a
DN
6
7Libgfortran is free software; you can redistribute it and/or
57dea9f6 8modify it under the terms of the GNU General Public
6de9cd9a 9License as published by the Free Software Foundation; either
57dea9f6
TM
10version 2 of the License, or (at your option) any later version.
11
12In addition to the permissions in the GNU General Public License, the
13Free Software Foundation gives you unlimited permission to link the
14compiled version of this file into combinations with other programs,
15and to distribute those combinations without any restriction coming
16from the use of this file. (The General Public License restrictions
17do apply in other respects; for example, they cover modification of
18the file, and distribution when not linked into a combine
19executable.)
6de9cd9a
DN
20
21Libgfortran is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57dea9f6 24GNU General Public License for more details.
6de9cd9a 25
57dea9f6
TM
26You should have received a copy of the GNU General Public
27License along with libgfortran; see the file COPYING. If not,
6de9cd9a
DN
28write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29Boston, MA 02111-1307, USA. */
30
31#include "config.h"
32#include <stdlib.h>
410d3bba 33#include <string.h>
6de9cd9a
DN
34#include <assert.h>
35#include "libgfortran.h"
36
410d3bba
VL
37/* This is a C version of the following fortran pseudo-code. The key
38 point is the loop order -- we access all arrays column-first, which
39 improves the performance enough to boost galgel spec score by 50%.
40
41 DIMENSION A(M,COUNT), B(COUNT,N), C(M,N)
42 C = 0
43 DO J=1,N
44 DO K=1,COUNT
45 DO I=1,M
46 C(I,J) = C(I,J)+A(I,K)*B(K,J)
47*/
48
7f68c75f
RH
49extern void matmul_i4 (gfc_array_i4 * retarray, gfc_array_i4 * a, gfc_array_i4 * b);
50export_proto(matmul_i4);
7d7b8bfe 51
6de9cd9a 52void
7f68c75f 53matmul_i4 (gfc_array_i4 * retarray, gfc_array_i4 * a, gfc_array_i4 * b)
6de9cd9a
DN
54{
55 GFC_INTEGER_4 *abase;
56 GFC_INTEGER_4 *bbase;
57 GFC_INTEGER_4 *dest;
410d3bba
VL
58
59 index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
60 index_type x, y, n, count, xcount, ycount;
6de9cd9a
DN
61
62 assert (GFC_DESCRIPTOR_RANK (a) == 2
63 || GFC_DESCRIPTOR_RANK (b) == 2);
883c9d4d 64
410d3bba
VL
65/* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
66
67 Either A or B (but not both) can be rank 1:
68
69 o One-dimensional argument A is implicitly treated as a row matrix
70 dimensioned [1,count], so xcount=1.
71
72 o One-dimensional argument B is implicitly treated as a column matrix
73 dimensioned [count, 1], so ycount=1.
74 */
75
883c9d4d
VL
76 if (retarray->data == NULL)
77 {
78 if (GFC_DESCRIPTOR_RANK (a) == 1)
79 {
80 retarray->dim[0].lbound = 0;
81 retarray->dim[0].ubound = b->dim[1].ubound - b->dim[1].lbound;
82 retarray->dim[0].stride = 1;
83 }
84 else if (GFC_DESCRIPTOR_RANK (b) == 1)
85 {
86 retarray->dim[0].lbound = 0;
87 retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
88 retarray->dim[0].stride = 1;
89 }
90 else
91 {
92 retarray->dim[0].lbound = 0;
93 retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
94 retarray->dim[0].stride = 1;
420aa7b8 95
883c9d4d
VL
96 retarray->dim[1].lbound = 0;
97 retarray->dim[1].ubound = b->dim[1].ubound - b->dim[1].lbound;
98 retarray->dim[1].stride = retarray->dim[0].ubound+1;
99 }
420aa7b8 100
07d3cebe 101 retarray->data
4b6903ec 102 = internal_malloc_size (sizeof (GFC_INTEGER_4) * size0 ((array_t *) retarray));
efd4dc1a 103 retarray->offset = 0;
883c9d4d
VL
104 }
105
6de9cd9a
DN
106 abase = a->data;
107 bbase = b->data;
108 dest = retarray->data;
109
110 if (retarray->dim[0].stride == 0)
111 retarray->dim[0].stride = 1;
112 if (a->dim[0].stride == 0)
113 a->dim[0].stride = 1;
114 if (b->dim[0].stride == 0)
115 b->dim[0].stride = 1;
116
117
118 if (GFC_DESCRIPTOR_RANK (retarray) == 1)
119 {
410d3bba
VL
120 /* One-dimensional result may be addressed in the code below
121 either as a row or a column matrix. We want both cases to
122 work. */
123 rxstride = rystride = retarray->dim[0].stride;
6de9cd9a
DN
124 }
125 else
126 {
127 rxstride = retarray->dim[0].stride;
128 rystride = retarray->dim[1].stride;
129 }
130
410d3bba 131
6de9cd9a
DN
132 if (GFC_DESCRIPTOR_RANK (a) == 1)
133 {
410d3bba
VL
134 /* Treat it as a a row matrix A[1,count]. */
135 axstride = a->dim[0].stride;
136 aystride = 1;
137
6de9cd9a 138 xcount = 1;
410d3bba 139 count = a->dim[0].ubound + 1 - a->dim[0].lbound;
6de9cd9a
DN
140 }
141 else
142 {
410d3bba
VL
143 axstride = a->dim[0].stride;
144 aystride = a->dim[1].stride;
145
6de9cd9a 146 count = a->dim[1].ubound + 1 - a->dim[1].lbound;
6de9cd9a
DN
147 xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
148 }
410d3bba
VL
149
150 assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
151
6de9cd9a
DN
152 if (GFC_DESCRIPTOR_RANK (b) == 1)
153 {
410d3bba
VL
154 /* Treat it as a column matrix B[count,1] */
155 bxstride = b->dim[0].stride;
156
157 /* bystride should never be used for 1-dimensional b.
158 in case it is we want it to cause a segfault, rather than
159 an incorrect result. */
420aa7b8 160 bystride = 0xDEADBEEF;
6de9cd9a
DN
161 ycount = 1;
162 }
163 else
164 {
410d3bba
VL
165 bxstride = b->dim[0].stride;
166 bystride = b->dim[1].stride;
6de9cd9a
DN
167 ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
168 }
169
410d3bba
VL
170 abase = a->data;
171 bbase = b->data;
172 dest = retarray->data;
173
174 if (rxstride == 1 && axstride == 1 && bxstride == 1)
6de9cd9a 175 {
410d3bba
VL
176 GFC_INTEGER_4 *bbase_y;
177 GFC_INTEGER_4 *dest_y;
178 GFC_INTEGER_4 *abase_n;
179 GFC_INTEGER_4 bbase_yn;
180
ae740cce
TK
181 if (rystride == ycount)
182 memset (dest, 0, (sizeof (GFC_INTEGER_4) * size0((array_t *) retarray)));
183 else
184 {
185 for (y = 0; y < ycount; y++)
186 for (x = 0; x < xcount; x++)
187 dest[x + y*rystride] = (GFC_INTEGER_4)0;
188 }
410d3bba
VL
189
190 for (y = 0; y < ycount; y++)
191 {
192 bbase_y = bbase + y*bystride;
193 dest_y = dest + y*rystride;
194 for (n = 0; n < count; n++)
195 {
196 abase_n = abase + n*aystride;
197 bbase_yn = bbase_y[n];
198 for (x = 0; x < xcount; x++)
199 {
200 dest_y[x] += abase_n[x] * bbase_yn;
201 }
202 }
203 }
204 }
205 else
206 {
207 for (y = 0; y < ycount; y++)
208 for (x = 0; x < xcount; x++)
209 dest[x*rxstride + y*rystride] = (GFC_INTEGER_4)0;
210
211 for (y = 0; y < ycount; y++)
212 for (n = 0; n < count; n++)
213 for (x = 0; x < xcount; x++)
214 /* dest[x,y] += a[x,n] * b[n,y] */
215 dest[x*rxstride + y*rystride] += abase[x*axstride + n*aystride] * bbase[n*bxstride + y*bystride];
6de9cd9a
DN
216 }
217}
This page took 0.187523 seconds and 5 git commands to generate.