• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6$assert NR % 8 == 0
7$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8#include <assert.h>
9
10#include <immintrin.h>
11
12#include <xnnpack/igemm.h>
13
14
15$ISA = {0: "avx", 3: "fma3"}[FMA]
16void xnn_f32_igemm_ukernel_${MR}x${NR}s4__${ISA}_broadcast(
17    size_t mr,
18    size_t nc,
19    size_t kc,
20    size_t ks,
21    const float**restrict a,
22    const float*restrict w,
23    float*restrict c,
24    size_t cm_stride,
25    size_t cn_stride,
26    size_t a_offset,
27    const float* zero,
28    const union xnn_f32_output_params params[restrict static 1])
29{
30  assert(mr != 0);
31  assert(mr <= ${MR});
32  assert(nc != 0);
33  assert(kc != 0);
34  assert(kc % sizeof(float) == 0);
35  assert(ks != 0);
36  assert(ks % (${MR} * sizeof(void*)) == 0);
37  assert(a_offset % sizeof(float) == 0);
38  assert(a != NULL);
39  assert(w != NULL);
40  assert(c != NULL);
41
42  float* c0 = c;
43  $for M in range(1, MR):
44    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
45    $if M % 2 == 0:
46      if XNN_UNPREDICTABLE(mr <= ${M}) {
47        c${M} = c${M-1};
48      }
49    $elif M + 1 == MR:
50      if XNN_UNPREDICTABLE(mr != ${M+1}) {
51        c${M} = c${M-1};
52      }
53    $else:
54      if XNN_UNPREDICTABLE(mr < ${M+1}) {
55        c${M} = c${M-1};
56      }
57
58  do {
59    __m256 vacc0x${ABC[0:8]} = _mm256_load_ps(w);
60    $for N in range(8, NR, 8):
61      __m256 vacc0x${ABC[N:N+8]} = _mm256_load_ps(w + ${N});
62    $for M in range(1, MR):
63      $for N in range(0, NR, 8):
64        __m256 vacc${M}x${ABC[N:N+8]} = vacc0x${ABC[N:N+8]};
65    w += ${NR};
66
67    size_t p = ks;
68    do {
69      $for M in range(MR):
70        const float* restrict a${M} = a[${M}];
71        assert(a${M} != NULL);
72        if XNN_UNPREDICTABLE(a${M} != zero) {
73          a${M} = (const float*) ((uintptr_t) a${M} + a_offset);
74        }
75      a += ${MR};
76
77      size_t k = kc;
78      while (k >= 4 * sizeof(float)) {
79        $for M in range(MR):
80          __m256 va${M} = _mm256_broadcast_ps((const __m128*) a${M});
81          a${M} += 4;
82
83        $for L in range(4):
84
85          $for N in range(0, NR, 8):
86            const __m256 vb${ABC[N:N+8]}c${L} = _mm256_load_ps(w + ${L * NR + N});
87
88          $for N in range(0, NR, 8):
89            $for M in range(MR):
90              $if FMA == 3:
91                vacc${M}x${ABC[N:N+8]} = _mm256_fmadd_ps(va${M}, vb${ABC[N:N+8]}c${L}, vacc${M}x${ABC[N:N+8]});
92              $else:
93                vacc${M}x${ABC[N:N+8]} = _mm_add_ps(vacc${M}x${ABC[N:N+8]}, _mm_mul_ps(va${M}, vb${ABC[N:N+8]}c${L}));
94
95          $if L + 1 != 4:
96            $for M in range(MR):
97              va${M} = _mm256_permute_ps(va${M}, _MM_SHUFFLE(0, 3, 2, 1));
98
99        w += ${4 * NR};
100        k -= 4 * sizeof(float);
101      }
102      if XNN_UNLIKELY(k != 0) {
103        do {
104          $for M in range(MR):
105            const __m256 va${M} = _mm256_broadcast_ss(a${M});
106            a${M} += 1;
107
108          const __m256 vb${ABC[0:8]} = _mm256_load_ps(w);
109          $for N in range(8, NR, 8):
110            const __m256 vb${ABC[N:N+8]} = _mm256_load_ps(w + ${N});
111          w += ${NR};
112
113          $for N in range(0, NR, 8):
114            $for M in range(MR):
115              $if FMA == 3:
116                vacc${M}x${ABC[N:N+8]} = _mm256_fmadd_ps(va${M}, vb${ABC[N:N+8]}, vacc${M}x${ABC[N:N+8]});
117              $else:
118                vacc${M}x${ABC[N:N+8]} = _mm256_add_ps(vacc${M}x${ABC[N:N+8]}, _mm256_mul_ps(va${M}, vb${ABC[N:N+8]}));
119
120          k -= sizeof(float);
121        } while (k != 0);
122      }
123      p -= ${MR} * sizeof(void*);
124    } while (p != 0);
125
126    const __m256 vmax = _mm256_broadcast_ps((const __m128*) params->sse.max);
127    $for N in range(0, NR, 8):
128      $for M in range(MR):
129        vacc${M}x${ABC[N:N+8]} = _mm256_min_ps(vacc${M}x${ABC[N:N+8]}, vmax);
130
131    const __m256 vmin = _mm256_broadcast_ps((const __m128*) params->sse.min);
132    $for N in range(0, NR, 8):
133      $for M in range(MR):
134        vacc${M}x${ABC[N:N+8]} = _mm256_max_ps(vacc${M}x${ABC[N:N+8]}, vmin);
135
136    if XNN_LIKELY(nc >= ${NR}) {
137      $for M in reversed(range(MR)):
138        _mm256_storeu_ps(c${M}, vacc${M}x${ABC[0:8]});
139        $for N in range(8, NR, 8):
140          _mm256_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+8]});
141        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
142
143      a = (const float**restrict) ((uintptr_t) a - ks);
144      nc -= ${NR};
145    } else {
146      $for LOG2N in reversed(range(NR.bit_length())):
147        $if NR != 1 << LOG2N:
148          if (nc & ${1 << LOG2N}) {
149            $if LOG2N >= 3:
150              $for M in reversed(range(MR)):
151                _mm256_storeu_ps(c${M}, vacc${M}x${ABC[0:8]});
152                $for N in range(8, 1 << LOG2N, 8):
153                  _mm256_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+8]});
154
155              $for M in reversed(range(MR)):
156                $for N in range(0, 1 << (LOG2N - 1), 8):
157                  vacc${M}x${ABC[N:N+8]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+8]};
158
159              $for M in reversed(range(MR)):
160                c${M} += ${1 << LOG2N};
161            $elif LOG2N == 2:
162              $for M in reversed(range(MR)):
163                _mm_storeu_ps(c${M}, vacc${M}x${ABC[0:4]});
164
165              $for M in reversed(range(MR)):
166                vacc${M}x${ABC[0:4]} = _mm256_extractf128_ps(vacc${M}x${ABC[0:8]}, 1);
167
168              $for M in reversed(range(MR)):
169                c${M} += 4;
170            $elif LOG2N == 1:
171              $for M in reversed(range(MR)):
172                _mm_storel_pi((__m64*) c${M}, vacc${M}x${ABC[0:4]});
173
174              $for M in reversed(range(MR)):
175                vacc${M}x${ABC[0:4]} = _mm_movehl_ps(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]});
176
177              $for M in reversed(range(MR)):
178                c${M} += 2;
179            $elif LOG2N == 0:
180              $for M in reversed(range(MR)):
181                _mm_store_ss(c${M}, vacc${M}x${ABC[0:4]});
182          }
183        $if LOG2N == 3:
184          $for M in reversed(range(MR)):
185            __m128 vacc${M}x${ABC[0:4]} = _mm256_castps256_ps128(vacc${M}x${ABC[0:8]});
186
187      nc = 0;
188    }
189  } while (nc != 0);
190}
191