blob: 341314fa28d89f8741f7e4a854d71382f179ecf5 [file] [log] [blame]
Marat Dukhanf9480682020-07-31 14:50:24 -07001// Auto-generated file. Do not edit!
Marat Dukhan66ccf642020-09-28 16:23:42 -07002// Template: src/qs8-igemm/MRx4c2-sse.c.in
Marat Dukhanf9480682020-07-31 14:50:24 -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 <tmmintrin.h>
13
14#include <xnnpack/igemm.h>
Frank Barchard6d8ca7d2021-03-01 11:05:08 -080015#include <xnnpack/math.h>
Marat Dukhanf9480682020-07-31 14:50:24 -070016
17
Marat Dukhanb07c26a2021-05-24 19:44:51 -070018void xnn_qs8_igemm_minmax_gemmlowp_ukernel_4x4c2__ssse3_ld64(
Marat Dukhanf9480682020-07-31 14:50:24 -070019 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const int8_t** restrict a,
24 const void* restrict w,
25 int8_t* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const int8_t* zero,
Marat Dukhane3d17bf2021-05-24 22:22:43 -070030 const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN
Marat Dukhanf9480682020-07-31 14:50:24 -070031{
32 assert(mr != 0);
33 assert(mr <= 4);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(ks != 0);
37 assert(ks % (4 * sizeof(void*)) == 0);
38 assert(a_offset % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
Frank Barchard6d8ca7d2021-03-01 11:05:08 -080043 kc = round_up_po2(kc, 2);
Marat Dukhanf9480682020-07-31 14:50:24 -070044 int8_t* c0 = c;
45 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
46 if XNN_UNPREDICTABLE(mr < 2) {
47 c1 = c0;
48 }
49 int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
50 if XNN_UNPREDICTABLE(mr <= 2) {
51 c2 = c1;
52 }
53 int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
54 if XNN_UNPREDICTABLE(mr != 4) {
55 c3 = c2;
56 }
57
58 do {
59 __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
60 __m128i vacc1x0123 = vacc0x0123;
61 __m128i vacc2x0123 = vacc0x0123;
62 __m128i vacc3x0123 = vacc0x0123;
Marat Dukhane5eee462021-07-01 19:34:39 -070063 w = (const void*) ((const int32_t*) w + 4);
Marat Dukhanf9480682020-07-31 14:50:24 -070064
65 size_t p = ks;
66 do {
67 const int8_t* restrict a0 = a[0];
68 if XNN_UNPREDICTABLE(a0 != zero) {
69 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
70 }
71 const int8_t* restrict a1 = a[1];
72 if XNN_UNPREDICTABLE(a1 != zero) {
73 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
74 }
75 const int8_t* restrict a2 = a[2];
76 if XNN_UNPREDICTABLE(a2 != zero) {
77 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
78 }
79 const int8_t* restrict a3 = a[3];
80 if XNN_UNPREDICTABLE(a3 != zero) {
81 a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
82 }
83 a += 4;
84
85 size_t k = kc;
86 while (k >= 8 * sizeof(int8_t)) {
87 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
Marat Dukhane5eee462021-07-01 19:34:39 -070088 const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -070089 a0 += 8;
90 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
Marat Dukhane5eee462021-07-01 19:34:39 -070091 const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -070092 a1 += 8;
93 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
Marat Dukhane5eee462021-07-01 19:34:39 -070094 const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -070095 a2 += 8;
96 const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
Marat Dukhane5eee462021-07-01 19:34:39 -070097 const __m128i vxa3 = _mm_srai_epi16(_mm_unpacklo_epi8(va3, va3), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -070098 a3 += 8;
99
100 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -0700101 const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700102
103 vacc0x0123 = _mm_add_epi32(vacc0x0123,
104 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
105 vacc1x0123 = _mm_add_epi32(vacc1x0123,
106 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
107 vacc2x0123 = _mm_add_epi32(vacc2x0123,
108 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
109 vacc3x0123 = _mm_add_epi32(vacc3x0123,
110 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
Marat Dukhane5eee462021-07-01 19:34:39 -0700111 const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8));
112 const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700113
114 vacc0x0123 = _mm_add_epi32(vacc0x0123,
115 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
116 vacc1x0123 = _mm_add_epi32(vacc1x0123,
117 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
118 vacc2x0123 = _mm_add_epi32(vacc2x0123,
119 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
120 vacc3x0123 = _mm_add_epi32(vacc3x0123,
121 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
Marat Dukhane5eee462021-07-01 19:34:39 -0700122 const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16));
123 const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700124
125 vacc0x0123 = _mm_add_epi32(vacc0x0123,
126 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
127 vacc1x0123 = _mm_add_epi32(vacc1x0123,
128 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
129 vacc2x0123 = _mm_add_epi32(vacc2x0123,
130 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
131 vacc3x0123 = _mm_add_epi32(vacc3x0123,
132 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
Marat Dukhane5eee462021-07-01 19:34:39 -0700133 const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24));
134 const __m128i vxb3 = _mm_srai_epi16(_mm_unpacklo_epi8(vb3, vb3), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700135
136 vacc0x0123 = _mm_add_epi32(vacc0x0123,
137 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
138 vacc1x0123 = _mm_add_epi32(vacc1x0123,
139 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
140 vacc2x0123 = _mm_add_epi32(vacc2x0123,
141 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
142 vacc3x0123 = _mm_add_epi32(vacc3x0123,
143 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
144
Marat Dukhane5eee462021-07-01 19:34:39 -0700145 w = (const void*) ((const int8_t*) w + 32);
Marat Dukhanf9480682020-07-31 14:50:24 -0700146 k -= 8 * sizeof(int8_t);
147 }
148 if (k != 0) {
149 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
Marat Dukhane5eee462021-07-01 19:34:39 -0700150 const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700151 a0 = (const int8_t*) ((uintptr_t) a0 + k);
152 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
Marat Dukhane5eee462021-07-01 19:34:39 -0700153 const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700154 a1 = (const int8_t*) ((uintptr_t) a1 + k);
155 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
Marat Dukhane5eee462021-07-01 19:34:39 -0700156 const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700157 a2 = (const int8_t*) ((uintptr_t) a2 + k);
158 const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
Marat Dukhane5eee462021-07-01 19:34:39 -0700159 const __m128i vxa3 = _mm_srai_epi16(_mm_unpacklo_epi8(va3, va3), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700160 a3 = (const int8_t*) ((uintptr_t) a3 + k);
161
162 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -0700163 w = (const void*) ((const int8_t*) w + 8);
164 const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700165
166 vacc0x0123 = _mm_add_epi32(vacc0x0123,
167 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
168 vacc1x0123 = _mm_add_epi32(vacc1x0123,
169 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
170 vacc2x0123 = _mm_add_epi32(vacc2x0123,
171 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
172 vacc3x0123 = _mm_add_epi32(vacc3x0123,
173 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
174
175 if (k > 2 * sizeof(int8_t)) {
176 const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -0700177 w = (const void*) ((const int8_t*) w + 8);
178 const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700179
180 vacc0x0123 = _mm_add_epi32(vacc0x0123,
181 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
182 vacc1x0123 = _mm_add_epi32(vacc1x0123,
183 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
184 vacc2x0123 = _mm_add_epi32(vacc2x0123,
185 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
186 vacc3x0123 = _mm_add_epi32(vacc3x0123,
187 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
188
189 if (k > 4 * sizeof(int8_t)) {
190 const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -0700191 w = (const void*) ((const int8_t*) w + 8);
192 const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8);
Marat Dukhanf9480682020-07-31 14:50:24 -0700193
194 vacc0x0123 = _mm_add_epi32(vacc0x0123,
195 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
196 vacc1x0123 = _mm_add_epi32(vacc1x0123,
197 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
198 vacc2x0123 = _mm_add_epi32(vacc2x0123,
199 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
200 vacc3x0123 = _mm_add_epi32(vacc3x0123,
201 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
Marat Dukhanf9480682020-07-31 14:50:24 -0700202 }
203 }
204 }
205 p -= 4 * sizeof(void*);
206 } while (p != 0);
207
Marat Dukhan9b474cf2021-05-25 16:37:48 -0700208 const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.multiplier);
209 const __m128i vrounding = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.rounding);
Marat Dukhanf9480682020-07-31 14:50:24 -0700210
211 const __m128i vnmask0x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0x0123);
212 const __m128i vnmask1x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc1x0123);
213 const __m128i vnmask2x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc2x0123);
214 const __m128i vnmask3x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc3x0123);
215
216 const __m128i vabsacc0x0123 = _mm_abs_epi32(vacc0x0123);
217 const __m128i vabsacc1x0123 = _mm_abs_epi32(vacc1x0123);
218 const __m128i vabsacc2x0123 = _mm_abs_epi32(vacc2x0123);
219 const __m128i vabsacc3x0123 = _mm_abs_epi32(vacc3x0123);
220
Marat Dukhan23848db2020-08-05 09:10:16 -0700221 const __m128i vabsacc0x1133 = _mm_shuffle_epi32(vabsacc0x0123, _MM_SHUFFLE(3, 3, 1, 1));
222 const __m128i vabsacc1x1133 = _mm_shuffle_epi32(vabsacc1x0123, _MM_SHUFFLE(3, 3, 1, 1));
223 const __m128i vabsacc2x1133 = _mm_shuffle_epi32(vabsacc2x0123, _MM_SHUFFLE(3, 3, 1, 1));
224 const __m128i vabsacc3x1133 = _mm_shuffle_epi32(vabsacc3x0123, _MM_SHUFFLE(3, 3, 1, 1));
Marat Dukhanf9480682020-07-31 14:50:24 -0700225
226 const __m128i vabsprod0x02 = _mm_mul_epu32(vabsacc0x0123, vmultiplier);
227 const __m128i vabsprod1x02 = _mm_mul_epu32(vabsacc1x0123, vmultiplier);
228 const __m128i vabsprod2x02 = _mm_mul_epu32(vabsacc2x0123, vmultiplier);
229 const __m128i vabsprod3x02 = _mm_mul_epu32(vabsacc3x0123, vmultiplier);
230
231 const __m128i vnmask0x02 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(2, 2, 0, 0));
232 const __m128i vnmask1x02 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(2, 2, 0, 0));
233 const __m128i vnmask2x02 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(2, 2, 0, 0));
234 const __m128i vnmask3x02 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(2, 2, 0, 0));
235
236 const __m128i vprod0x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x02, vnmask0x02), vnmask0x02);
237 const __m128i vprod1x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x02, vnmask1x02), vnmask1x02);
238 const __m128i vprod2x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x02, vnmask2x02), vnmask2x02);
239 const __m128i vprod3x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x02, vnmask3x02), vnmask3x02);
240
241 const __m128i vq31prod0x02 = _mm_srli_epi64(_mm_add_epi64(vprod0x02, vrounding), 31);
242 const __m128i vq31prod1x02 = _mm_srli_epi64(_mm_add_epi64(vprod1x02, vrounding), 31);
243 const __m128i vq31prod2x02 = _mm_srli_epi64(_mm_add_epi64(vprod2x02, vrounding), 31);
244 const __m128i vq31prod3x02 = _mm_srli_epi64(_mm_add_epi64(vprod3x02, vrounding), 31);
245
Marat Dukhan23848db2020-08-05 09:10:16 -0700246 const __m128i vabsprod0x13 = _mm_mul_epu32(vabsacc0x1133, vmultiplier);
247 const __m128i vabsprod1x13 = _mm_mul_epu32(vabsacc1x1133, vmultiplier);
248 const __m128i vabsprod2x13 = _mm_mul_epu32(vabsacc2x1133, vmultiplier);
249 const __m128i vabsprod3x13 = _mm_mul_epu32(vabsacc3x1133, vmultiplier);
Marat Dukhanf9480682020-07-31 14:50:24 -0700250
251 const __m128i vnmask0x13 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(3, 3, 1, 1));
252 const __m128i vnmask1x13 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(3, 3, 1, 1));
253 const __m128i vnmask2x13 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(3, 3, 1, 1));
254 const __m128i vnmask3x13 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(3, 3, 1, 1));
255
256 const __m128i vprod0x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x13, vnmask0x13), vnmask0x13);
257 const __m128i vprod1x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x13, vnmask1x13), vnmask1x13);
258 const __m128i vprod2x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x13, vnmask2x13), vnmask2x13);
259 const __m128i vprod3x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x13, vnmask3x13), vnmask3x13);
260
261 const __m128i vq31prod0x13 = _mm_srli_epi64(_mm_add_epi64(vprod0x13, vrounding), 31);
262 const __m128i vq31prod1x13 = _mm_srli_epi64(_mm_add_epi64(vprod1x13, vrounding), 31);
263 const __m128i vq31prod2x13 = _mm_srli_epi64(_mm_add_epi64(vprod2x13, vrounding), 31);
264 const __m128i vq31prod3x13 = _mm_srli_epi64(_mm_add_epi64(vprod3x13, vrounding), 31);
265
266 const __m128i vq31prod0x0213 = _mm_castps_si128(_mm_shuffle_ps(
267 _mm_castsi128_ps(vq31prod0x02), _mm_castsi128_ps(vq31prod0x13), _MM_SHUFFLE(2, 0, 2, 0)));
268 const __m128i vq31prod1x0213 = _mm_castps_si128(_mm_shuffle_ps(
269 _mm_castsi128_ps(vq31prod1x02), _mm_castsi128_ps(vq31prod1x13), _MM_SHUFFLE(2, 0, 2, 0)));
270 const __m128i vq31prod2x0213 = _mm_castps_si128(_mm_shuffle_ps(
271 _mm_castsi128_ps(vq31prod2x02), _mm_castsi128_ps(vq31prod2x13), _MM_SHUFFLE(2, 0, 2, 0)));
272 const __m128i vq31prod3x0213 = _mm_castps_si128(_mm_shuffle_ps(
273 _mm_castsi128_ps(vq31prod3x02), _mm_castsi128_ps(vq31prod3x13), _MM_SHUFFLE(2, 0, 2, 0)));
274
275 const __m128i vq31prod0x0123 = _mm_shuffle_epi32(vq31prod0x0213, _MM_SHUFFLE(3, 1, 2, 0));
276 const __m128i vq31prod1x0123 = _mm_shuffle_epi32(vq31prod1x0213, _MM_SHUFFLE(3, 1, 2, 0));
277 const __m128i vq31prod2x0123 = _mm_shuffle_epi32(vq31prod2x0213, _MM_SHUFFLE(3, 1, 2, 0));
278 const __m128i vq31prod3x0123 = _mm_shuffle_epi32(vq31prod3x0213, _MM_SHUFFLE(3, 1, 2, 0));
279
Marat Dukhan9b474cf2021-05-25 16:37:48 -0700280 const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.remainder_mask);
Marat Dukhanf9480682020-07-31 14:50:24 -0700281 const __m128i vrem0x0123 =
282 _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123));
283 const __m128i vrem1x0123 =
284 _mm_add_epi32(_mm_and_si128(vq31prod1x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod1x0123));
285 const __m128i vrem2x0123 =
286 _mm_add_epi32(_mm_and_si128(vq31prod2x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod2x0123));
287 const __m128i vrem3x0123 =
288 _mm_add_epi32(_mm_and_si128(vq31prod3x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod3x0123));
289
Marat Dukhan9b474cf2021-05-25 16:37:48 -0700290 const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.remainder_threshold);
291 const __m128i vshift = _mm_loadl_epi64((const __m128i*) params->gemmlowp_sse2.shift);
Marat Dukhanf9480682020-07-31 14:50:24 -0700292 vacc0x0123 =
293 _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold));
294 vacc1x0123 =
295 _mm_sub_epi32(_mm_sra_epi32(vq31prod1x0123, vshift), _mm_cmpgt_epi32(vrem1x0123, vremainder_threshold));
296 vacc2x0123 =
297 _mm_sub_epi32(_mm_sra_epi32(vq31prod2x0123, vshift), _mm_cmpgt_epi32(vrem2x0123, vremainder_threshold));
298 vacc3x0123 =
299 _mm_sub_epi32(_mm_sra_epi32(vq31prod3x0123, vshift), _mm_cmpgt_epi32(vrem3x0123, vremainder_threshold));
300
Marat Dukhan9b474cf2021-05-25 16:37:48 -0700301 const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_zero_point);
Marat Dukhanf9480682020-07-31 14:50:24 -0700302 __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
303 __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point);
304
Marat Dukhan9b474cf2021-05-25 16:37:48 -0700305 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_min);
306 const __m128i voutput_max = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_max);
Marat Dukhanf9480682020-07-31 14:50:24 -0700307 vacc01x0123 = _mm_min_epi16(_mm_max_epi16(vacc01x0123, voutput_min), voutput_max);
308 vacc23x0123 = _mm_min_epi16(_mm_max_epi16(vacc23x0123, voutput_min), voutput_max);
309
310 __m128i vout = _mm_packs_epi16(vacc01x0123, vacc23x0123);
311
Marat Dukhan748fd122021-05-22 20:46:56 -0700312
Marat Dukhanf9480682020-07-31 14:50:24 -0700313 if (nc >= 4) {
314 *((uint32_t*) c3) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(3, 3, 3, 3)));
315 c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
316 *((uint32_t*) c2) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(2, 2, 2, 2)));
317 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
318 *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(1, 1, 1, 1)));
319 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
320 *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
321 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
322
323 a = (const int8_t**restrict) ((uintptr_t) a - ks);
324
325 nc -= 4;
326 } else {
327 if (nc & 2) {
328 *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6);
329 c3 += 2;
330 *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4);
331 c2 += 2;
332 *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
333 c1 += 2;
334 *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
335 c0 += 2;
336 vout = _mm_srli_epi32(vout, 16);
337 }
338 if (nc & 1) {
Marat Dukhancdbe9a32021-07-01 23:52:04 -0700339 *c3 = (int8_t) _mm_extract_epi16(vout, 6);
340 *c2 = (int8_t) _mm_extract_epi16(vout, 4);
341 *c1 = (int8_t) _mm_extract_epi16(vout, 2);
342 *c0 = (int8_t) _mm_cvtsi128_si32(vout);
Marat Dukhanf9480682020-07-31 14:50:24 -0700343 }
344
345 nc = 0;
346 }
347 } while (nc != 0);
348}