1//===-- Fast rounding to nearest integer for floating point -----*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_NEAREST_INTEGER_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_NEAREST_INTEGER_H
11
12#include "src/__support/CPP/type_traits.h"
13#include "src/__support/macros/config.h"
14#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
15#include "src/__support/macros/properties/architectures.h"
16#include "src/__support/macros/properties/cpu_features.h"
17
18#if (defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE4_2))
19#include "x86_64/nearest_integer.h"
20#elif (defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP))
21#include "aarch64/nearest_integer.h"
22#elif defined(LIBC_TARGET_ARCH_IS_GPU)
23
24namespace LIBC_NAMESPACE_DECL {
25namespace fputil {
26
27LIBC_INLINE float nearest_integer(float x) { return __builtin_rintf(x); }
28
29LIBC_INLINE double nearest_integer(double x) { return __builtin_rint(x); }
30
31} // namespace fputil
32} // namespace LIBC_NAMESPACE_DECL
33
34#else
35
36namespace LIBC_NAMESPACE_DECL {
37namespace fputil {
38
39// This is a fast implementation for rounding to a nearest integer that.
40//
41// Notice that for AARCH64 and x86-64 with SSE4.2 support, we will use their
42// corresponding rounding instruction instead. And in those cases, the results
43// are rounded to the nearest integer, tie-to-even.
44LIBC_INLINE constexpr float nearest_integer(float x) {
45#if __has_builtin(__builtin_roundevenf) && defined(__ARM_ARCH) && \
46 (__ARM_ARCH >= 8) && defined(LIBC_TARGET_CPU_HAS_ARM_FPU_FLOAT)
47 if (!cpp::is_constant_evaluated())
48 return __builtin_roundevenf(x);
49#endif
50 if (x < 0x1p24f && x > -0x1p24f) {
51 float r = x < 0 ? (x - 0x1.0p23f) + 0x1.0p23f : (x + 0x1.0p23f) - 0x1.0p23f;
52#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
53 float diff = x - r;
54 // The expression above is correct for the default rounding mode, round-to-
55 // nearest, tie-to-even. For other rounding modes, it might be off by 1,
56 // which is corrected below.
57 if (LIBC_UNLIKELY(diff > 0.5f))
58 return r + 1.0f;
59 if (LIBC_UNLIKELY(diff < -0.5f))
60 return r - 1.0f;
61#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
62 return r;
63 }
64 return x;
65}
66
67LIBC_INLINE constexpr double nearest_integer(double x) {
68#if __has_builtin(__builtin_roundeven) && defined(__ARM_ARCH) && \
69 (__ARM_ARCH >= 8) && defined(LIBC_TARGET_CPU_HAS_ARM_FPU_DOUBLE)
70 if (!cpp::is_constant_evaluated())
71 return __builtin_roundeven(x);
72#endif
73 if (x < 0x1p53 && x > -0x1p53) {
74 double r = x < 0 ? (x - 0x1.0p52) + 0x1.0p52 : (x + 0x1.0p52) - 0x1.0p52;
75#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
76 double diff = x - r;
77 // The expression above is correct for the default rounding mode, round-to-
78 // nearest, tie-to-even. For other rounding modes, it might be off by 1,
79 // which is corrected below.
80 if (LIBC_UNLIKELY(diff > 0.5))
81 return r + 1.0;
82 if (LIBC_UNLIKELY(diff < -0.5))
83 return r - 1.0;
84#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
85 return r;
86 }
87 return x;
88}
89
90} // namespace fputil
91} // namespace LIBC_NAMESPACE_DECL
92
93#endif
94#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_NEAREST_INTEGER_H
95