Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-4x4c8-minmax-ssse3-madd.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx4c8-ssevnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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/gemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18
19
20 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_4x4c8__ssse3_madd(
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_qc4w_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 <= 4);
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 kc = round_up_po2(kc, 8 * sizeof(int8_t));
43 const int8_t* a0 = a;
44 float* c0 = c;
45 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
46 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
47 if XNN_UNPREDICTABLE(mr < 2) {
48 a1 = a0;
49 c1 = c0;
50 }
51 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
52 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
53 if XNN_UNPREDICTABLE(mr <= 2) {
54 a2 = a1;
55 c2 = c1;
56 }
57 const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
58 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
59 if XNN_UNPREDICTABLE(mr != 4) {
60 a3 = a2;
61 c3 = c2;
62 }
63
64 const __m128i vsign_mask = _mm_set1_epi8(0x80);
65 XNN_FORCE_REALIZATION(vsign_mask);
66 const __m128i vinput_zero_point0 = _mm_set1_epi32((int) quantization_params[0].zero_point + 128);
67 const __m128i vinput_zero_point1 = _mm_set1_epi32((int) quantization_params[1].zero_point + 128);
68 const __m128i vinput_zero_point2 = _mm_set1_epi32((int) quantization_params[2].zero_point + 128);
69 const __m128i vinput_zero_point3 = _mm_set1_epi32((int) quantization_params[3].zero_point + 128);
70 const __m128 voutput_min = _mm_set1_ps(params->scalar.min);
71 const __m128 voutput_max = _mm_set1_ps(params->scalar.max);
72 const __m128i vmask = _mm_set1_epi8(0x0F);
73 XNN_FORCE_REALIZATION(vmask);
74 do {
75 const __m128i vksum0123 = _mm_load_si128(w);
76 const __m128i vksum13 = _mm_shuffle_epi32(vksum0123, 0xF5);
77 const __m128i vsum0x02 = _mm_mul_epu32(vksum0123, vinput_zero_point0);
78 const __m128i vsum0x13 = _mm_mul_epu32(vksum13, vinput_zero_point0);
79 const __m128i vsum0x01 = _mm_unpacklo_epi32(vsum0x02, vsum0x13);
80 const __m128i vsum0x23 = _mm_unpackhi_epi32(vsum0x02, vsum0x13);
81 __m128i vacc0x01 = _mm_unpacklo_epi32(vsum0x01, _mm_setzero_si128());
82 __m128i vacc0x23 = _mm_unpacklo_epi32(vsum0x23, _mm_setzero_si128());
83 const __m128i vsum1x02 = _mm_mul_epu32(vksum0123, vinput_zero_point1);
84 const __m128i vsum1x13 = _mm_mul_epu32(vksum13, vinput_zero_point1);
85 const __m128i vsum1x01 = _mm_unpacklo_epi32(vsum1x02, vsum1x13);
86 const __m128i vsum1x23 = _mm_unpackhi_epi32(vsum1x02, vsum1x13);
87 __m128i vacc1x01 = _mm_unpacklo_epi32(vsum1x01, _mm_setzero_si128());
88 __m128i vacc1x23 = _mm_unpacklo_epi32(vsum1x23, _mm_setzero_si128());
89 const __m128i vsum2x02 = _mm_mul_epu32(vksum0123, vinput_zero_point2);
90 const __m128i vsum2x13 = _mm_mul_epu32(vksum13, vinput_zero_point2);
91 const __m128i vsum2x01 = _mm_unpacklo_epi32(vsum2x02, vsum2x13);
92 const __m128i vsum2x23 = _mm_unpackhi_epi32(vsum2x02, vsum2x13);
93 __m128i vacc2x01 = _mm_unpacklo_epi32(vsum2x01, _mm_setzero_si128());
94 __m128i vacc2x23 = _mm_unpacklo_epi32(vsum2x23, _mm_setzero_si128());
95 const __m128i vsum3x02 = _mm_mul_epu32(vksum0123, vinput_zero_point3);
96 const __m128i vsum3x13 = _mm_mul_epu32(vksum13, vinput_zero_point3);
97 const __m128i vsum3x01 = _mm_unpacklo_epi32(vsum3x02, vsum3x13);
98 const __m128i vsum3x23 = _mm_unpackhi_epi32(vsum3x02, vsum3x13);
99 __m128i vacc3x01 = _mm_unpacklo_epi32(vsum3x01, _mm_setzero_si128());
100 __m128i vacc3x23 = _mm_unpacklo_epi32(vsum3x23, _mm_setzero_si128());
101 w = (const int32_t*) w + 4;
102
103 size_t k = kc;
104 while (k >= 16 * sizeof(int8_t)) {
105 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
106 const __m128i va0x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
107 a0 += 16;
108 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
109 const __m128i va1x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
110 a1 += 16;
111 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
112 const __m128i va2x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
113 a2 += 16;
114 const __m128i va3x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
115 const __m128i va3x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a3 + 8)), vsign_mask);
116 a3 += 16;
117
118 const __m128i vbb01234567x0123 = _mm_load_si128(w);
119 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
120 const __m128i vbs01234567x01 = _mm_srli_epi32(vbb01234567x0123, 4);
121 const __m128i vbs89ABCDEFx01 = _mm_srli_epi32(vbb89ABCDEFx0123, 4);
122 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
123 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
124 const __m128i vb01234567x23 = _mm_and_si128(vbs01234567x01, vmask);
125 const __m128i vb89ABCDEFx23 = _mm_and_si128(vbs89ABCDEFx01, vmask);
126
127 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
128 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
129 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
130 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
131 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
132 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
133 vacc3x01 = _mm_dpbusd_epi32_madd(vacc3x01, va3x01234567, vb01234567x01);
134 vacc3x23 = _mm_dpbusd_epi32_madd(vacc3x23, va3x01234567, vb89ABCDEFx01);
135 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x89ABCDEF, vb01234567x23);
136 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x89ABCDEF, vb89ABCDEFx23);
137 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x89ABCDEF, vb01234567x23);
138 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x89ABCDEF, vb89ABCDEFx23);
139 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x89ABCDEF, vb01234567x23);
140 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x89ABCDEF, vb89ABCDEFx23);
141 vacc3x01 = _mm_dpbusd_epi32_madd(vacc3x01, va3x89ABCDEF, vb01234567x23);
142 vacc3x23 = _mm_dpbusd_epi32_madd(vacc3x23, va3x89ABCDEF, vb89ABCDEFx23);
143
144 w = (const int8_t*) w + 32;
145 k -= 16 * sizeof(int8_t);
146 }
147
148 if (k != 0) {
149 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
150 a0 += 8;
151 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
152 a1 += 8;
153 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
154 a2 += 8;
155 const __m128i va3x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
156 a3 += 8;
157
158 const __m128i vbb01234567x0123 = _mm_load_si128(w);
159 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
160 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
161 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
162
163 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
164 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
165 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
166 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
167 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
168 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
169 vacc3x01 = _mm_dpbusd_epi32_madd(vacc3x01, va3x01234567, vb01234567x01);
170 vacc3x23 = _mm_dpbusd_epi32_madd(vacc3x23, va3x01234567, vb89ABCDEFx01);
171
172 w = (const int8_t*) w + 32;
173 k -= 8 * sizeof(int8_t);
174 }
175
176 // Add adjacent pairs
177 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
178 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
179 __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23);
180 __m128i vacc3x0123 = _mm_hadd_epi32(vacc3x01, vacc3x23);
181
182 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
183 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
184 __m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123);
185 __m128 vout3x0123 = _mm_cvtepi32_ps(vacc3x0123);
186
187 vout0x0123 = _mm_mul_ps(vout0x0123, _mm_set1_ps(quantization_params[0].inv_scale));
188 vout1x0123 = _mm_mul_ps(vout1x0123, _mm_set1_ps(quantization_params[1].inv_scale));
189 vout2x0123 = _mm_mul_ps(vout2x0123, _mm_set1_ps(quantization_params[2].inv_scale));
190 vout3x0123 = _mm_mul_ps(vout3x0123, _mm_set1_ps(quantization_params[3].inv_scale));
191
192 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
193 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
194 w = (const float*) w + 8;
195
196 vout0x0123 = _mm_add_ps(_mm_mul_ps(vout0x0123, vfilter_output_scale0123), vbias0123);
197 vout1x0123 = _mm_add_ps(_mm_mul_ps(vout1x0123, vfilter_output_scale0123), vbias0123);
198 vout2x0123 = _mm_add_ps(_mm_mul_ps(vout2x0123, vfilter_output_scale0123), vbias0123);
199 vout3x0123 = _mm_add_ps(_mm_mul_ps(vout3x0123, vfilter_output_scale0123), vbias0123);
200
201 vout0x0123 = _mm_max_ps(vout0x0123, voutput_min);
202 vout1x0123 = _mm_max_ps(vout1x0123, voutput_min);
203 vout2x0123 = _mm_max_ps(vout2x0123, voutput_min);
204 vout3x0123 = _mm_max_ps(vout3x0123, voutput_min);
205
206 vout0x0123 = _mm_min_ps(vout0x0123, voutput_max);
207 vout1x0123 = _mm_min_ps(vout1x0123, voutput_max);
208 vout2x0123 = _mm_min_ps(vout2x0123, voutput_max);
209 vout3x0123 = _mm_min_ps(vout3x0123, voutput_max);
210
211 if XNN_LIKELY(nc >= 4) {
212 _mm_storeu_ps(c0, vout0x0123);
213 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
214 c0 = (float*) ((uintptr_t) c0 + cn_stride);
215 _mm_storeu_ps(c1, vout1x0123);
216 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
217 c1 = (float*) ((uintptr_t) c1 + cn_stride);
218 _mm_storeu_ps(c2, vout2x0123);
219 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
220 c2 = (float*) ((uintptr_t) c2 + cn_stride);
221 _mm_storeu_ps(c3, vout3x0123);
222 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
223 c3 = (float*) ((uintptr_t) c3 + cn_stride);
224 nc -= 4;
225 } else {
226 if (nc & 2) {
227 _mm_storel_pi((__m64*) c0, vout0x0123);
228 c0 += 2;
229 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
230 _mm_storel_pi((__m64*) c1, vout1x0123);
231 c1 += 2;
232 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
233 _mm_storel_pi((__m64*) c2, vout2x0123);
234 c2 += 2;
235 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
236 _mm_storel_pi((__m64*) c3, vout3x0123);
237 c3 += 2;
238 vout3x0123 = _mm_movehl_ps(vout3x0123, vout3x0123);
239 }
240 if (nc & 1) {
241 _mm_store_ss(c0, vout0x0123);
242 _mm_store_ss(c1, vout1x0123);
243 _mm_store_ss(c2, vout2x0123);
244 _mm_store_ss(c3, vout3x0123);
245 }
246 nc = 0;
247 }
248 } while (nc != 0);
249 }