blob: 530fea93d1c38b9e39c7416a40db760114c2459c [file] [log] [blame]
Marat Dukhan8853b822020-05-07 12:19:01 -07001// 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
Marat Dukhan075088a2020-05-12 19:42:12 -070014void xnn_math_f32_roundne__sse2_cvt(
Marat Dukhan8853b822020-05-07 12:19:01 -070015 size_t n,
16 const float* input,
17 float* output)
18{
19 assert(n % (4 * sizeof(float)) == 0);
20
Marat Dukhan03723f52020-05-13 00:59:24 -070021 // This magic number serves two purposes:
22 // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask.
Marat Dukhan8853b822020-05-07 12:19:01 -070023 // 2. Check if the input to CVTPS2DQ (_mm_cvtps_epi32) is out-of-range, which results in 0x80000000 output.
Marat Dukhan03723f52020-05-13 00:59:24 -070024 const __m128i vmagic = _mm_set1_epi32(0x80000000);
Marat Dukhan8853b822020-05-07 12:19:01 -070025
26 for (; n != 0; n -= 4 * sizeof(float)) {
27 const __m128 vx = _mm_load_ps(input);
28 input += 4;
29
Marat Dukhan8853b822020-05-07 12:19:01 -070030 // Convert floating-point value x to integer, with default rounding (to nearest-even).
31 // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000).
32 const __m128i vintx = _mm_cvtps_epi32(vx);
33
Marat Dukhan03723f52020-05-13 00:59:24 -070034 // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x.
35 // If x is out-of-range for CVTPS2DQ, we want all bits from x.
36 // If x is in-range for CVTPS2DQ, we want all but the sign bit from the rounded x and the sign bit from x.
37 const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic)));
Marat Dukhan8853b822020-05-07 12:19:01 -070038
39 // Convert integer back to floating-point.
40 // We binary OR the result with the sign of x to restore the sign of negative zero.
Marat Dukhan03723f52020-05-13 00:59:24 -070041 const __m128 vrndx = _mm_cvtepi32_ps(vintx);
Marat Dukhan8853b822020-05-07 12:19:01 -070042
43 // Combine x rounded via conversion to integer and the initial x value.
44 // For -2**31 < x < 2**31, the result is x rounded via conversion to integer.
45 // Otherwise (including NaN inputs), the result is x itself.
46 const __m128 vy = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vrndx));
47
48 _mm_store_ps(output, vy);
49 output += 4;
50 }
51}