xref: /aosp_15_r20/external/llvm-libc/src/__support/FPUtil/nearest_integer.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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/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"
16 
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)
22 
23 namespace LIBC_NAMESPACE_DECL {
24 namespace fputil {
25 
nearest_integer(float x)26 LIBC_INLINE float nearest_integer(float x) { return __builtin_rintf(x); }
27 
nearest_integer(double x)28 LIBC_INLINE double nearest_integer(double x) { return __builtin_rint(x); }
29 
30 } // namespace fputil
31 } // namespace LIBC_NAMESPACE_DECL
32 
33 #else
34 
35 namespace LIBC_NAMESPACE_DECL {
36 namespace fputil {
37 
38 // This is a fast implementation for rounding to a nearest integer that.
39 //
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.
nearest_integer(float x)43 LIBC_INLINE float nearest_integer(float x) {
44   if (x < 0x1p24f && x > -0x1p24f) {
45     float r = x < 0 ? (x - 0x1.0p23f) + 0x1.0p23f : (x + 0x1.0p23f) - 0x1.0p23f;
46     float diff = x - r;
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))
51       return r + 1.0f;
52     if (LIBC_UNLIKELY(diff < -0.5f))
53       return r - 1.0f;
54     return r;
55   }
56   return x;
57 }
58 
nearest_integer(double x)59 LIBC_INLINE double nearest_integer(double x) {
60   if (x < 0x1p53 && x > -0x1p53) {
61     double r = x < 0 ? (x - 0x1.0p52) + 0x1.0p52 : (x + 0x1.0p52) - 0x1.0p52;
62     double diff = x - r;
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))
67       return r + 1.0;
68     if (LIBC_UNLIKELY(diff < -0.5))
69       return r - 1.0;
70     return r;
71   }
72   return x;
73 }
74 
75 } // namespace fputil
76 } // namespace LIBC_NAMESPACE_DECL
77 
78 #endif
79 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_NEAREST_INTEGER_H
80