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 <arm_neon.h>
11
12 #include <xnnpack/math-stubs.h>
13
14
xnn_math_f32_roundu__neon_cvt(size_t n,const float * input,float * output)15 void xnn_math_f32_roundu__neon_cvt(
16 size_t n,
17 const float* input,
18 float* output)
19 {
20 assert(n % (4 * sizeof(float)) == 0);
21
22 // Threshold of non-integral values in single-precision floating-point representation.
23 // All inputs above this threshold (by absolute value) are integer numbers.
24 const float32x4_t vintegral_threshold = vmovq_n_f32(0x1.000000p+23f);
25 // Mask for the sign of a single-precision floating-point number.
26 const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000));
27 // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation.
28 const float32x4_t vone = vmovq_n_f32(1.0f);
29
30 for (; n != 0; n -= 4 * sizeof(float)) {
31 const float32x4_t vx = vld1q_f32(input); input += 4;
32
33 // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point.
34 // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23.
35 const float32x4_t vprerndx = vcvtq_f32_s32(vcvtq_s32_f32(vx));
36
37 // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x.
38 // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x.
39 // If x is guaranteed integral or NaN, use all bits from x.
40 const uint32x4_t vrndmask = vbicq_u32(vcaltq_f32(vx, vintegral_threshold), vsign_mask);
41
42 // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value.
43 // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion.
44 // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x.
45 // For abs(x) >= 2**23 or NaN inputs, the result is x itself.
46 const float32x4_t vrndx = vbslq_f32(vrndmask, vprerndx, vx);
47
48 // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x.
49 // If rounded x >= x, we want all bits from rounded x.
50 // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded
51 // x and the sign bit from rounded x (same as the sign bit of x).
52 const uint32x4_t vadjmask = vorrq_u32(vcgeq_f32(vrndx, vx), vsign_mask);
53 // Adjust the rounded x value.
54 // The adjusted value is a unit above the rounded-towards-zero x value, but is used only if the rounded value is
55 // below x. In these cases, the adjusted value is x rounded up.
56 // Note: addition implicitly converts SNaN inputs to QNaNs.
57 const float32x4_t vadjrndx = vaddq_f32(vrndx, vone);
58
59 // Combine the adjusted rounded x and the original rounded towards zero x.
60 // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of
61 // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero
62 // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0.
63 // For rounded x >= x, the result is the rounded-towards-zero x.
64 // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment).
65 const float32x4_t vy = vbslq_f32(vadjmask, vrndx, vadjrndx);
66
67 vst1q_f32(output, vy); output += 4;
68 }
69 }
70