Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-3x4c8-minmax-ssse3-madd-prfm.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 #include "xnnpack/prefetch.h"
19
20
21 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_3x4c8__ssse3_madd_prfm(
22 size_t mr,
23 size_t nc,
24 size_t kc,
25 const int8_t* restrict a,
26 size_t a_stride,
27 const void* restrict w,
28 float* restrict c,
29 size_t cm_stride,
30 size_t cn_stride,
31 const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
32 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
33 {
34 assert(mr != 0);
35 assert(mr <= 3);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(kc % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 kc = round_up_po2(kc, 8 * sizeof(int8_t));
44 const int8_t* a0 = a;
45 float* c0 = c;
46 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
47 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
48 if XNN_UNPREDICTABLE(mr < 2) {
49 a1 = a0;
50 c1 = c0;
51 }
52 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
53 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
54 if XNN_UNPREDICTABLE(mr <= 2) {
55 a2 = a1;
56 c2 = c1;
57 }
58
59 const __m128i vsign_mask = _mm_set1_epi8(0x80);
60 XNN_FORCE_REALIZATION(vsign_mask);
61 const __m128i vinput_zero_point0 = _mm_set1_epi32((int) quantization_params[0].zero_point + 128);
62 const __m128i vinput_zero_point1 = _mm_set1_epi32((int) quantization_params[1].zero_point + 128);
63 const __m128i vinput_zero_point2 = _mm_set1_epi32((int) quantization_params[2].zero_point + 128);
64 const __m128 voutput_min = _mm_set1_ps(params->scalar.min);
65 const __m128 voutput_max = _mm_set1_ps(params->scalar.max);
66 const __m128i vmask = _mm_set1_epi8(0x0F);
67 XNN_FORCE_REALIZATION(vmask);
68 do {
69 const __m128i vksum0123 = _mm_load_si128(w);
70 const __m128i vksum13 = _mm_shuffle_epi32(vksum0123, 0xF5);
71 const __m128i vsum0x02 = _mm_mul_epu32(vksum0123, vinput_zero_point0);
72 const __m128i vsum0x13 = _mm_mul_epu32(vksum13, vinput_zero_point0);
73 const __m128i vsum0x01 = _mm_unpacklo_epi32(vsum0x02, vsum0x13);
74 const __m128i vsum0x23 = _mm_unpackhi_epi32(vsum0x02, vsum0x13);
75 __m128i vacc0x01 = _mm_unpacklo_epi32(vsum0x01, _mm_setzero_si128());
76 __m128i vacc0x23 = _mm_unpacklo_epi32(vsum0x23, _mm_setzero_si128());
77 const __m128i vsum1x02 = _mm_mul_epu32(vksum0123, vinput_zero_point1);
78 const __m128i vsum1x13 = _mm_mul_epu32(vksum13, vinput_zero_point1);
79 const __m128i vsum1x01 = _mm_unpacklo_epi32(vsum1x02, vsum1x13);
80 const __m128i vsum1x23 = _mm_unpackhi_epi32(vsum1x02, vsum1x13);
81 __m128i vacc1x01 = _mm_unpacklo_epi32(vsum1x01, _mm_setzero_si128());
82 __m128i vacc1x23 = _mm_unpacklo_epi32(vsum1x23, _mm_setzero_si128());
83 const __m128i vsum2x02 = _mm_mul_epu32(vksum0123, vinput_zero_point2);
84 const __m128i vsum2x13 = _mm_mul_epu32(vksum13, vinput_zero_point2);
85 const __m128i vsum2x01 = _mm_unpacklo_epi32(vsum2x02, vsum2x13);
86 const __m128i vsum2x23 = _mm_unpackhi_epi32(vsum2x02, vsum2x13);
87 __m128i vacc2x01 = _mm_unpacklo_epi32(vsum2x01, _mm_setzero_si128());
88 __m128i vacc2x23 = _mm_unpacklo_epi32(vsum2x23, _mm_setzero_si128());
89 w = (const int32_t*) w + 4;
90
91 size_t k = kc;
92 while (k >= 16 * sizeof(int8_t)) {
93 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
94 const __m128i va0x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
95 a0 += 16;
96 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
97 const __m128i va1x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
98 a1 += 16;
99 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
100 const __m128i va2x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
101 a2 += 16;
102
103 const __m128i vbb01234567x0123 = _mm_load_si128(w);
104 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
105 const __m128i vbs01234567x01 = _mm_srli_epi32(vbb01234567x0123, 4);
106 const __m128i vbs89ABCDEFx01 = _mm_srli_epi32(vbb89ABCDEFx0123, 4);
107 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
108 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
109 const __m128i vb01234567x23 = _mm_and_si128(vbs01234567x01, vmask);
110 const __m128i vb89ABCDEFx23 = _mm_and_si128(vbs89ABCDEFx01, vmask);
111
112 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
113 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
114 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
115 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
116 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
117 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
118 xnn_prefetch_to_l1((const int8_t*) w + 960);
119 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x89ABCDEF, vb01234567x23);
120 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x89ABCDEF, vb89ABCDEFx23);
121 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x89ABCDEF, vb01234567x23);
122 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x89ABCDEF, vb89ABCDEFx23);
123 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x89ABCDEF, vb01234567x23);
124 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x89ABCDEF, vb89ABCDEFx23);
125
126 w = (const int8_t*) w + 32;
127 k -= 16 * sizeof(int8_t);
128 }
129
130 if (k != 0) {
131 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
132 a0 += 8;
133 const __m128i va1x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
134 a1 += 8;
135 const __m128i va2x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
136 a2 += 8;
137
138 const __m128i vbb01234567x0123 = _mm_load_si128(w);
139 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
140 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
141 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
142
143 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
144 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
145 vacc1x01 = _mm_dpbusd_epi32_madd(vacc1x01, va1x01234567, vb01234567x01);
146 vacc1x23 = _mm_dpbusd_epi32_madd(vacc1x23, va1x01234567, vb89ABCDEFx01);
147 vacc2x01 = _mm_dpbusd_epi32_madd(vacc2x01, va2x01234567, vb01234567x01);
148 vacc2x23 = _mm_dpbusd_epi32_madd(vacc2x23, va2x01234567, vb89ABCDEFx01);
149 xnn_prefetch_to_l1((const int8_t*) w + 960);
150
151 w = (const int8_t*) w + 32;
152 k -= 8 * sizeof(int8_t);
153 }
154
155 // Add adjacent pairs
156 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
157 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
158 __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23);
159
160 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
161 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
162 __m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123);
163
164 vout0x0123 = _mm_mul_ps(vout0x0123, _mm_set1_ps(quantization_params[0].inv_scale));
165 vout1x0123 = _mm_mul_ps(vout1x0123, _mm_set1_ps(quantization_params[1].inv_scale));
166 vout2x0123 = _mm_mul_ps(vout2x0123, _mm_set1_ps(quantization_params[2].inv_scale));
167
168 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
169 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
170 w = (const float*) w + 8;
171
172 vout0x0123 = _mm_add_ps(_mm_mul_ps(vout0x0123, vfilter_output_scale0123), vbias0123);
173 vout1x0123 = _mm_add_ps(_mm_mul_ps(vout1x0123, vfilter_output_scale0123), vbias0123);
174 vout2x0123 = _mm_add_ps(_mm_mul_ps(vout2x0123, vfilter_output_scale0123), vbias0123);
175
176 vout0x0123 = _mm_max_ps(vout0x0123, voutput_min);
177 vout1x0123 = _mm_max_ps(vout1x0123, voutput_min);
178 vout2x0123 = _mm_max_ps(vout2x0123, voutput_min);
179
180 vout0x0123 = _mm_min_ps(vout0x0123, voutput_max);
181 vout1x0123 = _mm_min_ps(vout1x0123, voutput_max);
182 vout2x0123 = _mm_min_ps(vout2x0123, voutput_max);
183
184 if XNN_LIKELY(nc >= 4) {
185 _mm_storeu_ps(c0, vout0x0123);
186 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
187 c0 = (float*) ((uintptr_t) c0 + cn_stride);
188 _mm_storeu_ps(c1, vout1x0123);
189 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
190 c1 = (float*) ((uintptr_t) c1 + cn_stride);
191 _mm_storeu_ps(c2, vout2x0123);
192 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
193 c2 = (float*) ((uintptr_t) c2 + cn_stride);
194 nc -= 4;
195 } else {
196 if (nc & 2) {
197 _mm_storel_pi((__m64*) c0, vout0x0123);
198 c0 += 2;
199 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
200 _mm_storel_pi((__m64*) c1, vout1x0123);
201 c1 += 2;
202 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
203 _mm_storel_pi((__m64*) c2, vout2x0123);
204 c2 += 2;
205 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
206 }
207 if (nc & 1) {
208 _mm_store_ss(c0, vout0x0123);
209 _mm_store_ss(c1, vout1x0123);
210 _mm_store_ss(c2, vout2x0123);
211 }
212 nc = 0;
213 }
214 } while (nc != 0);
215 }