Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 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> |
Marat Dukhan | 1fd91c8 | 2020-05-13 01:15:51 -0700 | [diff] [blame^] | 8 | #include <stdint.h> |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 9 | |
| 10 | #include <arm_neon.h> |
| 11 | |
| 12 | #include <xnnpack/math-stubs.h> |
| 13 | |
| 14 | |
Marat Dukhan | 075088a | 2020-05-12 19:42:12 -0700 | [diff] [blame] | 15 | void xnn_math_f32_roundne__neon_addsub( |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 16 | size_t n, |
| 17 | const float* input, |
| 18 | float* output) |
| 19 | { |
| 20 | assert(n % (4 * sizeof(float)) == 0); |
| 21 | |
| 22 | // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic |
| 23 | // number is subtracted back from the result to get original x rounded to integer. This trick works only for |
| 24 | // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to |
| 25 | // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. |
| 26 | const float32x4_t vmagic_number = vmovq_n_f32(0x1.000000p+23f); |
Marat Dukhan | 1fd91c8 | 2020-05-13 01:15:51 -0700 | [diff] [blame^] | 27 | // Mask for the sign bit of a floating-point number. |
| 28 | const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 29 | |
| 30 | for (; n != 0; n -= 4 * sizeof(float)) { |
| 31 | const float32x4_t vx = vld1q_f32(input); input += 4; |
| 32 | |
| 33 | // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in |
| 34 | // the end. This method works for round-to-nearest-even because it is an odd function. |
| 35 | const float32x4_t vabsx = vabsq_f32(vx); |
Marat Dukhan | 1fd91c8 | 2020-05-13 01:15:51 -0700 | [diff] [blame^] | 36 | // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. |
| 37 | // If abs(x) >= 2**23, we want all bits from x. |
| 38 | // 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. |
Marat Dukhan | 8d50380 | 2020-05-11 21:36:08 -0700 | [diff] [blame] | 39 | // Note: we do vcaltq_f32(vmagic_number, vx) instead of vcltq_f32(vmagic_number, vabsx) to reduce dependency chain. |
Marat Dukhan | 1fd91c8 | 2020-05-13 01:15:51 -0700 | [diff] [blame^] | 40 | const uint32x4_t vrndmask = vorrq_u32(vcaltq_f32(vmagic_number, vx), vsign_mask); |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 41 | |
| 42 | // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). |
| 43 | // Note: the result is valid only for 0 <= abs(x) < 2**23. |
Marat Dukhan | 8d50380 | 2020-05-11 21:36:08 -0700 | [diff] [blame] | 44 | // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 45 | const float32x4_t vrndabsx = vsubq_f32(vaddq_f32(vabsx, vmagic_number), vmagic_number); |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 46 | |
| 47 | // Combine abs(x) rounded via addition-subtraction trick and the input x value. |
Marat Dukhan | 1fd91c8 | 2020-05-13 01:15:51 -0700 | [diff] [blame^] | 48 | // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. |
Marat Dukhan | 8d50380 | 2020-05-11 21:36:08 -0700 | [diff] [blame] | 49 | // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. |
| 50 | // For abs(x) >= 2**23, the result is x itself. |
Marat Dukhan | 1fd91c8 | 2020-05-13 01:15:51 -0700 | [diff] [blame^] | 51 | const float32x4_t vy = vbslq_f32(vrndmask, vx, vrndabsx); |
Marat Dukhan | 8853b82 | 2020-05-07 12:19:01 -0700 | [diff] [blame] | 52 | |
| 53 | vst1q_f32(output, vy); output += 4; |
| 54 | } |
| 55 | } |