Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-4x4c8-minmax-avx-ld128.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
22
23 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_4x4c8__avx_ld128(
24 size_t mr,
25 size_t nc,
26 size_t kc,
27 const int8_t* restrict a,
28 size_t a_stride,
29 const void* restrict w,
30 float* restrict c,
31 size_t cm_stride,
32 size_t cn_stride,
33 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
34 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
35 {
36 assert(mr != 0);
37 assert(mr <= 4);
38 assert(nc != 0);
39 assert(kc != 0);
40 assert(kc % sizeof(int8_t) == 0);
41 assert(a != NULL);
42 assert(w != NULL);
43 assert(c != NULL);
44
45 kc = round_up_po2(kc, 8 * sizeof(int8_t));
46 const int8_t* a0 = a;
47 float* c0 = c;
48 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
49 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
50 if XNN_UNPREDICTABLE(mr < 2) {
51 a1 = a0;
52 c1 = c0;
53 }
54 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
55 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
56 if XNN_UNPREDICTABLE(mr <= 2) {
57 a2 = a1;
58 c2 = c1;
59 }
60 const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
61 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
62 if XNN_UNPREDICTABLE(mr != 4) {
63 a3 = a2;
64 c3 = c2;
65 }
66
67 const __m128 vmin = _mm_set1_ps(params->scalar.min);
68 const __m128 vmax = _mm_set1_ps(params->scalar.max);
69 XNN_FORCE_REALIZATION(vmin);
70 XNN_FORCE_REALIZATION(vmax);
71
72
73 do {
74 const __m128i vksum = _mm_load_si128((const __m128i*) w);
75 const __m128i vinput_zero_point0 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[0].zero_point));
76 const __m128i vinput_zero_point1 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[1].zero_point));
77 const __m128i vinput_zero_point2 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[2].zero_point));
78 const __m128i vinput_zero_point3 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[3].zero_point));
79 const __m128i vzero = _mm_setzero_si128();
80 const __m128i vinit0 = _mm_mullo_epi32(vksum, vinput_zero_point0);
81 const __m128i vinit1 = _mm_mullo_epi32(vksum, vinput_zero_point1);
82 const __m128i vinit2 = _mm_mullo_epi32(vksum, vinput_zero_point2);
83 const __m128i vinit3 = _mm_mullo_epi32(vksum, vinput_zero_point3);
84 __m128i vacc0x0 = _mm_blend_epi16(vinit0, vzero, 0xFC);
85 __m128i vacc0x1 = _mm_blend_epi16(vinit0, vzero, 0xF3);
86 __m128i vacc0x2 = _mm_blend_epi16(vinit0, vzero, 0xCF);
87 __m128i vacc0x3 = _mm_blend_epi16(vinit0, vzero, 0x3F);
88 __m128i vacc1x0 = _mm_blend_epi16(vinit1, vzero, 0xFC);
89 __m128i vacc1x1 = _mm_blend_epi16(vinit1, vzero, 0xF3);
90 __m128i vacc1x2 = _mm_blend_epi16(vinit1, vzero, 0xCF);
91 __m128i vacc1x3 = _mm_blend_epi16(vinit1, vzero, 0x3F);
92 __m128i vacc2x0 = _mm_blend_epi16(vinit2, vzero, 0xFC);
93 __m128i vacc2x1 = _mm_blend_epi16(vinit2, vzero, 0xF3);
94 __m128i vacc2x2 = _mm_blend_epi16(vinit2, vzero, 0xCF);
95 __m128i vacc2x3 = _mm_blend_epi16(vinit2, vzero, 0x3F);
96 __m128i vacc3x0 = _mm_blend_epi16(vinit3, vzero, 0xFC);
97 __m128i vacc3x1 = _mm_blend_epi16(vinit3, vzero, 0xF3);
98 __m128i vacc3x2 = _mm_blend_epi16(vinit3, vzero, 0xCF);
99 __m128i vacc3x3 = _mm_blend_epi16(vinit3, vzero, 0x3F);
100 w = (const int32_t*) w + 4;
101
102 size_t k = kc;
103
104
105 while (k >= 8 * sizeof(int8_t)) {
106 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
107 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
108 a0 += 8;
109 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
110 const __m128i vxa1 = _mm_cvtepi8_epi16(va1);
111 a1 += 8;
112 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
113 const __m128i vxa2 = _mm_cvtepi8_epi16(va2);
114 a2 += 8;
115 const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
116 const __m128i vxa3 = _mm_cvtepi8_epi16(va3);
117 a3 += 8;
118
119 const __m128i vb01 = _mm_load_si128((const __m128i*) w);
120
121 const __m128i vxbm1 = _mm_unpackhi_epi8(vb01, vb01);
122 const __m128i vxb0 = _mm_cvtepi8_epi16(vb01);
123 const __m128i vxb1 = _mm_srai_epi16(vxbm1, 8);
124
125 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
126 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
127 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0));
128 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1));
129 vacc2x0 = _mm_add_epi32(vacc2x0, _mm_madd_epi16(vxa2, vxb0));
130 vacc2x1 = _mm_add_epi32(vacc2x1, _mm_madd_epi16(vxa2, vxb1));
131 vacc3x0 = _mm_add_epi32(vacc3x0, _mm_madd_epi16(vxa3, vxb0));
132 vacc3x1 = _mm_add_epi32(vacc3x1, _mm_madd_epi16(vxa3, vxb1));
133 const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
134
135 const __m128i vxbm3 = _mm_unpackhi_epi8(vb23, vb23);
136 const __m128i vxb2 = _mm_cvtepi8_epi16(vb23);
137 const __m128i vxb3 = _mm_srai_epi16(vxbm3, 8);
138
139 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
140 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
141 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2));
142 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3));
143 vacc2x2 = _mm_add_epi32(vacc2x2, _mm_madd_epi16(vxa2, vxb2));
144 vacc2x3 = _mm_add_epi32(vacc2x3, _mm_madd_epi16(vxa2, vxb3));
145 vacc3x2 = _mm_add_epi32(vacc3x2, _mm_madd_epi16(vxa3, vxb2));
146 vacc3x3 = _mm_add_epi32(vacc3x3, _mm_madd_epi16(vxa3, vxb3));
147
148 w = (const int8_t*) w + 32;
149 k -= 8 * sizeof(int8_t);
150 }
151
152 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
153 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
154 const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1);
155 const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3);
156 const __m128i vacc2x01 = _mm_hadd_epi32(vacc2x0, vacc2x1);
157 const __m128i vacc2x23 = _mm_hadd_epi32(vacc2x2, vacc2x3);
158 const __m128i vacc3x01 = _mm_hadd_epi32(vacc3x0, vacc3x1);
159 const __m128i vacc3x23 = _mm_hadd_epi32(vacc3x2, vacc3x3);
160
161 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
162 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
163 __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23);
164 __m128i vacc3x0123 = _mm_hadd_epi32(vacc3x01, vacc3x23);
165
166 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
167 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
168 __m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123);
169 __m128 vout3x0123 = _mm_cvtepi32_ps(vacc3x0123);
170
171 const __m128 vinput_scale0 = _mm_broadcast_ss(&quantization_params[0].inv_scale);
172 const __m128 vinput_scale1 = _mm_broadcast_ss(&quantization_params[1].inv_scale);
173 const __m128 vinput_scale2 = _mm_broadcast_ss(&quantization_params[2].inv_scale);
174 const __m128 vinput_scale3 = _mm_broadcast_ss(&quantization_params[3].inv_scale);
175
176 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
177 vout1x0123 = _mm_mul_ps(vout1x0123, vinput_scale1);
178 vout2x0123 = _mm_mul_ps(vout2x0123, vinput_scale2);
179 vout3x0123 = _mm_mul_ps(vout3x0123, vinput_scale3);
180
181 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
182 vout0x0123 = _mm_mul_ps(vout0x0123, vfilter_output_scale0123);
183 vout1x0123 = _mm_mul_ps(vout1x0123, vfilter_output_scale0123);
184 vout2x0123 = _mm_mul_ps(vout2x0123, vfilter_output_scale0123);
185 vout3x0123 = _mm_mul_ps(vout3x0123, vfilter_output_scale0123);
186
187 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
188 w = (const float*) w + 8;
189 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
190 vout1x0123 = _mm_add_ps(vout1x0123, vbias0123);
191 vout2x0123 = _mm_add_ps(vout2x0123, vbias0123);
192 vout3x0123 = _mm_add_ps(vout3x0123, vbias0123);
193
194 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
195 vout1x0123 = _mm_max_ps(vout1x0123, vmin);
196 vout2x0123 = _mm_max_ps(vout2x0123, vmin);
197 vout3x0123 = _mm_max_ps(vout3x0123, vmin);
198
199 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
200 vout1x0123 = _mm_min_ps(vout1x0123, vmax);
201 vout2x0123 = _mm_min_ps(vout2x0123, vmax);
202 vout3x0123 = _mm_min_ps(vout3x0123, vmax);
203
204 if XNN_LIKELY(nc >= 4) {
205 _mm_storeu_ps(c0, vout0x0123);
206 _mm_storeu_ps(c1, vout1x0123);
207 _mm_storeu_ps(c2, vout2x0123);
208 _mm_storeu_ps(c3, vout3x0123);
209
210 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
211 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
212 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
213 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
214
215 c0 = (float*) ((uintptr_t) c0 + cn_stride);
216 c1 = (float*) ((uintptr_t) c1 + cn_stride);
217 c2 = (float*) ((uintptr_t) c2 + cn_stride);
218 c3 = (float*) ((uintptr_t) c3 + cn_stride);
219
220 nc -= 4;
221 } else {
222 if (nc & 2) {
223 _mm_storel_pi((__m64*) c0, vout0x0123);
224 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
225 c0 += 2;
226 _mm_storel_pi((__m64*) c1, vout1x0123);
227 vout1x0123 = _mm_unpackhi_ps(vout1x0123, vout1x0123);
228 c1 += 2;
229 _mm_storel_pi((__m64*) c2, vout2x0123);
230 vout2x0123 = _mm_unpackhi_ps(vout2x0123, vout2x0123);
231 c2 += 2;
232 _mm_storel_pi((__m64*) c3, vout3x0123);
233 vout3x0123 = _mm_unpackhi_ps(vout3x0123, vout3x0123);
234 c3 += 2;
235 }
236 if (nc & 1) {
237 _mm_store_ss(c0, vout0x0123);
238 _mm_store_ss(c1, vout1x0123);
239 _mm_store_ss(c2, vout2x0123);
240 _mm_store_ss(c3, vout3x0123);
241 }
242 nc = 0;
243 }
244 } while (nc != 0);
245 }