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
xnn_math_f32_roundz__sse_addsub(size_t n,const float * input,float * output)15 void xnn_math_f32_roundz__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 decrement absolute values rounded "wrong way" (i.e. away from zero) in the round-to-nearest-even
30 // operation.
31 const __m128 vone = _mm_set1_ps(1.0f);
32
33 for (; n != 0; n -= 4 * sizeof(float)) {
34 const __m128 vx = _mm_load_ps(input);
35 input += 4;
36
37 // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in
38 // the end. This method works for round-towards-zero because it is an odd function.
39 const __m128 vabsx = _mm_and_ps(vx, vnonsign_mask);
40
41 // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x.
42 // If abs(x) >= 2**23, we want all bits from x.
43 // 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.
44 const __m128 vrndmask = _mm_andnot_ps(_mm_cmpge_ps(vabsx, vmagic_number), vnonsign_mask);
45 // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x).
46 // Note: the result is valid only for 0 <= abs(x) < 2**23.
47 // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs.
48 const __m128 vrndabsx = _mm_sub_ps(_mm_add_ps(vabsx, vmagic_number), vmagic_number);
49
50 // Compute adjustment to be subtracted from the rounded-to-nearest-even abs(x) value.
51 // Adjustment is one if the rounded value is greater than the abs(x) value and zero otherwise (including NaN input).
52 const __m128 vadjustment = _mm_and_ps(vone, _mm_cmpgt_ps(vrndabsx, vabsx));
53 // Adjust abs(x) rounded to nearest-even via the addition-subtraction trick to get abs(x) rounded down.
54 // Note: subtraction implicitly converts SNaN inputs to QNaNs.
55 const __m128 vflrabsx = _mm_sub_ps(vrndabsx, vadjustment);
56
57 // Combine abs(x) rounded down via addition-subtraction trick with adjustment and the input x value.
58 // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x.
59 // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction and adjustment.
60 // For abs(x) >= 2**23, the result is x itself.
61 const __m128 vy = _mm_or_ps(_mm_and_ps(vflrabsx, vrndmask), _mm_andnot_ps(vrndmask, vx));
62
63 _mm_store_ps(output, vy);
64 output += 4;
65 }
66 }
67