• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/wasmsimd-magic.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 <arm_neon.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/vcvt.h>
17 
18 
xnn_f32_qu8_vcvt_ukernel__wasmsimd_magic_x24(size_t n,const float * x,uint8_t * y,const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_qu8_vcvt_ukernel__wasmsimd_magic_x24(
20     size_t n,
21     const float* x,
22     uint8_t* y,
23     const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(x != NULL);
28   assert(y != NULL);
29 
30   const v128_t vscale = wasm_v128_load64_splat(params->wasmsimd_magic.scale);
31   const v128_t vmagic_bias = wasm_v128_load64_splat(params->wasmsimd_magic.magic_bias);
32   const v128_t vmagic_min = wasm_v128_load64_splat(params->wasmsimd_magic.magic_min);
33   const v128_t vmagic_bias_less_zero_point = wasm_v128_load64_splat(params->wasmsimd_magic.magic_bias_less_zero_point);
34   const v128_t voutput_max = wasm_v128_load64_splat(params->wasmsimd_magic.output_max);
35   for (; n >= 24 * sizeof(float); n -= 24 * sizeof(float)) {
36     v128_t vx0123 = wasm_v128_load(x);
37     v128_t vx4567 = wasm_v128_load(x + 4);
38     v128_t vx89AB = wasm_v128_load(x + 8);
39     v128_t vxCDEF = wasm_v128_load(x + 12);
40     v128_t vxGHIJ = wasm_v128_load(x + 16);
41     v128_t vxKLMN = wasm_v128_load(x + 20);
42     x += 24;
43 
44     vx0123 = wasm_f32x4_mul(vx0123, vscale);
45     vx4567 = wasm_f32x4_mul(vx4567, vscale);
46     vx89AB = wasm_f32x4_mul(vx89AB, vscale);
47     vxCDEF = wasm_f32x4_mul(vxCDEF, vscale);
48     vxGHIJ = wasm_f32x4_mul(vxGHIJ, vscale);
49     vxKLMN = wasm_f32x4_mul(vxKLMN, vscale);
50 
51     vx0123 = wasm_f32x4_add(vx0123, vmagic_bias);
52     vx4567 = wasm_f32x4_add(vx4567, vmagic_bias);
53     vx89AB = wasm_f32x4_add(vx89AB, vmagic_bias);
54     vxCDEF = wasm_f32x4_add(vxCDEF, vmagic_bias);
55     vxGHIJ = wasm_f32x4_add(vxGHIJ, vmagic_bias);
56     vxKLMN = wasm_f32x4_add(vxKLMN, vmagic_bias);
57 
58     v128_t vacc0123 = wasm_i32x4_max(vx0123, vmagic_min);
59     v128_t vacc4567 = wasm_i32x4_max(vx4567, vmagic_min);
60     v128_t vacc89AB = wasm_i32x4_max(vx89AB, vmagic_min);
61     v128_t vaccCDEF = wasm_i32x4_max(vxCDEF, vmagic_min);
62     v128_t vaccGHIJ = wasm_i32x4_max(vxGHIJ, vmagic_min);
63     v128_t vaccKLMN = wasm_i32x4_max(vxKLMN, vmagic_min);
64 
65     vacc0123 = wasm_i32x4_sub(vacc0123, vmagic_bias_less_zero_point);
66     vacc4567 = wasm_i32x4_sub(vacc4567, vmagic_bias_less_zero_point);
67     vacc89AB = wasm_i32x4_sub(vacc89AB, vmagic_bias_less_zero_point);
68     vaccCDEF = wasm_i32x4_sub(vaccCDEF, vmagic_bias_less_zero_point);
69     vaccGHIJ = wasm_i32x4_sub(vaccGHIJ, vmagic_bias_less_zero_point);
70     vaccKLMN = wasm_i32x4_sub(vaccKLMN, vmagic_bias_less_zero_point);
71 
72     const v128_t vacc01234567 = wasm_i16x8_narrow_i32x4(vacc0123, vacc4567);
73     const v128_t vacc89ABCDEF = wasm_i16x8_narrow_i32x4(vacc89AB, vaccCDEF);
74     const v128_t vaccGHIJKLMN = wasm_i16x8_narrow_i32x4(vaccGHIJ, vaccKLMN);
75 
76     v128_t vy0123456789ABCDEF = wasm_u8x16_narrow_i16x8(vacc01234567, vacc89ABCDEF);
77     v128_t vyGHIJKLMN = wasm_u8x16_narrow_i16x8(vaccGHIJKLMN, vaccGHIJKLMN);
78 
79     vy0123456789ABCDEF = wasm_u8x16_min(vy0123456789ABCDEF, voutput_max);
80     vyGHIJKLMN = wasm_u8x16_min(vyGHIJKLMN, voutput_max);
81 
82     wasm_v128_store(y, vy0123456789ABCDEF);
83     *((double*) (y + 16)) = wasm_f64x2_extract_lane(vyGHIJKLMN, 0);
84     y += 24;
85   }
86   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
87     v128_t vx_lo = wasm_v128_load(x);
88     v128_t vx_hi = wasm_v128_load(x + 4);
89     x += 8;
90 
91     vx_lo = wasm_f32x4_mul(vx_lo, vscale);
92     vx_hi = wasm_f32x4_mul(vx_hi, vscale);
93 
94     vx_lo = wasm_f32x4_add(vx_lo, vmagic_bias);
95     vx_hi = wasm_f32x4_add(vx_hi, vmagic_bias);
96 
97     v128_t vacc_lo = wasm_i32x4_max(vx_lo, vmagic_min);
98     v128_t vacc_hi = wasm_i32x4_max(vx_hi, vmagic_min);
99 
100     vacc_lo = wasm_i32x4_sub(vacc_lo, vmagic_bias_less_zero_point);
101     vacc_hi = wasm_i32x4_sub(vacc_hi, vmagic_bias_less_zero_point);
102 
103     const v128_t vacc = wasm_i16x8_narrow_i32x4(vacc_lo, vacc_hi);
104 
105     v128_t vy = wasm_u8x16_narrow_i16x8(vacc, vacc);
106     vy = wasm_u8x16_min(vy, voutput_max);
107     *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
108     y += 8;
109   }
110   if XNN_UNLIKELY(n != 0) {
111     assert(n >= 1 * sizeof(float));
112     assert(n <= 7 * sizeof(float));
113     v128_t vx_lo = wasm_v128_load(x);
114     const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
115     v128_t vx_hi = wasm_v128_load(x_hi);
116 
117     vx_lo = wasm_f32x4_mul(vx_lo, vscale);
118     vx_hi = wasm_f32x4_mul(vx_hi, vscale);
119 
120     vx_lo = wasm_f32x4_add(vx_lo, vmagic_bias);
121     vx_hi = wasm_f32x4_add(vx_hi, vmagic_bias);
122 
123     v128_t vacc_lo = wasm_i32x4_max(vx_lo, vmagic_min);
124     v128_t vacc_hi = wasm_i32x4_max(vx_hi, vmagic_min);
125 
126     vacc_lo = wasm_i32x4_sub(vacc_lo, vmagic_bias_less_zero_point);
127     vacc_hi = wasm_i32x4_sub(vacc_hi, vmagic_bias_less_zero_point);
128 
129     const v128_t vacc = wasm_i16x8_narrow_i32x4(vacc_lo, vacc_hi);
130 
131     v128_t vy = wasm_u8x16_narrow_i16x8(vacc, vacc);
132     vy = wasm_u8x16_min(vy, voutput_max);
133 
134     if (n & (4 * sizeof(float))) {
135       *((float*) y) = wasm_f32x4_extract_lane(vy, 0);
136       y += 4;
137       vy = wasm_u64x2_shr(vy, 32);
138     }
139     uint32_t vy_lo = (uint32_t) wasm_i32x4_extract_lane(vy, 0);
140     if (n & (2 * sizeof(float))) {
141       *((uint16_t*) y) = (uint16_t) vy_lo;
142       y += 2;
143       vy_lo >>= 16;
144     }
145     if (n & (1 * sizeof(float))) {
146       *y = (uint8_t) vy_lo;
147     }
148   }
149 }
150