Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-1x4c8-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 #include "xnnpack/unaligned.h"
22
23
24 void xnn_qd8_f32_qb4w_gemm_minmax_ukernel_1x4c8__avx_ld128(
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 vb01c01 = _mm_loadu_si128((const __m128i*) w);
83 const __m128i vbs01c0 = _mm_slli_epi32(vb01c01, 4);
84 const __m128i vb01c0 = _mm_and_si128(vbs01c0, vmask);
85 const __m128i vsb01c0 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01c0);
86 const __m128i vxb0c0 = _mm_unpacklo_epi8(vb01c0, vsb01c0);
87 const __m128i vxb1c0 = _mm_unpackhi_epi8(vb01c0, vsb01c0);
88
89 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c0, vxb0c0));
90 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c0, vxb1c0));
91 const __m128i vb23c01 = _mm_loadu_si128((const __m128i*) ((const int8_t*) w + 16));
92 const __m128i vbs23c0 = _mm_slli_epi32(vb23c01, 4);
93 const __m128i vb23c0 = _mm_and_si128(vbs23c0, vmask);
94 const __m128i vsb23c0 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23c0);
95 const __m128i vxb2c0 = _mm_unpacklo_epi8(vb23c0, vsb23c0);
96 const __m128i vxb3c0 = _mm_unpackhi_epi8(vb23c0, vsb23c0);
97
98 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c0, vxb2c0));
99 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c0, vxb3c0));
100
101 const __m128i va0c1 = _mm_loadl_epi64((const __m128i*) a0);
102 const __m128i vxa0c1 = _mm_cvtepi8_epi16(va0c1);
103 a0 += 8;
104
105 const __m128i vb01c1 = _mm_and_si128(vb01c01, vmask);
106 const __m128i vsb01c1 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01c1);
107 const __m128i vxb0c1 = _mm_unpacklo_epi8(vb01c1, vsb01c1);
108 const __m128i vxb1c1 = _mm_unpackhi_epi8(vb01c1, vsb01c1);
109
110 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c1, vxb0c1));
111 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c1, vxb1c1));
112 const __m128i vb23c1 = _mm_and_si128(vb23c01, vmask);
113 const __m128i vsb23c1 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23c1);
114 const __m128i vxb2c1 = _mm_unpacklo_epi8(vb23c1, vsb23c1);
115 const __m128i vxb3c1 = _mm_unpackhi_epi8(vb23c1, vsb23c1);
116
117 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c1, vxb2c1));
118 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c1, vxb3c1));
119
120
121 w = (const int8_t*) w + 32;
122 k -= 16 * sizeof(int8_t);
123 }
124
125 while (k >= 8 * sizeof(int8_t)) {
126 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
127 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
128 a0 += 8;
129
130 __m128i vb01 = _mm_loadu_si128((const __m128i*) w);
131 vb01 = _mm_slli_epi32(vb01, 4);
132 vb01 = _mm_and_si128(vb01, vmask);
133
134 const __m128i vxbm1 = _mm_unpackhi_epi8(vb01, vb01);
135 const __m128i vxb0 = _mm_cvtepi8_epi16(vb01);
136 const __m128i vxb1 = _mm_srai_epi16(vxbm1, 8);
137
138 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
139 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
140 __m128i vb23 = _mm_loadu_si128((const __m128i*) ((const int8_t*) w + 16));
141 vb23 = _mm_slli_epi32(vb23, 4);
142 vb23 = _mm_and_si128(vb23, vmask);
143
144 const __m128i vxbm3 = _mm_unpackhi_epi8(vb23, vb23);
145 const __m128i vxb2 = _mm_cvtepi8_epi16(vb23);
146 const __m128i vxb3 = _mm_srai_epi16(vxbm3, 8);
147
148 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
149 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
150
151 w = (const int8_t*) w + 32;
152 k -= 8 * sizeof(int8_t);
153 }
154 // accumulate float
155 const __m128 vfilter_output_scale0123 = _mm_castsi128_ps(_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_loadl_epi64((const __m128i*) w)), 16));
156 w = (const uint16_t*) w + 4;
157
158 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
159 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
160
161 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
162
163 vout0x0123 = _mm_add_ps(vout0x0123, _mm_mul_ps(_mm_cvtepi32_ps(vacc0x0123), vfilter_output_scale0123));
164 }
165
166 const __m128 vinput_scale0 = _mm_broadcast_ss(&quantization_params[0].inv_scale);
167
168 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
169
170
171 const __m128 vbias0123 = _mm_loadu_ps((const float*) w);
172 w = (const float*) w + 4;
173 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
174
175 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
176
177 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
178
179 if XNN_LIKELY(nc >= 4) {
180 _mm_storeu_ps(c0, vout0x0123);
181
182 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
183
184 c0 = (float*) ((uintptr_t) c0 + cn_stride);
185
186 nc -= 4;
187 } else {
188 if (nc & 2) {
189 _mm_storel_pi((__m64*) c0, vout0x0123);
190 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
191 c0 += 2;
192 }
193 if (nc & 1) {
194 _mm_store_ss(c0, vout0x0123);
195 }
196 nc = 0;
197 }
198 } while (nc != 0);
199 }