1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx4c2-wasmsimd-dot16x2.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2021 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 <wasm_simd128.h>
13
14 #include <xnnpack/gemm.h>
15 #include <xnnpack/math.h>
16
17
18
xnn_qc8_gemm_minmax_fp32_ukernel_3x4c2__wasmsimd_dot16x2_ld64(size_t mr,size_t nc,size_t kc,const int8_t * restrict a,size_t a_stride,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qc8_gemm_minmax_fp32_ukernel_3x4c2__wasmsimd_dot16x2_ld64(
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 int8_t* restrict c,
27 size_t cm_stride,
28 size_t cn_stride,
29 const union xnn_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30 {
31 assert(mr != 0);
32 assert(mr <= 3);
33 assert(nc != 0);
34 assert(kc != 0);
35 assert(kc % sizeof(int8_t) == 0);
36 assert(a != NULL);
37 assert(w != NULL);
38 assert(c != NULL);
39
40 kc = round_up_po2(kc, 2);
41 const int8_t* a0 = a;
42 int8_t* c0 = c;
43 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
44 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
45 if XNN_UNPREDICTABLE(mr < 2) {
46 a1 = a0;
47 c1 = c0;
48 }
49 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
50 int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
51 if XNN_UNPREDICTABLE(mr <= 2) {
52 a2 = a1;
53 c2 = c1;
54 }
55
56 do {
57 v128_t vacc0x0123 = wasm_v128_load(w);
58 v128_t vacc1x0123 = vacc0x0123;
59 v128_t vacc2x0123 = vacc0x0123;
60 w = (const void*) ((const int32_t*) w + 4);
61
62 size_t k = kc;
63 while (k >= 8 * sizeof(int8_t)) {
64 const v128_t vxa0 = wasm_i16x8_load8x8((const v128_t*) a0);
65 a0 += 8;
66 const v128_t vxa1 = wasm_i16x8_load8x8((const v128_t*) a1);
67 a1 += 8;
68 const v128_t vxa2 = wasm_i16x8_load8x8((const v128_t*) a2);
69 a2 += 8;
70
71 const v128_t vxb0 = wasm_i16x8_load8x8(w);
72
73 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
74 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 0, 0, 0, 0), vxb0));
75 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
76 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 0, 0, 0, 0), vxb0));
77 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
78 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 0, 0, 0, 0), vxb0));
79 const v128_t vxb1 = wasm_i16x8_load8x8((const int8_t*) w + 8);
80
81 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
82 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 1, 1, 1, 1), vxb1));
83 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
84 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 1, 1, 1, 1), vxb1));
85 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
86 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 1, 1, 1, 1), vxb1));
87 const v128_t vxb2 = wasm_i16x8_load8x8((const int8_t*) w + 16);
88
89 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
90 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 2, 2, 2, 2), vxb2));
91 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
92 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 2, 2, 2, 2), vxb2));
93 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
94 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 2, 2, 2, 2), vxb2));
95 const v128_t vxb3 = wasm_i16x8_load8x8((const int8_t*) w + 24);
96
97 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
98 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 3, 3, 3, 3), vxb3));
99 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
100 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 3, 3, 3, 3), vxb3));
101 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
102 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 3, 3, 3, 3), vxb3));
103
104 w = (const void*) ((const int8_t*) w + 32);
105 k -= 8 * sizeof(int8_t);
106 }
107 if (k != 0) {
108 const v128_t vxa0 = wasm_i16x8_load8x8(a0);
109 a0 = (const int8_t*) ((uintptr_t) a0 + k);
110 const v128_t vxa1 = wasm_i16x8_load8x8(a1);
111 a1 = (const int8_t*) ((uintptr_t) a1 + k);
112 const v128_t vxa2 = wasm_i16x8_load8x8(a2);
113 a2 = (const int8_t*) ((uintptr_t) a2 + k);
114
115 const v128_t vxb0 = wasm_i16x8_load8x8(w);
116 w = (const void*) ((const int8_t*) w + 8);
117
118 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
119 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 0, 0, 0, 0), vxb0));
120 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
121 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 0, 0, 0, 0), vxb0));
122 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
123 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 0, 0, 0, 0), vxb0));
124
125 if (k > 2 * sizeof(int8_t)) {
126 const v128_t vxb1 = wasm_i16x8_load8x8(w);
127 w = (const void*) ((const int8_t*) w + 8);
128
129 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
130 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 1, 1, 1, 1), vxb1));
131 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
132 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 1, 1, 1, 1), vxb1));
133 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
134 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 1, 1, 1, 1), vxb1));
135
136 if (k > 4 * sizeof(int8_t)) {
137 const v128_t vxb2 = wasm_i16x8_load8x8(w);
138 w = (const void*) ((const int8_t*) w + 8);
139
140 vacc0x0123 = wasm_i32x4_add(vacc0x0123,
141 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 2, 2, 2, 2), vxb2));
142 vacc1x0123 = wasm_i32x4_add(vacc1x0123,
143 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 2, 2, 2, 2), vxb2));
144 vacc2x0123 = wasm_i32x4_add(vacc2x0123,
145 wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 2, 2, 2, 2), vxb2));
146 }
147 }
148 }
149
150 vacc0x0123 = wasm_f32x4_convert_i32x4(vacc0x0123);
151 vacc1x0123 = wasm_f32x4_convert_i32x4(vacc1x0123);
152 vacc2x0123 = wasm_f32x4_convert_i32x4(vacc2x0123);
153
154 const v128_t vscale0123 = wasm_v128_load(w);
155 w = (const void*) ((const float*) w + 4);
156 vacc0x0123 = wasm_f32x4_mul(vacc0x0123, vscale0123);
157 vacc1x0123 = wasm_f32x4_mul(vacc1x0123, vscale0123);
158 vacc2x0123 = wasm_f32x4_mul(vacc2x0123, vscale0123);
159
160 const v128_t vmagic_bias = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias);
161 vacc0x0123 = wasm_f32x4_add(vacc0x0123, vmagic_bias);
162 vacc1x0123 = wasm_f32x4_add(vacc1x0123, vmagic_bias);
163 vacc2x0123 = wasm_f32x4_add(vacc2x0123, vmagic_bias);
164
165 const v128_t vmagic_min = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_min);
166 vacc0x0123 = wasm_i32x4_max(vacc0x0123, vmagic_min);
167 vacc1x0123 = wasm_i32x4_max(vacc1x0123, vmagic_min);
168 vacc2x0123 = wasm_i32x4_max(vacc2x0123, vmagic_min);
169
170 const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias_less_output_zero_point);
171 vacc0x0123 = wasm_i32x4_sub(vacc0x0123, vmagic_bias_less_output_zero_point);
172 vacc1x0123 = wasm_i32x4_sub(vacc1x0123, vmagic_bias_less_output_zero_point);
173 vacc2x0123 = wasm_i32x4_sub(vacc2x0123, vmagic_bias_less_output_zero_point);
174
175 v128_t vacc01x0123 = wasm_i16x8_narrow_i32x4(vacc0x0123, vacc1x0123);
176 v128_t vacc22x0123 = wasm_i16x8_narrow_i32x4(vacc2x0123, vacc2x0123);
177
178 v128_t vout = wasm_i8x16_narrow_i16x8(vacc01x0123, vacc22x0123);
179
180 const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
181 vout = wasm_i8x16_min(vout, voutput_max);
182
183 if (nc >= 4) {
184 *((float*) c0) = (float) wasm_f32x4_extract_lane(vout, 0);
185 *((float*) c1) = (float) wasm_f32x4_extract_lane(vout, 1);
186 *((float*) c2) = (float) wasm_f32x4_extract_lane(vout, 2);
187
188 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
189 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
190 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
191
192 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
193 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
194 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
195
196 nc -= 4;
197 } else {
198 uint32_t vout0 = wasm_i32x4_extract_lane(vout, 0);
199 uint32_t vout1 = wasm_i32x4_extract_lane(vout, 1);
200 uint32_t vout2 = wasm_i32x4_extract_lane(vout, 2);
201 if (nc & 2) {
202 *((uint16_t*) c0) = (uint16_t) vout0;
203 vout0 >>= 16;
204 c0 += 2;
205 *((uint16_t*) c1) = (uint16_t) vout1;
206 vout1 >>= 16;
207 c1 += 2;
208 *((uint16_t*) c2) = (uint16_t) vout2;
209 vout2 >>= 16;
210 c2 += 2;
211 }
212 if (nc & 1) {
213 *c0 = (int8_t) vout0;
214 *c1 = (int8_t) vout1;
215 *c2 = (int8_t) vout2;
216 }
217
218 nc = 0;
219 }
220 } while (nc != 0);
221 }
222