2017-04-05 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / m4 / matmul_internal.m4
blobd35968b3be57b4c6f9840c985277666d6270e85d
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)
6   const 'rtype_name` * restrict abase;
7   const 'rtype_name` * restrict bbase;
8   'rtype_name` * restrict dest;
10   index_type rxstride, rystride, axstride, aystride, bxstride, bystride;
11   index_type x, y, n, count, xcount, ycount;
13   assert (GFC_DESCRIPTOR_RANK (a) == 2
14           || GFC_DESCRIPTOR_RANK (b) == 2);
16 /* C[xcount,ycount] = A[xcount, count] * B[count,ycount]
18    Either A or B (but not both) can be rank 1:
20    o One-dimensional argument A is implicitly treated as a row matrix
21      dimensioned [1,count], so xcount=1.
23    o One-dimensional argument B is implicitly treated as a column matrix
24      dimensioned [count, 1], so ycount=1.
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);
44           GFC_DIMENSION_SET(retarray->dim[1], 0,
45                             GFC_DESCRIPTOR_EXTENT(b,1) - 1,
46                             GFC_DESCRIPTOR_EXTENT(retarray,0));
47         }
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;
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 ("Incorrect extent in return array in"
63                            " MATMUL intrinsic: is %ld, should be %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 ("Incorrect extent in return array in"
72                            " MATMUL intrinsic: is %ld, should be %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 ("Incorrect extent in return array in"
81                            " MATMUL intrinsic for dimension 1:"
82                            " is %ld, should be %ld",
83                            (long int) ret_extent, (long int) arg_extent);
85           arg_extent = GFC_DESCRIPTOR_EXTENT(b,1);
86           ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,1);
87           if (arg_extent != ret_extent)
88             runtime_error ("Incorrect extent in return array in"
89                            " MATMUL intrinsic for dimension 2:"
90                            " is %ld, should be %ld",
91                            (long int) ret_extent, (long int) arg_extent);
92         }
93     }
95 sinclude(`matmul_asm_'rtype_code`.m4')dnl
97   if (GFC_DESCRIPTOR_RANK (retarray) == 1)
98     {
99       /* One-dimensional result may be addressed in the code below
100          either as a row or a column matrix. We want both cases to
101          work. */
102       rxstride = rystride = GFC_DESCRIPTOR_STRIDE(retarray,0);
103     }
104   else
105     {
106       rxstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
107       rystride = GFC_DESCRIPTOR_STRIDE(retarray,1);
108     }
111   if (GFC_DESCRIPTOR_RANK (a) == 1)
112     {
113       /* Treat it as a a row matrix A[1,count]. */
114       axstride = GFC_DESCRIPTOR_STRIDE(a,0);
115       aystride = 1;
117       xcount = 1;
118       count = GFC_DESCRIPTOR_EXTENT(a,0);
119     }
120   else
121     {
122       axstride = GFC_DESCRIPTOR_STRIDE(a,0);
123       aystride = GFC_DESCRIPTOR_STRIDE(a,1);
125       count = GFC_DESCRIPTOR_EXTENT(a,1);
126       xcount = GFC_DESCRIPTOR_EXTENT(a,0);
127     }
129   if (count != GFC_DESCRIPTOR_EXTENT(b,0))
130     {
131       if (count > 0 || GFC_DESCRIPTOR_EXTENT(b,0) > 0)
132         runtime_error ("dimension of array B incorrect in MATMUL intrinsic");
133     }
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);
140       /* bystride should never be used for 1-dimensional b.
141          in case it is we want it to cause a segfault, rather than
142          an incorrect result. */
143       bystride = 0xDEADBEEF;
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     }
153   abase = a->base_addr;
154   bbase = b->base_addr;
155   dest = retarray->base_addr;
157   /* Now that everything is set up, we perform the multiplication
158      itself.  */
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))
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;
174       if (lda > 0 && ldb > 0 && ldc > 0 && m > 1 && n > 1 && k > 1)
175         {
176           assert (gemm != NULL);
177           gemm (axstride == 1 ? "N" : "T", bxstride == 1 ? "N" : "T", &m,
178                 &n, &k, &one, abase, &lda, bbase, &ldb, &zero, dest,
179                 &ldc, 1, 1);
180           return;
181         }
182     }
184   if (rxstride == 1 && axstride == 1 && bxstride == 1)
185     {
186       /* This block of code implements a tuned matmul, derived from
187          Superscalar GEMM-based level 3 BLAS,  Beta version 0.1
189                Bo Kagstrom and Per Ling
190                Department of Computing Science
191                Umea University
192                S-901 87 Umea, Sweden
194          from netlib.org, translated to C, and modified for matmul.m4.  */
196       const 'rtype_name` *a, *b;
197       'rtype_name` *c;
198       const index_type m = xcount, n = ycount, k = count;
200       /* System generated locals */
201       index_type a_dim1, a_offset, b_dim1, b_offset, c_dim1, c_offset,
202                  i1, i2, i3, i4, i5, i6;
204       /* Local variables */
205       'rtype_name` t1[65536], /* was [256][256] */
206                  f11, f12, f21, f22, f31, f32, f41, f42,
207                  f13, f14, f23, f24, f33, f34, f43, f44;
208       index_type i, j, l, ii, jj, ll;
209       index_type isec, jsec, lsec, uisec, ujsec, ulsec;
211       a = abase;
212       b = bbase;
213       c = retarray->base_addr;
215       /* Parameter adjustments */
216       c_dim1 = rystride;
217       c_offset = 1 + c_dim1;
218       c -= c_offset;
219       a_dim1 = aystride;
220       a_offset = 1 + a_dim1;
221       a -= a_offset;
222       b_dim1 = bystride;
223       b_offset = 1 + b_dim1;
224       b -= b_offset;
226       /* Early exit if possible */
227       if (m == 0 || n == 0 || k == 0)
228         return;
230       /* Empty c first.  */
231       for (j=1; j<=n; j++)
232         for (i=1; i<=m; i++)
233           c[i + j * c_dim1] = ('rtype_name`)0;
235       /* Start turning the crank. */
236       i1 = n;
237       for (jj = 1; jj <= i1; jj += 512)
238         {
239           /* Computing MIN */
240           i2 = 512;
241           i3 = n - jj + 1;
242           jsec = min(i2,i3);
243           ujsec = jsec - jsec % 4;
244           i2 = k;
245           for (ll = 1; ll <= i2; ll += 256)
246             {
247               /* Computing MIN */
248               i3 = 256;
249               i4 = k - ll + 1;
250               lsec = min(i3,i4);
251               ulsec = lsec - lsec % 2;
253               i3 = m;
254               for (ii = 1; ii <= i3; ii += 256)
255                 {
256                   /* Computing MIN */
257                   i4 = 256;
258                   i5 = m - ii + 1;
259                   isec = min(i4,i5);
260                   uisec = isec - isec % 2;
261                   i4 = ll + ulsec - 1;
262                   for (l = ll; l <= i4; l += 2)
263                     {
264                       i5 = ii + uisec - 1;
265                       for (i = ii; i <= i5; i += 2)
266                         {
267                           t1[l - ll + 1 + ((i - ii + 1) << 8) - 257] =
268                                         a[i + l * a_dim1];
269                           t1[l - ll + 2 + ((i - ii + 1) << 8) - 257] =
270                                         a[i + (l + 1) * a_dim1];
271                           t1[l - ll + 1 + ((i - ii + 2) << 8) - 257] =
272                                         a[i + 1 + l * a_dim1];
273                           t1[l - ll + 2 + ((i - ii + 2) << 8) - 257] =
274                                         a[i + 1 + (l + 1) * a_dim1];
275                         }
276                       if (uisec < isec)
277                         {
278                           t1[l - ll + 1 + (isec << 8) - 257] =
279                                     a[ii + isec - 1 + l * a_dim1];
280                           t1[l - ll + 2 + (isec << 8) - 257] =
281                                     a[ii + isec - 1 + (l + 1) * a_dim1];
282                         }
283                     }
284                   if (ulsec < lsec)
285                     {
286                       i4 = ii + isec - 1;
287                       for (i = ii; i<= i4; ++i)
288                         {
289                           t1[lsec + ((i - ii + 1) << 8) - 257] =
290                                     a[i + (ll + lsec - 1) * a_dim1];
291                         }
292                     }
294                   uisec = isec - isec % 4;
295                   i4 = jj + ujsec - 1;
296                   for (j = jj; j <= i4; j += 4)
297                     {
298                       i5 = ii + uisec - 1;
299                       for (i = ii; i <= i5; i += 4)
300                         {
301                           f11 = c[i + j * c_dim1];
302                           f21 = c[i + 1 + j * c_dim1];
303                           f12 = c[i + (j + 1) * c_dim1];
304                           f22 = c[i + 1 + (j + 1) * c_dim1];
305                           f13 = c[i + (j + 2) * c_dim1];
306                           f23 = c[i + 1 + (j + 2) * c_dim1];
307                           f14 = c[i + (j + 3) * c_dim1];
308                           f24 = c[i + 1 + (j + 3) * c_dim1];
309                           f31 = c[i + 2 + j * c_dim1];
310                           f41 = c[i + 3 + j * c_dim1];
311                           f32 = c[i + 2 + (j + 1) * c_dim1];
312                           f42 = c[i + 3 + (j + 1) * c_dim1];
313                           f33 = c[i + 2 + (j + 2) * c_dim1];
314                           f43 = c[i + 3 + (j + 2) * c_dim1];
315                           f34 = c[i + 2 + (j + 3) * c_dim1];
316                           f44 = c[i + 3 + (j + 3) * c_dim1];
317                           i6 = ll + lsec - 1;
318                           for (l = ll; l <= i6; ++l)
319                             {
320                               f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
321                                       * b[l + j * b_dim1];
322                               f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
323                                       * b[l + j * b_dim1];
324                               f12 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
325                                       * b[l + (j + 1) * b_dim1];
326                               f22 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
327                                       * b[l + (j + 1) * b_dim1];
328                               f13 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
329                                       * b[l + (j + 2) * b_dim1];
330                               f23 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
331                                       * b[l + (j + 2) * b_dim1];
332                               f14 += t1[l - ll + 1 + ((i - ii + 1) << 8) - 257]
333                                       * b[l + (j + 3) * b_dim1];
334                               f24 += t1[l - ll + 1 + ((i - ii + 2) << 8) - 257]
335                                       * b[l + (j + 3) * b_dim1];
336                               f31 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
337                                       * b[l + j * b_dim1];
338                               f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
339                                       * b[l + j * b_dim1];
340                               f32 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
341                                       * b[l + (j + 1) * b_dim1];
342                               f42 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
343                                       * b[l + (j + 1) * b_dim1];
344                               f33 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
345                                       * b[l + (j + 2) * b_dim1];
346                               f43 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
347                                       * b[l + (j + 2) * b_dim1];
348                               f34 += t1[l - ll + 1 + ((i - ii + 3) << 8) - 257]
349                                       * b[l + (j + 3) * b_dim1];
350                               f44 += t1[l - ll + 1 + ((i - ii + 4) << 8) - 257]
351                                       * b[l + (j + 3) * b_dim1];
352                             }
353                           c[i + j * c_dim1] = f11;
354                           c[i + 1 + j * c_dim1] = f21;
355                           c[i + (j + 1) * c_dim1] = f12;
356                           c[i + 1 + (j + 1) * c_dim1] = f22;
357                           c[i + (j + 2) * c_dim1] = f13;
358                           c[i + 1 + (j + 2) * c_dim1] = f23;
359                           c[i + (j + 3) * c_dim1] = f14;
360                           c[i + 1 + (j + 3) * c_dim1] = f24;
361                           c[i + 2 + j * c_dim1] = f31;
362                           c[i + 3 + j * c_dim1] = f41;
363                           c[i + 2 + (j + 1) * c_dim1] = f32;
364                           c[i + 3 + (j + 1) * c_dim1] = f42;
365                           c[i + 2 + (j + 2) * c_dim1] = f33;
366                           c[i + 3 + (j + 2) * c_dim1] = f43;
367                           c[i + 2 + (j + 3) * c_dim1] = f34;
368                           c[i + 3 + (j + 3) * c_dim1] = f44;
369                         }
370                       if (uisec < isec)
371                         {
372                           i5 = ii + isec - 1;
373                           for (i = ii + uisec; i <= i5; ++i)
374                             {
375                               f11 = c[i + j * c_dim1];
376                               f12 = c[i + (j + 1) * c_dim1];
377                               f13 = c[i + (j + 2) * c_dim1];
378                               f14 = c[i + (j + 3) * c_dim1];
379                               i6 = ll + lsec - 1;
380                               for (l = ll; l <= i6; ++l)
381                                 {
382                                   f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
383                                           257] * b[l + j * b_dim1];
384                                   f12 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
385                                           257] * b[l + (j + 1) * b_dim1];
386                                   f13 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
387                                           257] * b[l + (j + 2) * b_dim1];
388                                   f14 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
389                                           257] * b[l + (j + 3) * b_dim1];
390                                 }
391                               c[i + j * c_dim1] = f11;
392                               c[i + (j + 1) * c_dim1] = f12;
393                               c[i + (j + 2) * c_dim1] = f13;
394                               c[i + (j + 3) * c_dim1] = f14;
395                             }
396                         }
397                     }
398                   if (ujsec < jsec)
399                     {
400                       i4 = jj + jsec - 1;
401                       for (j = jj + ujsec; j <= i4; ++j)
402                         {
403                           i5 = ii + uisec - 1;
404                           for (i = ii; i <= i5; i += 4)
405                             {
406                               f11 = c[i + j * c_dim1];
407                               f21 = c[i + 1 + j * c_dim1];
408                               f31 = c[i + 2 + j * c_dim1];
409                               f41 = c[i + 3 + j * c_dim1];
410                               i6 = ll + lsec - 1;
411                               for (l = ll; l <= i6; ++l)
412                                 {
413                                   f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
414                                           257] * b[l + j * b_dim1];
415                                   f21 += t1[l - ll + 1 + ((i - ii + 2) << 8) -
416                                           257] * b[l + j * b_dim1];
417                                   f31 += t1[l - ll + 1 + ((i - ii + 3) << 8) -
418                                           257] * b[l + j * b_dim1];
419                                   f41 += t1[l - ll + 1 + ((i - ii + 4) << 8) -
420                                           257] * b[l + j * b_dim1];
421                                 }
422                               c[i + j * c_dim1] = f11;
423                               c[i + 1 + j * c_dim1] = f21;
424                               c[i + 2 + j * c_dim1] = f31;
425                               c[i + 3 + j * c_dim1] = f41;
426                             }
427                           i5 = ii + isec - 1;
428                           for (i = ii + uisec; i <= i5; ++i)
429                             {
430                               f11 = c[i + j * c_dim1];
431                               i6 = ll + lsec - 1;
432                               for (l = ll; l <= i6; ++l)
433                                 {
434                                   f11 += t1[l - ll + 1 + ((i - ii + 1) << 8) -
435                                           257] * b[l + j * b_dim1];
436                                 }
437                               c[i + j * c_dim1] = f11;
438                             }
439                         }
440                     }
441                 }
442             }
443         }
444       return;
445     }
446   else if (rxstride == 1 && aystride == 1 && bxstride == 1)
447     {
448       if (GFC_DESCRIPTOR_RANK (a) != 1)
449         {
450           const 'rtype_name` *restrict abase_x;
451           const 'rtype_name` *restrict bbase_y;
452           'rtype_name` *restrict dest_y;
453           'rtype_name` s;
455           for (y = 0; y < ycount; y++)
456             {
457               bbase_y = &bbase[y*bystride];
458               dest_y = &dest[y*rystride];
459               for (x = 0; x < xcount; x++)
460                 {
461                   abase_x = &abase[x*axstride];
462                   s = ('rtype_name`) 0;
463                   for (n = 0; n < count; n++)
464                     s += abase_x[n] * bbase_y[n];
465                   dest_y[x] = s;
466                 }
467             }
468         }
469       else
470         {
471           const 'rtype_name` *restrict bbase_y;
472           'rtype_name` s;
474           for (y = 0; y < ycount; y++)
475             {
476               bbase_y = &bbase[y*bystride];
477               s = ('rtype_name`) 0;
478               for (n = 0; n < count; n++)
479                 s += abase[n*axstride] * bbase_y[n];
480               dest[y*rystride] = s;
481             }
482         }
483     }
484   else if (axstride < aystride)
485     {
486       for (y = 0; y < ycount; y++)
487         for (x = 0; x < xcount; x++)
488           dest[x*rxstride + y*rystride] = ('rtype_name`)0;
490       for (y = 0; y < ycount; y++)
491         for (n = 0; n < count; n++)
492           for (x = 0; x < xcount; x++)
493             /* dest[x,y] += a[x,n] * b[n,y] */
494             dest[x*rxstride + y*rystride] +=
495                                         abase[x*axstride + n*aystride] *
496                                         bbase[n*bxstride + y*bystride];
497     }
498   else if (GFC_DESCRIPTOR_RANK (a) == 1)
499     {
500       const 'rtype_name` *restrict bbase_y;
501       'rtype_name` s;
503       for (y = 0; y < ycount; y++)
504         {
505           bbase_y = &bbase[y*bystride];
506           s = ('rtype_name`) 0;
507           for (n = 0; n < count; n++)
508             s += abase[n*axstride] * bbase_y[n*bxstride];
509           dest[y*rxstride] = s;
510         }
511     }
512   else
513     {
514       const 'rtype_name` *restrict abase_x;
515       const 'rtype_name` *restrict bbase_y;
516       'rtype_name` *restrict dest_y;
517       'rtype_name` s;
519       for (y = 0; y < ycount; y++)
520         {
521           bbase_y = &bbase[y*bystride];
522           dest_y = &dest[y*rystride];
523           for (x = 0; x < xcount; x++)
524             {
525               abase_x = &abase[x*axstride];
526               s = ('rtype_name`) 0;
527               for (n = 0; n < count; n++)
528                 s += abase_x[n*aystride] * bbase_y[n*bxstride];
529               dest_y[x*rxstride] = s;
530             }
531         }
532     }
534 #undef POW3
535 #undef min
536 #undef max