1 // Copyright 2020 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_roundd__wasmsimd_addsub(size_t n,const float * input,float * output)15 void xnn_math_f32_roundd__wasmsimd_addsub(
16 size_t n,
17 const float* input,
18 float* output)
19 {
20 assert(n % (4 * sizeof(float)) == 0);
21
22 // Mask for the sign bit of a floating-point number.
23 const v128_t vsign_mask = wasm_i32x4_splat(INT32_C(0x80000000));
24 // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic
25 // number is subtracted back from the result to get original x rounded to integer. This trick works only for
26 // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to
27 // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number.
28 const v128_t vmagic_number = wasm_f32x4_splat(0x1.000000p+23f);
29 // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation.
30 const v128_t vone = wasm_f32x4_splat(1.0f);
31
32 for (; n != 0; n -= 4 * sizeof(float)) {
33 const v128_t vx = wasm_v128_load(input);
34 input += 4;
35
36 // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in
37 // the end. This method works for round-to-nearest-even because it is an odd function.
38 const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask);
39
40 // Compute bitmask for the bits we want to copy from x. Other bits will be copied from the rounded abs(x).
41 // If abs(x) < 2**23 or x is NaN, we want the sign bit from x and the rest from the rounded abs(x).
42 // Otherwise (abs(x) >= 2**23), we want all bits from x.
43 const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_ge(vabsx, vmagic_number));
44 // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x).
45 // Note: the result is valid only for 0 <= abs(x) < 2**23.
46 // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs.
47 const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number);
48
49 // Combine abs(x) rounded via addition-subtraction trick and the input x value.
50 // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x.
51 // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction.
52 // For abs(x) >= 2**23, the result is x itself.
53 const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, vrndmask);
54
55 // Adjust x rounded towards nearest-even to get x rounded down.
56 // Note: subtraction implicitly converts SNaN inputs to QNaNs.
57 const v128_t vy = wasm_f32x4_sub(vrndx, wasm_v128_and(wasm_f32x4_gt(vrndx, vx), vone));
58
59 wasm_v128_store(output, vy);
60 output += 4;
61 }
62 }
63