Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-3x4c8-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_3x4c8__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 <= 3);
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
58 const __m128i vsign_mask = _mm_set1_epi8(0x80);
59 XNN_FORCE_REALIZATION(vsign_mask);
60 const __m128i vinput_zero_point0 = _mm_set1_epi32((int) quantization_params[0].zero_point + 128);
61 const __m128i vinput_zero_point1 = _mm_set1_epi32((int) quantization_params[1].zero_point + 128);
62 const __m128i vinput_zero_point2 = _mm_set1_epi32((int) quantization_params[2].zero_point + 128);
63 const __m128 voutput_min = _mm_set1_ps(params->scalar.min);
64 const __m128 voutput_max = _mm_set1_ps(params->scalar.max);
65 const __m128i vmask = _mm_set1_epi8(0x0F);
66 XNN_FORCE_REALIZATION(vmask);
67 do {
68 const __m128i vksum0123 = _mm_load_si128(w);
69 const __m128i vsum0x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point0);
70 __m128i vacc0x01 = _mm_unpacklo_epi32(vsum0x0123, _mm_setzero_si128());
71 __m128i vacc0x23 = _mm_unpackhi_epi32(vsum0x0123, _mm_setzero_si128());
72 const __m128i vsum1x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point1);
73 __m128i vacc1x01 = _mm_unpacklo_epi32(vsum1x0123, _mm_setzero_si128());
74 __m128i vacc1x23 = _mm_unpackhi_epi32(vsum1x0123, _mm_setzero_si128());
75 const __m128i vsum2x0123 = _mm_mullo_epi32(vksum0123, vinput_zero_point2);
76 __m128i vacc2x01 = _mm_unpacklo_epi32(vsum2x0123, _mm_setzero_si128());
77 __m128i vacc2x23 = _mm_unpackhi_epi32(vsum2x0123, _mm_setzero_si128());
78 w = (const int32_t*) w + 4;
79
80 size_t k = kc;
81 while (k >= 16 * sizeof(int8_t)) {
82 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
83 const __m128i va0x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
84 a0 += 16;
85 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
86 const __m128i va1x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
87 a1 += 16;
88 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
89 const __m128i va2x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
90 a2 += 16;
91
92 const __m128i vbb01234567x0123 = _mm_load_si128(w);
93 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
94 const __m128i vbs01234567x01 = _mm_srli_epi32(vbb01234567x0123, 4);
95 const __m128i vbs89ABCDEFx01 = _mm_srli_epi32(vbb89ABCDEFx0123, 4);
96 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
97 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
98 const __m128i vb01234567x23 = _mm_and_si128(vbs01234567x01, vmask);
99 const __m128i vb89ABCDEFx23 = _mm_and_si128(vbs89ABCDEFx01, vmask);
100
101 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
102 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
103 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
104 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
105 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
106 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
107 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x89ABCDEF, vb01234567x23);
108 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x89ABCDEF, vb89ABCDEFx23);
109 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x89ABCDEF, vb01234567x23);
110 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x89ABCDEF, vb89ABCDEFx23);
111 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x89ABCDEF, vb01234567x23);
112 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x89ABCDEF, vb89ABCDEFx23);
113
114 w = (const int8_t*) w + 32;
115 k -= 16 * sizeof(int8_t);
116 }
117
118 if (k != 0) {
119 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
120 a0 += 8;
121 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
122 a1 += 8;
123 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
124 a2 += 8;
125
126 const __m128i vbb01234567x0123 = _mm_load_si128(w);
127 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
128 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
129 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
130
131 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
132 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
133 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
134 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
135 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
136 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
137
138 w = (const int8_t*) w + 32;
139 k -= 8 * sizeof(int8_t);
140 }
141
142 // Add adjacent pairs
143 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
144 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
145 __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23);
146
147 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
148 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
149 __m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123);
150
151 vout0x0123 = _mm_mul_ps(vout0x0123, _mm_set1_ps(quantization_params[0].inv_scale));
152 vout1x0123 = _mm_mul_ps(vout1x0123, _mm_set1_ps(quantization_params[1].inv_scale));
153 vout2x0123 = _mm_mul_ps(vout2x0123, _mm_set1_ps(quantization_params[2].inv_scale));
154
155 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
156 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
157 w = (const float*) w + 8;
158
159 vout0x0123 = _mm_add_ps(_mm_mul_ps(vout0x0123, vfilter_output_scale0123), vbias0123);
160 vout1x0123 = _mm_add_ps(_mm_mul_ps(vout1x0123, vfilter_output_scale0123), vbias0123);
161 vout2x0123 = _mm_add_ps(_mm_mul_ps(vout2x0123, vfilter_output_scale0123), vbias0123);
162
163 vout0x0123 = _mm_max_ps(vout0x0123, voutput_min);
164 vout1x0123 = _mm_max_ps(vout1x0123, voutput_min);
165 vout2x0123 = _mm_max_ps(vout2x0123, voutput_min);
166
167 vout0x0123 = _mm_min_ps(vout0x0123, voutput_max);
168 vout1x0123 = _mm_min_ps(vout1x0123, voutput_max);
169 vout2x0123 = _mm_min_ps(vout2x0123, voutput_max);
170
171 if XNN_LIKELY(nc >= 4) {
172 _mm_storeu_ps(c0, vout0x0123);
173 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
174 c0 = (float*) ((uintptr_t) c0 + cn_stride);
175 _mm_storeu_ps(c1, vout1x0123);
176 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
177 c1 = (float*) ((uintptr_t) c1 + cn_stride);
178 _mm_storeu_ps(c2, vout2x0123);
179 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
180 c2 = (float*) ((uintptr_t) c2 + cn_stride);
181 nc -= 4;
182 } else {
183 if (nc & 2) {
184 _mm_storel_pi((__m64*) c0, vout0x0123);
185 c0 += 2;
186 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
187 _mm_storel_pi((__m64*) c1, vout1x0123);
188 c1 += 2;
189 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
190 _mm_storel_pi((__m64*) c2, vout2x0123);
191 c2 += 2;
192 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
193 }
194 if (nc & 1) {
195 _mm_store_ss(c0, vout0x0123);
196 _mm_store_ss(c1, vout1x0123);
197 _mm_store_ss(c2, vout2x0123);
198 }
199 nc = 0;
200 }
201 } while (nc != 0);
202 }