[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / FPUtil / nearest_integer.h
blob6d5b8bfbee791a1c313f67c564e87dff545f8ae4
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 //===----------------------------------------------------------------------===//
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"
20 #else
22 namespace __llvm_libc {
23 namespace fputil {
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 < 0x1p24f && x > -0x1p24f) {
32 float r = x < 0 ? (x - 0x1.0p23f) + 0x1.0p23f : (x + 0x1.0p23f) - 0x1.0p23f;
33 float diff = x - r;
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))
38 return r + 1.0f;
39 if (LIBC_UNLIKELY(diff < -0.5f))
40 return r - 1.0f;
41 return r;
43 return x;
46 LIBC_INLINE double nearest_integer(double x) {
47 if (x < 0x1p53 && x > -0x1p53) {
48 double r = x < 0 ? (x - 0x1.0p52) + 0x1.0p52 : (x + 0x1.0p52) - 0x1.0p52;
49 double diff = x - r;
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))
54 return r + 1.0;
55 if (LIBC_UNLIKELY(diff < -0.5))
56 return r - 1.0;
57 return r;
59 return x;
62 } // namespace fputil
63 } // namespace __llvm_libc
65 #endif
66 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_NEAREST_INTEGER_H