Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-3x4c8-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_3x4c8__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 <= 3);
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
61 const __m128 vmin = _mm_set1_ps(params->scalar.min);
62 const __m128 vmax = _mm_set1_ps(params->scalar.max);
63 XNN_FORCE_REALIZATION(vmin);
64 XNN_FORCE_REALIZATION(vmax);
65
66
67 do {
68 const __m128i vksum = _mm_load_si128((const __m128i*) w);
69 const __m128i vinput_zero_point0 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[0].zero_point));
70 const __m128i vinput_zero_point1 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[1].zero_point));
71 const __m128i vinput_zero_point2 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[2].zero_point));
72 const __m128i vzero = _mm_setzero_si128();
73 const __m128i vinit0 = _mm_mullo_epi32(vksum, vinput_zero_point0);
74 const __m128i vinit1 = _mm_mullo_epi32(vksum, vinput_zero_point1);
75 const __m128i vinit2 = _mm_mullo_epi32(vksum, vinput_zero_point2);
76 __m128i vacc0x0 = _mm_blend_epi16(vinit0, vzero, 0xFC);
77 __m128i vacc0x1 = _mm_blend_epi16(vinit0, vzero, 0xF3);
78 __m128i vacc0x2 = _mm_blend_epi16(vinit0, vzero, 0xCF);
79 __m128i vacc0x3 = _mm_blend_epi16(vinit0, vzero, 0x3F);
80 __m128i vacc1x0 = _mm_blend_epi16(vinit1, vzero, 0xFC);
81 __m128i vacc1x1 = _mm_blend_epi16(vinit1, vzero, 0xF3);
82 __m128i vacc1x2 = _mm_blend_epi16(vinit1, vzero, 0xCF);
83 __m128i vacc1x3 = _mm_blend_epi16(vinit1, vzero, 0x3F);
84 __m128i vacc2x0 = _mm_blend_epi16(vinit2, vzero, 0xFC);
85 __m128i vacc2x1 = _mm_blend_epi16(vinit2, vzero, 0xF3);
86 __m128i vacc2x2 = _mm_blend_epi16(vinit2, vzero, 0xCF);
87 __m128i vacc2x3 = _mm_blend_epi16(vinit2, vzero, 0x3F);
88 w = (const int32_t*) w + 4;
89
90 size_t k = kc;
91
92
93 while (k >= 8 * sizeof(int8_t)) {
94 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
95 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
96 a0 += 8;
97 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
98 const __m128i vxa1 = _mm_cvtepi8_epi16(va1);
99 a1 += 8;
100 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
101 const __m128i vxa2 = _mm_cvtepi8_epi16(va2);
102 a2 += 8;
103
104 const __m128i vb01 = _mm_load_si128((const __m128i*) w);
105
106 const __m128i vxbm1 = _mm_unpackhi_epi8(vb01, vb01);
107 const __m128i vxb0 = _mm_cvtepi8_epi16(vb01);
108 const __m128i vxb1 = _mm_srai_epi16(vxbm1, 8);
109
110 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
111 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
112 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0));
113 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1));
114 vacc2x0 = _mm_add_epi32(vacc2x0, _mm_madd_epi16(vxa2, vxb0));
115 vacc2x1 = _mm_add_epi32(vacc2x1, _mm_madd_epi16(vxa2, vxb1));
116 const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
117
118 const __m128i vxbm3 = _mm_unpackhi_epi8(vb23, vb23);
119 const __m128i vxb2 = _mm_cvtepi8_epi16(vb23);
120 const __m128i vxb3 = _mm_srai_epi16(vxbm3, 8);
121
122 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
123 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
124 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2));
125 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3));
126 vacc2x2 = _mm_add_epi32(vacc2x2, _mm_madd_epi16(vxa2, vxb2));
127 vacc2x3 = _mm_add_epi32(vacc2x3, _mm_madd_epi16(vxa2, vxb3));
128
129 w = (const int8_t*) w + 32;
130 k -= 8 * sizeof(int8_t);
131 }
132
133 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
134 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
135 const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1);
136 const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3);
137 const __m128i vacc2x01 = _mm_hadd_epi32(vacc2x0, vacc2x1);
138 const __m128i vacc2x23 = _mm_hadd_epi32(vacc2x2, vacc2x3);
139
140 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
141 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
142 __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23);
143
144 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
145 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
146 __m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123);
147
148 const __m128 vinput_scale0 = _mm_broadcast_ss(&quantization_params[0].inv_scale);
149 const __m128 vinput_scale1 = _mm_broadcast_ss(&quantization_params[1].inv_scale);
150 const __m128 vinput_scale2 = _mm_broadcast_ss(&quantization_params[2].inv_scale);
151
152 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
153 vout1x0123 = _mm_mul_ps(vout1x0123, vinput_scale1);
154 vout2x0123 = _mm_mul_ps(vout2x0123, vinput_scale2);
155
156 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
157 vout0x0123 = _mm_mul_ps(vout0x0123, vfilter_output_scale0123);
158 vout1x0123 = _mm_mul_ps(vout1x0123, vfilter_output_scale0123);
159 vout2x0123 = _mm_mul_ps(vout2x0123, vfilter_output_scale0123);
160
161 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
162 w = (const float*) w + 8;
163 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
164 vout1x0123 = _mm_add_ps(vout1x0123, vbias0123);
165 vout2x0123 = _mm_add_ps(vout2x0123, vbias0123);
166
167 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
168 vout1x0123 = _mm_max_ps(vout1x0123, vmin);
169 vout2x0123 = _mm_max_ps(vout2x0123, vmin);
170
171 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
172 vout1x0123 = _mm_min_ps(vout1x0123, vmax);
173 vout2x0123 = _mm_min_ps(vout2x0123, vmax);
174
175 if XNN_LIKELY(nc >= 4) {
176 _mm_storeu_ps(c0, vout0x0123);
177 _mm_storeu_ps(c1, vout1x0123);
178 _mm_storeu_ps(c2, vout2x0123);
179
180 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
181 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
182 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
183
184 c0 = (float*) ((uintptr_t) c0 + cn_stride);
185 c1 = (float*) ((uintptr_t) c1 + cn_stride);
186 c2 = (float*) ((uintptr_t) c2 + cn_stride);
187
188 nc -= 4;
189 } else {
190 if (nc & 2) {
191 _mm_storel_pi((__m64*) c0, vout0x0123);
192 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
193 c0 += 2;
194 _mm_storel_pi((__m64*) c1, vout1x0123);
195 vout1x0123 = _mm_unpackhi_ps(vout1x0123, vout1x0123);
196 c1 += 2;
197 _mm_storel_pi((__m64*) c2, vout2x0123);
198 vout2x0123 = _mm_unpackhi_ps(vout2x0123, vout2x0123);
199 c2 += 2;
200 }
201 if (nc & 1) {
202 _mm_store_ss(c0, vout0x0123);
203 _mm_store_ss(c1, vout1x0123);
204 _mm_store_ss(c2, vout2x0123);
205 }
206 nc = 0;
207 }
208 } while (nc != 0);
209 }