xref: /aosp_15_r20/external/XNNPACK/src/math/roundu-neon-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 <arm_neon.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__neon_addsub(size_t n,const float * input,float * output)15*4bdc9457SAndroid Build Coastguard Worker void xnn_math_f32_roundu__neon_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   // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic
23*4bdc9457SAndroid Build Coastguard Worker   // number is subtracted back from the result to get original x rounded to integer. This trick works only for
24*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
25*4bdc9457SAndroid Build Coastguard Worker   // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number.
26*4bdc9457SAndroid Build Coastguard Worker   const float32x4_t vmagic_number = vmovq_n_f32(0x1.000000p+23f);
27*4bdc9457SAndroid Build Coastguard Worker   // Mask for the sign bit of a floating-point number.
28*4bdc9457SAndroid Build Coastguard Worker   const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000));
29*4bdc9457SAndroid Build Coastguard Worker   // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-to-nearest-even operation.
30*4bdc9457SAndroid Build Coastguard Worker   const float32x4_t vone = vmovq_n_f32(1.0f);
31*4bdc9457SAndroid Build Coastguard Worker 
32*4bdc9457SAndroid Build Coastguard Worker   for (; n != 0; n -= 4 * sizeof(float)) {
33*4bdc9457SAndroid Build Coastguard Worker     const float32x4_t vx = vld1q_f32(input); input += 4;
34*4bdc9457SAndroid Build Coastguard Worker 
35*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
36*4bdc9457SAndroid Build Coastguard Worker     // the end. This method works for round-to-nearest-even because it is an odd function.
37*4bdc9457SAndroid Build Coastguard Worker     const float32x4_t vabsx = vabsq_f32(vx);
38*4bdc9457SAndroid Build Coastguard Worker     // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x.
39*4bdc9457SAndroid Build Coastguard Worker     // If abs(x) >= 2**23, we want all bits from x.
40*4bdc9457SAndroid Build Coastguard Worker     // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x.
41*4bdc9457SAndroid Build Coastguard Worker     // Note: we do vcaltq_f32(vmagic_number, vx) instead of vcltq_f32(vmagic_number, vabsx) to reduce dependency chain.
42*4bdc9457SAndroid Build Coastguard Worker     const uint32x4_t vrndmask = vorrq_u32(vcaltq_f32(vmagic_number, vx), vsign_mask);
43*4bdc9457SAndroid Build Coastguard Worker 
44*4bdc9457SAndroid Build Coastguard Worker     // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x).
45*4bdc9457SAndroid Build Coastguard Worker     // Note: the result is valid only for 0 <= abs(x) < 2**23.
46*4bdc9457SAndroid Build Coastguard Worker     // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs.
47*4bdc9457SAndroid Build Coastguard Worker     const float32x4_t vrndabsx = vsubq_f32(vaddq_f32(vabsx, vmagic_number), vmagic_number);
48*4bdc9457SAndroid Build Coastguard Worker 
49*4bdc9457SAndroid Build Coastguard Worker     // Combine abs(x) rounded via addition-subtraction trick and the input x value.
50*4bdc9457SAndroid Build Coastguard Worker     // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x.
51*4bdc9457SAndroid Build Coastguard Worker     // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction.
52*4bdc9457SAndroid Build Coastguard Worker     // For abs(x) >= 2**23, the result is x itself.
53*4bdc9457SAndroid Build Coastguard Worker     const float32x4_t vrndx = vbslq_f32(vrndmask, vx, vrndabsx);
54*4bdc9457SAndroid Build Coastguard Worker 
55*4bdc9457SAndroid Build Coastguard Worker     // Compute bitmask for the bits to copy from the adjusted rounded x. Other bits will be copied from rounded x.
56*4bdc9457SAndroid Build Coastguard Worker     // If rounded x < x, we want all but the sign bit from the adjusted rounded x and the sign bit from rounded x (same
57*4bdc9457SAndroid Build Coastguard Worker     // as the sign bit of x).
58*4bdc9457SAndroid Build Coastguard Worker     // If rounded x >= x or rounded x is NaN (implies x is NaN), we want all bits from rounded x.
59*4bdc9457SAndroid Build Coastguard Worker     const uint32x4_t vadjmask = vbicq_u32(vcltq_f32(vrndx, vx), vsign_mask);
60*4bdc9457SAndroid Build Coastguard Worker     // Adjust the rounded x value.
61*4bdc9457SAndroid Build Coastguard Worker     // The adjusted value is a unit above the rounded-to-nearest-even x value, but is used only if the rounded value is
62*4bdc9457SAndroid Build Coastguard Worker     // below x. In these cases, the adjusted value is x rounded up.
63*4bdc9457SAndroid Build Coastguard Worker     const float32x4_t vadjrndx = vaddq_f32(vrndx, vone);
64*4bdc9457SAndroid Build Coastguard Worker 
65*4bdc9457SAndroid Build Coastguard Worker     // Combine the adjusted rounded x and the original rounded to nearest-even x.
66*4bdc9457SAndroid Build Coastguard Worker     // For rounded x < x, the result is the absolute value of adjusted rounded-to-nearest-even x with the sign of
67*4bdc9457SAndroid Build Coastguard Worker     // rounded-to-nearest-even x (same as sign of x). Propagating the sign of x is important to produce negative zero
68*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.
69*4bdc9457SAndroid Build Coastguard Worker     // For rounded x >= x, the result is the rounded-to-nearest-even x.
70*4bdc9457SAndroid Build Coastguard Worker     // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of addition-subtraction).
71*4bdc9457SAndroid Build Coastguard Worker     const float32x4_t vy = vbslq_f32(vadjmask, vadjrndx, vrndx);
72*4bdc9457SAndroid Build Coastguard Worker 
73*4bdc9457SAndroid Build Coastguard Worker     vst1q_f32(output, vy); output += 4;
74*4bdc9457SAndroid Build Coastguard Worker   }
75*4bdc9457SAndroid Build Coastguard Worker }
76