• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
xnn_math_f32_roundd__sse2_cvt(size_t n,const float * input,float * output)14 void xnn_math_f32_roundd__sse2_cvt(
15     size_t n,
16     const float* input,
17     float* output)
18 {
19   assert(n % (4 * sizeof(float)) == 0);
20 
21   // This magic number serves two purposes:
22   // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask.
23   // 2. Check if the input to CVTTPS2DQ (_mm_cvttps_epi32) is out-of-range, which results in 0x80000000 output.
24   const __m128i vmagic = _mm_set1_epi32(0x80000000);
25   // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-towards-zero operation.
26   const __m128 vone = _mm_set1_ps(1.0f);
27 
28   for (; n != 0; n -= 4 * sizeof(float)) {
29     const __m128 vx = _mm_load_ps(input);
30     input += 4;
31 
32     // Convert floating-point value x to integer, with rounding towards zero.
33     // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000).
34     const __m128i vintx = _mm_cvttps_epi32(vx);
35 
36     // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x.
37     // If x is out-of-range for CVTTPS2DQ, we want all bits from x.
38     // If x is in-range for CVTTPS2DQ, we want all but the sign bit from the rounded x and the sign bit from x.
39     const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
40 
41     // Convert integer back to floating-point.
42     // We binary OR the result with the sign of x to restore the sign of negative zero.
43     const __m128 vprerndx = _mm_cvtepi32_ps(vintx);
44 
45     // Combine x rounded via conversion to integer and the initial x value.
46     // For -2**31 < x < 2**31, the result is x rounded via conversion to integer.
47     // Otherwise (including NaN inputs), the result is x itself.
48     const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx));
49 
50     // Adjust x rounded towards zero to get x rounded down.
51     // Note: subtraction implicitly converts SNaN inputs to QNaNs.
52     const __m128 vy = _mm_sub_ps(vrndx, _mm_and_ps(_mm_cmpgt_ps(vrndx, vx), vone));
53 
54     _mm_store_ps(output, vy);
55     output += 4;
56   }
57 }
58