PR middle-end/17835
[official-gcc.git] / libgfortran / m4 / matmul.m4
blob7a54b05595cacec5136b1982219ed3500665ab8e
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 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.
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.
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.  */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include "libgfortran.h"'
26 include(iparm.m4)dnl
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)
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;
46   rtype_name *pa;
47   rtype_name *pb;
48   index_type astride;
49   index_type bstride;
50   index_type count;
51   index_type n;
53   assert (GFC_DESCRIPTOR_RANK (a) == 2
54           || GFC_DESCRIPTOR_RANK (b) == 2);
56   if (retarray->data == NULL)
57     {
58       if (GFC_DESCRIPTOR_RANK (a) == 1)
59         {
60           retarray->dim[0].lbound = 0;
61           retarray->dim[0].ubound = b->dim[1].ubound - b->dim[1].lbound;
62           retarray->dim[0].stride = 1;
63         }
64       else if (GFC_DESCRIPTOR_RANK (b) == 1)
65         {
66           retarray->dim[0].lbound = 0;
67           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
68           retarray->dim[0].stride = 1;
69         }
70       else
71         {
72           retarray->dim[0].lbound = 0;
73           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
74           retarray->dim[0].stride = 1;
75           
76           retarray->dim[1].lbound = 0;
77           retarray->dim[1].ubound = b->dim[1].ubound - b->dim[1].lbound;
78           retarray->dim[1].stride = retarray->dim[0].ubound+1;
79         }
80           
81       retarray->data = internal_malloc (sizeof (rtype_name) * size0 (retarray));
82       retarray->base = 0;
83     }
85   abase = a->data;
86   bbase = b->data;
87   dest = retarray->data;
89   if (retarray->dim[0].stride == 0)
90     retarray->dim[0].stride = 1;
91   if (a->dim[0].stride == 0)
92     a->dim[0].stride = 1;
93   if (b->dim[0].stride == 0)
94     b->dim[0].stride = 1;
96 sinclude(`matmul_asm_'rtype_code`.m4')dnl
98   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
99     {
100       rxstride = retarray->dim[0].stride;
101       rystride = rxstride;
102     }
103   else
104     {
105       rxstride = retarray->dim[0].stride;
106       rystride = retarray->dim[1].stride;
107     }
109   /* If we have rank 1 parameters, zero the absent stride, and set the size to
110      one.  */
111   if (GFC_DESCRIPTOR_RANK (a) == 1)
112     {
113       astride = a->dim[0].stride;
114       count = a->dim[0].ubound + 1 - a->dim[0].lbound;
115       xstride = 0;
116       rxstride = 0;
117       xcount = 1;
118     }
119   else
120     {
121       astride = a->dim[1].stride;
122       count = a->dim[1].ubound + 1 - a->dim[1].lbound;
123       xstride = a->dim[0].stride;
124       xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
125     }
126   if (GFC_DESCRIPTOR_RANK (b) == 1)
127     {
128       bstride = b->dim[0].stride;
129       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
130       ystride = 0;
131       rystride = 0;
132       ycount = 1;
133     }
134   else
135     {
136       bstride = b->dim[0].stride;
137       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
138       ystride = b->dim[1].stride;
139       ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
140     }
142   for (y = 0; y < ycount; y++)
143     {
144       for (x = 0; x < xcount; x++)
145         {
146           /* Do the summation for this element.  For real and integer types
147              this is the same as DOT_PRODUCT.  For complex types we use do
148              a*b, not conjg(a)*b.  */
149           pa = abase;
150           pb = bbase;
151           res = 0;
153           for (n = 0; n < count; n++)
154             {
155               res += *pa * *pb;
156               pa += astride;
157               pb += bstride;
158             }
160           *dest = res;
162           dest += rxstride;
163           abase += xstride;
164         }
165       abase -= xstride * xcount;
166       bbase += ystride;
167       dest += rystride - (rxstride * xcount);
168     }