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> |
| 8 | |
| 9 | #include <emmintrin.h> |
| 10 | |
| 11 | #include <xnnpack/math-stubs.h> |
| 12 | |
| 13 | |
| 14 | void xnn_math_f32_roundne__sse2( |
| 15 | size_t n, |
| 16 | const float* input, |
| 17 | float* output) |
| 18 | { |
| 19 | assert(n % (4 * sizeof(float)) == 0); |
| 20 | |
| 21 | // This magic number with a bit representation 0x80000000 serves two purposes: |
| 22 | // 1. Extract the sign of a floating-point number. |
| 23 | // 2. Check if the input to CVTPS2DQ (_mm_cvtps_epi32) is out-of-range, which results in 0x80000000 output. |
| 24 | const __m128 vmagic = _mm_set1_ps(-0.0f); |
| 25 | |
| 26 | for (; n != 0; n -= 4 * sizeof(float)) { |
| 27 | const __m128 vx = _mm_load_ps(input); |
| 28 | input += 4; |
| 29 | |
| 30 | // Extract the sign of the input. |
| 31 | // We need the sign to preserve negative zero value, which would otherwise get lost in FP->INT->FP conversion. |
| 32 | const __m128 vsignx = _mm_and_ps(vx, vmagic); |
| 33 | // Convert floating-point value x to integer, with default rounding (to nearest-even). |
| 34 | // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000). |
| 35 | const __m128i vintx = _mm_cvtps_epi32(vx); |
| 36 | |
| 37 | // Compute bitmask for out-of-range conversion input. |
| 38 | // The bitmask is set to all ones when x is out-of-range for CVTPS2DQ, and also when x == -2**31. The latter case |
| 39 | // is ok, because this x is already an integer, and can be passed to output as is. |
| 40 | const __m128 vrndmask = _mm_castsi128_ps(_mm_cmpeq_epi32(vintx, _mm_castps_si128(vmagic))); |
| 41 | |
| 42 | // Convert integer back to floating-point. |
| 43 | // We binary OR the result with the sign of x to restore the sign of negative zero. |
| 44 | const __m128 vrndx = _mm_or_ps(_mm_cvtepi32_ps(vintx), vsignx); |
| 45 | |
| 46 | // Combine x rounded via conversion to integer and the initial x value. |
| 47 | // For -2**31 < x < 2**31, the result is x rounded via conversion to integer. |
| 48 | // Otherwise (including NaN inputs), the result is x itself. |
| 49 | const __m128 vy = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vrndx)); |
| 50 | |
| 51 | _mm_store_ps(output, vy); |
| 52 | output += 4; |
| 53 | } |
| 54 | } |