• 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}__${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      do {
79        const __m256 vb${ABC[0:8]} = _mm256_load_ps(w);
80        $for N in range(8, NR, 8):
81          const __m256 vb${ABC[N:N+8]} = _mm256_load_ps(w + ${N});
82        w += ${NR};
83
84        $for M in range(MR):
85          const __m256 va${M} = _mm256_broadcast_ss(a${M});
86          a${M} += 1;
87
88        $for M in range(MR):
89          $for N in range(0, NR, 8):
90            $if FMA == 3:
91              vacc${M}x${ABC[N:N+8]} = _mm256_fmadd_ps(va${M}, vb${ABC[N:N+8]}, vacc${M}x${ABC[N:N+8]});
92            $else:
93              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]}));
94        k -= sizeof(float);
95      } while (k != 0);
96      p -= ${MR} * sizeof(void*);
97    } while (p != 0);
98
99    const __m256 vmax = _mm256_broadcast_ps((const __m128*) params->sse.max);
100    $for N in range(0, NR, 8):
101      $for M in range(MR):
102        vacc${M}x${ABC[N:N+8]} = _mm256_min_ps(vacc${M}x${ABC[N:N+8]}, vmax);
103
104    const __m256 vmin = _mm256_broadcast_ps((const __m128*) params->sse.min);
105    $for N in range(0, NR, 8):
106      $for M in range(MR):
107        vacc${M}x${ABC[N:N+8]} = _mm256_max_ps(vacc${M}x${ABC[N:N+8]}, vmin);
108
109    if XNN_LIKELY(nc >= ${NR}) {
110      $for M in reversed(range(MR)):
111        _mm256_storeu_ps(c${M}, vacc${M}x${ABC[0:8]});
112        $for N in range(8, NR, 8):
113          _mm256_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+8]});
114        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
115
116      a = (const float**restrict) ((uintptr_t) a - ks);
117      nc -= ${NR};
118    } else {
119      $for LOG2N in reversed(range(NR.bit_length())):
120        $if NR != 1 << LOG2N:
121          if (nc & ${1 << LOG2N}) {
122            $if LOG2N >= 3:
123              $for M in reversed(range(MR)):
124                _mm256_storeu_ps(c${M}, vacc${M}x${ABC[0:8]});
125                $for N in range(8, 1 << LOG2N, 8):
126                  _mm256_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+8]});
127
128              $for M in reversed(range(MR)):
129                $for N in range(0, 1 << (LOG2N - 1), 8):
130                  vacc${M}x${ABC[N:N+8]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+8]};
131
132              $for M in reversed(range(MR)):
133                c${M} += ${1 << LOG2N};
134            $elif LOG2N == 2:
135              $for M in reversed(range(MR)):
136                _mm_storeu_ps(c${M}, vacc${M}x${ABC[0:4]});
137
138              $for M in reversed(range(MR)):
139                vacc${M}x${ABC[0:4]} = _mm256_extractf128_ps(vacc${M}x${ABC[0:8]}, 1);
140
141              $for M in reversed(range(MR)):
142                c${M} += 4;
143            $elif LOG2N == 1:
144              $for M in reversed(range(MR)):
145                _mm_storel_pi((__m64*) c${M}, vacc${M}x${ABC[0:4]});
146
147              $for M in reversed(range(MR)):
148                vacc${M}x${ABC[0:4]} = _mm_movehl_ps(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]});
149
150              $for M in reversed(range(MR)):
151                c${M} += 2;
152            $elif LOG2N == 0:
153              $for M in reversed(range(MR)):
154                _mm_store_ss(c${M}, vacc${M}x${ABC[0:4]});
155          }
156        $if LOG2N == 3:
157          $for M in reversed(range(MR)):
158            __m128 vacc${M}x${ABC[0:4]} = _mm256_castps256_ps128(vacc${M}x${ABC[0:8]});
159
160      nc = 0;
161    }
162  } while (nc != 0);
163}
164