Debian Codegraph @

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