xref: /aosp_15_r20/external/XNNPACK/src/math/roundu-sse2-cvt.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 
9*4bdc9457SAndroid Build Coastguard Worker #include <emmintrin.h>
10*4bdc9457SAndroid Build Coastguard Worker 
11*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/math-stubs.h>
12*4bdc9457SAndroid Build Coastguard Worker 
13*4bdc9457SAndroid Build Coastguard Worker 
xnn_math_f32_roundu__sse2_cvt(size_t n,const float * input,float * output)14*4bdc9457SAndroid Build Coastguard Worker void xnn_math_f32_roundu__sse2_cvt(
15*4bdc9457SAndroid Build Coastguard Worker     size_t n,
16*4bdc9457SAndroid Build Coastguard Worker     const float* input,
17*4bdc9457SAndroid Build Coastguard Worker     float* output)
18*4bdc9457SAndroid Build Coastguard Worker {
19*4bdc9457SAndroid Build Coastguard Worker   assert(n % (4 * sizeof(float)) == 0);
20*4bdc9457SAndroid Build Coastguard Worker 
21*4bdc9457SAndroid Build Coastguard Worker   // This magic number serves two purposes:
22*4bdc9457SAndroid Build Coastguard Worker   // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask.
23*4bdc9457SAndroid Build Coastguard Worker   // 2. Check if the input to CVTTPS2DQ (_mm_cvttps_epi32) is out-of-range, which results in 0x80000000 output.
24*4bdc9457SAndroid Build Coastguard Worker   const __m128i vmagic = _mm_set1_epi32(0x80000000);
25*4bdc9457SAndroid Build Coastguard Worker   // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation.
26*4bdc9457SAndroid Build Coastguard Worker   const __m128 vone = _mm_set1_ps(1.0f);
27*4bdc9457SAndroid Build Coastguard Worker 
28*4bdc9457SAndroid Build Coastguard Worker   for (; n != 0; n -= 4 * sizeof(float)) {
29*4bdc9457SAndroid Build Coastguard Worker     const __m128 vx = _mm_load_ps(input);
30*4bdc9457SAndroid Build Coastguard Worker     input += 4;
31*4bdc9457SAndroid Build Coastguard Worker 
32*4bdc9457SAndroid Build Coastguard Worker     // Convert floating-point value x to integer, with rounding towards zero.
33*4bdc9457SAndroid Build Coastguard Worker     // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000).
34*4bdc9457SAndroid Build Coastguard Worker     const __m128i vintx = _mm_cvttps_epi32(vx);
35*4bdc9457SAndroid Build Coastguard Worker 
36*4bdc9457SAndroid Build Coastguard Worker     // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x.
37*4bdc9457SAndroid Build Coastguard Worker     // If x is out-of-range for CVTTPS2DQ, we want all bits from x.
38*4bdc9457SAndroid Build Coastguard Worker     // 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*4bdc9457SAndroid Build Coastguard Worker     const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
40*4bdc9457SAndroid Build Coastguard Worker 
41*4bdc9457SAndroid Build Coastguard Worker     // Convert integer back to floating-point.
42*4bdc9457SAndroid Build Coastguard Worker     // We binary OR the result with the sign of x to restore the sign of negative zero.
43*4bdc9457SAndroid Build Coastguard Worker     const __m128 vprerndx = _mm_cvtepi32_ps(vintx);
44*4bdc9457SAndroid Build Coastguard Worker 
45*4bdc9457SAndroid Build Coastguard Worker     // Combine x rounded via conversion to integer and the initial x value.
46*4bdc9457SAndroid Build Coastguard Worker     // For -2**31 < x < 2**31, the result is x rounded via conversion to integer.
47*4bdc9457SAndroid Build Coastguard Worker     // Otherwise (including NaN inputs), the result is x itself.
48*4bdc9457SAndroid Build Coastguard Worker     const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx));
49*4bdc9457SAndroid Build Coastguard Worker 
50*4bdc9457SAndroid Build Coastguard Worker     // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x.
51*4bdc9457SAndroid Build Coastguard Worker     // If rounded x >= x, we want all bits from rounded x.
52*4bdc9457SAndroid Build Coastguard Worker     // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded
53*4bdc9457SAndroid Build Coastguard Worker     // x and the sign bit from rounded x (same as the sign bit of x).
54*4bdc9457SAndroid Build Coastguard Worker     const __m128 vadjmask = _mm_or_ps(_mm_cmpge_ps(vrndx, vx), _mm_castsi128_ps(vmagic));
55*4bdc9457SAndroid Build Coastguard Worker     // Adjust the rounded x value.
56*4bdc9457SAndroid Build Coastguard Worker     // The adjusted value is a unit above the rounded-towards-zero x value, but is used only if the rounded value is
57*4bdc9457SAndroid Build Coastguard Worker     // below x. In these cases, the adjusted value is x rounded up.
58*4bdc9457SAndroid Build Coastguard Worker     // Note: addition implicitly converts SNaN inputs to QNaNs.
59*4bdc9457SAndroid Build Coastguard Worker     const __m128 vadjrndx = _mm_add_ps(vrndx, vone);
60*4bdc9457SAndroid Build Coastguard Worker 
61*4bdc9457SAndroid Build Coastguard Worker     // Combine the adjusted rounded x and the original rounded towards zero x.
62*4bdc9457SAndroid Build Coastguard Worker     // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of
63*4bdc9457SAndroid Build Coastguard Worker     // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero
64*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.
65*4bdc9457SAndroid Build Coastguard Worker     // For rounded x >= x, the result is the rounded-towards-zero x.
66*4bdc9457SAndroid Build Coastguard Worker     // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment).
67*4bdc9457SAndroid Build Coastguard Worker     const __m128 vy = _mm_or_ps(_mm_and_ps(vrndx, vadjmask), _mm_andnot_ps(vadjmask, vadjrndx));
68*4bdc9457SAndroid Build Coastguard Worker 
69*4bdc9457SAndroid Build Coastguard Worker     _mm_store_ps(output, vy);
70*4bdc9457SAndroid Build Coastguard Worker     output += 4;
71*4bdc9457SAndroid Build Coastguard Worker   }
72*4bdc9457SAndroid Build Coastguard Worker }
73