OSDN Git Service

2004-09-09 Victor Leikehman <lei@il.ibm.com>
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / matmul_c8.c
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 (libgfortran).
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
27 /* Dimensions: retarray(x,y) a(x, count) b(count,y).
28    Either a or b can be rank 1.  In this case x or y is 1.  */
29 void
30 __matmul_c8 (gfc_array_c8 * retarray, gfc_array_c8 * a, gfc_array_c8 * b)
31 {
32   GFC_COMPLEX_8 *abase;
33   GFC_COMPLEX_8 *bbase;
34   GFC_COMPLEX_8 *dest;
35   GFC_COMPLEX_8 res;
36   index_type rxstride;
37   index_type rystride;
38   index_type xcount;
39   index_type ycount;
40   index_type xstride;
41   index_type ystride;
42   index_type x;
43   index_type y;
44
45   GFC_COMPLEX_8 *pa;
46   GFC_COMPLEX_8 *pb;
47   index_type astride;
48   index_type bstride;
49   index_type count;
50   index_type n;
51
52   assert (GFC_DESCRIPTOR_RANK (a) == 2
53           || GFC_DESCRIPTOR_RANK (b) == 2);
54
55   if (retarray->data == NULL)
56     {
57       if (GFC_DESCRIPTOR_RANK (a) == 1)
58         {
59           retarray->dim[0].lbound = 0;
60           retarray->dim[0].ubound = b->dim[1].ubound - b->dim[1].lbound;
61           retarray->dim[0].stride = 1;
62         }
63       else if (GFC_DESCRIPTOR_RANK (b) == 1)
64         {
65           retarray->dim[0].lbound = 0;
66           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
67           retarray->dim[0].stride = 1;
68         }
69       else
70         {
71           retarray->dim[0].lbound = 0;
72           retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
73           retarray->dim[0].stride = 1;
74           
75           retarray->dim[1].lbound = 0;
76           retarray->dim[1].ubound = b->dim[1].ubound - b->dim[1].lbound;
77           retarray->dim[1].stride = retarray->dim[0].ubound+1;
78         }
79           
80       retarray->data = internal_malloc (sizeof (GFC_COMPLEX_8) * size0 (retarray));
81       retarray->base = 0;
82     }
83
84   abase = a->data;
85   bbase = b->data;
86   dest = retarray->data;
87
88   if (retarray->dim[0].stride == 0)
89     retarray->dim[0].stride = 1;
90   if (a->dim[0].stride == 0)
91     a->dim[0].stride = 1;
92   if (b->dim[0].stride == 0)
93     b->dim[0].stride = 1;
94
95
96   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
97     {
98       rxstride = retarray->dim[0].stride;
99       rystride = rxstride;
100     }
101   else
102     {
103       rxstride = retarray->dim[0].stride;
104       rystride = retarray->dim[1].stride;
105     }
106
107   /* If we have rank 1 parameters, zero the absent stride, and set the size to
108      one.  */
109   if (GFC_DESCRIPTOR_RANK (a) == 1)
110     {
111       astride = a->dim[0].stride;
112       count = a->dim[0].ubound + 1 - a->dim[0].lbound;
113       xstride = 0;
114       rxstride = 0;
115       xcount = 1;
116     }
117   else
118     {
119       astride = a->dim[1].stride;
120       count = a->dim[1].ubound + 1 - a->dim[1].lbound;
121       xstride = a->dim[0].stride;
122       xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
123     }
124   if (GFC_DESCRIPTOR_RANK (b) == 1)
125     {
126       bstride = b->dim[0].stride;
127       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
128       ystride = 0;
129       rystride = 0;
130       ycount = 1;
131     }
132   else
133     {
134       bstride = b->dim[0].stride;
135       assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
136       ystride = b->dim[1].stride;
137       ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
138     }
139
140   for (y = 0; y < ycount; y++)
141     {
142       for (x = 0; x < xcount; x++)
143         {
144           /* Do the summation for this element.  For real and integer types
145              this is the same as DOT_PRODUCT.  For complex types we use do
146              a*b, not conjg(a)*b.  */
147           pa = abase;
148           pb = bbase;
149           res = 0;
150
151           for (n = 0; n < count; n++)
152             {
153               res += *pa * *pb;
154               pa += astride;
155               pb += bstride;
156             }
157
158           *dest = res;
159
160           dest += rxstride;
161           abase += xstride;
162         }
163       abase -= xstride * xcount;
164       bbase += ystride;
165       dest += rystride - (rxstride * xcount);
166     }
167 }
168