Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-1x4c8-minmax-avx-ld64.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 #include "xnnpack/unaligned.h"
22
23
24 void xnn_qd8_f32_qb4w_gemm_minmax_ukernel_1x4c8__avx_ld64(
25 size_t mr,
26 size_t nc,
27 size_t kc,
28 const int8_t* restrict a,
29 size_t a_stride,
30 const void* restrict w,
31 float* restrict c,
32 size_t cm_stride,
33 size_t cn_stride,
34 const struct xnn_f32_qb4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
35 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
36 {
37 assert(mr != 0);
38 assert(mr <= 1);
39 assert(nc != 0);
40 assert(kc != 0);
41 assert(kc % sizeof(int8_t) == 0);
42 assert(a != NULL);
43 assert(w != NULL);
44 assert(c != NULL);
45
46 size_t bl = params->scalar.blocksize;
47 assert(bl <= round_up_po2(kc, 2));
48 assert(bl != 0);
49 assert(bl % 32 == 0);
50 kc = round_up_po2(kc, 8 * sizeof(int8_t));
51 const int8_t* a0 = a;
52 float* c0 = c;
53
54 const __m128 vmin = _mm_set1_ps(params->scalar.min);
55 const __m128 vmax = _mm_set1_ps(params->scalar.max);
56 XNN_FORCE_REALIZATION(vmin);
57 XNN_FORCE_REALIZATION(vmax);
58
59 const __m128i vmask = _mm_set1_epi8(0xF0);
60 XNN_FORCE_REALIZATION(vmask);
61
62 do {
63 const __m128 vksum = _mm_load_ps((const float*) w);
64 const __m128i vinput_zero_point0 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[0].zero_point));
65
66 __m128 vinput_zero_point0_float = _mm_cvtepi32_ps(vinput_zero_point0);
67 __m128 vout0x0123 = _mm_mul_ps(vksum, vinput_zero_point0_float);
68 w = (const int32_t*) w + 4;
69
70 for (size_t kb=0; kb < kc; kb += bl) {
71 __m128i vacc0x0 = _mm_setzero_si128();
72 __m128i vacc0x1 = _mm_setzero_si128();
73 __m128i vacc0x2 = _mm_setzero_si128();
74 __m128i vacc0x3 = _mm_setzero_si128();
75 size_t k = bl;
76
77 while (k >= 16 * sizeof(int8_t)) {
78 const __m128i va0c0 = _mm_loadl_epi64((const __m128i*) a0);
79 const __m128i vxa0c0 = _mm_cvtepi8_epi16(va0c0);
80 a0 += 8;
81
82 const __m128i vb0c01 = _mm_loadl_epi64((const __m128i*) w);
83 const __m128i vbs0c0 = _mm_slli_epi32(vb0c01, 4);
84 const __m128i vb0c0 = _mm_and_si128(vbs0c0, vmask);
85 const __m128i vxb0c0 = _mm_cvtepi8_epi16(vb0c0);
86
87 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c0, vxb0c0));
88 const __m128i vb1c01 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8));
89 const __m128i vbs1c0 = _mm_slli_epi32(vb1c01, 4);
90 const __m128i vb1c0 = _mm_and_si128(vbs1c0, vmask);
91 const __m128i vxb1c0 = _mm_cvtepi8_epi16(vb1c0);
92
93 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c0, vxb1c0));
94 const __m128i vb2c01 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16));
95 const __m128i vbs2c0 = _mm_slli_epi32(vb2c01, 4);
96 const __m128i vb2c0 = _mm_and_si128(vbs2c0, vmask);
97 const __m128i vxb2c0 = _mm_cvtepi8_epi16(vb2c0);
98
99 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c0, vxb2c0));
100 const __m128i vb3c01 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24));
101 const __m128i vbs3c0 = _mm_slli_epi32(vb3c01, 4);
102 const __m128i vb3c0 = _mm_and_si128(vbs3c0, vmask);
103 const __m128i vxb3c0 = _mm_cvtepi8_epi16(vb3c0);
104
105 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c0, vxb3c0));
106
107 const __m128i va0c1 = _mm_loadl_epi64((const __m128i*) a0);
108 const __m128i vxa0c1 = _mm_cvtepi8_epi16(va0c1);
109 a0 += 8;
110
111 const __m128i vb0c1 = _mm_and_si128(vb0c01, vmask);
112 const __m128i vxb0c1 = _mm_cvtepi8_epi16(vb0c1);
113
114 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c1, vxb0c1));
115 const __m128i vb1c1 = _mm_and_si128(vb1c01, vmask);
116 const __m128i vxb1c1 = _mm_cvtepi8_epi16(vb1c1);
117
118 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c1, vxb1c1));
119 const __m128i vb2c1 = _mm_and_si128(vb2c01, vmask);
120 const __m128i vxb2c1 = _mm_cvtepi8_epi16(vb2c1);
121
122 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c1, vxb2c1));
123 const __m128i vb3c1 = _mm_and_si128(vb3c01, vmask);
124 const __m128i vxb3c1 = _mm_cvtepi8_epi16(vb3c1);
125
126 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c1, vxb3c1));
127
128
129 w = (const int8_t*) w + 32;
130 k -= 16 * sizeof(int8_t);
131 }
132
133 while (k >= 8 * sizeof(int8_t)) {
134 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
135 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
136 a0 += 8;
137
138 __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
139 vb0 = _mm_slli_epi32(vb0, 4);
140 vb0 = _mm_and_si128(vb0, vmask);
141
142 const __m128i vxb0 = _mm_cvtepi8_epi16(vb0);
143
144 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
145 __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8));
146 vb1 = _mm_slli_epi32(vb1, 4);
147 vb1 = _mm_and_si128(vb1, vmask);
148
149 const __m128i vxb1 = _mm_cvtepi8_epi16(vb1);
150
151 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
152 __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16));
153 vb2 = _mm_slli_epi32(vb2, 4);
154 vb2 = _mm_and_si128(vb2, vmask);
155
156 const __m128i vxb2 = _mm_cvtepi8_epi16(vb2);
157
158 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
159 __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24));
160 vb3 = _mm_slli_epi32(vb3, 4);
161 vb3 = _mm_and_si128(vb3, vmask);
162
163 const __m128i vxb3 = _mm_cvtepi8_epi16(vb3);
164
165 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
166
167 w = (const int8_t*) w + 32;
168 k -= 8 * sizeof(int8_t);
169 }
170 // accumulate float
171 const __m128 vfilter_output_scale0123 = _mm_castsi128_ps(_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_loadl_epi64((const __m128i*) w)), 16));
172 w = (const uint16_t*) w + 4;
173
174 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
175 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
176
177 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
178
179 vout0x0123 = _mm_add_ps(vout0x0123, _mm_mul_ps(_mm_cvtepi32_ps(vacc0x0123), vfilter_output_scale0123));
180 }
181
182 const __m128 vinput_scale0 = _mm_broadcast_ss(&quantization_params[0].inv_scale);
183
184 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
185
186
187 const __m128 vbias0123 = _mm_loadu_ps((const float*) w);
188 w = (const float*) w + 4;
189 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
190
191 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
192
193 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
194
195 if XNN_LIKELY(nc >= 4) {
196 _mm_storeu_ps(c0, vout0x0123);
197
198 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
199
200 c0 = (float*) ((uintptr_t) c0 + cn_stride);
201
202 nc -= 4;
203 } else {
204 if (nc & 2) {
205 _mm_storel_pi((__m64*) c0, vout0x0123);
206 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
207 c0 += 2;
208 }
209 if (nc & 1) {
210 _mm_store_ss(c0, vout0x0123);
211 }
212 nc = 0;
213 }
214 } while (nc != 0);
215 }