Marat Dukhan | c9852ba | 2020-05-13 17:21:29 -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> |
| 8 | |
| 9 | #include <xmmintrin.h> |
| 10 | |
| 11 | #include <xnnpack/math.h> |
| 12 | #include <xnnpack/math-stubs.h> |
| 13 | |
| 14 | |
| 15 | void xnn_math_f32_roundu__sse_addsub( |
| 16 | size_t n, |
| 17 | const float* input, |
| 18 | float* output) |
| 19 | { |
| 20 | assert(n % (4 * sizeof(float)) == 0); |
| 21 | |
| 22 | // Mask for all bits of a floating-point number except the sign bit. |
| 23 | const __m128 vnonsign_mask = _mm_set1_ps(math_nonsign_mask_f32()); |
| 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 __m128 vmagic_number = _mm_set1_ps(0x1.000000p+23f); |
| 29 | // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-to-nearest-even operation. |
| 30 | const __m128 vone = _mm_set1_ps(1.0f); |
| 31 | |
| 32 | for (; n != 0; n -= 4 * sizeof(float)) { |
| 33 | const __m128 vx = _mm_load_ps(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 __m128 vabsx = _mm_and_ps(vx, vnonsign_mask); |
| 39 | |
| 40 | // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. |
| 41 | // If abs(x) >= 2**23, we want all bits from x. |
| 42 | // 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. |
| 43 | const __m128 vrndmask = _mm_andnot_ps(_mm_cmpge_ps(vabsx, vmagic_number), vnonsign_mask); |
| 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 __m128 vrndabsx = _mm_sub_ps(_mm_add_ps(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 __m128 vrndx = _mm_or_ps(_mm_and_ps(vrndabsx, vrndmask), _mm_andnot_ps(vrndmask, vx)); |
| 54 | |
| 55 | // Compute bitmask for the bits to copy from the adjusted rounded x. Other bits will be copied from rounded x. |
| 56 | // 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 | // as the sign bit of x). |
| 58 | // If rounded x >= x or rounded x is NaN (implies x is NaN), we want all bits from rounded x. |
| 59 | const __m128 vadjmask = _mm_and_ps(_mm_cmplt_ps(vrndx, vx), vnonsign_mask); |
| 60 | // Compute adjusted rounded x value. |
| 61 | // The adjusted value is a unit above the rounded-to-nearest-even x value, but is used only if the rounded value is |
| 62 | // below x. In this cases, the adjusted value is x rounded up. |
| 63 | const __m128 vadjrndx = _mm_add_ps(vrndx, vone); |
| 64 | |
| 65 | // Combine the adjusted rounded x and the original rounded to nearest-even x. |
| 66 | // For rounded x < x, the result is the absolute value of adjusted rounded-to-nearest-even x with the sign of |
| 67 | // rounded-to-nearest-even x (same as sign of x). Propagating the sign of x is important to produce negative zero |
| 68 | // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. |
| 69 | // For rounded x >= x, the result is the rounded-to-nearest-even x. |
| 70 | // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of addition-subtraction). |
| 71 | const __m128 vy = _mm_or_ps(_mm_and_ps(vadjrndx, vadjmask), _mm_andnot_ps(vadjmask, vrndx)); |
| 72 | |
| 73 | _mm_store_ps(output, vy); |
| 74 | output += 4; |
| 75 | } |
| 76 | } |