xref: /aosp_15_r20/external/XNNPACK/src/math/roundne-wasmsimd-addsub.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1*4bdc9457SAndroid Build Coastguard Worker // Copyright 2020 Google LLC
2*4bdc9457SAndroid Build Coastguard Worker //
3*4bdc9457SAndroid Build Coastguard Worker // This source code is licensed under the BSD-style license found in the
4*4bdc9457SAndroid Build Coastguard Worker // LICENSE file in the root directory of this source tree.
5*4bdc9457SAndroid Build Coastguard Worker 
6*4bdc9457SAndroid Build Coastguard Worker #include <assert.h>
7*4bdc9457SAndroid Build Coastguard Worker #include <stddef.h>
8*4bdc9457SAndroid Build Coastguard Worker #include <stdint.h>
9*4bdc9457SAndroid Build Coastguard Worker 
10*4bdc9457SAndroid Build Coastguard Worker #include <wasm_simd128.h>
11*4bdc9457SAndroid Build Coastguard Worker 
12*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/math-stubs.h>
13*4bdc9457SAndroid Build Coastguard Worker 
14*4bdc9457SAndroid Build Coastguard Worker 
xnn_math_f32_roundne__wasmsimd_addsub(size_t n,const float * input,float * output)15*4bdc9457SAndroid Build Coastguard Worker void xnn_math_f32_roundne__wasmsimd_addsub(
16*4bdc9457SAndroid Build Coastguard Worker     size_t n,
17*4bdc9457SAndroid Build Coastguard Worker     const float* input,
18*4bdc9457SAndroid Build Coastguard Worker     float* output)
19*4bdc9457SAndroid Build Coastguard Worker {
20*4bdc9457SAndroid Build Coastguard Worker   assert(n % (4 * sizeof(float)) == 0);
21*4bdc9457SAndroid Build Coastguard Worker 
22*4bdc9457SAndroid Build Coastguard Worker   // Mask for the sign bit of a floating-point number.
23*4bdc9457SAndroid Build Coastguard Worker   const v128_t vsign_mask = wasm_i32x4_const_splat(INT32_C(0x80000000));
24*4bdc9457SAndroid Build Coastguard Worker   // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic
25*4bdc9457SAndroid Build Coastguard Worker   // number is subtracted back from the result to get original x rounded to integer. This trick works only for
26*4bdc9457SAndroid Build Coastguard Worker   // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to
27*4bdc9457SAndroid Build Coastguard Worker   // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number.
28*4bdc9457SAndroid Build Coastguard Worker   const v128_t vmagic_number = wasm_f32x4_const_splat(0x1.000000p+23f);
29*4bdc9457SAndroid Build Coastguard Worker 
30*4bdc9457SAndroid Build Coastguard Worker   for (; n != 0; n -= 4 * sizeof(float)) {
31*4bdc9457SAndroid Build Coastguard Worker     const v128_t vx = wasm_v128_load(input);
32*4bdc9457SAndroid Build Coastguard Worker     input += 4;
33*4bdc9457SAndroid Build Coastguard Worker 
34*4bdc9457SAndroid Build Coastguard Worker     // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in
35*4bdc9457SAndroid Build Coastguard Worker     // the end. This method works for round-to-nearest-even because it is an odd function.
36*4bdc9457SAndroid Build Coastguard Worker     const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask);
37*4bdc9457SAndroid Build Coastguard Worker 
38*4bdc9457SAndroid Build Coastguard Worker     // Compute bitmask for the bits we want to copy from x. Other bits will be copied from the rounded abs(x).
39*4bdc9457SAndroid Build Coastguard Worker     // If abs(x) < 2**23 or x is NaN, we want the sign bit from x and the rest from the rounded abs(x).
40*4bdc9457SAndroid Build Coastguard Worker     // Otherwise (abs(x) >= 2**23), we want all bits from x.
41*4bdc9457SAndroid Build Coastguard Worker     const v128_t vrndmask = wasm_v128_or(vsign_mask,  wasm_f32x4_gt(vabsx, vmagic_number));
42*4bdc9457SAndroid Build Coastguard Worker     // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x).
43*4bdc9457SAndroid Build Coastguard Worker     // Note: the result is valid only for 0 <= abs(x) < 2**23.
44*4bdc9457SAndroid Build Coastguard Worker     // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs.
45*4bdc9457SAndroid Build Coastguard Worker     const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number);
46*4bdc9457SAndroid Build Coastguard Worker 
47*4bdc9457SAndroid Build Coastguard Worker     // Combine abs(x) rounded via addition-subtraction trick and the input x value.
48*4bdc9457SAndroid Build Coastguard Worker     // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x.
49*4bdc9457SAndroid Build Coastguard Worker     // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction.
50*4bdc9457SAndroid Build Coastguard Worker     // For abs(x) >= 2**23, the result is x itself.
51*4bdc9457SAndroid Build Coastguard Worker     const v128_t vy = wasm_v128_bitselect(vx, vrndabsx, vrndmask);
52*4bdc9457SAndroid Build Coastguard Worker 
53*4bdc9457SAndroid Build Coastguard Worker     wasm_v128_store(output, vy);
54*4bdc9457SAndroid Build Coastguard Worker     output += 4;
55*4bdc9457SAndroid Build Coastguard Worker   }
56*4bdc9457SAndroid Build Coastguard Worker }
57