Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-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_qc4w_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_qc4w_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 kc = round_up_po2(kc, 8 * sizeof(int8_t));
47 const int8_t* a0 = a;
48 float* c0 = c;
49
50 const __m128 vmin = _mm_set1_ps(params->scalar.min);
51 const __m128 vmax = _mm_set1_ps(params->scalar.max);
52 XNN_FORCE_REALIZATION(vmin);
53 XNN_FORCE_REALIZATION(vmax);
54
55 const __m128i vmask = _mm_set1_epi8(0xF0);
56 XNN_FORCE_REALIZATION(vmask);
57
58 do {
59 const __m128i vksum = _mm_load_si128((const __m128i*) w);
60 const __m128i vinput_zero_point0 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[0].zero_point));
61 const __m128i vzero = _mm_setzero_si128();
62 const __m128i vinit0 = _mm_mullo_epi32(vksum, vinput_zero_point0);
63 __m128i vacc0x0 = _mm_blend_epi16(vinit0, vzero, 0xFC);
64 __m128i vacc0x1 = _mm_blend_epi16(vinit0, vzero, 0xF3);
65 __m128i vacc0x2 = _mm_blend_epi16(vinit0, vzero, 0xCF);
66 __m128i vacc0x3 = _mm_blend_epi16(vinit0, vzero, 0x3F);
67 w = (const int32_t*) w + 4;
68
69 size_t k = kc;
70
71 while (k >= 16 * sizeof(int8_t)) {
72 const __m128i va0c0 = _mm_loadl_epi64((const __m128i*) a0);
73 const __m128i vxa0c0 = _mm_cvtepi8_epi16(va0c0);
74 a0 += 8;
75
76 const __m128i vb01c01 = _mm_load_si128((const __m128i*) w);
77 const __m128i vbs01c0 = _mm_slli_epi32(vb01c01, 4);
78 const __m128i vb01c0 = _mm_and_si128(vbs01c0, vmask);
79 const __m128i vsb01c0 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01c0);
80 const __m128i vxb0c0 = _mm_unpacklo_epi8(vb01c0, vsb01c0);
81 const __m128i vxb1c0 = _mm_unpackhi_epi8(vb01c0, vsb01c0);
82
83 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c0, vxb0c0));
84 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c0, vxb1c0));
85 const __m128i vb23c01 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
86 const __m128i vbs23c0 = _mm_slli_epi32(vb23c01, 4);
87 const __m128i vb23c0 = _mm_and_si128(vbs23c0, vmask);
88 const __m128i vsb23c0 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23c0);
89 const __m128i vxb2c0 = _mm_unpacklo_epi8(vb23c0, vsb23c0);
90 const __m128i vxb3c0 = _mm_unpackhi_epi8(vb23c0, vsb23c0);
91
92 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c0, vxb2c0));
93 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c0, vxb3c0));
94
95 const __m128i va0c1 = _mm_loadl_epi64((const __m128i*) a0);
96 const __m128i vxa0c1 = _mm_cvtepi8_epi16(va0c1);
97 a0 += 8;
98
99 const __m128i vb01c1 = _mm_and_si128(vb01c01, vmask);
100 const __m128i vsb01c1 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01c1);
101 const __m128i vxb0c1 = _mm_unpacklo_epi8(vb01c1, vsb01c1);
102 const __m128i vxb1c1 = _mm_unpackhi_epi8(vb01c1, vsb01c1);
103
104 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0c1, vxb0c1));
105 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0c1, vxb1c1));
106 const __m128i vb23c1 = _mm_and_si128(vb23c01, vmask);
107 const __m128i vsb23c1 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23c1);
108 const __m128i vxb2c1 = _mm_unpacklo_epi8(vb23c1, vsb23c1);
109 const __m128i vxb3c1 = _mm_unpackhi_epi8(vb23c1, vsb23c1);
110
111 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0c1, vxb2c1));
112 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0c1, vxb3c1));
113
114
115 w = (const int8_t*) w + 32;
116 k -= 16 * sizeof(int8_t);
117 }
118
119 while (k >= 8 * sizeof(int8_t)) {
120 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
121 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
122 a0 += 8;
123
124 __m128i vb01 = _mm_load_si128((const __m128i*) w);
125 vb01 = _mm_slli_epi32(vb01, 4);
126 vb01 = _mm_and_si128(vb01, vmask);
127
128 const __m128i vxbm1 = _mm_unpackhi_epi8(vb01, vb01);
129 const __m128i vxb0 = _mm_cvtepi8_epi16(vb01);
130 const __m128i vxb1 = _mm_srai_epi16(vxbm1, 8);
131
132 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
133 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
134 __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
135 vb23 = _mm_slli_epi32(vb23, 4);
136 vb23 = _mm_and_si128(vb23, vmask);
137
138 const __m128i vxbm3 = _mm_unpackhi_epi8(vb23, vb23);
139 const __m128i vxb2 = _mm_cvtepi8_epi16(vb23);
140 const __m128i vxb3 = _mm_srai_epi16(vxbm3, 8);
141
142 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
143 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
144
145 w = (const int8_t*) w + 32;
146 k -= 8 * sizeof(int8_t);
147 }
148
149 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
150 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
151
152 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
153
154 vacc0x0123 = _mm_srai_epi32(vacc0x0123, 4);
155 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
156
157 const __m128 vinput_scale0 = _mm_broadcast_ss(&quantization_params[0].inv_scale);
158
159 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
160
161 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
162 vout0x0123 = _mm_mul_ps(vout0x0123, vfilter_output_scale0123);
163
164 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
165 w = (const float*) w + 8;
166 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
167
168 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
169
170 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
171
172 if XNN_LIKELY(nc >= 4) {
173 _mm_storeu_ps(c0, vout0x0123);
174
175 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
176
177 c0 = (float*) ((uintptr_t) c0 + cn_stride);
178
179 nc -= 4;
180 } else {
181 if (nc & 2) {
182 _mm_storel_pi((__m64*) c0, vout0x0123);
183 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
184 c0 += 2;
185 }
186 if (nc & 1) {
187 _mm_store_ss(c0, vout0x0123);
188 }
189 nc = 0;
190 }
191 } while (nc != 0);
192 }