• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-dwconv/unipass-scalar.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 <xnnpack/dwconv.h>
13 #include <xnnpack/math.h>
14 
15 
xnn_qu8_dwconv_minmax_fp32_ukernel_up4x9__wasm_fmagic(size_t channels,size_t output_width,const uint8_t ** input,const void * weights,uint8_t * output,size_t input_stride,size_t output_increment,size_t input_offset,const uint8_t * zero,const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])16 void xnn_qu8_dwconv_minmax_fp32_ukernel_up4x9__wasm_fmagic(
17     size_t channels,
18     size_t output_width,
19     const uint8_t** input,
20     const void* weights,
21     uint8_t* output,
22     size_t input_stride,
23     size_t output_increment,
24     size_t input_offset,
25     const uint8_t* zero,
26     const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28   assert(channels != 0);
29   assert(output_width != 0);
30 
31   const float vscale = params->fp32_scalar_fmagic.scale;
32   const float voutput_min_less_zero_point = params->fp32_scalar_fmagic.output_min_less_zero_point;
33   const float voutput_max_less_zero_point = params->fp32_scalar_fmagic.output_max_less_zero_point;
34   const float vmagic_bias = params->fp32_scalar_fmagic.magic_bias;
35   const int32_t vmagic_bias_less_output_zero_point = params->fp32_scalar_fmagic.magic_bias_less_output_zero_point;
36   const int32_t vkernel_zero_point = params->fp32_scalar_fmagic.kernel_zero_point;
37   do {
38     const uint8_t* i0 = input[0];
39     assert(i0 != NULL);
40     if XNN_UNPREDICTABLE(i0 != zero) {
41       i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
42     }
43     const uint8_t* i1 = input[1];
44     assert(i1 != NULL);
45     if XNN_UNPREDICTABLE(i1 != zero) {
46       i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
47     }
48     const uint8_t* i2 = input[2];
49     assert(i2 != NULL);
50     if XNN_UNPREDICTABLE(i2 != zero) {
51       i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
52     }
53     const uint8_t* i3 = input[3];
54     assert(i3 != NULL);
55     if XNN_UNPREDICTABLE(i3 != zero) {
56       i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
57     }
58     const uint8_t* i4 = input[4];
59     assert(i4 != NULL);
60     if XNN_UNPREDICTABLE(i4 != zero) {
61       i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
62     }
63     const uint8_t* i5 = input[5];
64     assert(i5 != NULL);
65     if XNN_UNPREDICTABLE(i5 != zero) {
66       i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
67     }
68     const uint8_t* i6 = input[6];
69     assert(i6 != NULL);
70     if XNN_UNPREDICTABLE(i6 != zero) {
71       i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
72     }
73     const uint8_t* i7 = input[7];
74     assert(i7 != NULL);
75     if XNN_UNPREDICTABLE(i7 != zero) {
76       i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
77     }
78     const uint8_t* i8 = input[8];
79     assert(i8 != NULL);
80     if XNN_UNPREDICTABLE(i8 != zero) {
81       i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset);
82     }
83     input = (const uint8_t**) ((uintptr_t) input + input_stride);
84 
85     size_t c = channels;
86     const void* w = weights;
87     for (; c >= 4; c -= 4) {
88       int32_t vacc0 = ((const int32_t*) w)[0];
89       int32_t vacc1 = ((const int32_t*) w)[1];
90       int32_t vacc2 = ((const int32_t*) w)[2];
91       int32_t vacc3 = ((const int32_t*) w)[3];
92 
93 
94       const int32_t vi0x0 = (int32_t) (uint32_t) i0[0];
95       const int32_t vi0x1 = (int32_t) (uint32_t) i0[1];
96       const int32_t vi0x2 = (int32_t) (uint32_t) i0[2];
97       const int32_t vi0x3 = (int32_t) (uint32_t) i0[3];
98       i0 += 4;
99 
100       const int32_t vk0x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[0] - vkernel_zero_point;
101       const int32_t vk0x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[1] - vkernel_zero_point;
102       const int32_t vk0x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[2] - vkernel_zero_point;
103       const int32_t vk0x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[3] - vkernel_zero_point;
104 
105       vacc0 += vi0x0 * vk0x0;
106       vacc1 += vi0x1 * vk0x1;
107       vacc2 += vi0x2 * vk0x2;
108       vacc3 += vi0x3 * vk0x3;
109 
110       const int32_t vi1x0 = (int32_t) (uint32_t) i1[0];
111       const int32_t vi1x1 = (int32_t) (uint32_t) i1[1];
112       const int32_t vi1x2 = (int32_t) (uint32_t) i1[2];
113       const int32_t vi1x3 = (int32_t) (uint32_t) i1[3];
114       i1 += 4;
115 
116       const int32_t vk1x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[4] - vkernel_zero_point;
117       const int32_t vk1x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[5] - vkernel_zero_point;
118       const int32_t vk1x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[6] - vkernel_zero_point;
119       const int32_t vk1x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[7] - vkernel_zero_point;
120 
121       vacc0 += vi1x0 * vk1x0;
122       vacc1 += vi1x1 * vk1x1;
123       vacc2 += vi1x2 * vk1x2;
124       vacc3 += vi1x3 * vk1x3;
125 
126       const int32_t vi2x0 = (int32_t) (uint32_t) i2[0];
127       const int32_t vi2x1 = (int32_t) (uint32_t) i2[1];
128       const int32_t vi2x2 = (int32_t) (uint32_t) i2[2];
129       const int32_t vi2x3 = (int32_t) (uint32_t) i2[3];
130       i2 += 4;
131 
132       const int32_t vk2x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[8] - vkernel_zero_point;
133       const int32_t vk2x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[9] - vkernel_zero_point;
134       const int32_t vk2x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[10] - vkernel_zero_point;
135       const int32_t vk2x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[11] - vkernel_zero_point;
136 
137       vacc0 += vi2x0 * vk2x0;
138       vacc1 += vi2x1 * vk2x1;
139       vacc2 += vi2x2 * vk2x2;
140       vacc3 += vi2x3 * vk2x3;
141 
142       const int32_t vi3x0 = (int32_t) (uint32_t) i3[0];
143       const int32_t vi3x1 = (int32_t) (uint32_t) i3[1];
144       const int32_t vi3x2 = (int32_t) (uint32_t) i3[2];
145       const int32_t vi3x3 = (int32_t) (uint32_t) i3[3];
146       i3 += 4;
147 
148       const int32_t vk3x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[12] - vkernel_zero_point;
149       const int32_t vk3x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[13] - vkernel_zero_point;
150       const int32_t vk3x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[14] - vkernel_zero_point;
151       const int32_t vk3x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[15] - vkernel_zero_point;
152 
153       vacc0 += vi3x0 * vk3x0;
154       vacc1 += vi3x1 * vk3x1;
155       vacc2 += vi3x2 * vk3x2;
156       vacc3 += vi3x3 * vk3x3;
157 
158       const int32_t vi4x0 = (int32_t) (uint32_t) i4[0];
159       const int32_t vi4x1 = (int32_t) (uint32_t) i4[1];
160       const int32_t vi4x2 = (int32_t) (uint32_t) i4[2];
161       const int32_t vi4x3 = (int32_t) (uint32_t) i4[3];
162       i4 += 4;
163 
164       const int32_t vk4x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[16] - vkernel_zero_point;
165       const int32_t vk4x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[17] - vkernel_zero_point;
166       const int32_t vk4x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[18] - vkernel_zero_point;
167       const int32_t vk4x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[19] - vkernel_zero_point;
168 
169       vacc0 += vi4x0 * vk4x0;
170       vacc1 += vi4x1 * vk4x1;
171       vacc2 += vi4x2 * vk4x2;
172       vacc3 += vi4x3 * vk4x3;
173 
174       const int32_t vi5x0 = (int32_t) (uint32_t) i5[0];
175       const int32_t vi5x1 = (int32_t) (uint32_t) i5[1];
176       const int32_t vi5x2 = (int32_t) (uint32_t) i5[2];
177       const int32_t vi5x3 = (int32_t) (uint32_t) i5[3];
178       i5 += 4;
179 
180       const int32_t vk5x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[20] - vkernel_zero_point;
181       const int32_t vk5x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[21] - vkernel_zero_point;
182       const int32_t vk5x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[22] - vkernel_zero_point;
183       const int32_t vk5x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[23] - vkernel_zero_point;
184 
185       vacc0 += vi5x0 * vk5x0;
186       vacc1 += vi5x1 * vk5x1;
187       vacc2 += vi5x2 * vk5x2;
188       vacc3 += vi5x3 * vk5x3;
189 
190       const int32_t vi6x0 = (int32_t) (uint32_t) i6[0];
191       const int32_t vi6x1 = (int32_t) (uint32_t) i6[1];
192       const int32_t vi6x2 = (int32_t) (uint32_t) i6[2];
193       const int32_t vi6x3 = (int32_t) (uint32_t) i6[3];
194       i6 += 4;
195 
196       const int32_t vk6x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[24] - vkernel_zero_point;
197       const int32_t vk6x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[25] - vkernel_zero_point;
198       const int32_t vk6x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[26] - vkernel_zero_point;
199       const int32_t vk6x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[27] - vkernel_zero_point;
200 
201       vacc0 += vi6x0 * vk6x0;
202       vacc1 += vi6x1 * vk6x1;
203       vacc2 += vi6x2 * vk6x2;
204       vacc3 += vi6x3 * vk6x3;
205 
206       const int32_t vi7x0 = (int32_t) (uint32_t) i7[0];
207       const int32_t vi7x1 = (int32_t) (uint32_t) i7[1];
208       const int32_t vi7x2 = (int32_t) (uint32_t) i7[2];
209       const int32_t vi7x3 = (int32_t) (uint32_t) i7[3];
210       i7 += 4;
211 
212       const int32_t vk7x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[28] - vkernel_zero_point;
213       const int32_t vk7x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[29] - vkernel_zero_point;
214       const int32_t vk7x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[30] - vkernel_zero_point;
215       const int32_t vk7x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[31] - vkernel_zero_point;
216 
217       vacc0 += vi7x0 * vk7x0;
218       vacc1 += vi7x1 * vk7x1;
219       vacc2 += vi7x2 * vk7x2;
220       vacc3 += vi7x3 * vk7x3;
221 
222       const int32_t vi8x0 = (int32_t) (uint32_t) i8[0];
223       const int32_t vi8x1 = (int32_t) (uint32_t) i8[1];
224       const int32_t vi8x2 = (int32_t) (uint32_t) i8[2];
225       const int32_t vi8x3 = (int32_t) (uint32_t) i8[3];
226       i8 += 4;
227 
228       const int32_t vk8x0 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[32] - vkernel_zero_point;
229       const int32_t vk8x1 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[33] - vkernel_zero_point;
230       const int32_t vk8x2 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[34] - vkernel_zero_point;
231       const int32_t vk8x3 = (int32_t) (uint32_t) ((const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t)))[35] - vkernel_zero_point;
232 
233       vacc0 += vi8x0 * vk8x0;
234       vacc1 += vi8x1 * vk8x1;
235       vacc2 += vi8x2 * vk8x2;
236       vacc3 += vi8x3 * vk8x3;
237 
238       w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t) + 36 * sizeof(uint8_t));
239 
240       float vfpacc0 = (float) vacc0;
241       float vfpacc1 = (float) vacc1;
242       float vfpacc2 = (float) vacc2;
243       float vfpacc3 = (float) vacc3;
244 
245       vfpacc0 *= vscale;
246       vfpacc1 *= vscale;
247       vfpacc2 *= vscale;
248       vfpacc3 *= vscale;
249 
250       vfpacc0 = __builtin_wasm_max_f32(vfpacc0, voutput_min_less_zero_point);
251       vfpacc1 = __builtin_wasm_max_f32(vfpacc1, voutput_min_less_zero_point);
252       vfpacc2 = __builtin_wasm_max_f32(vfpacc2, voutput_min_less_zero_point);
253       vfpacc3 = __builtin_wasm_max_f32(vfpacc3, voutput_min_less_zero_point);
254 
255       vfpacc0 = __builtin_wasm_min_f32(vfpacc0, voutput_max_less_zero_point);
256       vfpacc1 = __builtin_wasm_min_f32(vfpacc1, voutput_max_less_zero_point);
257       vfpacc2 = __builtin_wasm_min_f32(vfpacc2, voutput_max_less_zero_point);
258       vfpacc3 = __builtin_wasm_min_f32(vfpacc3, voutput_max_less_zero_point);
259 
260       vfpacc0 += vmagic_bias;
261       vfpacc1 += vmagic_bias;
262       vfpacc2 += vmagic_bias;
263       vfpacc3 += vmagic_bias;
264 
265       int32_t vout0 = (int32_t) float_as_uint32(vfpacc0) - vmagic_bias_less_output_zero_point;
266       int32_t vout1 = (int32_t) float_as_uint32(vfpacc1) - vmagic_bias_less_output_zero_point;
267       int32_t vout2 = (int32_t) float_as_uint32(vfpacc2) - vmagic_bias_less_output_zero_point;
268       int32_t vout3 = (int32_t) float_as_uint32(vfpacc3) - vmagic_bias_less_output_zero_point;
269 
270       output[0] = (uint8_t) vout0;
271       output[1] = (uint8_t) vout1;
272       output[2] = (uint8_t) vout2;
273       output[3] = (uint8_t) vout3;
274       output += 4;
275     }
276     if XNN_UNLIKELY(c != 0) {
277       const uint8_t* k = (const uint8_t*) ((uintptr_t) w + 4 * sizeof(int32_t));
278       do {
279         int32_t vacc = *((const int32_t*) w);
280         w = (const void*) ((uintptr_t) w + sizeof(int32_t));
281 
282         const int32_t vi0 = (int32_t) (uint32_t) *i0++;
283         const int32_t vk0 = (int32_t) (uint32_t) k[0] - vkernel_zero_point;
284         vacc += vi0 * vk0;
285         const int32_t vi1 = (int32_t) (uint32_t) *i1++;
286         const int32_t vk1 = (int32_t) (uint32_t) k[4] - vkernel_zero_point;
287         vacc += vi1 * vk1;
288         const int32_t vi2 = (int32_t) (uint32_t) *i2++;
289         const int32_t vk2 = (int32_t) (uint32_t) k[8] - vkernel_zero_point;
290         vacc += vi2 * vk2;
291         const int32_t vi3 = (int32_t) (uint32_t) *i3++;
292         const int32_t vk3 = (int32_t) (uint32_t) k[12] - vkernel_zero_point;
293         vacc += vi3 * vk3;
294         const int32_t vi4 = (int32_t) (uint32_t) *i4++;
295         const int32_t vk4 = (int32_t) (uint32_t) k[16] - vkernel_zero_point;
296         vacc += vi4 * vk4;
297         const int32_t vi5 = (int32_t) (uint32_t) *i5++;
298         const int32_t vk5 = (int32_t) (uint32_t) k[20] - vkernel_zero_point;
299         vacc += vi5 * vk5;
300         const int32_t vi6 = (int32_t) (uint32_t) *i6++;
301         const int32_t vk6 = (int32_t) (uint32_t) k[24] - vkernel_zero_point;
302         vacc += vi6 * vk6;
303         const int32_t vi7 = (int32_t) (uint32_t) *i7++;
304         const int32_t vk7 = (int32_t) (uint32_t) k[28] - vkernel_zero_point;
305         vacc += vi7 * vk7;
306         const int32_t vi8 = (int32_t) (uint32_t) *i8++;
307         const int32_t vk8 = (int32_t) (uint32_t) k[32] - vkernel_zero_point;
308         vacc += vi8 * vk8;
309         k += 1;
310 
311         float vfpacc = (float) vacc * vscale;
312 
313         vfpacc = __builtin_wasm_max_f32(vfpacc, voutput_min_less_zero_point);
314         vfpacc = __builtin_wasm_min_f32(vfpacc, voutput_max_less_zero_point);
315         vfpacc += vmagic_bias;
316         int32_t vout = (int32_t) float_as_uint32(vfpacc) - vmagic_bias_less_output_zero_point;
317 
318         *output++ = (uint8_t) vout;
319       } while (--c != 0);
320     }
321 
322     output = (uint8_t*) ((uintptr_t) output + output_increment);
323   } while (--output_width != 0);
324 }
325