xref: /aosp_15_r20/external/XNNPACK/src/qc8-dwconv/gen/up8x9-minmax-fp32-wasmsimd-mul16-add16.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-dwconv/unipass-wasmsimd-mul16.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/dwconv.h>
15 
16 
xnn_qc8_dwconv_minmax_fp32_ukernel_up8x9__wasmsimd_mul16_add16(size_t channels,size_t output_width,const int8_t ** input,const void * weights,int8_t * output,size_t input_stride,size_t output_increment,size_t input_offset,const int8_t * zero,const union xnn_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_qc8_dwconv_minmax_fp32_ukernel_up8x9__wasmsimd_mul16_add16(
18     size_t channels,
19     size_t output_width,
20     const int8_t** input,
21     const void* weights,
22     int8_t* output,
23     size_t input_stride,
24     size_t output_increment,
25     size_t input_offset,
26     const int8_t* zero,
27     const union xnn_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
28 {
29   assert(channels != 0);
30   assert(output_width != 0);
31 
32   do {
33     const int8_t* i0 = input[0];
34     assert(i0 != NULL);
35     if XNN_UNPREDICTABLE(i0 != zero) {
36       i0 = (const int8_t*) ((uintptr_t) i0 + input_offset);
37     }
38     const int8_t* i1 = input[1];
39     assert(i1 != NULL);
40     if XNN_UNPREDICTABLE(i1 != zero) {
41       i1 = (const int8_t*) ((uintptr_t) i1 + input_offset);
42     }
43     const int8_t* i2 = input[2];
44     assert(i2 != NULL);
45     if XNN_UNPREDICTABLE(i2 != zero) {
46       i2 = (const int8_t*) ((uintptr_t) i2 + input_offset);
47     }
48     const int8_t* i3 = input[3];
49     assert(i3 != NULL);
50     if XNN_UNPREDICTABLE(i3 != zero) {
51       i3 = (const int8_t*) ((uintptr_t) i3 + input_offset);
52     }
53     const int8_t* i4 = input[4];
54     assert(i4 != NULL);
55     if XNN_UNPREDICTABLE(i4 != zero) {
56       i4 = (const int8_t*) ((uintptr_t) i4 + input_offset);
57     }
58     const int8_t* i5 = input[5];
59     assert(i5 != NULL);
60     if XNN_UNPREDICTABLE(i5 != zero) {
61       i5 = (const int8_t*) ((uintptr_t) i5 + input_offset);
62     }
63     const int8_t* i6 = input[6];
64     assert(i6 != NULL);
65     if XNN_UNPREDICTABLE(i6 != zero) {
66       i6 = (const int8_t*) ((uintptr_t) i6 + input_offset);
67     }
68     const int8_t* i7 = input[7];
69     assert(i7 != NULL);
70     if XNN_UNPREDICTABLE(i7 != zero) {
71       i7 = (const int8_t*) ((uintptr_t) i7 + input_offset);
72     }
73     const int8_t* i8 = input[8];
74     assert(i8 != NULL);
75     if XNN_UNPREDICTABLE(i8 != zero) {
76       i8 = (const int8_t*) ((uintptr_t) i8 + input_offset);
77     }
78     input = (const int8_t**) ((uintptr_t) input + input_stride);
79 
80     size_t c = channels;
81     const void* w = weights;
82     for (; c >= 8; c -= 8) {
83       v128_t vacc0123 = wasm_v128_load(w);
84       v128_t vacc4567 = wasm_v128_load((const void*) ((uintptr_t) w + 4 * sizeof(int32_t)));
85 
86 
87       const v128_t vi0x01234567 = wasm_i16x8_load8x8(i0);
88       const v128_t vk0x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 0 * sizeof(int8_t)));
89       i0 += 8;
90 
91       v128_t vprod01234567 = wasm_i16x8_mul(vi0x01234567, vk0x01234567);
92 
93 
94       const v128_t vi1x01234567 = wasm_i16x8_load8x8(i1);
95       const v128_t vk1x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 8 * sizeof(int8_t)));
96       i1 += 8;
97 
98       vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi1x01234567, vk1x01234567));
99 
100       vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
101       vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
102 
103       const v128_t vi2x01234567 = wasm_i16x8_load8x8(i2);
104       const v128_t vk2x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 16 * sizeof(int8_t)));
105       i2 += 8;
106 
107       vprod01234567 = wasm_i16x8_mul(vi2x01234567, vk2x01234567);
108 
109 
110       const v128_t vi3x01234567 = wasm_i16x8_load8x8(i3);
111       const v128_t vk3x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 24 * sizeof(int8_t)));
112       i3 += 8;
113 
114       vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi3x01234567, vk3x01234567));
115 
116       vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
117       vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
118 
119       const v128_t vi4x01234567 = wasm_i16x8_load8x8(i4);
120       const v128_t vk4x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 32 * sizeof(int8_t)));
121       i4 += 8;
122 
123       vprod01234567 = wasm_i16x8_mul(vi4x01234567, vk4x01234567);
124 
125 
126       const v128_t vi5x01234567 = wasm_i16x8_load8x8(i5);
127       const v128_t vk5x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 40 * sizeof(int8_t)));
128       i5 += 8;
129 
130       vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi5x01234567, vk5x01234567));
131 
132       vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
133       vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
134 
135       const v128_t vi6x01234567 = wasm_i16x8_load8x8(i6);
136       const v128_t vk6x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 48 * sizeof(int8_t)));
137       i6 += 8;
138 
139       vprod01234567 = wasm_i16x8_mul(vi6x01234567, vk6x01234567);
140 
141 
142       const v128_t vi7x01234567 = wasm_i16x8_load8x8(i7);
143       const v128_t vk7x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 56 * sizeof(int8_t)));
144       i7 += 8;
145 
146       vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi7x01234567, vk7x01234567));
147 
148       vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
149       vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
150 
151       const v128_t vi8x01234567 = wasm_i16x8_load8x8(i8);
152       const v128_t vk8x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 64 * sizeof(int8_t)));
153       i8 += 8;
154 
155       vprod01234567 = wasm_i16x8_mul(vi8x01234567, vk8x01234567);
156 
157       vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
158       vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
159 
160 
161       w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 72 * sizeof(int8_t));
162 
163       vacc0123 = wasm_f32x4_convert_i32x4(vacc0123);
164       vacc4567 = wasm_f32x4_convert_i32x4(vacc4567);
165 
166       const v128_t vscale0123 = wasm_v128_load(w);
167       const v128_t vscale4567 = wasm_v128_load((const float*) w + 4);
168       w = (const void*) ((const float*) w + 8);
169 
170       vacc0123 = wasm_f32x4_mul(vacc0123, vscale0123);
171       vacc4567 = wasm_f32x4_mul(vacc4567, vscale4567);
172 
173       const v128_t vmagic_bias = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias);
174       vacc0123 = wasm_f32x4_add(vacc0123, vmagic_bias);
175       vacc4567 = wasm_f32x4_add(vacc4567, vmagic_bias);
176 
177       const v128_t vmagic_min = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_min);
178       vacc0123 = wasm_i32x4_max(vacc0123, vmagic_min);
179       vacc4567 = wasm_i32x4_max(vacc4567, vmagic_min);
180 
181       const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias_less_output_zero_point);
182       vacc0123 = wasm_i32x4_sub(vacc0123, vmagic_bias_less_output_zero_point);
183       vacc4567 = wasm_i32x4_sub(vacc4567, vmagic_bias_less_output_zero_point);
184 
185       v128_t vout01234567 = wasm_i16x8_narrow_i32x4(vacc0123, vacc4567);
186 
187       v128_t vout0123456701234567 = wasm_i8x16_narrow_i16x8(vout01234567, vout01234567);
188 
189       const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
190       vout0123456701234567 = wasm_i8x16_min(vout0123456701234567, voutput_max);
191 
192       *((double*) output) = wasm_f64x2_extract_lane(vout0123456701234567, 0);
193       output += 8;
194     }
195     if XNN_UNLIKELY(c != 0) {
196       {
197         v128_t vacc0123 = wasm_v128_load(w);
198         v128_t vacc4567 = wasm_v128_load((const void*) ((uintptr_t) w + 4 * sizeof(int32_t)));
199 
200 
201         const v128_t vi0x01234567 = wasm_i16x8_load8x8(i0);
202         const v128_t vk0x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 0 * sizeof(int8_t)));
203 
204         v128_t vprod01234567 = wasm_i16x8_mul(vi0x01234567, vk0x01234567);
205 
206 
207         const v128_t vi1x01234567 = wasm_i16x8_load8x8(i1);
208         const v128_t vk1x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 8 * sizeof(int8_t)));
209 
210         vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi1x01234567, vk1x01234567));
211 
212         vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
213         vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
214 
215         const v128_t vi2x01234567 = wasm_i16x8_load8x8(i2);
216         const v128_t vk2x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 16 * sizeof(int8_t)));
217 
218         vprod01234567 = wasm_i16x8_mul(vi2x01234567, vk2x01234567);
219 
220 
221         const v128_t vi3x01234567 = wasm_i16x8_load8x8(i3);
222         const v128_t vk3x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 24 * sizeof(int8_t)));
223 
224         vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi3x01234567, vk3x01234567));
225 
226         vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
227         vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
228 
229         const v128_t vi4x01234567 = wasm_i16x8_load8x8(i4);
230         const v128_t vk4x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 32 * sizeof(int8_t)));
231 
232         vprod01234567 = wasm_i16x8_mul(vi4x01234567, vk4x01234567);
233 
234 
235         const v128_t vi5x01234567 = wasm_i16x8_load8x8(i5);
236         const v128_t vk5x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 40 * sizeof(int8_t)));
237 
238         vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi5x01234567, vk5x01234567));
239 
240         vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
241         vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
242 
243         const v128_t vi6x01234567 = wasm_i16x8_load8x8(i6);
244         const v128_t vk6x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 48 * sizeof(int8_t)));
245 
246         vprod01234567 = wasm_i16x8_mul(vi6x01234567, vk6x01234567);
247 
248 
249         const v128_t vi7x01234567 = wasm_i16x8_load8x8(i7);
250         const v128_t vk7x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 56 * sizeof(int8_t)));
251 
252         vprod01234567 = wasm_i16x8_add(vprod01234567, wasm_i16x8_mul(vi7x01234567, vk7x01234567));
253 
254         vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
255         vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
256 
257         const v128_t vi8x01234567 = wasm_i16x8_load8x8(i8);
258         const v128_t vk8x01234567 = wasm_i16x8_load8x8((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 64 * sizeof(int8_t)));
259 
260         vprod01234567 = wasm_i16x8_mul(vi8x01234567, vk8x01234567);
261 
262         vacc0123 = wasm_i32x4_add(vacc0123, wasm_i32x4_extend_low_i16x8(vprod01234567));
263         vacc4567 = wasm_i32x4_add(vacc4567, wasm_i32x4_extend_high_i16x8(vprod01234567));
264 
265 
266 
267       vacc0123 = wasm_f32x4_convert_i32x4(vacc0123);
268       vacc4567 = wasm_f32x4_convert_i32x4(vacc4567);
269 
270       const v128_t vscale0123 = wasm_v128_load((const float*) ((uintptr_t) w + 8 * sizeof(int32_t) + 72 * sizeof(int8_t)));
271       const v128_t vscale4567 = wasm_v128_load((const float*) ((uintptr_t) w + 8 * sizeof(int32_t) + 72 * sizeof(int8_t) + 4 * sizeof(float)));
272 
273       vacc0123 = wasm_f32x4_mul(vacc0123, vscale0123);
274       vacc4567 = wasm_f32x4_mul(vacc4567, vscale4567);
275 
276       const v128_t vmagic_bias = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias);
277       vacc0123 = wasm_f32x4_add(vacc0123, vmagic_bias);
278       vacc4567 = wasm_f32x4_add(vacc4567, vmagic_bias);
279 
280       const v128_t vmagic_min = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_min);
281       vacc0123 = wasm_i32x4_max(vacc0123, vmagic_min);
282       vacc4567 = wasm_i32x4_max(vacc4567, vmagic_min);
283 
284       const v128_t vmagic_bias_less_output_zero_point = wasm_v128_load64_splat(params->fp32_wasmsimd.magic_bias_less_output_zero_point);
285       vacc0123 = wasm_i32x4_sub(vacc0123, vmagic_bias_less_output_zero_point);
286       vacc4567 = wasm_i32x4_sub(vacc4567, vmagic_bias_less_output_zero_point);
287 
288       v128_t vout01234567 = wasm_i16x8_narrow_i32x4(vacc0123, vacc4567);
289       v128_t vout0123456701234567 = wasm_i8x16_narrow_i16x8(vout01234567, vout01234567);
290 
291       const v128_t voutput_max = wasm_v128_load64_splat(params->fp32_wasmsimd.output_max);
292       vout0123456701234567 = wasm_i8x16_min(vout0123456701234567, voutput_max);
293 
294 
295       if (c & 4) {
296         *((float*) output) = wasm_f32x4_extract_lane(vout0123456701234567, 0);
297         vout0123456701234567 = wasm_u64x2_shr(vout0123456701234567, 32);
298         output += 4;
299       }
300       uint32_t vout0123 = wasm_i32x4_extract_lane(vout0123456701234567, 0);
301       if (c & 2) {
302         *((uint16_t*) output) = (uint16_t) vout0123;
303         vout0123 >>= 16;
304         output += 2;
305       }
306       if (c & 1) {
307         *output = (int8_t) vout0123;
308         output += 1;
309       }
310       }
311     }
312 
313     output = (int8_t*) ((uintptr_t) output + output_increment);
314   } while (--output_width != 0);
315 }
316