xref: /aosp_15_r20/external/XNNPACK/src/qu8-gemm/gen/4x4c2-minmax-fp32-wasmsimd-dot16x2-ld64.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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_qu8_gemm_minmax_fp32_ukernel_4x4c2__wasmsimd_dot16x2_ld64(size_t mr,size_t nc,size_t kc,const uint8_t * restrict a,size_t a_stride,const void * restrict w,uint8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qu8_gemm_minmax_fp32_ukernel_4x4c2__wasmsimd_dot16x2_ld64(
20     size_t mr,
21     size_t nc,
22     size_t kc,
23     const uint8_t* restrict a,
24     size_t a_stride,
25     const void* restrict w,
26     uint8_t* restrict c,
27     size_t cm_stride,
28     size_t cn_stride,
29     const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30 {
31   assert(mr != 0);
32   assert(mr <= 4);
33   assert(nc != 0);
34   assert(kc != 0);
35   assert(kc % sizeof(uint8_t) == 0);
36   assert(a != NULL);
37   assert(w != NULL);
38   assert(c != NULL);
39 
40   kc = round_up_po2(kc, 2);
41   const uint8_t* a0 = a;
42   uint8_t* c0 = c;
43   const uint8_t* a1 = (const uint8_t*) ((uintptr_t) a0 + a_stride);
44   uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
45   if XNN_UNPREDICTABLE(mr < 2) {
46     a1 = a0;
47     c1 = c0;
48   }
49   const uint8_t* a2 = (const uint8_t*) ((uintptr_t) a1 + a_stride);
50   uint8_t* c2 = (uint8_t*) ((uintptr_t) c1 + cm_stride);
51   if XNN_UNPREDICTABLE(mr <= 2) {
52     a2 = a1;
53     c2 = c1;
54   }
55   const uint8_t* a3 = (const uint8_t*) ((uintptr_t) a2 + a_stride);
56   uint8_t* c3 = (uint8_t*) ((uintptr_t) c2 + cm_stride);
57   if XNN_UNPREDICTABLE(mr != 4) {
58     a3 = a2;
59     c3 = c2;
60   }
61 
62   do {
63     v128_t vacc0x0123 = wasm_v128_load(w);
64     v128_t vacc1x0123 = vacc0x0123;
65     v128_t vacc2x0123 = vacc0x0123;
66     v128_t vacc3x0123 = vacc0x0123;
67     w = (const void*) ((const int32_t*) w + 4);
68 
69     size_t k = kc;
70     const v128_t vb_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.kernel_zero_point);
71     while (k >= 8 * sizeof(uint8_t)) {
72       const v128_t vxa0 = wasm_u16x8_load8x8((const v128_t*) a0);
73       a0 += 8;
74       const v128_t vxa1 = wasm_u16x8_load8x8((const v128_t*) a1);
75       a1 += 8;
76       const v128_t vxa2 = wasm_u16x8_load8x8((const v128_t*) a2);
77       a2 += 8;
78       const v128_t vxa3 = wasm_u16x8_load8x8((const v128_t*) a3);
79       a3 += 8;
80 
81       const v128_t vxb0 = wasm_i16x8_sub(wasm_u16x8_load8x8(w), vb_zero_point);
82 
83       vacc0x0123 = wasm_i32x4_add(vacc0x0123,
84         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 0, 0, 0, 0), vxb0));
85       vacc1x0123 = wasm_i32x4_add(vacc1x0123,
86         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 0, 0, 0, 0), vxb0));
87       vacc2x0123 = wasm_i32x4_add(vacc2x0123,
88         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 0, 0, 0, 0), vxb0));
89       vacc3x0123 = wasm_i32x4_add(vacc3x0123,
90         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 0, 0, 0, 0), vxb0));
91       const v128_t vxb1 = wasm_i16x8_sub(wasm_u16x8_load8x8((const uint8_t*) w + 8), vb_zero_point);
92 
93       vacc0x0123 = wasm_i32x4_add(vacc0x0123,
94         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 1, 1, 1, 1), vxb1));
95       vacc1x0123 = wasm_i32x4_add(vacc1x0123,
96         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 1, 1, 1, 1), vxb1));
97       vacc2x0123 = wasm_i32x4_add(vacc2x0123,
98         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 1, 1, 1, 1), vxb1));
99       vacc3x0123 = wasm_i32x4_add(vacc3x0123,
100         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 1, 1, 1, 1), vxb1));
101       const v128_t vxb2 = wasm_i16x8_sub(wasm_u16x8_load8x8((const uint8_t*) w + 16), vb_zero_point);
102 
103       vacc0x0123 = wasm_i32x4_add(vacc0x0123,
104         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 2, 2, 2, 2), vxb2));
105       vacc1x0123 = wasm_i32x4_add(vacc1x0123,
106         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 2, 2, 2, 2), vxb2));
107       vacc2x0123 = wasm_i32x4_add(vacc2x0123,
108         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 2, 2, 2, 2), vxb2));
109       vacc3x0123 = wasm_i32x4_add(vacc3x0123,
110         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 2, 2, 2, 2), vxb2));
111       const v128_t vxb3 = wasm_i16x8_sub(wasm_u16x8_load8x8((const uint8_t*) w + 24), vb_zero_point);
112 
113       vacc0x0123 = wasm_i32x4_add(vacc0x0123,
114         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 3, 3, 3, 3), vxb3));
115       vacc1x0123 = wasm_i32x4_add(vacc1x0123,
116         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 3, 3, 3, 3), vxb3));
117       vacc2x0123 = wasm_i32x4_add(vacc2x0123,
118         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 3, 3, 3, 3), vxb3));
119       vacc3x0123 = wasm_i32x4_add(vacc3x0123,
120         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 3, 3, 3, 3), vxb3));
121 
122       w = (const void*) ((const uint8_t*) w + 32);
123       k -= 8 * sizeof(uint8_t);
124     }
125     if (k != 0) {
126       const v128_t vxa0 = wasm_u16x8_load8x8(a0);
127       a0 = (const uint8_t*) ((uintptr_t) a0 + k);
128       const v128_t vxa1 = wasm_u16x8_load8x8(a1);
129       a1 = (const uint8_t*) ((uintptr_t) a1 + k);
130       const v128_t vxa2 = wasm_u16x8_load8x8(a2);
131       a2 = (const uint8_t*) ((uintptr_t) a2 + k);
132       const v128_t vxa3 = wasm_u16x8_load8x8(a3);
133       a3 = (const uint8_t*) ((uintptr_t) a3 + k);
134 
135       const v128_t vxb0 = wasm_i16x8_sub(wasm_u16x8_load8x8(w), vb_zero_point);
136       w = (const void*) ((const uint8_t*) w + 8);
137 
138       vacc0x0123 = wasm_i32x4_add(vacc0x0123,
139         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 0, 0, 0, 0), vxb0));
140       vacc1x0123 = wasm_i32x4_add(vacc1x0123,
141         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 0, 0, 0, 0), vxb0));
142       vacc2x0123 = wasm_i32x4_add(vacc2x0123,
143         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 0, 0, 0, 0), vxb0));
144       vacc3x0123 = wasm_i32x4_add(vacc3x0123,
145         wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 0, 0, 0, 0), vxb0));
146 
147       if (k > 2 * sizeof(uint8_t)) {
148         const v128_t vxb1 = wasm_i16x8_sub(wasm_u16x8_load8x8(w), vb_zero_point);
149         w = (const void*) ((const uint8_t*) w + 8);
150 
151         vacc0x0123 = wasm_i32x4_add(vacc0x0123,
152           wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 1, 1, 1, 1), vxb1));
153         vacc1x0123 = wasm_i32x4_add(vacc1x0123,
154           wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 1, 1, 1, 1), vxb1));
155         vacc2x0123 = wasm_i32x4_add(vacc2x0123,
156           wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 1, 1, 1, 1), vxb1));
157         vacc3x0123 = wasm_i32x4_add(vacc3x0123,
158           wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 1, 1, 1, 1), vxb1));
159 
160         if (k > 4 * sizeof(uint8_t)) {
161           const v128_t vxb2 = wasm_i16x8_sub(wasm_u16x8_load8x8(w), vb_zero_point);
162           w = (const void*) ((const uint8_t*) w + 8);
163 
164           vacc0x0123 = wasm_i32x4_add(vacc0x0123,
165             wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa0, vxa0, 2, 2, 2, 2), vxb2));
166           vacc1x0123 = wasm_i32x4_add(vacc1x0123,
167             wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa1, vxa1, 2, 2, 2, 2), vxb2));
168           vacc2x0123 = wasm_i32x4_add(vacc2x0123,
169             wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa2, vxa2, 2, 2, 2, 2), vxb2));
170           vacc3x0123 = wasm_i32x4_add(vacc3x0123,
171             wasm_i32x4_dot_i16x8(wasm_v32x4_shuffle(vxa3, vxa3, 2, 2, 2, 2), vxb2));
172         }
173       }
174     }
175 
176     vacc0x0123 = wasm_f32x4_convert_i32x4(vacc0x0123);
177     vacc1x0123 = wasm_f32x4_convert_i32x4(vacc1x0123);
178     vacc2x0123 = wasm_f32x4_convert_i32x4(vacc2x0123);
179     vacc3x0123 = wasm_f32x4_convert_i32x4(vacc3x0123);
180 
181     const v128_t vscale = wasm_v128_load64_splat(params->fp32_wasmsimd.scale);
182     vacc0x0123 = wasm_f32x4_mul(vacc0x0123, vscale);
183     vacc1x0123 = wasm_f32x4_mul(vacc1x0123, vscale);
184     vacc2x0123 = wasm_f32x4_mul(vacc2x0123, vscale);
185     vacc3x0123 = wasm_f32x4_mul(vacc3x0123, vscale);
186 
187     const v128_t vmagic_bias = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias);
188     vacc0x0123 = wasm_f32x4_add(vacc0x0123, vmagic_bias);
189     vacc1x0123 = wasm_f32x4_add(vacc1x0123, vmagic_bias);
190     vacc2x0123 = wasm_f32x4_add(vacc2x0123, vmagic_bias);
191     vacc3x0123 = wasm_f32x4_add(vacc3x0123, vmagic_bias);
192 
193     const v128_t vmagic_min = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_min);
194     vacc0x0123 = wasm_i32x4_max(vacc0x0123, vmagic_min);
195     vacc1x0123 = wasm_i32x4_max(vacc1x0123, vmagic_min);
196     vacc2x0123 = wasm_i32x4_max(vacc2x0123, vmagic_min);
197     vacc3x0123 = wasm_i32x4_max(vacc3x0123, vmagic_min);
198 
199     const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias_less_output_zero_point);
200     vacc0x0123 = wasm_i32x4_sub(vacc0x0123, vmagic_bias_less_output_zero_point);
201     vacc1x0123 = wasm_i32x4_sub(vacc1x0123, vmagic_bias_less_output_zero_point);
202     vacc2x0123 = wasm_i32x4_sub(vacc2x0123, vmagic_bias_less_output_zero_point);
203     vacc3x0123 = wasm_i32x4_sub(vacc3x0123, vmagic_bias_less_output_zero_point);
204 
205     v128_t vacc01x0123 = wasm_i16x8_narrow_i32x4(vacc0x0123, vacc1x0123);
206     v128_t vacc23x0123 = wasm_i16x8_narrow_i32x4(vacc2x0123, vacc3x0123);
207 
208     v128_t vout = wasm_u8x16_narrow_i16x8(vacc01x0123, vacc23x0123);
209 
210     const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
211     vout = wasm_u8x16_min(vout, voutput_max);
212 
213     if (nc >= 4) {
214       *((float*) c0) = (float) wasm_f32x4_extract_lane(vout, 0);
215       *((float*) c1) = (float) wasm_f32x4_extract_lane(vout, 1);
216       *((float*) c2) = (float) wasm_f32x4_extract_lane(vout, 2);
217       *((float*) c3) = (float) wasm_f32x4_extract_lane(vout, 3);
218 
219       c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
220       c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
221       c2 = (uint8_t*) ((uintptr_t) c2 + cn_stride);
222       c3 = (uint8_t*) ((uintptr_t) c3 + cn_stride);
223 
224       a0 = (const uint8_t*) ((uintptr_t) a0 - kc);
225       a1 = (const uint8_t*) ((uintptr_t) a1 - kc);
226       a2 = (const uint8_t*) ((uintptr_t) a2 - kc);
227       a3 = (const uint8_t*) ((uintptr_t) a3 - kc);
228 
229       nc -= 4;
230     } else {
231       uint32_t vout0 = wasm_i32x4_extract_lane(vout, 0);
232       uint32_t vout1 = wasm_i32x4_extract_lane(vout, 1);
233       uint32_t vout2 = wasm_i32x4_extract_lane(vout, 2);
234       uint32_t vout3 = wasm_i32x4_extract_lane(vout, 3);
235       if (nc & 2) {
236         *((uint16_t*) c0) = (uint16_t) vout0;
237         vout0 >>= 16;
238         c0 += 2;
239         *((uint16_t*) c1) = (uint16_t) vout1;
240         vout1 >>= 16;
241         c1 += 2;
242         *((uint16_t*) c2) = (uint16_t) vout2;
243         vout2 >>= 16;
244         c2 += 2;
245         *((uint16_t*) c3) = (uint16_t) vout3;
246         vout3 >>= 16;
247         c3 += 2;
248       }
249       if (nc & 1) {
250         *c0 = (uint8_t) vout0;
251         *c1 = (uint8_t) vout1;
252         *c2 = (uint8_t) vout2;
253         *c3 = (uint8_t) vout3;
254       }
255 
256       nc = 0;
257     }
258   } while (nc != 0);
259 }
260