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