Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / generated / matmul_c8.c
blob7b14ba8c031c3aac1c96b19a6164ae46bb65c964
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"
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%.
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)
49 extern void matmul_c8 (gfc_array_c8 * retarray, gfc_array_c8 * a, gfc_array_c8 * b);
50 export_proto(matmul_c8);
52 void
53 matmul_c8 (gfc_array_c8 * retarray, gfc_array_c8 * a, gfc_array_c8 * b)
55 GFC_COMPLEX_8 *abase;
56 GFC_COMPLEX_8 *bbase;
57 GFC_COMPLEX_8 *dest;
59 index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
60 index_type x, y, n, count, xcount, ycount;
62 assert (GFC_DESCRIPTOR_RANK (a) == 2
63 || GFC_DESCRIPTOR_RANK (b) == 2);
65 /* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
67 Either A or B (but not both) can be rank 1:
69 o One-dimensional argument A is implicitly treated as a row matrix
70 dimensioned [1,count], so xcount=1.
72 o One-dimensional argument B is implicitly treated as a column matrix
73 dimensioned [count, 1], so ycount=1.
76 if (retarray->data == NULL)
78 if (GFC_DESCRIPTOR_RANK (a) == 1)
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;
84 else if (GFC_DESCRIPTOR_RANK (b) == 1)
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;
90 else
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;
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;
101 retarray->data
102 = internal_malloc_size (sizeof (GFC_COMPLEX_8) * size0 (retarray));
103 retarray->base = 0;
106 abase = a->data;
107 bbase = b->data;
108 dest = retarray->data;
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;
118 if (GFC_DESCRIPTOR_RANK (retarray) == 1)
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;
125 else
127 rxstride = retarray->dim[0].stride;
128 rystride = retarray->dim[1].stride;
132 if (GFC_DESCRIPTOR_RANK (a) == 1)
134 /* Treat it as a a row matrix A[1,count]. */
135 axstride = a->dim[0].stride;
136 aystride = 1;
138 xcount = 1;
139 count = a->dim[0].ubound + 1 - a->dim[0].lbound;
141 else
143 axstride = a->dim[0].stride;
144 aystride = a->dim[1].stride;
146 count = a->dim[1].ubound + 1 - a->dim[1].lbound;
147 xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
150 assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
152 if (GFC_DESCRIPTOR_RANK (b) == 1)
154 /* Treat it as a column matrix B[count,1] */
155 bxstride = b->dim[0].stride;
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. */
160 bystride = 0xDEADBEEF;
161 ycount = 1;
163 else
165 bxstride = b->dim[0].stride;
166 bystride = b->dim[1].stride;
167 ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
170 assert (a->base == 0);
171 assert (b->base == 0);
172 assert (retarray->base == 0);
174 abase = a->data;
175 bbase = b->data;
176 dest = retarray->data;
178 if (rxstride == 1 && axstride == 1 && bxstride == 1)
180 GFC_COMPLEX_8 *bbase_y;
181 GFC_COMPLEX_8 *dest_y;
182 GFC_COMPLEX_8 *abase_n;
183 GFC_COMPLEX_8 bbase_yn;
185 memset (dest, 0, (sizeof (GFC_COMPLEX_8) * size0(retarray)));
187 for (y = 0; y < ycount; y++)
189 bbase_y = bbase + y*bystride;
190 dest_y = dest + y*rystride;
191 for (n = 0; n < count; n++)
193 abase_n = abase + n*aystride;
194 bbase_yn = bbase_y[n];
195 for (x = 0; x < xcount; x++)
197 dest_y[x] += abase_n[x] * bbase_yn;
202 else
204 for (y = 0; y < ycount; y++)
205 for (x = 0; x < xcount; x++)
206 dest[x*rxstride + y*rystride] = (GFC_COMPLEX_8)0;
208 for (y = 0; y < ycount; y++)
209 for (n = 0; n < count; n++)
210 for (x = 0; x < xcount; x++)
211 /* dest[x,y] += a[x,n] * b[n,y] */
212 dest[x*rxstride + y*rystride] += abase[x*axstride + n*aystride] * bbase[n*bxstride + y*bystride];