Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-2x4c8-minmax-sse41-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 #include <smmintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/gemm.h"
16 #include "xnnpack/math.h"
17
18
19 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_2x4c8__sse41_ld128(
20 size_t mr,
21 size_t nc,
22 size_t kc,
23 const int8_t* restrict a,
24 size_t a_stride,
25 const void* restrict w,
26 float* restrict c,
27 size_t cm_stride,
28 size_t cn_stride,
29 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
30 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
31 {
32 assert(mr != 0);
33 assert(mr <= 2);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(kc % sizeof(int8_t) == 0);
37 assert(a != NULL);
38 assert(w != NULL);
39 assert(c != NULL);
40
41 kc = round_up_po2(kc, 8 * sizeof(int8_t));
42 const int8_t* a0 = a;
43 float* c0 = c;
44 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
45 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
46 if XNN_UNPREDICTABLE(mr != 2) {
47 a1 = a0;
48 c1 = c0;
49 }
50
51 const __m128 vmin = _mm_set1_ps(params->scalar.min);
52 const __m128 vmax = _mm_set1_ps(params->scalar.max);
53 XNN_FORCE_REALIZATION(vmin);
54 XNN_FORCE_REALIZATION(vmax);
55
56
57 do {
58 const __m128i vksum = _mm_load_si128((const __m128i*) w);
59 const __m128i vinput_zero_point01 = _mm_loadu_si128((const __m128i*) &quantization_params[0]);
60 const __m128i vinput_zero_point0 = _mm_shuffle_epi32(vinput_zero_point01, _MM_SHUFFLE(0, 0, 0, 0));
61 const __m128i vinput_zero_point1 = _mm_shuffle_epi32(vinput_zero_point01, _MM_SHUFFLE(2, 2, 2, 2));
62 const __m128i vzero = _mm_setzero_si128();
63 const __m128i vinit0 = _mm_mullo_epi32(vksum, vinput_zero_point0);
64 const __m128i vinit1 = _mm_mullo_epi32(vksum, vinput_zero_point1);
65 __m128i vacc0x0 = _mm_blend_epi16(vinit0, vzero, 0xFC);
66 __m128i vacc0x1 = _mm_blend_epi16(vinit0, vzero, 0xF3);
67 __m128i vacc0x2 = _mm_blend_epi16(vinit0, vzero, 0xCF);
68 __m128i vacc0x3 = _mm_blend_epi16(vinit0, vzero, 0x3F);
69 __m128i vacc1x0 = _mm_blend_epi16(vinit1, vzero, 0xFC);
70 __m128i vacc1x1 = _mm_blend_epi16(vinit1, vzero, 0xF3);
71 __m128i vacc1x2 = _mm_blend_epi16(vinit1, vzero, 0xCF);
72 __m128i vacc1x3 = _mm_blend_epi16(vinit1, vzero, 0x3F);
73 w = (const int32_t*) w + 4;
74
75 size_t k = kc;
76
77
78 while (k >= 8 * sizeof(int8_t)) {
79 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
80 const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
81 a0 += 8;
82 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
83 const __m128i vxa1 = _mm_cvtepi8_epi16(va1);
84 a1 += 8;
85
86 const __m128i vb01 = _mm_load_si128((const __m128i*) w);
87
88 const __m128i vxbm1 = _mm_unpackhi_epi8(vb01, vb01);
89 const __m128i vxb0 = _mm_cvtepi8_epi16(vb01);
90 const __m128i vxb1 = _mm_srai_epi16(vxbm1, 8);
91
92 vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
93 vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
94 vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0));
95 vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1));
96 const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
97
98 const __m128i vxbm3 = _mm_unpackhi_epi8(vb23, vb23);
99 const __m128i vxb2 = _mm_cvtepi8_epi16(vb23);
100 const __m128i vxb3 = _mm_srai_epi16(vxbm3, 8);
101
102 vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
103 vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
104 vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2));
105 vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3));
106
107 w = (const int8_t*) w + 32;
108 k -= 8 * sizeof(int8_t);
109 }
110
111 const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1);
112 const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3);
113 const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1);
114 const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3);
115
116 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
117 __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23);
118
119 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
120 __m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123);
121
122 const __m128i vinput_scale01 = _mm_loadu_si128((const __m128i*) &quantization_params[0]);
123 const __m128 vinput_scale0 = _mm_castsi128_ps(_mm_shuffle_epi32(vinput_scale01, _MM_SHUFFLE(1, 1, 1, 1)));
124 const __m128 vinput_scale1 = _mm_castsi128_ps(_mm_shuffle_epi32(vinput_scale01, _MM_SHUFFLE(3, 3, 3, 3)));
125
126 vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0);
127 vout1x0123 = _mm_mul_ps(vout1x0123, vinput_scale1);
128
129 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
130 vout0x0123 = _mm_mul_ps(vout0x0123, vfilter_output_scale0123);
131 vout1x0123 = _mm_mul_ps(vout1x0123, vfilter_output_scale0123);
132
133 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
134 w = (const float*) w + 8;
135 vout0x0123 = _mm_add_ps(vout0x0123, vbias0123);
136 vout1x0123 = _mm_add_ps(vout1x0123, vbias0123);
137
138 vout0x0123 = _mm_max_ps(vout0x0123, vmin);
139 vout1x0123 = _mm_max_ps(vout1x0123, vmin);
140
141 vout0x0123 = _mm_min_ps(vout0x0123, vmax);
142 vout1x0123 = _mm_min_ps(vout1x0123, vmax);
143
144 if XNN_LIKELY(nc >= 4) {
145 _mm_storeu_ps(c0, vout0x0123);
146 _mm_storeu_ps(c1, vout1x0123);
147
148 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
149 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
150
151 c0 = (float*) ((uintptr_t) c0 + cn_stride);
152 c1 = (float*) ((uintptr_t) c1 + cn_stride);
153
154 nc -= 4;
155 } else {
156 if (nc & 2) {
157 _mm_storel_pi((__m64*) c0, vout0x0123);
158 vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123);
159 c0 += 2;
160 _mm_storel_pi((__m64*) c1, vout1x0123);
161 vout1x0123 = _mm_unpackhi_ps(vout1x0123, vout1x0123);
162 c1 += 2;
163 }
164 if (nc & 1) {
165 _mm_store_ss(c0, vout0x0123);
166 _mm_store_ss(c1, vout1x0123);
167 }
168 nc = 0;
169 }
170 } while (nc != 0);
171 }