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_roundu__wasmsimd_cvt(size_t n,const float * input,float * output)15*4bdc9457SAndroid Build Coastguard Worker void xnn_math_f32_roundu__wasmsimd_cvt(
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 // Threshold of non-integral values in single-precision floating-point representation.
23*4bdc9457SAndroid Build Coastguard Worker // All inputs above this threshold (by absolute value) are integer numbers.
24*4bdc9457SAndroid Build Coastguard Worker const v128_t vintegral_threshold = wasm_f32x4_const_splat(0x1.000000p+23f);
25*4bdc9457SAndroid Build Coastguard Worker // Mask for the sign of a single-precision floating-point number.
26*4bdc9457SAndroid Build Coastguard Worker const v128_t vsign_mask = wasm_f32x4_const_splat(-0.0f);
27*4bdc9457SAndroid Build Coastguard Worker // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation.
28*4bdc9457SAndroid Build Coastguard Worker const v128_t vone = wasm_f32x4_const_splat(1.0f);
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 // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point.
35*4bdc9457SAndroid Build Coastguard Worker // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23.
36*4bdc9457SAndroid Build Coastguard Worker const v128_t vprerndx = wasm_f32x4_convert_i32x4(wasm_i32x4_trunc_sat_f32x4(vx));
37*4bdc9457SAndroid Build Coastguard Worker
38*4bdc9457SAndroid Build Coastguard Worker // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x.
39*4bdc9457SAndroid Build Coastguard Worker // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x.
40*4bdc9457SAndroid Build Coastguard Worker // If x is guaranteed integral or NaN, use all bits from x.
41*4bdc9457SAndroid Build Coastguard Worker const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(wasm_f32x4_abs(vx), vintegral_threshold), vsign_mask);
42*4bdc9457SAndroid Build Coastguard Worker
43*4bdc9457SAndroid Build Coastguard Worker // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value.
44*4bdc9457SAndroid Build Coastguard Worker // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion.
45*4bdc9457SAndroid Build Coastguard Worker // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x.
46*4bdc9457SAndroid Build Coastguard Worker // For abs(x) >= 2**23 or NaN inputs, the result is x itself.
47*4bdc9457SAndroid Build Coastguard Worker const v128_t vrndx = wasm_v128_bitselect(vprerndx, vx, vrndmask);
48*4bdc9457SAndroid Build Coastguard Worker
49*4bdc9457SAndroid Build Coastguard Worker // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x.
50*4bdc9457SAndroid Build Coastguard Worker // If rounded x >= x, we want all bits from rounded x.
51*4bdc9457SAndroid Build Coastguard Worker // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded
52*4bdc9457SAndroid Build Coastguard Worker // x and the sign bit from rounded x (same as the sign bit of x).
53*4bdc9457SAndroid Build Coastguard Worker const v128_t vadjmask = wasm_v128_or(wasm_f32x4_ge(vrndx, vx), vsign_mask);
54*4bdc9457SAndroid Build Coastguard Worker // Adjust the rounded x value.
55*4bdc9457SAndroid Build Coastguard Worker // The adjusted value is a unit above the rounded-towards-zero x value, but is used only if the rounded value is
56*4bdc9457SAndroid Build Coastguard Worker // below x. In these cases, the adjusted value is x rounded up.
57*4bdc9457SAndroid Build Coastguard Worker // Note: addition implicitly converts SNaN inputs to QNaNs.
58*4bdc9457SAndroid Build Coastguard Worker const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone);
59*4bdc9457SAndroid Build Coastguard Worker
60*4bdc9457SAndroid Build Coastguard Worker // Combine the adjusted rounded x and the original rounded towards zero x.
61*4bdc9457SAndroid Build Coastguard Worker // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of
62*4bdc9457SAndroid Build Coastguard Worker // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero
63*4bdc9457SAndroid Build Coastguard Worker // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0.
64*4bdc9457SAndroid Build Coastguard Worker // For rounded x >= x, the result is the rounded-towards-zero x.
65*4bdc9457SAndroid Build Coastguard Worker // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment).
66*4bdc9457SAndroid Build Coastguard Worker const v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask);
67*4bdc9457SAndroid Build Coastguard Worker
68*4bdc9457SAndroid Build Coastguard Worker wasm_v128_store(output, vy);
69*4bdc9457SAndroid Build Coastguard Worker output += 4;
70*4bdc9457SAndroid Build Coastguard Worker }
71*4bdc9457SAndroid Build Coastguard Worker }
72