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