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/config.h"
13 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
14 #include "src/__support/macros/properties/architectures.h"
15 #include "src/__support/macros/properties/cpu_features.h"
17 #if (defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE4_2))
18 #include "x86_64/nearest_integer.h"
19 #elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
20 #include "aarch64/nearest_integer.h"
21 #elif defined(LIBC_TARGET_ARCH_IS_GPU)
23 namespace LIBC_NAMESPACE_DECL
{
26 LIBC_INLINE
float nearest_integer(float x
) { return __builtin_rintf(x
); }
28 LIBC_INLINE
double nearest_integer(double x
) { return __builtin_rint(x
); }
31 } // namespace LIBC_NAMESPACE_DECL
35 namespace LIBC_NAMESPACE_DECL
{
38 // This is a fast implementation for rounding to a nearest integer that.
40 // Notice that for AARCH64 and x86-64 with SSE4.2 support, we will use their
41 // corresponding rounding instruction instead. And in those cases, the results
42 // are rounded to the nearest integer, tie-to-even.
43 LIBC_INLINE
float nearest_integer(float x
) {
44 if (x
< 0x1p
24f
&& x
> -0x1p
24f
) {
45 float r
= x
< 0 ? (x
- 0x1.0p23f
) + 0x1.0p23f
: (x
+ 0x1.0p23f
) - 0x1.0p23f
;
47 // The expression above is correct for the default rounding mode, round-to-
48 // nearest, tie-to-even. For other rounding modes, it might be off by 1,
49 // which is corrected below.
50 if (LIBC_UNLIKELY(diff
> 0.5f
))
52 if (LIBC_UNLIKELY(diff
< -0.5f
))
59 LIBC_INLINE
double nearest_integer(double x
) {
60 if (x
< 0x1p
53 && x
> -0x1p
53) {
61 double r
= x
< 0 ? (x
- 0x1.0p52
) + 0x1.0p52
: (x
+ 0x1.0p52
) - 0x1.0p52
;
63 // The expression above is correct for the default rounding mode, round-to-
64 // nearest, tie-to-even. For other rounding modes, it might be off by 1,
65 // which is corrected below.
66 if (LIBC_UNLIKELY(diff
> 0.5))
68 if (LIBC_UNLIKELY(diff
< -0.5))
76 } // namespace LIBC_NAMESPACE_DECL
79 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_NEAREST_INTEGER_H