• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_qc8_igemm_minmax_fp32_ukernel_3x4c8__wasmsimd_dot16x2_ld128(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_3x4c8__wasmsimd_dot16x2_ld128(
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 <= 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(int8_t) == 0);
39   assert(a != NULL);
40   assert(w != NULL);
41   assert(c != NULL);
42 
43   kc = round_up_po2(kc, 8);
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   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     c2 = c1;
52   }
53 
54   do {
55     v128_t vacc0x0 = wasm_v128_load32_zero(w);
56     v128_t vacc0x1 = wasm_v128_load32_zero((const int32_t*) w + 1);
57     v128_t vacc0x2 = wasm_v128_load32_zero((const int32_t*) w + 2);
58     v128_t vacc0x3 = wasm_v128_load32_zero((const int32_t*) w + 3);
59     v128_t vacc1x0 = vacc0x0;
60     v128_t vacc1x1 = vacc0x1;
61     v128_t vacc1x2 = vacc0x2;
62     v128_t vacc1x3 = vacc0x3;
63     v128_t vacc2x0 = vacc0x0;
64     v128_t vacc2x1 = vacc0x1;
65     v128_t vacc2x2 = vacc0x2;
66     v128_t vacc2x3 = vacc0x3;
67     w = (const void*) ((const int32_t*) w + 4);
68 
69     size_t p = ks;
70     do {
71       const int8_t* restrict a0 = a[0];
72       if XNN_UNPREDICTABLE(a0 != zero) {
73         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
74       }
75       const int8_t* restrict a1 = a[1];
76       if XNN_UNPREDICTABLE(a1 != zero) {
77         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
78       }
79       const int8_t* restrict a2 = a[2];
80       if XNN_UNPREDICTABLE(a2 != zero) {
81         a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
82       }
83       a += 3;
84 
85       size_t k = 0;
86       while (k < kc) {
87         const v128_t vxa0 = wasm_i16x8_load8x8(a0);
88         a0 += 8;
89         const v128_t vxa1 = wasm_i16x8_load8x8(a1);
90         a1 += 8;
91         const v128_t vxa2 = wasm_i16x8_load8x8(a2);
92         a2 += 8;
93 
94         const v128_t vb01 = wasm_v128_load(w);
95         const v128_t vxb0 = wasm_i16x8_extend_low_i8x16(vb01);
96         const v128_t vxb1 = wasm_i16x8_extend_high_i8x16(vb01);
97 
98         vacc0x0 = wasm_i32x4_add(vacc0x0, wasm_i32x4_dot_i16x8(vxa0, vxb0));
99         vacc0x1 = wasm_i32x4_add(vacc0x1, wasm_i32x4_dot_i16x8(vxa0, vxb1));
100         vacc1x0 = wasm_i32x4_add(vacc1x0, wasm_i32x4_dot_i16x8(vxa1, vxb0));
101         vacc1x1 = wasm_i32x4_add(vacc1x1, wasm_i32x4_dot_i16x8(vxa1, vxb1));
102         vacc2x0 = wasm_i32x4_add(vacc2x0, wasm_i32x4_dot_i16x8(vxa2, vxb0));
103         vacc2x1 = wasm_i32x4_add(vacc2x1, wasm_i32x4_dot_i16x8(vxa2, vxb1));
104         const v128_t vb23 = wasm_v128_load((const int8_t*) w + 16);
105         const v128_t vxb2 = wasm_i16x8_extend_low_i8x16(vb23);
106         const v128_t vxb3 = wasm_i16x8_extend_high_i8x16(vb23);
107 
108         vacc0x2 = wasm_i32x4_add(vacc0x2, wasm_i32x4_dot_i16x8(vxa0, vxb2));
109         vacc0x3 = wasm_i32x4_add(vacc0x3, wasm_i32x4_dot_i16x8(vxa0, vxb3));
110         vacc1x2 = wasm_i32x4_add(vacc1x2, wasm_i32x4_dot_i16x8(vxa1, vxb2));
111         vacc1x3 = wasm_i32x4_add(vacc1x3, wasm_i32x4_dot_i16x8(vxa1, vxb3));
112         vacc2x2 = wasm_i32x4_add(vacc2x2, wasm_i32x4_dot_i16x8(vxa2, vxb2));
113         vacc2x3 = wasm_i32x4_add(vacc2x3, wasm_i32x4_dot_i16x8(vxa2, vxb3));
114 
115         w = (const void*) ((const int8_t*) w + 32);
116         k += 8 * sizeof(int8_t);
117       }
118       p -= 3 * sizeof(void*);
119     } while (p != 0);
120 
121     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));
122     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));
123     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));
124     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));
125     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));
126     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));
127 
128     v128_t vacc0x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc0x02, vacc0x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc0x02, vacc0x13, 2, 6, 3, 7));
129     v128_t vacc1x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc1x02, vacc1x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc1x02, vacc1x13, 2, 6, 3, 7));
130     v128_t vacc2x0123 = wasm_i32x4_add(wasm_v32x4_shuffle(vacc2x02, vacc2x13, 0, 4, 1, 5), wasm_v32x4_shuffle(vacc2x02, vacc2x13, 2, 6, 3, 7));
131 
132     vacc0x0123 = wasm_f32x4_convert_i32x4(vacc0x0123);
133     vacc1x0123 = wasm_f32x4_convert_i32x4(vacc1x0123);
134     vacc2x0123 = wasm_f32x4_convert_i32x4(vacc2x0123);
135 
136     const v128_t vscale0123 = wasm_v128_load(w);
137     w = (const void*) ((const float*) w + 4);
138     vacc0x0123 = wasm_f32x4_mul(vacc0x0123, vscale0123);
139     vacc1x0123 = wasm_f32x4_mul(vacc1x0123, vscale0123);
140     vacc2x0123 = wasm_f32x4_mul(vacc2x0123, vscale0123);
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_i8x16_narrow_i16x8(vacc01x0123, vacc22x0123);
161 
162     const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
163     vout = wasm_i8x16_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 = (int8_t*) ((uintptr_t) c2 + cn_stride);
171       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
172       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
173 
174       a = (const int8_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 = (int8_t) vout2;
194         *c1 = (int8_t) vout1;
195         *c0 = (int8_t) vout0;
196       }
197 
198       nc = 0;
199     }
200   } while (nc != 0);
201 }
202