OSDN Git Service

26b241dedad0db63f2d570c54188b1a9b9d01935
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / matmul.m4
1 `/* Implementation of the MATMUL 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 (libgfor).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include "libgfortran.h"'
26 include(iparm.m4)dnl
27
28 /* Dimensions: retarray(x,y) a(x, count) b(count,y).
29    Either a or b can be rank 1.  In this case x or y is 1.  */
30 void
31 `__matmul_'rtype_code (rtype * retarray, rtype * a, rtype * b)
32 {
33   rtype_name *abase;
34   rtype_name *bbase;
35   rtype_name *dest;
36   rtype_name res;
37   index_type rxstride;
38   index_type rystride;
39   index_type xcount;
40   index_type ycount;
41   index_type xstride;
42   index_type ystride;
43   index_type x;
44   index_type y;
45
46   rtype_name *pa;
47   rtype_name *pb;
48   index_type astride;
49   index_type bstride;
50   index_type count;
51   index_type n;
52
53   assert (GFC_DESCRIPTOR_RANK (a) == 2
54           || GFC_DESCRIPTOR_RANK (b) == 2);
55   abase = a->data;
56   bbase = b->data;
57   dest = retarray->data;
58
59   if (retarray->dim[0].stride == 0)
60     retarray->dim[0].stride = 1;
61   if (a->dim[0].stride == 0)
62     a->dim[0].stride = 1;
63   if (b->dim[0].stride == 0)
64     b->dim[0].stride = 1;
65
66 sinclude(`matmul_asm_'rtype_code`.m4')dnl
67
68   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
69     {
70       rxstride = retarray->dim[0].stride;
71       rystride = rxstride;
72     }
73   else
74     {
75       rxstride = retarray->dim[0].stride;
76       rystride = retarray->dim[1].stride;
77     }
78
79   /* If we have rank 1 parameters, zero the absent stride, and set the size to
80      one.  */
81   if (GFC_DESCRIPTOR_RANK (a) == 1)
82     {
83       astride = a->dim[0].stride;
84       count = a->dim[0].ubound + 1 - a->dim[0].lbound;
85       xstride = 0;
86       rxstride = 0;
87       xcount = 1;
88     }
89   else
90     {
91       astride = a->dim[1].stride;
92       count = a->dim[1].ubound + 1 - a->dim[1].lbound;
93       xstride = a->dim[0].stride;
94       xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
95     }
96   if (GFC_DESCRIPTOR_RANK (b) == 1)
97     {
98       bstride = b->dim[0].stride;
99       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
100       ystride = 0;
101       rystride = 0;
102       ycount = 1;
103     }
104   else
105     {
106       bstride = b->dim[0].stride;
107       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
108       ystride = b->dim[1].stride;
109       ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
110     }
111
112   for (y = 0; y < ycount; y++)
113     {
114       for (x = 0; x < xcount; x++)
115         {
116           /* Do the summation for this element.  For real and integer types
117              this is the same as DOT_PRODUCT.  For complex types we use do
118              a*b, not conjg(a)*b.  */
119           pa = abase;
120           pb = bbase;
121           res = 0;
122
123           for (n = 0; n < count; n++)
124             {
125               res += *pa * *pb;
126               pa += astride;
127               pb += bstride;
128             }
129
130           *dest = res;
131
132           dest += rxstride;
133           abase += xstride;
134         }
135       abase -= xstride * xcount;
136       bbase += ystride;
137       dest += rystride - (rxstride * xcount);
138     }
139 }
140