1 `/* Implementation of the MATMUL intrinsic
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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"
33 `#if defined (HAVE_'rtype_name`)
35 /* Prototype for the BLAS ?gemm subroutine, a pointer to which can be
36 passed to us by the front-end, in which case we call it for large
39 typedef void (*blas_call)(const char *, const char *, const int *, const int *,
40 const int *, const 'rtype_name` *, const 'rtype_name` *,
41 const int *, const 'rtype_name` *, const int *,
42 const 'rtype_name` *, 'rtype_name` *, const int *,
45 /* The order of loops is different in the case of plain matrix
46 multiplication C=MATMUL(A,B), and in the frequent special case where
47 the argument A is the temporary result of a TRANSPOSE intrinsic:
48 C=MATMUL(TRANSPOSE(A),B). Transposed temporaries are detected by
49 looking at their strides.
51 The equivalent Fortran pseudo-code is:
53 DIMENSION A(M,COUNT), B(COUNT,N), C(M,N)
54 IF (.NOT.IS_TRANSPOSED(A)) THEN
59 C(I,J) = C(I,J)+A(I,K)*B(K,J)
70 /* If try_blas is set to a nonzero value, then the matmul function will
71 see if there is a way to perform the matrix multiplication by a call
72 to the BLAS gemm function. */
74 extern void matmul_'rtype_code` ('rtype` * const restrict retarray,
75 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
76 int blas_limit, blas_call gemm);
77 export_proto(matmul_'rtype_code`);
79 'ifelse(rtype_letter,`r',dnl
80 `#if defined(HAVE_AVX) && defined(HAVE_AVX2)
81 /* REAL types generate identical code for AVX and AVX2. Only generate
82 an AVX2 function if we are dealing with integer. */
87 /* Put exhaustive list of possible architectures here here, ORed together. */
89 #if defined(HAVE_AVX) || defined(HAVE_AVX2) || defined(HAVE_AVX512F)
92 'define(`matmul_name',`matmul_'rtype_code`_avx')dnl
94 'matmul_name` ('rtype` * const restrict retarray,
95 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
96 int blas_limit, blas_call gemm) __attribute__((__target__("avx")));
97 static' include(matmul_internal.m4)dnl
98 `#endif /* HAVE_AVX */
101 'define(`matmul_name',`matmul_'rtype_code`_avx2')dnl
103 'matmul_name` ('rtype` * const restrict retarray,
104 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
105 int blas_limit, blas_call gemm) __attribute__((__target__("avx2")));
106 static' include(matmul_internal.m4)dnl
107 `#endif /* HAVE_AVX2 */
110 'define(`matmul_name',`matmul_'rtype_code`_avx512f')dnl
112 'matmul_name` ('rtype` * const restrict retarray,
113 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
114 int blas_limit, blas_call gemm) __attribute__((__target__("avx512f")));
115 static' include(matmul_internal.m4)dnl
116 `#endif /* HAVE_AVX512F */
118 /* Function to fall back to if there is no special processor-specific version. */
119 'define(`matmul_name',`matmul_'rtype_code`_vanilla')dnl
120 `static' include(matmul_internal.m4)dnl
122 `/* Compiling main function, with selection code for the processor. */
124 /* Currently, this is i386 only. Adjust for other architectures. */
126 #include <config/i386/cpuinfo.h>
127 void matmul_'rtype_code` ('rtype` * const restrict retarray,
128 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
129 int blas_limit, blas_call gemm)
131 static void (*matmul_p) ('rtype` * const restrict retarray,
132 'rtype` * const restrict a, 'rtype` * const restrict b, int try_blas,
133 int blas_limit, blas_call gemm) = NULL;
135 if (matmul_p == NULL)
137 matmul_p = matmul_'rtype_code`_vanilla;
138 if (__cpu_model.__cpu_vendor == VENDOR_INTEL)
140 /* Run down the available processors in order of preference. */
142 if (__cpu_model.__cpu_features[0] & (1 << FEATURE_AVX512F))
144 matmul_p = matmul_'rtype_code`_avx512f;
148 #endif /* HAVE_AVX512F */
151 if (__cpu_model.__cpu_features[0] & (1 << FEATURE_AVX2))
153 matmul_p = matmul_'rtype_code`_avx2;
160 if (__cpu_model.__cpu_features[0] & (1 << FEATURE_AVX))
162 matmul_p = matmul_'rtype_code`_avx;
165 #endif /* HAVE_AVX */
170 (*matmul_p) (retarray, a, b, try_blas, blas_limit, gemm);
173 #else /* Just the vanilla function. */
175 'define(`matmul_name',`matmul_'rtype_code)dnl
176 define(`target_attribute',`')dnl
177 include(matmul_internal.m4)dnl