Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-4x4c8-minmax-sse41-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 <smmintrin.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__sse41_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 vsum0x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point0);
77 __m128i vacc0x01 = _mm_unpacklo_epi32(vsum0x0123, _mm_setzero_si128());
78 __m128i vacc0x23 = _mm_unpackhi_epi32(vsum0x0123, _mm_setzero_si128());
79 const __m128i vsum1x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point1);
80 __m128i vacc1x01 = _mm_unpacklo_epi32(vsum1x0123, _mm_setzero_si128());
81 __m128i vacc1x23 = _mm_unpackhi_epi32(vsum1x0123, _mm_setzero_si128());
82 const __m128i vsum2x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point2);
83 __m128i vacc2x01 = _mm_unpacklo_epi32(vsum2x0123, _mm_setzero_si128());
84 __m128i vacc2x23 = _mm_unpackhi_epi32(vsum2x0123, _mm_setzero_si128());
85 const __m128i vsum3x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point3);
86 __m128i vacc3x01 = _mm_unpacklo_epi32(vsum3x0123, _mm_setzero_si128());
87 __m128i vacc3x23 = _mm_unpackhi_epi32(vsum3x0123, _mm_setzero_si128());
88 w = (const int32_t*) w + 4;
89
90 size_t k = kc;
91 while (k >= 16 * sizeof(int8_t)) {
92 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
93 const __m128i va0x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
94 a0 += 16;
95 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
96 const __m128i va1x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
97 a1 += 16;
98 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
99 const __m128i va2x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
100 a2 += 16;
101 const __m128i va3x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
102 const __m128i va3x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a3 + 8)), vsign_mask);
103 a3 += 16;
104
105 const __m128i vbb01234567x0123 = _mm_load_si128(w);
106 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
107 const __m128i vbs01234567x01 = _mm_srli_epi32(vbb01234567x0123, 4);
108 const __m128i vbs89ABCDEFx01 = _mm_srli_epi32(vbb89ABCDEFx0123, 4);
109 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
110 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
111 const __m128i vb01234567x23 = _mm_and_si128(vbs01234567x01, vmask);
112 const __m128i vb89ABCDEFx23 = _mm_and_si128(vbs89ABCDEFx01, vmask);
113
114 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
115 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
116 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
117 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
118 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
119 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
120 vacc3x01 = _mm_dpbusd_epi32_madd(vacc3x01, va3x01234567, vb01234567x01);
121 vacc3x23 = _mm_dpbusd_epi32_madd(vacc3x23, va3x01234567, vb89ABCDEFx01);
122 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x89ABCDEF, vb01234567x23);
123 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x89ABCDEF, vb89ABCDEFx23);
124 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x89ABCDEF, vb01234567x23);
125 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x89ABCDEF, vb89ABCDEFx23);
126 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x89ABCDEF, vb01234567x23);
127 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x89ABCDEF, vb89ABCDEFx23);
128 vacc3x01 = _mm_dpbusd_epi32_madd(vacc3x01, va3x89ABCDEF, vb01234567x23);
129 vacc3x23 = _mm_dpbusd_epi32_madd(vacc3x23, va3x89ABCDEF, vb89ABCDEFx23);
130
131 w = (const int8_t*) w + 32;
132 k -= 16 * sizeof(int8_t);
133 }
134
135 if (k != 0) {
136 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
137 a0 += 8;
138 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
139 a1 += 8;
140 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
141 a2 += 8;
142 const __m128i va3x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
143 a3 += 8;
144
145 const __m128i vbb01234567x0123 = _mm_load_si128(w);
146 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
147 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
148 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
149
150 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
151 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
152 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
153 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
154 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
155 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
156 vacc3x01 = _mm_dpbusd_epi32_madd(vacc3x01, va3x01234567, vb01234567x01);
157 vacc3x23 = _mm_dpbusd_epi32_madd(vacc3x23, va3x01234567, vb89ABCDEFx01);
158
159 w = (const int8_t*) w + 32;
160 k -= 8 * sizeof(int8_t);
161 }
162
163 // Add adjacent pairs
164 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
165 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
166 __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23);
167 __m128i vacc3x0123 = _mm_hadd_epi32(vacc3x01, vacc3x23);
168
169 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
170 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
171 __m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123);
172 __m128 vout3x0123 = _mm_cvtepi32_ps(vacc3x0123);
173
174 vout0x0123 = _mm_mul_ps(vout0x0123, _mm_set1_ps(quantization_params[0].inv_scale));
175 vout1x0123 = _mm_mul_ps(vout1x0123, _mm_set1_ps(quantization_params[1].inv_scale));
176 vout2x0123 = _mm_mul_ps(vout2x0123, _mm_set1_ps(quantization_params[2].inv_scale));
177 vout3x0123 = _mm_mul_ps(vout3x0123, _mm_set1_ps(quantization_params[3].inv_scale));
178
179 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
180 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
181 w = (const float*) w + 8;
182
183 vout0x0123 = _mm_add_ps(_mm_mul_ps(vout0x0123, vfilter_output_scale0123), vbias0123);
184 vout1x0123 = _mm_add_ps(_mm_mul_ps(vout1x0123, vfilter_output_scale0123), vbias0123);
185 vout2x0123 = _mm_add_ps(_mm_mul_ps(vout2x0123, vfilter_output_scale0123), vbias0123);
186 vout3x0123 = _mm_add_ps(_mm_mul_ps(vout3x0123, vfilter_output_scale0123), vbias0123);
187
188 vout0x0123 = _mm_max_ps(vout0x0123, voutput_min);
189 vout1x0123 = _mm_max_ps(vout1x0123, voutput_min);
190 vout2x0123 = _mm_max_ps(vout2x0123, voutput_min);
191 vout3x0123 = _mm_max_ps(vout3x0123, voutput_min);
192
193 vout0x0123 = _mm_min_ps(vout0x0123, voutput_max);
194 vout1x0123 = _mm_min_ps(vout1x0123, voutput_max);
195 vout2x0123 = _mm_min_ps(vout2x0123, voutput_max);
196 vout3x0123 = _mm_min_ps(vout3x0123, voutput_max);
197
198 if XNN_LIKELY(nc >= 4) {
199 _mm_storeu_ps(c0, vout0x0123);
200 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
201 c0 = (float*) ((uintptr_t) c0 + cn_stride);
202 _mm_storeu_ps(c1, vout1x0123);
203 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
204 c1 = (float*) ((uintptr_t) c1 + cn_stride);
205 _mm_storeu_ps(c2, vout2x0123);
206 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
207 c2 = (float*) ((uintptr_t) c2 + cn_stride);
208 _mm_storeu_ps(c3, vout3x0123);
209 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
210 c3 = (float*) ((uintptr_t) c3 + cn_stride);
211 nc -= 4;
212 } else {
213 if (nc & 2) {
214 _mm_storel_pi((__m64*) c0, vout0x0123);
215 c0 += 2;
216 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
217 _mm_storel_pi((__m64*) c1, vout1x0123);
218 c1 += 2;
219 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
220 _mm_storel_pi((__m64*) c2, vout2x0123);
221 c2 += 2;
222 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
223 _mm_storel_pi((__m64*) c3, vout3x0123);
224 c3 += 2;
225 vout3x0123 = _mm_movehl_ps(vout3x0123, vout3x0123);
226 }
227 if (nc & 1) {
228 _mm_store_ss(c0, vout0x0123);
229 _mm_store_ss(c1, vout1x0123);
230 _mm_store_ss(c2, vout2x0123);
231 _mm_store_ss(c3, vout3x0123);
232 }
233 nc = 0;
234 }
235 } while (nc != 0);
236 }