blob: e22891634f05f0796fc5e34402720f53ad96cbe6 [file] [log] [blame]
Marat Dukhan683fab32020-08-03 19:42:52 -07001// Auto-generated file. Do not edit!
Marat Dukhan66ccf642020-09-28 16:23:42 -07002// Template: src/qs8-gemm/MRx8c8-avx2.c.in
Marat Dukhan683fab32020-08-03 19:42:52 -07003// Generator: tools/xngen
4//
5// Copyright 2020 Google LLC
6//
7// This source code is licensed under the BSD-style license found in the
8// LICENSE file in the root directory of this source tree.
9
10#include <assert.h>
11
12#include <immintrin.h>
13
14#include <xnnpack/gemm.h>
Marat Dukhanf124e882020-08-09 19:48:10 -070015#include <xnnpack/intrinsics-polyfill.h>
Frank Barchard6d8ca7d2021-03-01 11:05:08 -080016#include <xnnpack/math.h>
Marat Dukhan683fab32020-08-03 19:42:52 -070017
18
19void xnn_qs8_gemm_xw_minmax_ukernel_2x8c8__avx2(
20 size_t mr,
21 size_t nc,
22 size_t kc,
23 const int8_t* restrict a,
24 size_t a_stride,
25 const void* restrict w,
26 int8_t* restrict c,
27 size_t cm_stride,
28 size_t cn_stride,
29 const union xnn_qs8_gemm_xw_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
30{
31 assert(mr != 0);
32 assert(mr <= 2);
33 assert(nc != 0);
34 assert(kc != 0);
35 assert(kc % sizeof(int8_t) == 0);
36 assert(a != NULL);
37 assert(w != NULL);
38 assert(c != NULL);
39
Frank Barchard6d8ca7d2021-03-01 11:05:08 -080040 kc = round_up_po2(kc, 8);
Marat Dukhan683fab32020-08-03 19:42:52 -070041 const int8_t* a0 = a;
42 int8_t* c0 = c;
43 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
44 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
45 if XNN_UNPREDICTABLE(mr != 2) {
46 a1 = a0;
47 c1 = c0;
48 }
49
50 do {
51 const __m128i vbias0x0 = _mm_loadu_si32(w);
52 const __m128i vbias0x1 = _mm_loadu_si32((const void*) ((uintptr_t) w + sizeof(int32_t)));
53 __m256i vacc0x01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x0), vbias0x1, 1);
54 const __m128i vbias0x2 = _mm_loadu_si32((const void*) ((uintptr_t) w + 2 * sizeof(int32_t)));
55 const __m128i vbias0x3 = _mm_loadu_si32((const void*) ((uintptr_t) w + 3 * sizeof(int32_t)));
56 __m256i vacc0x23 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x2), vbias0x3, 1);
57 const __m128i vbias0x4 = _mm_loadu_si32((const void*) ((uintptr_t) w + 4 * sizeof(int32_t)));
58 const __m128i vbias0x5 = _mm_loadu_si32((const void*) ((uintptr_t) w + 5 * sizeof(int32_t)));
59 __m256i vacc0x45 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x4), vbias0x5, 1);
60 const __m128i vbias0x6 = _mm_loadu_si32((const void*) ((uintptr_t) w + 6 * sizeof(int32_t)));
61 const __m128i vbias0x7 = _mm_loadu_si32((const void*) ((uintptr_t) w + 7 * sizeof(int32_t)));
62 __m256i vacc0x67 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x6), vbias0x7, 1);
63 __m256i vacc1x01 = vacc0x01;
64 __m256i vacc1x23 = vacc0x23;
65 __m256i vacc1x45 = vacc0x45;
66 __m256i vacc1x67 = vacc0x67;
67 w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t));
68
69 size_t k = 0;
70 while (k < kc) {
71 const __m128i va0 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a0));
72 const __m256i vxa0 = _mm256_cvtepi8_epi16(va0);
73 a0 += 8;
74 const __m128i va1 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a1));
75 const __m256i vxa1 = _mm256_cvtepi8_epi16(va1);
76 a1 += 8;
77
78 const __m256i vxb01 = _mm256_load_si256((const __m256i*) w);
79
80 vacc0x01 = _mm256_add_epi32(vacc0x01, _mm256_madd_epi16(vxa0, vxb01));
81 vacc1x01 = _mm256_add_epi32(vacc1x01, _mm256_madd_epi16(vxa1, vxb01));
82 const __m256i vxb23 = _mm256_load_si256((const __m256i*) ((uintptr_t) w + 16 * sizeof(int16_t)));
83
84 vacc0x23 = _mm256_add_epi32(vacc0x23, _mm256_madd_epi16(vxa0, vxb23));
85 vacc1x23 = _mm256_add_epi32(vacc1x23, _mm256_madd_epi16(vxa1, vxb23));
86 const __m256i vxb45 = _mm256_load_si256((const __m256i*) ((uintptr_t) w + 32 * sizeof(int16_t)));
87
88 vacc0x45 = _mm256_add_epi32(vacc0x45, _mm256_madd_epi16(vxa0, vxb45));
89 vacc1x45 = _mm256_add_epi32(vacc1x45, _mm256_madd_epi16(vxa1, vxb45));
90 const __m256i vxb67 = _mm256_load_si256((const __m256i*) ((uintptr_t) w + 48 * sizeof(int16_t)));
91
92 vacc0x67 = _mm256_add_epi32(vacc0x67, _mm256_madd_epi16(vxa0, vxb67));
93 vacc1x67 = _mm256_add_epi32(vacc1x67, _mm256_madd_epi16(vxa1, vxb67));
94
95 w = (const void*) ((uintptr_t) w + 64 * sizeof(int16_t));
96 k += 8 * sizeof(int8_t);
97 }
98
99 const __m256i vacc0x0213 = _mm256_hadd_epi32(vacc0x01, vacc0x23);
100 const __m256i vacc0x4657 = _mm256_hadd_epi32(vacc0x45, vacc0x67);
101 const __m256i vacc1x0213 = _mm256_hadd_epi32(vacc1x01, vacc1x23);
102 const __m256i vacc1x4657 = _mm256_hadd_epi32(vacc1x45, vacc1x67);
103
104 const __m256i vacc0x02461357 = _mm256_hadd_epi32(vacc0x0213, vacc0x4657);
105 const __m256i vacc1x02461357 = _mm256_hadd_epi32(vacc1x0213, vacc1x4657);
106
Marat Dukhan23848db2020-08-05 09:10:16 -0700107 const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
108 __m256i vacc0x01234567 = _mm256_permutevar8x32_epi32(vacc0x02461357, vpermute_mask);
109 __m256i vacc1x01234567 = _mm256_permutevar8x32_epi32(vacc1x02461357, vpermute_mask);
Marat Dukhan683fab32020-08-03 19:42:52 -0700110
111 const __m256i vmultiplier = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.multiplier));
112 const __m256i vrounding = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.rounding));
113
Marat Dukhan23848db2020-08-05 09:10:16 -0700114 const __m256i vacc0x11335577 = _mm256_shuffle_epi32(vacc0x01234567, _MM_SHUFFLE(3, 3, 1, 1));
115 const __m256i vacc1x11335577 = _mm256_shuffle_epi32(vacc1x01234567, _MM_SHUFFLE(3, 3, 1, 1));
Marat Dukhan683fab32020-08-03 19:42:52 -0700116
117 const __m256i vprod0x0246 = _mm256_add_epi64(_mm256_mul_epi32(vacc0x01234567, vmultiplier), vrounding);
118 const __m256i vprod1x0246 = _mm256_add_epi64(_mm256_mul_epi32(vacc1x01234567, vmultiplier), vrounding);
119
Marat Dukhan23848db2020-08-05 09:10:16 -0700120 const __m256i vprod0x1357 = _mm256_add_epi64(_mm256_mul_epi32(vacc0x11335577, vmultiplier), vrounding);
121 const __m256i vprod1x1357 = _mm256_add_epi64(_mm256_mul_epi32(vacc1x11335577, vmultiplier), vrounding);
Marat Dukhan683fab32020-08-03 19:42:52 -0700122
123 const __m256i vq31prod0x0246 = _mm256_srli_epi64(vprod0x0246, 31);
124 const __m256i vq31prod0x1357 = _mm256_add_epi64(vprod0x1357, vprod0x1357);
125 const __m256i vq31prod1x0246 = _mm256_srli_epi64(vprod1x0246, 31);
126 const __m256i vq31prod1x1357 = _mm256_add_epi64(vprod1x1357, vprod1x1357);
127
128 const __m256i vq31prod0x01234567 = _mm256_blend_epi16(vq31prod0x0246, vq31prod0x1357, 0xCC);
129 const __m256i vq31prod1x01234567 = _mm256_blend_epi16(vq31prod1x0246, vq31prod1x1357, 0xCC);
130
131 const __m256i vremainder_mask = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_mask));
132 const __m256i vrem0x01234567 =
133 _mm256_add_epi32(_mm256_and_si256(vq31prod0x01234567, vremainder_mask), _mm256_cmpgt_epi32(_mm256_setzero_si256(), vq31prod0x01234567));
134 const __m256i vrem1x01234567 =
135 _mm256_add_epi32(_mm256_and_si256(vq31prod1x01234567, vremainder_mask), _mm256_cmpgt_epi32(_mm256_setzero_si256(), vq31prod1x01234567));
136
137 const __m256i vremainder_threshold = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_threshold));
138 const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
139 vacc0x01234567 =
140 _mm256_sub_epi32(_mm256_sra_epi32(vq31prod0x01234567, vshift), _mm256_cmpgt_epi32(vrem0x01234567, vremainder_threshold));
141 vacc1x01234567 =
142 _mm256_sub_epi32(_mm256_sra_epi32(vq31prod1x01234567, vshift), _mm256_cmpgt_epi32(vrem1x01234567, vremainder_threshold));
143
144 const __m256i voutput_zero_point = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_zero_point));
145 __m256i vacc01x01234567 = _mm256_adds_epi16(_mm256_packs_epi32(vacc0x01234567, vacc1x01234567), voutput_zero_point);
146
147 vacc01x01234567 = _mm256_permute4x64_epi64(vacc01x01234567, _MM_SHUFFLE(3, 1, 2, 0));
148
149 const __m256i voutput_min = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_min));
150 const __m256i voutput_max = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_max));
151 vacc01x01234567 = _mm256_min_epi16(_mm256_max_epi16(vacc01x01234567, voutput_min), voutput_max);
152
153 __m256i vout = _mm256_packs_epi16(vacc01x01234567, vacc01x01234567);
154 __m128i vout_lo = _mm256_castsi256_si128(vout);
155 __m128i vout_hi = _mm256_extracti128_si256(vout, 1);
156
157 if (nc >= 8) {
158 _mm_storel_epi64((__m128i*) c0, vout_lo);
159 _mm_storel_epi64((__m128i*) c1, vout_hi);
160
Marat Dukhan683fab32020-08-03 19:42:52 -0700161 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
162 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
163
Frank Barchard6d8ca7d2021-03-01 11:05:08 -0800164 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
165 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
166
Marat Dukhan683fab32020-08-03 19:42:52 -0700167 nc -= 8;
168 } else {
169 if (nc & 4) {
170 _mm_storeu_si32(c0, vout_lo);
171 _mm_storeu_si32(c1, vout_hi);
172
173 c0 += 4;
174 c1 += 4;
175
176 vout_lo = _mm_srli_epi64(vout_lo, 32);
177 vout_hi = _mm_srli_epi64(vout_hi, 32);
178 }
179 if (nc & 2) {
180 *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout_lo, 0);
181 *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout_hi, 0);
182
183 c0 += 2;
184 c1 += 2;
185
186 vout_lo = _mm_srli_epi32(vout_lo, 16);
187 vout_hi = _mm_srli_epi32(vout_hi, 16);
188 }
189 if (nc & 1) {
190 *c0 = (int8_t) _mm_extract_epi8(vout_lo, 0);
191 *c1 = (uint8_t) _mm_extract_epi8(vout_hi, 0);
192 }
193
194 nc = 0;
195 }
196 } while (nc != 0);
197}