1// Copyright 2020 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 VARIANT in ["LD128", "EXTENDED"]
7$assert MR <= 4
8#include <assert.h>
9
10#include <immintrin.h>
11
12#include <xnnpack/gemm.h>
13#include <xnnpack/intrinsics-polyfill.h>
14#include <xnnpack/math.h>
15
16
17$GEMM_SUFFIX = "_xw" if VARIANT == "EXTENDED" else ""
18void xnn_qs8_gemm${GEMM_SUFFIX}_minmax_ukernel_${MR}x8c8__avx2(
19    size_t mr,
20    size_t nc,
21    size_t kc,
22    const int8_t* restrict a,
23    size_t a_stride,
24    const void* restrict w,
25    int8_t* restrict c,
26    size_t cm_stride,
27    size_t cn_stride,
28    const union xnn_qs8_gemm${GEMM_SUFFIX}_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
29{
30  assert(mr != 0);
31  assert(mr <= ${MR});
32  assert(nc != 0);
33  assert(kc != 0);
34  assert(kc % sizeof(int8_t) == 0);
35  assert(a != NULL);
36  assert(w != NULL);
37  assert(c != NULL);
38
39  kc = round_up_po2(kc, 8);
40  const int8_t* a0 = a;
41  int8_t* c0 = c;
42  $for M in range(1, MR):
43    const int8_t* a${M} = (const int8_t*) ((uintptr_t) a${M-1} + a_stride);
44    int8_t* c${M} = (int8_t*) ((uintptr_t) c${M-1} + cm_stride);
45    $if M % 2 == 0:
46      if XNN_UNPREDICTABLE(mr <= ${M}) {
47        a${M} = a${M-1};
48        c${M} = c${M-1};
49      }
50    $elif M + 1 == MR:
51      if XNN_UNPREDICTABLE(mr != ${M+1}) {
52        a${M} = a${M-1};
53        c${M} = c${M-1};
54      }
55    $else:
56      if XNN_UNPREDICTABLE(mr < ${M+1}) {
57        a${M} = a${M-1};
58        c${M} = c${M-1};
59      }
60
61  do {
62    const __m128i vbias0x0 = _mm_loadu_si32(w);
63    const __m128i vbias0x1 = _mm_loadu_si32((const void*) ((uintptr_t) w + sizeof(int32_t)));
64    __m256i vacc0x01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x0), vbias0x1, 1);
65    $for N in range(2, 8, 2):
66      const __m128i vbias0x${N} = _mm_loadu_si32((const void*) ((uintptr_t) w + ${N} * sizeof(int32_t)));
67      const __m128i vbias0x${N+1} = _mm_loadu_si32((const void*) ((uintptr_t) w + ${N+1} * sizeof(int32_t)));
68      __m256i vacc0x${N}${N+1} = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x${N}), vbias0x${N+1}, 1);
69    $for M in range(1, MR):
70      $for N in range(0, 8, 2):
71        __m256i vacc${M}x${N}${N+1} = vacc0x${N}${N+1};
72    w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t));
73
74    size_t k = 0;
75    while (k < kc) {
76      $for M in range(MR):
77        const __m128i va${M} = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a${M}));
78        const __m256i vxa${M} = _mm256_cvtepi8_epi16(va${M});
79        a${M} += 8;
80
81      $for N in range(0, 8, 2):
82        $if VARIANT == "EXTENDED":
83          $if N == 0:
84            const __m256i vxb${N}${N+1} = _mm256_load_si256((const __m256i*) w);
85          $else:
86            const __m256i vxb${N}${N+1} = _mm256_load_si256((const __m256i*) ((uintptr_t) w + ${N * 8} * sizeof(int16_t)));
87        $else:
88          $if N == 0:
89            const __m128i vb${N}${N+1} = _mm_load_si128((const __m128i*) w);
90          $else:
91            const __m128i vb${N}${N+1} = _mm_load_si128((const __m128i*) ((uintptr_t) w + ${N * 8} * sizeof(int8_t)));
92          const __m256i vxb${N}${N+1} = _mm256_cvtepi8_epi16(vb${N}${N+1});
93
94        $for M in range(MR):
95          vacc${M}x${N}${N+1} = _mm256_add_epi32(vacc${M}x${N}${N+1}, _mm256_madd_epi16(vxa${M}, vxb${N}${N+1}));
96
97      $if VARIANT == "EXTENDED":
98        w = (const void*) ((uintptr_t) w + 64 * sizeof(int16_t));
99      $else:
100        w = (const void*) ((uintptr_t) w + 64 * sizeof(int8_t));
101      k += 8 * sizeof(int8_t);
102    }
103
104    $for M in range(MR):
105      const __m256i vacc${M}x0213 = _mm256_hadd_epi32(vacc${M}x01, vacc${M}x23);
106      const __m256i vacc${M}x4657 = _mm256_hadd_epi32(vacc${M}x45, vacc${M}x67);
107
108    $for M in range(MR):
109      const __m256i vacc${M}x02461357 = _mm256_hadd_epi32(vacc${M}x0213, vacc${M}x4657);
110
111    const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
112    $for M in range(MR):
113      __m256i vacc${M}x01234567 = _mm256_permutevar8x32_epi32(vacc${M}x02461357, vpermute_mask);
114
115    const __m256i vmultiplier = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.multiplier));
116    const __m256i vrounding = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.rounding));
117
118    $for M in range(MR):
119      const __m256i vacc${M}x11335577 = _mm256_shuffle_epi32(vacc${M}x01234567, _MM_SHUFFLE(3, 3, 1, 1));
120
121    $for M in range(MR):
122      const __m256i vprod${M}x0246 = _mm256_add_epi64(_mm256_mul_epi32(vacc${M}x01234567, vmultiplier), vrounding);
123
124    $for M in range(MR):
125      const __m256i vprod${M}x1357 = _mm256_add_epi64(_mm256_mul_epi32(vacc${M}x11335577, vmultiplier), vrounding);
126
127    $for M in range(MR):
128      const __m256i vq31prod${M}x0246 = _mm256_srli_epi64(vprod${M}x0246, 31);
129      const __m256i vq31prod${M}x1357 = _mm256_add_epi64(vprod${M}x1357, vprod${M}x1357);
130
131    $for M in range(MR):
132      const __m256i vq31prod${M}x01234567 = _mm256_blend_epi16(vq31prod${M}x0246, vq31prod${M}x1357, 0xCC);
133
134    const __m256i vremainder_mask = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_mask));
135    $for M in range(MR):
136      const __m256i vrem${M}x01234567 =
137        _mm256_add_epi32(_mm256_and_si256(vq31prod${M}x01234567, vremainder_mask), _mm256_cmpgt_epi32(_mm256_setzero_si256(), vq31prod${M}x01234567));
138
139    const __m256i vremainder_threshold = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_threshold));
140    const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
141    $for M in range(MR):
142      vacc${M}x01234567 =
143        _mm256_sub_epi32(_mm256_sra_epi32(vq31prod${M}x01234567, vshift), _mm256_cmpgt_epi32(vrem${M}x01234567, vremainder_threshold));
144
145    const __m256i voutput_zero_point = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_zero_point));
146    $for M in range(0, MR, 2):
147      __m256i vacc${M}${min(M+1, MR-1)}x01234567 = _mm256_adds_epi16(_mm256_packs_epi32(vacc${M}x01234567, vacc${min(M+1, MR-1)}x01234567), voutput_zero_point);
148
149    $for M in range(0, MR, 2):
150      vacc${M}${min(M+1, MR-1)}x01234567 = _mm256_permute4x64_epi64(vacc${M}${min(M+1, MR-1)}x01234567, _MM_SHUFFLE(3, 1, 2, 0));
151
152    const __m256i voutput_min = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_min));
153    const __m256i voutput_max = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_max));
154    $for M in range(0, MR, 2):
155      vacc${M}${min(M+1, MR-1)}x01234567 = _mm256_min_epi16(_mm256_max_epi16(vacc${M}${min(M+1, MR-1)}x01234567, voutput_min), voutput_max);
156
157    $if MR > 2:
158      __m256i vout = _mm256_packs_epi16(vacc0${min(1, MR-1)}x01234567, vacc${min(2, MR-1)}${min(3, MR-1)}x01234567);
159    $else:
160      __m256i vout = _mm256_packs_epi16(vacc0${min(1, MR-1)}x01234567, vacc0${min(1, MR-1)}x01234567);
161    __m128i vout_lo = _mm256_castsi256_si128(vout);
162    __m128i vout_hi = _mm256_extracti128_si256(vout, 1);
163
164    if (nc >= 8) {
165      _mm_storel_epi64((__m128i*) c0, vout_lo);
166      $if MR > 1:
167        _mm_storel_epi64((__m128i*) c1, vout_hi);
168      $if MR > 2:
169        _mm_storeh_pi((__m64*) c2, _mm_castsi128_ps(vout_lo));
170      $if MR > 3:
171        _mm_storeh_pi((__m64*) c3, _mm_castsi128_ps(vout_hi));
172
173      $for M in range(MR):
174        c${M} = (int8_t*) ((uintptr_t) c${M} + cn_stride);
175
176      $for M in range(MR):
177        a${M} = (const int8_t*) ((uintptr_t) a${M} - kc);
178
179      nc -= 8;
180    } else {
181      if (nc & 4) {
182        _mm_storeu_si32(c0, vout_lo);
183        $if MR > 1:
184          _mm_storeu_si32(c1, vout_hi);
185        $if MR > 2:
186          *((uint32_t*) c2) = (uint32_t) _mm_extract_epi32(vout_lo, 2);
187        $if MR > 3:
188          *((uint32_t*) c3) = (uint32_t) _mm_extract_epi32(vout_hi, 2);
189
190        $for M in range(MR):
191          c${M} += 4;
192
193        vout_lo = _mm_srli_epi64(vout_lo, 32);
194        vout_hi = _mm_srli_epi64(vout_hi, 32);
195      }
196      if (nc & 2) {
197        *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout_lo, 0);
198        $if MR > 1:
199          *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout_hi, 0);
200        $if MR > 2:
201          *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout_lo, 4);
202        $if MR > 3:
203          *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout_hi, 4);
204
205        $for M in range(MR):
206          c${M} += 2;
207
208        vout_lo = _mm_srli_epi32(vout_lo, 16);
209        vout_hi = _mm_srli_epi32(vout_hi, 16);
210      }
211      if (nc & 1) {
212        *c0 = (int8_t) _mm_extract_epi8(vout_lo, 0);
213        $if MR > 1:
214          *c1 = (uint8_t) _mm_extract_epi8(vout_hi, 0);
215        $if MR > 2:
216          *c2 = (uint8_t) _mm_extract_epi8(vout_lo, 8);
217        $if MR > 3:
218          *c3 = (uint8_t) _mm_extract_epi8(vout_hi, 8);
219      }
220
221      nc = 0;
222    }
223  } while (nc != 0);
224}
225