]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/m4/matmul_internal.m4
* config/microblaze/microblaze.c (microblaze_expand_block_move): Treat
[thirdparty/gcc.git] / libgfortran / m4 / matmul_internal.m4
1 `void
2 'matmul_name` ('rtype` * const restrict retarray,
3 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
4 int blas_limit, blas_call gemm)
5 {
6 const 'rtype_name` * restrict abase;
7 const 'rtype_name` * restrict bbase;
8 'rtype_name` * restrict dest;
9
10 index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
11 index_type x, y, n, count, xcount, ycount;
12
13 assert (GFC_DESCRIPTOR_RANK (a) == 2
14 || GFC_DESCRIPTOR_RANK (b) == 2);
15
16 /* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
17
18 Either A or B (but not both) can be rank 1:
19
20 o One-dimensional argument A is implicitly treated as a row matrix
21 dimensioned [1,count], so xcount=1.
22
23 o One-dimensional argument B is implicitly treated as a column matrix
24 dimensioned [count, 1], so ycount=1.
25 */
26
27 if (retarray->base_addr == NULL)
28 {
29 if (GFC_DESCRIPTOR_RANK (a) == 1)
30 {
31 GFC_DIMENSION_SET(retarray->dim[0], 0,
32 GFC_DESCRIPTOR_EXTENT(b,1) - 1, 1);
33 }
34 else if (GFC_DESCRIPTOR_RANK (b) == 1)
35 {
36 GFC_DIMENSION_SET(retarray->dim[0], 0,
37 GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
38 }
39 else
40 {
41 GFC_DIMENSION_SET(retarray->dim[0], 0,
42 GFC_DESCRIPTOR_EXTENT(a,0) - 1, 1);
43
44 GFC_DIMENSION_SET(retarray->dim[1], 0,
45 GFC_DESCRIPTOR_EXTENT(b,1) - 1,
46 GFC_DESCRIPTOR_EXTENT(retarray,0));
47 }
48
49 retarray->base_addr
50 = xmallocarray (size0 ((array_t *) retarray), sizeof ('rtype_name`));
51 retarray->offset = 0;
52 }
53 else if (unlikely (compile_options.bounds_check))
54 {
55 index_type ret_extent, arg_extent;
56
57 if (GFC_DESCRIPTOR_RANK (a) == 1)
58 {
59 arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
60 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
61 if (arg_extent != ret_extent)
62 runtime_error ("Array bound mismatch for dimension 1 of "
63 "array (%ld/%ld) ",
64 (long int) ret_extent, (long int) arg_extent);
65 }
66 else if (GFC_DESCRIPTOR_RANK (b) == 1)
67 {
68 arg_extent = GFC_DESCRIPTOR_EXTENT(a,0);
69 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
70 if (arg_extent != ret_extent)
71 runtime_error ("Array bound mismatch for dimension 1 of "
72 "array (%ld/%ld) ",
73 (long int) ret_extent, (long int) arg_extent);
74 }
75 else
76 {
77 arg_extent = GFC_DESCRIPTOR_EXTENT(a,0);
78 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
79 if (arg_extent != ret_extent)
80 runtime_error ("Array bound mismatch for dimension 1 of "
81 "array (%ld/%ld) ",
82 (long int) ret_extent, (long int) arg_extent);
83
84 arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
85 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,1);
86 if (arg_extent != ret_extent)
87 runtime_error ("Array bound mismatch for dimension 2 of "
88 "array (%ld/%ld) ",
89 (long int) ret_extent, (long int) arg_extent);
90 }
91 }
92 '
93 sinclude(`matmul_asm_'rtype_code`.m4')dnl
94 `
95 if (GFC_DESCRIPTOR_RANK (retarray) == 1)
96 {
97 /* One-dimensional result may be addressed in the code below
98 either as a row or a column matrix. We want both cases to
99 work. */
100 rxstride = rystride = GFC_DESCRIPTOR_STRIDE(retarray,0);
101 }
102 else
103 {
104 rxstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
105 rystride = GFC_DESCRIPTOR_STRIDE(retarray,1);
106 }
107
108
109 if (GFC_DESCRIPTOR_RANK (a) == 1)
110 {
111 /* Treat it as a a row matrix A[1,count]. */
112 axstride = GFC_DESCRIPTOR_STRIDE(a,0);
113 aystride = 1;
114
115 xcount = 1;
116 count = GFC_DESCRIPTOR_EXTENT(a,0);
117 }
118 else
119 {
120 axstride = GFC_DESCRIPTOR_STRIDE(a,0);
121 aystride = GFC_DESCRIPTOR_STRIDE(a,1);
122
123 count = GFC_DESCRIPTOR_EXTENT(a,1);
124 xcount = GFC_DESCRIPTOR_EXTENT(a,0);
125 }
126
127 if (count != GFC_DESCRIPTOR_EXTENT(b,0))
128 {
129 if (count > 0 || GFC_DESCRIPTOR_EXTENT(b,0) > 0)
130 runtime_error ("Incorrect extent in argument B in MATMUL intrinsic "
131 "in dimension 1: is %ld, should be %ld",
132 (long int) GFC_DESCRIPTOR_EXTENT(b,0), (long int) count);
133 }
134
135 if (GFC_DESCRIPTOR_RANK (b) == 1)
136 {
137 /* Treat it as a column matrix B[count,1] */
138 bxstride = GFC_DESCRIPTOR_STRIDE(b,0);
139
140 /* bystride should never be used for 1-dimensional b.
141 The value is only used for calculation of the
142 memory by the buffer. */
143 bystride = 256;
144 ycount = 1;
145 }
146 else
147 {
148 bxstride = GFC_DESCRIPTOR_STRIDE(b,0);
149 bystride = GFC_DESCRIPTOR_STRIDE(b,1);
150 ycount = GFC_DESCRIPTOR_EXTENT(b,1);
151 }
152
153 abase = a->base_addr;
154 bbase = b->base_addr;
155 dest = retarray->base_addr;
156
157 /* Now that everything is set up, we perform the multiplication
158 itself. */
159
160 #define POW3(x) (((float) (x)) * ((float) (x)) * ((float) (x)))
161 #define min(a,b) ((a) <= (b) ? (a) : (b))
162 #define max(a,b) ((a) >= (b) ? (a) : (b))
163
164 if (try_blas && rxstride == 1 && (axstride == 1 || aystride == 1)
165 && (bxstride == 1 || bystride == 1)
166 && (((float) xcount) * ((float) ycount) * ((float) count)
167 > POW3(blas_limit)))
168 {
169 const int m = xcount, n = ycount, k = count, ldc = rystride;
170 const 'rtype_name` one = 1, zero = 0;
171 const int lda = (axstride == 1) ? aystride : axstride,
172 ldb = (bxstride == 1) ? bystride : bxstride;
173
174 if (lda > 0 && ldb > 0 && ldc > 0 && m > 1 && n > 1 && k > 1)
175 {
176 assert (gemm != NULL);
177 const char *transa, *transb;
178 if (try_blas & 2)
179 transa = "C";
180 else
181 transa = axstride == 1 ? "N" : "T";
182
183 if (try_blas & 4)
184 transb = "C";
185 else
186 transb = bxstride == 1 ? "N" : "T";
187
188 gemm (transa, transb , &m,
189 &n, &k, &one, abase, &lda, bbase, &ldb, &zero, dest,
190 &ldc, 1, 1);
191 return;
192 }
193 }
194
195 if (rxstride == 1 && axstride == 1 && bxstride == 1)
196 {
197 /* This block of code implements a tuned matmul, derived from
198 Superscalar GEMM-based level 3 BLAS, Beta version 0.1
199
200 Bo Kagstrom and Per Ling
201 Department of Computing Science
202 Umea University
203 S-901 87 Umea, Sweden
204
205 from netlib.org, translated to C, and modified for matmul.m4. */
206
207 const 'rtype_name` *a, *b;
208 'rtype_name` *c;
209 const index_type m = xcount, n = ycount, k = count;
210
211 /* System generated locals */
212 index_type a_dim1, a_offset, b_dim1, b_offset, c_dim1, c_offset,
213 i1, i2, i3, i4, i5, i6;
214
215 /* Local variables */
216 'rtype_name` f11, f12, f21, f22, f31, f32, f41, f42,
217 f13, f14, f23, f24, f33, f34, f43, f44;
218 index_type i, j, l, ii, jj, ll;
219 index_type isec, jsec, lsec, uisec, ujsec, ulsec;
220 'rtype_name` *t1;
221
222 a = abase;
223 b = bbase;
224 c = retarray->base_addr;
225
226 /* Parameter adjustments */
227 c_dim1 = rystride;
228 c_offset = 1 + c_dim1;
229 c -= c_offset;
230 a_dim1 = aystride;
231 a_offset = 1 + a_dim1;
232 a -= a_offset;
233 b_dim1 = bystride;
234 b_offset = 1 + b_dim1;
235 b -= b_offset;
236
237 /* Empty c first. */
238 for (j=1; j<=n; j++)
239 for (i=1; i<=m; i++)
240 c[i + j * c_dim1] = ('rtype_name`)0;
241
242 /* Early exit if possible */
243 if (m == 0 || n == 0 || k == 0)
244 return;
245
246 /* Adjust size of t1 to what is needed. */
247 index_type t1_dim, a_sz;
248 if (aystride == 1)
249 a_sz = rystride;
250 else
251 a_sz = a_dim1;
252
253 t1_dim = a_sz * 256 + b_dim1;
254 if (t1_dim > 65536)
255 t1_dim = 65536;
256
257 t1 = malloc (t1_dim * sizeof('rtype_name`));
258
259 /* Start turning the crank. */
260 i1 = n;
261 for (jj = 1; jj <= i1; jj += 512)
262 {
263 /* Computing MIN */
264 i2 = 512;
265 i3 = n - jj + 1;
266 jsec = min(i2,i3);
267 ujsec = jsec - jsec % 4;
268 i2 = k;
269 for (ll = 1; ll <= i2; ll += 256)
270 {
271 /* Computing MIN */
272 i3 = 256;
273 i4 = k - ll + 1;
274 lsec = min(i3,i4);
275 ulsec = lsec - lsec % 2;
276
277 i3 = m;
278 for (ii = 1; ii <= i3; ii += 256)
279 {
280 /* Computing MIN */
281 i4 = 256;
282 i5 = m - ii + 1;
283 isec = min(i4,i5);
284 uisec = isec - isec % 2;
285 i4 = ll + ulsec - 1;
286 for (l = ll; l <= i4; l += 2)
287 {
288 i5 = ii + uisec - 1;
289 for (i = ii; i <= i5; i += 2)
290 {
291 t1[l - ll + 1 + ((i - ii + 1) << 8) - 257] =
292 a[i + l * a_dim1];
293 t1[l - ll + 2 + ((i - ii + 1) << 8) - 257] =
294 a[i + (l + 1) * a_dim1];
295 t1[l - ll + 1 + ((i - ii + 2) << 8) - 257] =
296 a[i + 1 + l * a_dim1];
297 t1[l - ll + 2 + ((i - ii + 2) << 8) - 257] =
298 a[i + 1 + (l + 1) * a_dim1];
299 }
300 if (uisec < isec)
301 {
302 t1[l - ll + 1 + (isec << 8) - 257] =
303 a[ii + isec - 1 + l * a_dim1];
304 t1[l - ll + 2 + (isec << 8) - 257] =
305 a[ii + isec - 1 + (l + 1) * a_dim1];
306 }
307 }
308 if (ulsec < lsec)
309 {
310 i4 = ii + isec - 1;
311 for (i = ii; i<= i4; ++i)
312 {
313 t1[lsec + ((i - ii + 1) << 8) - 257] =
314 a[i + (ll + lsec - 1) * a_dim1];
315 }
316 }
317
318 uisec = isec - isec % 4;
319 i4 = jj + ujsec - 1;
320 for (j = jj; j <= i4; j += 4)
321 {
322 i5 = ii + uisec - 1;
323 for (i = ii; i <= i5; i += 4)
324 {
325 f11 = c[i + j * c_dim1];
326 f21 = c[i + 1 + j * c_dim1];
327 f12 = c[i + (j + 1) * c_dim1];
328 f22 = c[i + 1 + (j + 1) * c_dim1];
329 f13 = c[i + (j + 2) * c_dim1];
330 f23 = c[i + 1 + (j + 2) * c_dim1];
331 f14 = c[i + (j + 3) * c_dim1];
332 f24 = c[i + 1 + (j + 3) * c_dim1];
333 f31 = c[i + 2 + j * c_dim1];
334 f41 = c[i + 3 + j * c_dim1];
335 f32 = c[i + 2 + (j + 1) * c_dim1];
336 f42 = c[i + 3 + (j + 1) * c_dim1];
337 f33 = c[i + 2 + (j + 2) * c_dim1];
338 f43 = c[i + 3 + (j + 2) * c_dim1];
339 f34 = c[i + 2 + (j + 3) * c_dim1];
340 f44 = c[i + 3 + (j + 3) * c_dim1];
341 i6 = ll + lsec - 1;
342 for (l = ll; l <= i6; ++l)
343 {
344 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
345 * b[l + j * b_dim1];
346 f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
347 * b[l + j * b_dim1];
348 f12 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
349 * b[l + (j + 1) * b_dim1];
350 f22 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
351 * b[l + (j + 1) * b_dim1];
352 f13 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
353 * b[l + (j + 2) * b_dim1];
354 f23 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
355 * b[l + (j + 2) * b_dim1];
356 f14 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
357 * b[l + (j + 3) * b_dim1];
358 f24 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
359 * b[l + (j + 3) * b_dim1];
360 f31 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
361 * b[l + j * b_dim1];
362 f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
363 * b[l + j * b_dim1];
364 f32 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
365 * b[l + (j + 1) * b_dim1];
366 f42 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
367 * b[l + (j + 1) * b_dim1];
368 f33 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
369 * b[l + (j + 2) * b_dim1];
370 f43 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
371 * b[l + (j + 2) * b_dim1];
372 f34 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
373 * b[l + (j + 3) * b_dim1];
374 f44 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
375 * b[l + (j + 3) * b_dim1];
376 }
377 c[i + j * c_dim1] = f11;
378 c[i + 1 + j * c_dim1] = f21;
379 c[i + (j + 1) * c_dim1] = f12;
380 c[i + 1 + (j + 1) * c_dim1] = f22;
381 c[i + (j + 2) * c_dim1] = f13;
382 c[i + 1 + (j + 2) * c_dim1] = f23;
383 c[i + (j + 3) * c_dim1] = f14;
384 c[i + 1 + (j + 3) * c_dim1] = f24;
385 c[i + 2 + j * c_dim1] = f31;
386 c[i + 3 + j * c_dim1] = f41;
387 c[i + 2 + (j + 1) * c_dim1] = f32;
388 c[i + 3 + (j + 1) * c_dim1] = f42;
389 c[i + 2 + (j + 2) * c_dim1] = f33;
390 c[i + 3 + (j + 2) * c_dim1] = f43;
391 c[i + 2 + (j + 3) * c_dim1] = f34;
392 c[i + 3 + (j + 3) * c_dim1] = f44;
393 }
394 if (uisec < isec)
395 {
396 i5 = ii + isec - 1;
397 for (i = ii + uisec; i <= i5; ++i)
398 {
399 f11 = c[i + j * c_dim1];
400 f12 = c[i + (j + 1) * c_dim1];
401 f13 = c[i + (j + 2) * c_dim1];
402 f14 = c[i + (j + 3) * c_dim1];
403 i6 = ll + lsec - 1;
404 for (l = ll; l <= i6; ++l)
405 {
406 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
407 257] * b[l + j * b_dim1];
408 f12 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
409 257] * b[l + (j + 1) * b_dim1];
410 f13 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
411 257] * b[l + (j + 2) * b_dim1];
412 f14 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
413 257] * b[l + (j + 3) * b_dim1];
414 }
415 c[i + j * c_dim1] = f11;
416 c[i + (j + 1) * c_dim1] = f12;
417 c[i + (j + 2) * c_dim1] = f13;
418 c[i + (j + 3) * c_dim1] = f14;
419 }
420 }
421 }
422 if (ujsec < jsec)
423 {
424 i4 = jj + jsec - 1;
425 for (j = jj + ujsec; j <= i4; ++j)
426 {
427 i5 = ii + uisec - 1;
428 for (i = ii; i <= i5; i += 4)
429 {
430 f11 = c[i + j * c_dim1];
431 f21 = c[i + 1 + j * c_dim1];
432 f31 = c[i + 2 + j * c_dim1];
433 f41 = c[i + 3 + j * c_dim1];
434 i6 = ll + lsec - 1;
435 for (l = ll; l <= i6; ++l)
436 {
437 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
438 257] * b[l + j * b_dim1];
439 f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) -
440 257] * b[l + j * b_dim1];
441 f31 += t1[l - ll + 1 + ((i - ii + 3) << 8) -
442 257] * b[l + j * b_dim1];
443 f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) -
444 257] * b[l + j * b_dim1];
445 }
446 c[i + j * c_dim1] = f11;
447 c[i + 1 + j * c_dim1] = f21;
448 c[i + 2 + j * c_dim1] = f31;
449 c[i + 3 + j * c_dim1] = f41;
450 }
451 i5 = ii + isec - 1;
452 for (i = ii + uisec; i <= i5; ++i)
453 {
454 f11 = c[i + j * c_dim1];
455 i6 = ll + lsec - 1;
456 for (l = ll; l <= i6; ++l)
457 {
458 f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
459 257] * b[l + j * b_dim1];
460 }
461 c[i + j * c_dim1] = f11;
462 }
463 }
464 }
465 }
466 }
467 }
468 free(t1);
469 return;
470 }
471 else if (rxstride == 1 && aystride == 1 && bxstride == 1)
472 {
473 if (GFC_DESCRIPTOR_RANK (a) != 1)
474 {
475 const 'rtype_name` *restrict abase_x;
476 const 'rtype_name` *restrict bbase_y;
477 'rtype_name` *restrict dest_y;
478 'rtype_name` s;
479
480 for (y = 0; y < ycount; y++)
481 {
482 bbase_y = &bbase[y*bystride];
483 dest_y = &dest[y*rystride];
484 for (x = 0; x < xcount; x++)
485 {
486 abase_x = &abase[x*axstride];
487 s = ('rtype_name`) 0;
488 for (n = 0; n < count; n++)
489 s += abase_x[n] * bbase_y[n];
490 dest_y[x] = s;
491 }
492 }
493 }
494 else
495 {
496 const 'rtype_name` *restrict bbase_y;
497 'rtype_name` s;
498
499 for (y = 0; y < ycount; y++)
500 {
501 bbase_y = &bbase[y*bystride];
502 s = ('rtype_name`) 0;
503 for (n = 0; n < count; n++)
504 s += abase[n*axstride] * bbase_y[n];
505 dest[y*rystride] = s;
506 }
507 }
508 }
509 else if (axstride < aystride)
510 {
511 for (y = 0; y < ycount; y++)
512 for (x = 0; x < xcount; x++)
513 dest[x*rxstride + y*rystride] = ('rtype_name`)0;
514
515 for (y = 0; y < ycount; y++)
516 for (n = 0; n < count; n++)
517 for (x = 0; x < xcount; x++)
518 /* dest[x,y] += a[x,n] * b[n,y] */
519 dest[x*rxstride + y*rystride] +=
520 abase[x*axstride + n*aystride] *
521 bbase[n*bxstride + y*bystride];
522 }
523 else if (GFC_DESCRIPTOR_RANK (a) == 1)
524 {
525 const 'rtype_name` *restrict bbase_y;
526 'rtype_name` s;
527
528 for (y = 0; y < ycount; y++)
529 {
530 bbase_y = &bbase[y*bystride];
531 s = ('rtype_name`) 0;
532 for (n = 0; n < count; n++)
533 s += abase[n*axstride] * bbase_y[n*bxstride];
534 dest[y*rxstride] = s;
535 }
536 }
537 else
538 {
539 const 'rtype_name` *restrict abase_x;
540 const 'rtype_name` *restrict bbase_y;
541 'rtype_name` *restrict dest_y;
542 'rtype_name` s;
543
544 for (y = 0; y < ycount; y++)
545 {
546 bbase_y = &bbase[y*bystride];
547 dest_y = &dest[y*rystride];
548 for (x = 0; x < xcount; x++)
549 {
550 abase_x = &abase[x*axstride];
551 s = ('rtype_name`) 0;
552 for (n = 0; n < count; n++)
553 s += abase_x[n*aystride] * bbase_y[n*bxstride];
554 dest_y[x*rxstride] = s;
555 }
556 }
557 }
558 }
559 #undef POW3
560 #undef min
561 #undef max
562 '