• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/MRx4c8-wasmsimd-mul32.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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_2x4c8__wasmsimd_mul32_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_2x4c8__wasmsimd_mul32_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 <= 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(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 
50   const v128_t vb_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.kernel_zero_point);
51   do {
52     v128_t vacc0x0 = wasm_f32x4_replace_lane(wasm_f32x4_const_splat(0.0f), 0, ((const float*) w)[0]);
53     v128_t vacc0x1 = wasm_f32x4_replace_lane(wasm_f32x4_const_splat(0.0f), 0, ((const float*) w)[1]);
54     v128_t vacc0x2 = wasm_f32x4_replace_lane(wasm_f32x4_const_splat(0.0f), 0, ((const float*) w)[2]);
55     v128_t vacc0x3 = wasm_f32x4_replace_lane(wasm_f32x4_const_splat(0.0f), 0, ((const float*) w)[3]);
56     v128_t vacc1x0 = vacc0x0;
57     v128_t vacc1x1 = vacc0x1;
58     v128_t vacc1x2 = vacc0x2;
59     v128_t vacc1x3 = vacc0x3;
60     w = (const void*) ((const int32_t*) w + 4);
61 
62     size_t p = ks;
63     do {
64       const uint8_t* restrict a0 = a[0];
65       if XNN_UNPREDICTABLE(a0 != zero) {
66         a0 = (const uint8_t*) ((uintptr_t) a0 + a_offset);
67       }
68       const uint8_t* restrict a1 = a[1];
69       if XNN_UNPREDICTABLE(a1 != zero) {
70         a1 = (const uint8_t*) ((uintptr_t) a1 + a_offset);
71       }
72       a += 2;
73 
74       size_t k = 0;
75       while (k < kc) {
76         const v128_t vxa0 = wasm_u16x8_load8x8(a0);
77         a0 += 8;
78         const v128_t vxa1 = wasm_u16x8_load8x8(a1);
79         a1 += 8;
80 
81         const v128_t vb01 = wasm_v128_load(w);
82         const v128_t vxb0 = wasm_i16x8_sub(wasm_u16x8_extend_low_u8x16(vb01), vb_zero_point);
83         const v128_t vxb1 = wasm_i16x8_sub(wasm_u16x8_extend_high_u8x16(vb01), vb_zero_point);
84 
85         vacc0x0 = wasm_i32x4_add(vacc0x0, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa0), wasm_i32x4_extend_low_i16x8(vxb0)));
86         vacc0x0 = wasm_i32x4_add(vacc0x0, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa0), wasm_i32x4_extend_high_i16x8(vxb0)));
87         vacc0x1 = wasm_i32x4_add(vacc0x1, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa0), wasm_i32x4_extend_low_i16x8(vxb1)));
88         vacc0x1 = wasm_i32x4_add(vacc0x1, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa0), wasm_i32x4_extend_high_i16x8(vxb1)));
89         vacc1x0 = wasm_i32x4_add(vacc1x0, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa1), wasm_i32x4_extend_low_i16x8(vxb0)));
90         vacc1x0 = wasm_i32x4_add(vacc1x0, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa1), wasm_i32x4_extend_high_i16x8(vxb0)));
91         vacc1x1 = wasm_i32x4_add(vacc1x1, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa1), wasm_i32x4_extend_low_i16x8(vxb1)));
92         vacc1x1 = wasm_i32x4_add(vacc1x1, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa1), wasm_i32x4_extend_high_i16x8(vxb1)));
93         const v128_t vb23 = wasm_v128_load((const uint8_t*) w + 16);
94         const v128_t vxb2 = wasm_i16x8_sub(wasm_u16x8_extend_low_u8x16(vb23), vb_zero_point);
95         const v128_t vxb3 = wasm_i16x8_sub(wasm_u16x8_extend_high_u8x16(vb23), vb_zero_point);
96 
97         vacc0x2 = wasm_i32x4_add(vacc0x2, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa0), wasm_i32x4_extend_low_i16x8(vxb2)));
98         vacc0x2 = wasm_i32x4_add(vacc0x2, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa0), wasm_i32x4_extend_high_i16x8(vxb2)));
99         vacc0x3 = wasm_i32x4_add(vacc0x3, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa0), wasm_i32x4_extend_low_i16x8(vxb3)));
100         vacc0x3 = wasm_i32x4_add(vacc0x3, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa0), wasm_i32x4_extend_high_i16x8(vxb3)));
101         vacc1x2 = wasm_i32x4_add(vacc1x2, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa1), wasm_i32x4_extend_low_i16x8(vxb2)));
102         vacc1x2 = wasm_i32x4_add(vacc1x2, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa1), wasm_i32x4_extend_high_i16x8(vxb2)));
103         vacc1x3 = wasm_i32x4_add(vacc1x3, wasm_i32x4_mul(wasm_i32x4_extend_low_i16x8(vxa1), wasm_i32x4_extend_low_i16x8(vxb3)));
104         vacc1x3 = wasm_i32x4_add(vacc1x3, wasm_i32x4_mul(wasm_i32x4_extend_high_i16x8(vxa1), wasm_i32x4_extend_high_i16x8(vxb3)));
105 
106         w = (const void*) ((const uint8_t*) w + 32);
107         k += 8 * sizeof(uint8_t);
108       }
109       p -= 2 * sizeof(void*);
110     } while (p != 0);
111 
112     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));
113     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));
114     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));
115     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));
116 
117     v128_t vacc0x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc0x02, vacc0x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc0x02, vacc0x13, 2, 6, 3, 7));
118     v128_t vacc1x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc1x02, vacc1x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc1x02, vacc1x13, 2, 6, 3, 7));
119 
120     vacc0x0123 = wasm_f32x4_convert_i32x4(vacc0x0123);
121     vacc1x0123 = wasm_f32x4_convert_i32x4(vacc1x0123);
122 
123     const v128_t vscale = wasm_v128_load64_splat(params->fp32_wasmsimd.scale);
124     vacc0x0123 = wasm_f32x4_mul(vacc0x0123, vscale);
125     vacc1x0123 = wasm_f32x4_mul(vacc1x0123, vscale);
126 
127     const v128_t vmagic_bias = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias);
128     vacc0x0123 = wasm_f32x4_add(vacc0x0123, vmagic_bias);
129     vacc1x0123 = wasm_f32x4_add(vacc1x0123, vmagic_bias);
130 
131     const v128_t vmagic_min = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_min);
132     vacc0x0123 = wasm_i32x4_max(vacc0x0123, vmagic_min);
133     vacc1x0123 = wasm_i32x4_max(vacc1x0123, vmagic_min);
134 
135     const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias_less_output_zero_point);
136     vacc0x0123 = wasm_i32x4_sub(vacc0x0123, vmagic_bias_less_output_zero_point);
137     vacc1x0123 = wasm_i32x4_sub(vacc1x0123, vmagic_bias_less_output_zero_point);
138 
139     v128_t vacc01x0123 = wasm_i16x8_narrow_i32x4(vacc0x0123, vacc1x0123);
140 
141     v128_t vout = wasm_u8x16_narrow_i16x8(vacc01x0123, vacc01x0123);
142 
143     const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
144     vout = wasm_u8x16_min(vout, voutput_max);
145 
146     if (nc >= 4) {
147       *((float*) c1) = (float) wasm_f32x4_extract_lane(vout, 1);
148       *((float*) c0) = (float) wasm_f32x4_extract_lane(vout, 0);
149 
150       c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
151       c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
152 
153       a = (const uint8_t**restrict) ((uintptr_t) a - ks);
154 
155       nc -= 4;
156     } else {
157       uint32_t vout1 = wasm_i32x4_extract_lane(vout, 1);
158       uint32_t vout0 = wasm_i32x4_extract_lane(vout, 0);
159       if (nc & 2) {
160         *((uint16_t*) c1) = (uint16_t) vout1;
161         vout1 >>= 16;
162         c1 += 2;
163         *((uint16_t*) c0) = (uint16_t) vout0;
164         vout0 >>= 16;
165         c0 += 2;
166       }
167       if (nc & 1) {
168         *c1 = (uint8_t) vout1;
169         *c0 = (uint8_t) vout0;
170       }
171 
172       nc = 0;
173     }
174   } while (nc != 0);
175 }
176