1 //===-- Fast rounding to nearest integer for floating point -----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_NEAREST_INTEGER_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_NEAREST_INTEGER_H
12 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
13 #include "src/__support/macros/properties/architectures.h"
14 #include "src/__support/macros/properties/cpu_features.h"
16 #if (defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE4_2))
17 #include "x86_64/nearest_integer.h"
18 #elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
19 #include "aarch64/nearest_integer.h"
22 namespace __llvm_libc
{
25 // This is a fast implementation for rounding to a nearest integer that.
27 // Notice that for AARCH64 and x86-64 with SSE4.2 support, we will use their
28 // corresponding rounding instruction instead. And in those cases, the results
29 // are rounded to the nearest integer, tie-to-even.
30 LIBC_INLINE
float nearest_integer(float x
) {
31 if (x
< 0x1p
24f
&& x
> -0x1p
24f
) {
32 float r
= x
< 0 ? (x
- 0x1.0p23f
) + 0x1.0p23f
: (x
+ 0x1.0p23f
) - 0x1.0p23f
;
34 // The expression above is correct for the default rounding mode, round-to-
35 // nearest, tie-to-even. For other rounding modes, it might be off by 1,
36 // which is corrected below.
37 if (LIBC_UNLIKELY(diff
> 0.5f
))
39 if (LIBC_UNLIKELY(diff
< -0.5f
))
46 LIBC_INLINE
double nearest_integer(double x
) {
47 if (x
< 0x1p
53 && x
> -0x1p
53) {
48 double r
= x
< 0 ? (x
- 0x1.0p52
) + 0x1.0p52
: (x
+ 0x1.0p52
) - 0x1.0p52
;
50 // The expression above is correct for the default rounding mode, round-to-
51 // nearest, tie-to-even. For other rounding modes, it might be off by 1,
52 // which is corrected below.
53 if (LIBC_UNLIKELY(diff
> 0.5))
55 if (LIBC_UNLIKELY(diff
< -0.5))
63 } // namespace __llvm_libc
66 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_NEAREST_INTEGER_H