OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / matmull.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(types.m4)dnl
27 define(rtype_kind, regexp(file, `_l\([0-9]+\)\.', `\1'))dnl
28 define(rtype_code,`l'rtype_kind)dnl
29 define(rtype,get_arraytype(l,rtype_kind))dnl
30 define(rtype_name, get_typename(l, rtype_kind))dnl
31
32 /* Dimensions: retarray(x,y) a(x, count) b(count,y).
33    Either a or b can be rank 1.  In this case x or y is 1.  */
34 void
35 `__matmul_'rtype_code (rtype * retarray, gfc_array_l4 * a, gfc_array_l4 * b)
36 {
37   GFC_INTEGER_4 *abase;
38   GFC_INTEGER_4 *bbase;
39   rtype_name *dest;
40   index_type rxstride;
41   index_type rystride;
42   index_type xcount;
43   index_type ycount;
44   index_type xstride;
45   index_type ystride;
46   index_type x;
47   index_type y;
48
49   GFC_INTEGER_4 *pa;
50   GFC_INTEGER_4 *pb;
51   index_type astride;
52   index_type bstride;
53   index_type count;
54   index_type n;
55
56   assert (GFC_DESCRIPTOR_RANK (a) == 2
57           || GFC_DESCRIPTOR_RANK (b) == 2);
58   abase = a->data;
59   if (GFC_DESCRIPTOR_SIZE (a) != 4)
60     {
61       assert (GFC_DESCRIPTOR_SIZE (a) == 8);
62       abase = GFOR_POINTER_L8_TO_L4 (abase);
63       astride <<= 1;
64     }
65   bbase = b->data;
66   if (GFC_DESCRIPTOR_SIZE (b) != 4)
67     {
68       assert (GFC_DESCRIPTOR_SIZE (b) == 8);
69       bbase = GFOR_POINTER_L8_TO_L4 (bbase);
70       bstride <<= 1;
71     }
72   dest = retarray->data;
73
74   if (retarray->dim[0].stride == 0)
75     retarray->dim[0].stride = 1;
76   if (a->dim[0].stride == 0)
77     a->dim[0].stride = 1;
78   if (b->dim[0].stride == 0)
79     b->dim[0].stride = 1;
80
81 sinclude(`matmul_asm_'rtype_code`.m4')dnl
82
83   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
84     {
85       rxstride = retarray->dim[0].stride;
86       rystride = rxstride;
87     }
88   else
89     {
90       rxstride = retarray->dim[0].stride;
91       rystride = retarray->dim[1].stride;
92     }
93
94   /* If we have rank 1 parameters, zero the absent stride, and set the size to
95      one.  */
96   if (GFC_DESCRIPTOR_RANK (a) == 1)
97     {
98       astride = a->dim[0].stride;
99       count = a->dim[0].ubound + 1 - a->dim[0].lbound;
100       xstride = 0;
101       rxstride = 0;
102       xcount = 1;
103     }
104   else
105     {
106       astride = a->dim[1].stride;
107       count = a->dim[1].ubound + 1 - a->dim[1].lbound;
108       xstride = a->dim[0].stride;
109       xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
110     }
111   if (GFC_DESCRIPTOR_RANK (b) == 1)
112     {
113       bstride = b->dim[0].stride;
114       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
115       ystride = 0;
116       rystride = 0;
117       ycount = 1;
118     }
119   else
120     {
121       bstride = b->dim[0].stride;
122       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
123       ystride = b->dim[1].stride;
124       ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
125     }
126
127   for (y = 0; y < ycount; y++)
128     {
129       for (x = 0; x < xcount; x++)
130         {
131           /* Do the summation for this element.  For real and integer types
132              this is the same as DOT_PRODUCT.  For complex types we use do
133              a*b, not conjg(a)*b.  */
134           pa = abase;
135           pb = bbase;
136           *dest = 0;
137
138           for (n = 0; n < count; n++)
139             {
140               if (*pa && *pb)
141                 {
142                   *dest = 1;
143                   break;
144                 }
145               pa += astride;
146               pb += bstride;
147             }
148
149           dest += rxstride;
150           abase += xstride;
151         }
152       abase -= xstride * xcount;
153       bbase += ystride;
154       dest += rystride - (rxstride * xcount);
155     }
156 }
157