1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/MRx4c8-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
xnn_qu8_igemm_minmax_fp32_ukernel_3x4c8__wasmsimd_dot16x2_ld128(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_3x4c8__wasmsimd_dot16x2_ld128(
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 <= 3);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(ks != 0);
37 assert(ks % (3 * 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 kc = round_up_po2(kc, 8);
44 uint8_t* c0 = c;
45 uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
46 if XNN_UNPREDICTABLE(mr < 2) {
47 c1 = c0;
48 }
49 uint8_t* c2 = (uint8_t*) ((uintptr_t) c1 + cm_stride);
50 if XNN_UNPREDICTABLE(mr <= 2) {
51 c2 = c1;
52 }
53
54 const v128_t vb_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.kernel_zero_point);
55 do {
56 v128_t vacc0x0 = wasm_v128_load32_zero(w);
57 v128_t vacc0x1 = wasm_v128_load32_zero((const int32_t*) w + 1);
58 v128_t vacc0x2 = wasm_v128_load32_zero((const int32_t*) w + 2);
59 v128_t vacc0x3 = wasm_v128_load32_zero((const int32_t*) w + 3);
60 v128_t vacc1x0 = vacc0x0;
61 v128_t vacc1x1 = vacc0x1;
62 v128_t vacc1x2 = vacc0x2;
63 v128_t vacc1x3 = vacc0x3;
64 v128_t vacc2x0 = vacc0x0;
65 v128_t vacc2x1 = vacc0x1;
66 v128_t vacc2x2 = vacc0x2;
67 v128_t vacc2x3 = vacc0x3;
68 w = (const void*) ((const int32_t*) w + 4);
69
70 size_t p = ks;
71 do {
72 const uint8_t* restrict a0 = a[0];
73 if XNN_UNPREDICTABLE(a0 != zero) {
74 a0 = (const uint8_t*) ((uintptr_t) a0 + a_offset);
75 }
76 const uint8_t* restrict a1 = a[1];
77 if XNN_UNPREDICTABLE(a1 != zero) {
78 a1 = (const uint8_t*) ((uintptr_t) a1 + a_offset);
79 }
80 const uint8_t* restrict a2 = a[2];
81 if XNN_UNPREDICTABLE(a2 != zero) {
82 a2 = (const uint8_t*) ((uintptr_t) a2 + a_offset);
83 }
84 a += 3;
85
86 size_t k = 0;
87 while (k < kc) {
88 const v128_t vxa0 = wasm_u16x8_load8x8(a0);
89 a0 += 8;
90 const v128_t vxa1 = wasm_u16x8_load8x8(a1);
91 a1 += 8;
92 const v128_t vxa2 = wasm_u16x8_load8x8(a2);
93 a2 += 8;
94
95 const v128_t vb01 = wasm_v128_load(w);
96 const v128_t vxb0 = wasm_i16x8_sub(wasm_u16x8_extend_low_u8x16(vb01), vb_zero_point);
97 const v128_t vxb1 = wasm_i16x8_sub(wasm_u16x8_extend_high_u8x16(vb01), vb_zero_point);
98
99 vacc0x0 = wasm_i32x4_add(vacc0x0, wasm_i32x4_dot_i16x8(vxa0, vxb0));
100 vacc0x1 = wasm_i32x4_add(vacc0x1, wasm_i32x4_dot_i16x8(vxa0, vxb1));
101 vacc1x0 = wasm_i32x4_add(vacc1x0, wasm_i32x4_dot_i16x8(vxa1, vxb0));
102 vacc1x1 = wasm_i32x4_add(vacc1x1, wasm_i32x4_dot_i16x8(vxa1, vxb1));
103 vacc2x0 = wasm_i32x4_add(vacc2x0, wasm_i32x4_dot_i16x8(vxa2, vxb0));
104 vacc2x1 = wasm_i32x4_add(vacc2x1, wasm_i32x4_dot_i16x8(vxa2, vxb1));
105 const v128_t vb23 = wasm_v128_load((const uint8_t*) w + 16);
106 const v128_t vxb2 = wasm_i16x8_sub(wasm_u16x8_extend_low_u8x16(vb23), vb_zero_point);
107 const v128_t vxb3 = wasm_i16x8_sub(wasm_u16x8_extend_high_u8x16(vb23), vb_zero_point);
108
109 vacc0x2 = wasm_i32x4_add(vacc0x2, wasm_i32x4_dot_i16x8(vxa0, vxb2));
110 vacc0x3 = wasm_i32x4_add(vacc0x3, wasm_i32x4_dot_i16x8(vxa0, vxb3));
111 vacc1x2 = wasm_i32x4_add(vacc1x2, wasm_i32x4_dot_i16x8(vxa1, vxb2));
112 vacc1x3 = wasm_i32x4_add(vacc1x3, wasm_i32x4_dot_i16x8(vxa1, vxb3));
113 vacc2x2 = wasm_i32x4_add(vacc2x2, wasm_i32x4_dot_i16x8(vxa2, vxb2));
114 vacc2x3 = wasm_i32x4_add(vacc2x3, wasm_i32x4_dot_i16x8(vxa2, vxb3));
115
116 w = (const void*) ((const uint8_t*) w + 32);
117 k += 8 * sizeof(uint8_t);
118 }
119 p -= 3 * sizeof(void*);
120 } while (p != 0);
121
122 const v128_t vacc0x02 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc0x0, vacc0x2, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc0x0, vacc0x2, 2, 6, 3, 7));
123 const v128_t vacc0x13 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc0x1, vacc0x3, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc0x1, vacc0x3, 2, 6, 3, 7));
124 const v128_t vacc1x02 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc1x0, vacc1x2, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc1x0, vacc1x2, 2, 6, 3, 7));
125 const v128_t vacc1x13 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc1x1, vacc1x3, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc1x1, vacc1x3, 2, 6, 3, 7));
126 const v128_t vacc2x02 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc2x0, vacc2x2, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc2x0, vacc2x2, 2, 6, 3, 7));
127 const v128_t vacc2x13 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc2x1, vacc2x3, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc2x1, vacc2x3, 2, 6, 3, 7));
128
129 v128_t vacc0x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc0x02, vacc0x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc0x02, vacc0x13, 2, 6, 3, 7));
130 v128_t vacc1x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc1x02, vacc1x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc1x02, vacc1x13, 2, 6, 3, 7));
131 v128_t vacc2x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc2x02, vacc2x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc2x02, vacc2x13, 2, 6, 3, 7));
132
133 vacc0x0123 = wasm_f32x4_convert_i32x4(vacc0x0123);
134 vacc1x0123 = wasm_f32x4_convert_i32x4(vacc1x0123);
135 vacc2x0123 = wasm_f32x4_convert_i32x4(vacc2x0123);
136
137 const v128_t vscale = wasm_v128_load64_splat(params->fp32_wasmsimd.scale);
138 vacc0x0123 = wasm_f32x4_mul(vacc0x0123, vscale);
139 vacc1x0123 = wasm_f32x4_mul(vacc1x0123, vscale);
140 vacc2x0123 = wasm_f32x4_mul(vacc2x0123, vscale);
141
142 const v128_t vmagic_bias = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias);
143 vacc0x0123 = wasm_f32x4_add(vacc0x0123, vmagic_bias);
144 vacc1x0123 = wasm_f32x4_add(vacc1x0123, vmagic_bias);
145 vacc2x0123 = wasm_f32x4_add(vacc2x0123, vmagic_bias);
146
147 const v128_t vmagic_min = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_min);
148 vacc0x0123 = wasm_i32x4_max(vacc0x0123, vmagic_min);
149 vacc1x0123 = wasm_i32x4_max(vacc1x0123, vmagic_min);
150 vacc2x0123 = wasm_i32x4_max(vacc2x0123, vmagic_min);
151
152 const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias_less_output_zero_point);
153 vacc0x0123 = wasm_i32x4_sub(vacc0x0123, vmagic_bias_less_output_zero_point);
154 vacc1x0123 = wasm_i32x4_sub(vacc1x0123, vmagic_bias_less_output_zero_point);
155 vacc2x0123 = wasm_i32x4_sub(vacc2x0123, vmagic_bias_less_output_zero_point);
156
157 v128_t vacc01x0123 = wasm_i16x8_narrow_i32x4(vacc0x0123, vacc1x0123);
158 v128_t vacc22x0123 = wasm_i16x8_narrow_i32x4(vacc2x0123, vacc2x0123);
159
160 v128_t vout = wasm_u8x16_narrow_i16x8(vacc01x0123, vacc22x0123);
161
162 const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
163 vout = wasm_u8x16_min(vout, voutput_max);
164
165 if (nc >= 4) {
166 *((float*) c2) = (float) wasm_f32x4_extract_lane(vout, 2);
167 *((float*) c1) = (float) wasm_f32x4_extract_lane(vout, 1);
168 *((float*) c0) = (float) wasm_f32x4_extract_lane(vout, 0);
169
170 c2 = (uint8_t*) ((uintptr_t) c2 + cn_stride);
171 c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
172 c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
173
174 a = (const uint8_t**restrict) ((uintptr_t) a - ks);
175
176 nc -= 4;
177 } else {
178 uint32_t vout2 = wasm_i32x4_extract_lane(vout, 2);
179 uint32_t vout1 = wasm_i32x4_extract_lane(vout, 1);
180 uint32_t vout0 = wasm_i32x4_extract_lane(vout, 0);
181 if (nc & 2) {
182 *((uint16_t*) c2) = (uint16_t) vout2;
183 vout2 >>= 16;
184 c2 += 2;
185 *((uint16_t*) c1) = (uint16_t) vout1;
186 vout1 >>= 16;
187 c1 += 2;
188 *((uint16_t*) c0) = (uint16_t) vout0;
189 vout0 >>= 16;
190 c0 += 2;
191 }
192 if (nc & 1) {
193 *c2 = (uint8_t) vout2;
194 *c1 = (uint8_t) vout1;
195 *c0 = (uint8_t) vout0;
196 }
197
198 nc = 0;
199 }
200 } while (nc != 0);
201 }
202