1 `/* Implementation of the MATMUL intrinsic
2 Copyright 2002, 2005, 2006, 2007, 2009 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 3 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 General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
32 `#if defined (HAVE_'rtype_name`)
34 /* Dimensions: retarray(x,y) a(x, count) b(count,y).
35 Either a or b can be rank 1. In this case x or y is 1. */
37 extern void matmul_'rtype_code` ('rtype` * const restrict,
38 gfc_array_l1 * const restrict, gfc_array_l1 * const restrict);
39 export_proto(matmul_'rtype_code`);
42 matmul_'rtype_code` ('rtype` * const restrict retarray,
43 gfc_array_l1 * const restrict a, gfc_array_l1 * const restrict b)
45 const GFC_LOGICAL_1 * restrict abase;
46 const GFC_LOGICAL_1 * restrict bbase;
47 'rtype_name` * restrict dest;
59 const GFC_LOGICAL_1 * restrict pa;
60 const GFC_LOGICAL_1 * restrict pb;
66 assert (GFC_DESCRIPTOR_RANK (a) == 2
67 || GFC_DESCRIPTOR_RANK (b) == 2);
69 if (retarray->data == NULL)
71 if (GFC_DESCRIPTOR_RANK (a) == 1)
73 retarray->dim[0].lbound = 0;
74 retarray->dim[0].ubound = b->dim[1].ubound - b->dim[1].lbound;
75 retarray->dim[0].stride = 1;
77 else if (GFC_DESCRIPTOR_RANK (b) == 1)
79 retarray->dim[0].lbound = 0;
80 retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
81 retarray->dim[0].stride = 1;
85 retarray->dim[0].lbound = 0;
86 retarray->dim[0].ubound = a->dim[0].ubound - a->dim[0].lbound;
87 retarray->dim[0].stride = 1;
89 retarray->dim[1].lbound = 0;
90 retarray->dim[1].ubound = b->dim[1].ubound - b->dim[1].lbound;
91 retarray->dim[1].stride = retarray->dim[0].ubound+1;
95 = internal_malloc_size (sizeof ('rtype_name`) * size0 ((array_t *) retarray));
98 else if (unlikely (compile_options.bounds_check))
100 index_type ret_extent, arg_extent;
102 if (GFC_DESCRIPTOR_RANK (a) == 1)
104 arg_extent = b->dim[1].ubound + 1 - b->dim[1].lbound;
105 ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
106 if (arg_extent != ret_extent)
107 runtime_error ("Incorrect extent in return array in"
108 " MATMUL intrinsic: is %ld, should be %ld",
109 (long int) ret_extent, (long int) arg_extent);
111 else if (GFC_DESCRIPTOR_RANK (b) == 1)
113 arg_extent = a->dim[0].ubound + 1 - a->dim[0].lbound;
114 ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
115 if (arg_extent != ret_extent)
116 runtime_error ("Incorrect extent in return array in"
117 " MATMUL intrinsic: is %ld, should be %ld",
118 (long int) ret_extent, (long int) arg_extent);
122 arg_extent = a->dim[0].ubound + 1 - a->dim[0].lbound;
123 ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
124 if (arg_extent != ret_extent)
125 runtime_error ("Incorrect extent in return array in"
126 " MATMUL intrinsic for dimension 1:"
127 " is %ld, should be %ld",
128 (long int) ret_extent, (long int) arg_extent);
130 arg_extent = b->dim[1].ubound + 1 - b->dim[1].lbound;
131 ret_extent = retarray->dim[1].ubound + 1 - retarray->dim[1].lbound;
132 if (arg_extent != ret_extent)
133 runtime_error ("Incorrect extent in return array in"
134 " MATMUL intrinsic for dimension 2:"
135 " is %ld, should be %ld",
136 (long int) ret_extent, (long int) arg_extent);
141 a_kind = GFC_DESCRIPTOR_SIZE (a);
143 if (a_kind == 1 || a_kind == 2 || a_kind == 4 || a_kind == 8
144 #ifdef HAVE_GFC_LOGICAL_16
148 abase = GFOR_POINTER_TO_L1 (abase, a_kind);
150 internal_error (NULL, "Funny sized logical array");
153 b_kind = GFC_DESCRIPTOR_SIZE (b);
155 if (b_kind == 1 || b_kind == 2 || b_kind == 4 || b_kind == 8
156 #ifdef HAVE_GFC_LOGICAL_16
160 bbase = GFOR_POINTER_TO_L1 (bbase, b_kind);
162 internal_error (NULL, "Funny sized logical array");
164 dest = retarray->data;
166 sinclude(`matmul_asm_'rtype_code`.m4')dnl
168 if (GFC_DESCRIPTOR_RANK (retarray) == 1)
170 rxstride = retarray->dim[0].stride;
175 rxstride = retarray->dim[0].stride;
176 rystride = retarray->dim[1].stride;
179 /* If we have rank 1 parameters, zero the absent stride, and set the size to
181 if (GFC_DESCRIPTOR_RANK (a) == 1)
183 astride = a->dim[0].stride * a_kind;
184 count = a->dim[0].ubound + 1 - a->dim[0].lbound;
191 astride = a->dim[1].stride * a_kind;
192 count = a->dim[1].ubound + 1 - a->dim[1].lbound;
193 xstride = a->dim[0].stride * a_kind;
194 xcount = a->dim[0].ubound + 1 - a->dim[0].lbound;
196 if (GFC_DESCRIPTOR_RANK (b) == 1)
198 bstride = b->dim[0].stride * b_kind;
199 assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
206 bstride = b->dim[0].stride * b_kind;
207 assert(count == b->dim[0].ubound + 1 - b->dim[0].lbound);
208 ystride = b->dim[1].stride * b_kind;
209 ycount = b->dim[1].ubound + 1 - b->dim[1].lbound;
212 for (y = 0; y < ycount; y++)
214 for (x = 0; x < xcount; x++)
216 /* Do the summation for this element. For real and integer types
217 this is the same as DOT_PRODUCT. For complex types we use do
218 a*b, not conjg(a)*b. */
223 for (n = 0; n < count; n++)
237 abase -= xstride * xcount;
239 dest += rystride - (rxstride * xcount);