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