1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx4c2-sse.c.in
3 // 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 <emmintrin.h>
13
14 #include <xnnpack/gemm.h>
15 #include <xnnpack/math.h>
16
17
18
xnn_qs8_gemm_xw_minmax_fp32_ukernel_3x4c2__sse2(size_t mr,size_t nc,size_t kc,const int8_t * restrict a,size_t a_stride,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qs8_gemm_xw_minmax_fp32_ukernel_3x4c2__sse2(
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_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30 {
31 assert(mr != 0);
32 assert(mr <= 3);
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
40 kc = round_up_po2(kc, 2);
41 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 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
50 int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
51 if XNN_UNPREDICTABLE(mr <= 2) {
52 a2 = a1;
53 c2 = c1;
54 }
55
56 do {
57 __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
58 __m128i vacc1x0123 = vacc0x0123;
59 __m128i vacc2x0123 = vacc0x0123;
60 w = (const void*) ((const int32_t*) w + 4);
61
62 size_t k = kc;
63 while (k >= 8 * sizeof(int8_t)) {
64 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
65 const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
66 a0 += 8;
67 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
68 const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
69 a1 += 8;
70 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
71 const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8);
72 a2 += 8;
73
74 const __m128i vxb0 = _mm_load_si128((const __m128i*) w);
75
76 vacc0x0123 = _mm_add_epi32(vacc0x0123,
77 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
78 vacc1x0123 = _mm_add_epi32(vacc1x0123,
79 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
80 vacc2x0123 = _mm_add_epi32(vacc2x0123,
81 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
82 const __m128i vxb1 = _mm_load_si128((const __m128i*) ((const int16_t*) w + 8));
83
84 vacc0x0123 = _mm_add_epi32(vacc0x0123,
85 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
86 vacc1x0123 = _mm_add_epi32(vacc1x0123,
87 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
88 vacc2x0123 = _mm_add_epi32(vacc2x0123,
89 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
90 const __m128i vxb2 = _mm_load_si128((const __m128i*) ((const int16_t*) w + 16));
91
92 vacc0x0123 = _mm_add_epi32(vacc0x0123,
93 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
94 vacc1x0123 = _mm_add_epi32(vacc1x0123,
95 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
96 vacc2x0123 = _mm_add_epi32(vacc2x0123,
97 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
98 const __m128i vxb3 = _mm_load_si128((const __m128i*) ((const int16_t*) w + 24));
99
100 vacc0x0123 = _mm_add_epi32(vacc0x0123,
101 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
102 vacc1x0123 = _mm_add_epi32(vacc1x0123,
103 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
104 vacc2x0123 = _mm_add_epi32(vacc2x0123,
105 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
106
107 w = (const void*) ((const int16_t*) w + 32);
108 k -= 8 * sizeof(int8_t);
109 }
110 if (k != 0) {
111 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
112 const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
113 a0 = (const int8_t*) ((uintptr_t) a0 + k);
114 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
115 const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
116 a1 = (const int8_t*) ((uintptr_t) a1 + k);
117 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
118 const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8);
119 a2 = (const int8_t*) ((uintptr_t) a2 + k);
120
121 const __m128i vxb0 = _mm_load_si128((const __m128i*) w);
122 w = (const void*) ((const int16_t*) w + 8);
123
124 vacc0x0123 = _mm_add_epi32(vacc0x0123,
125 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
126 vacc1x0123 = _mm_add_epi32(vacc1x0123,
127 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
128 vacc2x0123 = _mm_add_epi32(vacc2x0123,
129 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
130
131 if (k > 2 * sizeof(int8_t)) {
132 const __m128i vxb1 = _mm_load_si128((const __m128i*) w);
133 w = (const void*) ((const int16_t*) w + 8);
134
135 vacc0x0123 = _mm_add_epi32(vacc0x0123,
136 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
137 vacc1x0123 = _mm_add_epi32(vacc1x0123,
138 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
139 vacc2x0123 = _mm_add_epi32(vacc2x0123,
140 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
141
142 if (k > 4 * sizeof(int8_t)) {
143 const __m128i vxb2 = _mm_load_si128((const __m128i*) w);
144 w = (const void*) ((const int16_t*) w + 8);
145
146 vacc0x0123 = _mm_add_epi32(vacc0x0123,
147 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
148 vacc1x0123 = _mm_add_epi32(vacc1x0123,
149 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
150 vacc2x0123 = _mm_add_epi32(vacc2x0123,
151 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
152 }
153 }
154 }
155
156 __m128 vscaled0x0123 = _mm_cvtepi32_ps(vacc0x0123);
157 __m128 vscaled1x0123 = _mm_cvtepi32_ps(vacc1x0123);
158 __m128 vscaled2x0123 = _mm_cvtepi32_ps(vacc2x0123);
159
160 const __m128 vscale = _mm_load_ps(params->fp32_sse2.scale);
161 vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale);
162 vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale);
163 vscaled2x0123 = _mm_mul_ps(vscaled2x0123, vscale);
164
165 const __m128 voutput_max_less_zero_point = _mm_load_ps(params->fp32_sse2.output_max_less_zero_point);
166 vscaled0x0123 = _mm_min_ps(vscaled0x0123, voutput_max_less_zero_point);
167 vscaled1x0123 = _mm_min_ps(vscaled1x0123, voutput_max_less_zero_point);
168 vscaled2x0123 = _mm_min_ps(vscaled2x0123, voutput_max_less_zero_point);
169
170 vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123);
171 vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123);
172 vacc2x0123 = _mm_cvtps_epi32(vscaled2x0123);
173
174 const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->fp32_sse2.output_zero_point);
175 __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
176 __m128i vacc22x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc2x0123), voutput_zero_point);
177
178 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->fp32_sse2.output_min);
179 vacc01x0123 = _mm_max_epi16(vacc01x0123, voutput_min);
180 vacc22x0123 = _mm_max_epi16(vacc22x0123, voutput_min);
181
182 __m128i vout = _mm_packs_epi16(vacc01x0123, vacc22x0123);
183
184
185 if (nc >= 4) {
186 *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
187 vout = _mm_srli_si128(vout, 4);
188 *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(vout);
189 vout = _mm_srli_si128(vout, 4);
190 *((uint32_t*) c2) = (uint32_t) _mm_cvtsi128_si32(vout);
191
192 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
193 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
194 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
195
196 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
197 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
198 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
199
200 nc -= 4;
201 } else {
202 if (nc & 2) {
203 *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
204 c0 += 2;
205 *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
206 c1 += 2;
207 *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4);
208 c2 += 2;
209 vout = _mm_srli_epi32(vout, 16);
210 }
211 if (nc & 1) {
212 *c0 = (int8_t) _mm_cvtsi128_si32(vout);
213 *c1 = (int8_t) _mm_extract_epi16(vout, 2);
214 *c2 = (int8_t) _mm_extract_epi16(vout, 4);
215 }
216
217 nc = 0;
218 }
219 } while (nc != 0);
220 }
221