Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-2x4c8-minmax-sse41-ld128.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx4c8-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 <smmintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/gemm.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18
19
20 void xnn_qd8_f32_qb4w_gemm_minmax_ukernel_2x4c8__sse41_ld128(
21 size_t mr,
22 size_t nc,
23 size_t kc,
24 const int8_t* restrict a,
25 size_t a_stride,
26 const void* restrict w,
27 float* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 const struct xnn_f32_qb4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
31 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33 assert(mr != 0);
34 assert(mr <= 2);
35 assert(nc != 0);
36 assert(kc != 0);
37 assert(kc % sizeof(int8_t) == 0);
38 assert(a != NULL);
39 assert(w != NULL);
40 assert(c != NULL);
41
42 size_t bl = params->scalar.blocksize;
43 assert(bl <= round_up_po2(kc, 2));
44 assert(bl != 0);
45 assert(bl % 32 == 0);
46 kc = round_up_po2(kc, 8 * sizeof(int8_t));
47 const int8_t* a0 = a;
48 float* c0 = c;
49 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
50 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
51 if XNN_UNPREDICTABLE(mr != 2) {
52 a1 = a0;
53 c1 = c0;
54 }
55
56 const __m128 vmin = _mm_set1_ps(params->scalar.min);
57 const __m128 vmax = _mm_set1_ps(params->scalar.max);
58 XNN_FORCE_REALIZATION(vmin);
59 XNN_FORCE_REALIZATION(vmax);
60
61 const __m128i vmask = _mm_set1_epi8(0xF0);
62 XNN_FORCE_REALIZATION(vmask);
63
64 do {
65 const __m128 vksum = _mm_load_ps((const float*) w);
66 const __m128i vinput_zero_point01 = _mm_loadu_si128((const __m128i*) &quantization_params[0]);
67 const __m128i vinput_zero_point0 = _mm_shuffle_epi32(vinput_zero_point01, _MM_SHUFFLE(0, 0, 0, 0));
68 const __m128i vinput_zero_point1 = _mm_shuffle_epi32(vinput_zero_point01, _MM_SHUFFLE(2, 2, 2, 2));
69
70 __m128 vinput_zero_point0_float = _mm_cvtepi32_ps(vinput_zero_point0);
71 __m128 vout0x0123 = _mm_mul_ps(vksum, vinput_zero_point0_float);
72 __m128 vinput_zero_point1_float = _mm_cvtepi32_ps(vinput_zero_point1);
73 __m128 vout1x0123 = _mm_mul_ps(vksum, vinput_zero_point1_float);
74 w = (const int32_t*) w + 4;
75
76 for (size_t kb=0; kb < kc; kb += bl) {
77 __m128i vacc0x0 = _mm_setzero_si128();
78 __m128i vacc0x1 = _mm_setzero_si128();
79 __m128i vacc0x2 = _mm_setzero_si128();
80 __m128i vacc0x3 = _mm_setzero_si128();
81 __m128i vacc1x0 = _mm_setzero_si128();
82 __m128i vacc1x1 = _mm_setzero_si128();
83 __m128i vacc1x2 = _mm_setzero_si128();
84 __m128i vacc1x3 = _mm_setzero_si128();
85 size_t k = bl;
86
87 while (k >= 16 * sizeof(int8_t)) {
88 const __m128i va0c0 = _mm_loadl_epi64((const __m128i*) a0);
89 const __m128i vxa0c0 = _mm_cvtepi8_epi16(va0c0);
90 a0 += 8;
91 const __m128i va1c0 = _mm_loadl_epi64((const __m128i*) a1);
92 const __m128i vxa1c0 = _mm_cvtepi8_epi16(va1c0);
93 a1 += 8;
94
95 const __m128i vb01c01 = _mm_loadu_si128((const __m128i*) w);
96 const __m128i vbs01c0 = _mm_slli_epi32(vb01c01, 4);
97 const __m128i vb01c0 = _mm_and_si128(vbs01c0, vmask);
98 const __m128i vsb01c0 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01c0);
99 const __m128i vxb0c0 = _mm_unpacklo_epi8(vb01c0, vsb01c0);
100 const __m128i vxb1c0 = _mm_unpackhi_epi8(vb01c0, vsb01c0);
101
102 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c0, vxb0c0));
103 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c0, vxb1c0));
104 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1c0, vxb0c0));
105 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1c0, vxb1c0));
106 const __m128i vb23c01 = _mm_loadu_si128((const __m128i*) ((const int8_t*) w + 16));
107 const __m128i vbs23c0 = _mm_slli_epi32(vb23c01, 4);
108 const __m128i vb23c0 = _mm_and_si128(vbs23c0, vmask);
109 const __m128i vsb23c0 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23c0);
110 const __m128i vxb2c0 = _mm_unpacklo_epi8(vb23c0, vsb23c0);
111 const __m128i vxb3c0 = _mm_unpackhi_epi8(vb23c0, vsb23c0);
112
113 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c0, vxb2c0));
114 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c0, vxb3c0));
115 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1c0, vxb2c0));
116 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1c0, vxb3c0));
117
118 const __m128i va0c1 = _mm_loadl_epi64((const __m128i*) a0);
119 const __m128i vxa0c1 = _mm_cvtepi8_epi16(va0c1);
120 a0 += 8;
121 const __m128i va1c1 = _mm_loadl_epi64((const __m128i*) a1);
122 const __m128i vxa1c1 = _mm_cvtepi8_epi16(va1c1);
123 a1 += 8;
124
125 const __m128i vb01c1 = _mm_and_si128(vb01c01, vmask);
126 const __m128i vsb01c1 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01c1);
127 const __m128i vxb0c1 = _mm_unpacklo_epi8(vb01c1, vsb01c1);
128 const __m128i vxb1c1 = _mm_unpackhi_epi8(vb01c1, vsb01c1);
129
130 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c1, vxb0c1));
131 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c1, vxb1c1));
132 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1c1, vxb0c1));
133 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1c1, vxb1c1));
134 const __m128i vb23c1 = _mm_and_si128(vb23c01, vmask);
135 const __m128i vsb23c1 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23c1);
136 const __m128i vxb2c1 = _mm_unpacklo_epi8(vb23c1, vsb23c1);
137 const __m128i vxb3c1 = _mm_unpackhi_epi8(vb23c1, vsb23c1);
138
139 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c1, vxb2c1));
140 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c1, vxb3c1));
141 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1c1, vxb2c1));
142 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1c1, vxb3c1));
143
144
145 w = (const int8_t*) w + 32;
146 k -= 16 * sizeof(int8_t);
147 }
148
149 while (k >= 8 * sizeof(int8_t)) {
150 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
151 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
152 a0 += 8;
153 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
154 const __m128i vxa1 = _mm_cvtepi8_epi16(va1);
155 a1 += 8;
156
157 __m128i vb01 = _mm_loadu_si128((const __m128i*) w);
158 vb01 = _mm_slli_epi32(vb01, 4);
159 vb01 = _mm_and_si128(vb01, vmask);
160
161 const __m128i vxbm1 = _mm_unpackhi_epi8(vb01, vb01);
162 const __m128i vxb0 = _mm_cvtepi8_epi16(vb01);
163 const __m128i vxb1 = _mm_srai_epi16(vxbm1, 8);
164
165 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
166 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
167 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0));
168 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1));
169 __m128i vb23 = _mm_loadu_si128((const __m128i*) ((const int8_t*) w + 16));
170 vb23 = _mm_slli_epi32(vb23, 4);
171 vb23 = _mm_and_si128(vb23, vmask);
172
173 const __m128i vxbm3 = _mm_unpackhi_epi8(vb23, vb23);
174 const __m128i vxb2 = _mm_cvtepi8_epi16(vb23);
175 const __m128i vxb3 = _mm_srai_epi16(vxbm3, 8);
176
177 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
178 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
179 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2));
180 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3));
181
182 w = (const int8_t*) w + 32;
183 k -= 8 * sizeof(int8_t);
184 }
185 // accumulate float
186 const __m128 vfilter_output_scale0123 = _mm_castsi128_ps(_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_loadl_epi64((const __m128i*) w)), 16));
187 w = (const uint16_t*) w + 4;
188
189 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
190 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
191 const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1);
192 const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3);
193
194 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
195 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
196
197 vout0x0123 = _mm_add_ps(vout0x0123, _mm_mul_ps(_mm_cvtepi32_ps(vacc0x0123), vfilter_output_scale0123));
198 vout1x0123 = _mm_add_ps(vout1x0123, _mm_mul_ps(_mm_cvtepi32_ps(vacc1x0123), vfilter_output_scale0123));
199 }
200
201 const __m128i vinput_scale01 = _mm_loadu_si128((const __m128i*) &quantization_params[0]);
202 const __m128 vinput_scale0 = _mm_castsi128_ps(_mm_shuffle_epi32(vinput_scale01, _MM_SHUFFLE(1, 1, 1, 1)));
203 const __m128 vinput_scale1 = _mm_castsi128_ps(_mm_shuffle_epi32(vinput_scale01, _MM_SHUFFLE(3, 3, 3, 3)));
204
205 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
206 vout1x0123 = _mm_mul_ps(vout1x0123, vinput_scale1);
207
208
209 const __m128 vbias0123 = _mm_loadu_ps((const float*) w);
210 w = (const float*) w + 4;
211 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
212 vout1x0123 = _mm_add_ps(vout1x0123, vbias0123);
213
214 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
215 vout1x0123 = _mm_max_ps(vout1x0123, vmin);
216
217 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
218 vout1x0123 = _mm_min_ps(vout1x0123, vmax);
219
220 if XNN_LIKELY(nc >= 4) {
221 _mm_storeu_ps(c0, vout0x0123);
222 _mm_storeu_ps(c1, vout1x0123);
223
224 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
225 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
226
227 c0 = (float*) ((uintptr_t) c0 + cn_stride);
228 c1 = (float*) ((uintptr_t) c1 + cn_stride);
229
230 nc -= 4;
231 } else {
232 if (nc & 2) {
233 _mm_storel_pi((__m64*) c0, vout0x0123);
234 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
235 c0 += 2;
236 _mm_storel_pi((__m64*) c1, vout1x0123);
237 vout1x0123 = _mm_unpackhi_ps(vout1x0123, vout1x0123);
238 c1 += 2;
239 }
240 if (nc & 1) {
241 _mm_store_ss(c0, vout0x0123);
242 _mm_store_ss(c1, vout1x0123);
243 }
244 nc = 0;
245 }
246 } while (nc != 0);
247 }