xref: /aosp_15_r20/external/XNNPACK/src/math/cvt-f32-qu8-wasmsimd.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2022 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <wasm_simd128.h>
11 
12 #include <xnnpack/math-stubs.h>
13 
14 
xnn_math_f32_qu8_cvt__wasmsimd(size_t n,const float * input,uint8_t * output,uint8_t output_zero_point)15 void xnn_math_f32_qu8_cvt__wasmsimd(
16     size_t n,
17     const float* input,
18     uint8_t* output,
19     uint8_t output_zero_point)
20 {
21   assert(n % (16 * sizeof(uint8_t)) == 0);
22 
23   const v128_t vmin = wasm_f32x4_splat(12582912.0f - (float) (int32_t) output_zero_point);
24   const v128_t vfmagic = wasm_f32x4_const_splat(12582912.0f);
25   const v128_t vimagic = wasm_i32x4_splat(INT32_C(0x4B400000) - (int32_t) output_zero_point);
26   for (; n != 0; n -= 16 * sizeof(uint8_t)) {
27     const v128_t vx_ll = wasm_v128_load(input);
28     const v128_t vx_lh = wasm_v128_load(input + 4);
29     const v128_t vx_hl = wasm_v128_load(input + 8);
30     const v128_t vx_hh = wasm_v128_load(input + 12);
31     input += 16;
32 
33     v128_t vy_ll = wasm_f32x4_add(vx_ll, vfmagic);
34     v128_t vy_lh = wasm_f32x4_add(vx_lh, vfmagic);
35     v128_t vy_hl = wasm_f32x4_add(vx_hl, vfmagic);
36     v128_t vy_hh = wasm_f32x4_add(vx_hh, vfmagic);
37 
38     vy_ll = wasm_i32x4_max(vy_ll, vmin);
39     vy_lh = wasm_i32x4_max(vy_lh, vmin);
40     vy_hl = wasm_i32x4_max(vy_hl, vmin);
41     vy_hh = wasm_i32x4_max(vy_hh, vmin);
42 
43     vy_ll = wasm_i32x4_sub(vy_ll, vimagic);
44     vy_lh = wasm_i32x4_sub(vy_lh, vimagic);
45     vy_hl = wasm_i32x4_sub(vy_hl, vimagic);
46     vy_hh = wasm_i32x4_sub(vy_hh, vimagic);
47 
48     const v128_t vy_lo = wasm_i16x8_narrow_i32x4(vy_ll, vy_lh);
49     const v128_t vy_hi = wasm_i16x8_narrow_i32x4(vy_hl, vy_hh);
50 
51     const v128_t vout = wasm_u8x16_narrow_i16x8(vy_lo, vy_hi);
52     wasm_v128_store(output, vout);
53     output += 16;
54   }
55 }
56