Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-2x4c8-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
22
23 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_2x4c8__avx_ld64(
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 <= 2);
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
55 const __m128 vmin = _mm_set1_ps(params->scalar.min);
56 const __m128 vmax = _mm_set1_ps(params->scalar.max);
57 XNN_FORCE_REALIZATION(vmin);
58 XNN_FORCE_REALIZATION(vmax);
59
60
61 do {
62 const __m128i vksum = _mm_load_si128((const __m128i*) w);
63 const __m128i vinput_zero_point0 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[0].zero_point));
64 const __m128i vinput_zero_point1 = _mm_castps_si128(_mm_broadcast_ss((const float*) &quantization_params[1].zero_point));
65 const __m128i vzero = _mm_setzero_si128();
66 const __m128i vinit0 = _mm_mullo_epi32(vksum, vinput_zero_point0);
67 const __m128i vinit1 = _mm_mullo_epi32(vksum, vinput_zero_point1);
68 __m128i vacc0x0 = _mm_blend_epi16(vinit0, vzero, 0xFC);
69 __m128i vacc0x1 = _mm_blend_epi16(vinit0, vzero, 0xF3);
70 __m128i vacc0x2 = _mm_blend_epi16(vinit0, vzero, 0xCF);
71 __m128i vacc0x3 = _mm_blend_epi16(vinit0, vzero, 0x3F);
72 __m128i vacc1x0 = _mm_blend_epi16(vinit1, vzero, 0xFC);
73 __m128i vacc1x1 = _mm_blend_epi16(vinit1, vzero, 0xF3);
74 __m128i vacc1x2 = _mm_blend_epi16(vinit1, vzero, 0xCF);
75 __m128i vacc1x3 = _mm_blend_epi16(vinit1, vzero, 0x3F);
76 w = (const int32_t*) w + 4;
77
78 size_t k = kc;
79
80
81 while (k >= 8 * sizeof(int8_t)) {
82 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
83 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
84 a0 += 8;
85 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
86 const __m128i vxa1 = _mm_cvtepi8_epi16(va1);
87 a1 += 8;
88
89 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
90
91 const __m128i vxb0 = _mm_cvtepi8_epi16(vb0);
92
93 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
94 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0));
95 const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8));
96
97 const __m128i vxb1 = _mm_cvtepi8_epi16(vb1);
98
99 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
100 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1));
101 const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16));
102
103 const __m128i vxb2 = _mm_cvtepi8_epi16(vb2);
104
105 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
106 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2));
107 const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24));
108
109 const __m128i vxb3 = _mm_cvtepi8_epi16(vb3);
110
111 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
112 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3));
113
114 w = (const int8_t*) w + 32;
115 k -= 8 * sizeof(int8_t);
116 }
117
118 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
119 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
120 const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1);
121 const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3);
122
123 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
124 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
125
126 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
127 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
128
129 const __m128 vinput_scale0 = _mm_broadcast_ss(&quantization_params[0].inv_scale);
130 const __m128 vinput_scale1 = _mm_broadcast_ss(&quantization_params[1].inv_scale);
131
132 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
133 vout1x0123 = _mm_mul_ps(vout1x0123, vinput_scale1);
134
135 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
136 vout0x0123 = _mm_mul_ps(vout0x0123, vfilter_output_scale0123);
137 vout1x0123 = _mm_mul_ps(vout1x0123, vfilter_output_scale0123);
138
139 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
140 w = (const float*) w + 8;
141 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
142 vout1x0123 = _mm_add_ps(vout1x0123, vbias0123);
143
144 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
145 vout1x0123 = _mm_max_ps(vout1x0123, vmin);
146
147 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
148 vout1x0123 = _mm_min_ps(vout1x0123, vmax);
149
150 if XNN_LIKELY(nc >= 4) {
151 _mm_storeu_ps(c0, vout0x0123);
152 _mm_storeu_ps(c1, vout1x0123);
153
154 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
155 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
156
157 c0 = (float*) ((uintptr_t) c0 + cn_stride);
158 c1 = (float*) ((uintptr_t) c1 + cn_stride);
159
160 nc -= 4;
161 } else {
162 if (nc & 2) {
163 _mm_storel_pi((__m64*) c0, vout0x0123);
164 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
165 c0 += 2;
166 _mm_storel_pi((__m64*) c1, vout1x0123);
167 vout1x0123 = _mm_unpackhi_ps(vout1x0123, vout1x0123);
168 c1 += 2;
169 }
170 if (nc & 1) {
171 _mm_store_ss(c0, vout0x0123);
172 _mm_store_ss(c1, vout1x0123);
173 }
174 nc = 0;
175 }
176 } while (nc != 0);
177 }