[libc-commits] [libc] [libc][stdfix] Add round functions for fixed point types. (PR #81994)
via libc-commits
libc-commits at lists.llvm.org
Fri Feb 16 06:45:32 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: None (lntue)
<details>
<summary>Changes</summary>
---
Patch is 41.20 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/81994.diff
44 Files Affected:
- (modified) libc/config/linux/x86_64/entrypoints.txt (+18-7)
- (modified) libc/docs/math/stdfix.rst (+1-1)
- (modified) libc/spec/stdc_ext.td (+16)
- (modified) libc/src/__support/fixed_point/CMakeLists.txt (+1)
- (modified) libc/src/__support/fixed_point/fx_bits.h (+53)
- (modified) libc/src/stdfix/CMakeLists.txt (+15)
- (added) libc/src/stdfix/roundhk.cpp (+19)
- (added) libc/src/stdfix/roundhk.h (+20)
- (added) libc/src/stdfix/roundhr.cpp (+19)
- (added) libc/src/stdfix/roundhr.h (+20)
- (added) libc/src/stdfix/roundk.cpp (+19)
- (added) libc/src/stdfix/roundk.h (+20)
- (added) libc/src/stdfix/roundlk.cpp (+19)
- (added) libc/src/stdfix/roundlk.h (+20)
- (added) libc/src/stdfix/roundlr.cpp (+19)
- (added) libc/src/stdfix/roundlr.h (+20)
- (added) libc/src/stdfix/roundr.cpp (+19)
- (added) libc/src/stdfix/roundr.h (+20)
- (added) libc/src/stdfix/rounduhk.cpp (+20)
- (added) libc/src/stdfix/rounduhk.h (+20)
- (added) libc/src/stdfix/rounduhr.cpp (+20)
- (added) libc/src/stdfix/rounduhr.h (+20)
- (added) libc/src/stdfix/rounduk.cpp (+19)
- (added) libc/src/stdfix/rounduk.h (+20)
- (added) libc/src/stdfix/roundulk.cpp (+20)
- (added) libc/src/stdfix/roundulk.h (+20)
- (added) libc/src/stdfix/roundulr.cpp (+20)
- (added) libc/src/stdfix/roundulr.h (+20)
- (added) libc/src/stdfix/roundur.cpp (+19)
- (added) libc/src/stdfix/roundur.h (+20)
- (modified) libc/test/src/stdfix/CMakeLists.txt (+18)
- (added) libc/test/src/stdfix/RoundTest.h (+65)
- (added) libc/test/src/stdfix/roundhk_test.cpp (+13)
- (added) libc/test/src/stdfix/roundhr_test.cpp (+13)
- (added) libc/test/src/stdfix/roundk_test.cpp (+13)
- (added) libc/test/src/stdfix/roundlk_test.cpp (+13)
- (added) libc/test/src/stdfix/roundlr_test.cpp (+13)
- (added) libc/test/src/stdfix/roundr_test.cpp (+13)
- (added) libc/test/src/stdfix/rounduhk_test.cpp (+13)
- (added) libc/test/src/stdfix/rounduhr_test.cpp (+13)
- (added) libc/test/src/stdfix/rounduk_test.cpp (+13)
- (added) libc/test/src/stdfix/roundulk_test.cpp (+13)
- (added) libc/test/src/stdfix/roundulr_test.cpp (+13)
- (added) libc/test/src/stdfix/roundur_test.cpp (+13)
``````````diff
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 802c42b967cfaf..beb7a5ed448ded 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -445,13 +445,24 @@ endif()
if(LIBC_COMPILER_HAS_FIXED_POINT)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# stdfix.h _Fract and _Accum entrypoints
- # Re-enable the entry points once #80757 is merged.
- # libc.src.stdfix.abshk
- # libc.src.stdfix.abshr
- # libc.src.stdfix.absk
- # libc.src.stdfix.absr
- # libc.src.stdfix.abslk
- # libc.src.stdfix.abslr
+ libc.src.stdfix.abshk
+ libc.src.stdfix.abshr
+ libc.src.stdfix.absk
+ libc.src.stdfix.absr
+ libc.src.stdfix.abslk
+ libc.src.stdfix.abslr
+ libc.src.stdfix.roundhk
+ libc.src.stdfix.roundhr
+ libc.src.stdfix.roundk
+ libc.src.stdfix.roundr
+ libc.src.stdfix.roundlk
+ libc.src.stdfix.roundlr
+ libc.src.stdfix.rounduhk
+ libc.src.stdfix.rounduhr
+ libc.src.stdfix.rounduk
+ libc.src.stdfix.roundur
+ libc.src.stdfix.roundulk
+ libc.src.stdfix.roundulr
)
endif()
diff --git a/libc/docs/math/stdfix.rst b/libc/docs/math/stdfix.rst
index d05e3fdeabf1c5..080066e53bd2f1 100644
--- a/libc/docs/math/stdfix.rst
+++ b/libc/docs/math/stdfix.rst
@@ -76,7 +76,7 @@ Fixed-point Arithmetics
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| rdivi | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
-| round | | | | | | | | | | | | |
+| round | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| sqrt | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
diff --git a/libc/spec/stdc_ext.td b/libc/spec/stdc_ext.td
index dd9f06d1a8cba4..6620142146c471 100644
--- a/libc/spec/stdc_ext.td
+++ b/libc/spec/stdc_ext.td
@@ -31,6 +31,22 @@ def StdcExt : StandardSpec<"stdc_ext"> {
GuardedFunctionSpec<"abshk", RetValSpec<ShortAccumType>, [ArgSpec<ShortAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"absk", RetValSpec<AccumType>, [ArgSpec<AccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"abslk", RetValSpec<LongAccumType>, [ArgSpec<LongAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+
+ GuardedFunctionSpec<"roundhr", RetValSpec<ShortFractType>, [ArgSpec<ShortFractType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundr", RetValSpec<FractType>, [ArgSpec<FractType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundlr", RetValSpec<LongFractType>, [ArgSpec<LongFractType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+
+ GuardedFunctionSpec<"roundhk", RetValSpec<ShortAccumType>, [ArgSpec<ShortAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundk", RetValSpec<AccumType>, [ArgSpec<AccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundlk", RetValSpec<LongAccumType>, [ArgSpec<LongAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+
+ GuardedFunctionSpec<"rounduhr", RetValSpec<UnsignedShortFractType>, [ArgSpec<UnsignedShortFractType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundur", RetValSpec<UnsignedFractType>, [ArgSpec<UnsignedFractType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundulr", RetValSpec<UnsignedLongFractType>, [ArgSpec<UnsignedLongFractType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+
+ GuardedFunctionSpec<"rounduhk", RetValSpec<UnsignedShortAccumType>, [ArgSpec<UnsignedShortAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"rounduk", RetValSpec<UnsignedAccumType>, [ArgSpec<UnsignedAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+ GuardedFunctionSpec<"roundulk", RetValSpec<UnsignedLongAccumType>, [ArgSpec<UnsignedLongAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
]
>;
diff --git a/libc/src/__support/fixed_point/CMakeLists.txt b/libc/src/__support/fixed_point/CMakeLists.txt
index 4dac9b33325adb..c6bb9e17adfa8e 100644
--- a/libc/src/__support/fixed_point/CMakeLists.txt
+++ b/libc/src/__support/fixed_point/CMakeLists.txt
@@ -17,4 +17,5 @@ add_header_library(
libc.include.llvm-libc-macros.stdfix_macros
libc.src.__support.macros.attributes
libc.src.__support.macros.optimization
+ libc.src.__support.CPP.bit
)
diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index ad3a6fc97c0a1f..626cd689cdfd0f 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -10,6 +10,8 @@
#define LLVM_LIBC_SRC___SUPPORT_FIXEDPOINT_FXBITS_H
#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
@@ -19,6 +21,36 @@
namespace LIBC_NAMESPACE::fixed_point {
+// Bit-wise operations are not available for fixed point types yet.
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
+bit_and(T x, T y) {
+ using BitType = typename FXRep<T>::StorageType;
+ static_assert(sizeof(BitType) * CHAR_BIT == sizeof(T) * CHAR_BIT);
+ BitType x_bit = cpp::bit_cast<BitType>(x);
+ BitType y_bit = cpp::bit_cast<BitType>(y);
+ return cpp::bit_cast<T, BitType>(x_bit & y_bit);
+}
+
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
+bit_or(T x, T y) {
+ using BitType = typename FXRep<T>::StorageType;
+ static_assert(sizeof(BitType) * CHAR_BIT == FXRep<T>::TOTAL_LEN);
+ BitType x_bit = cpp::bit_cast<BitType>(x);
+ BitType y_bit = cpp::bit_cast<BitType>(y);
+ return cpp::bit_cast<T, BitType>(x_bit | y_bit);
+}
+
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
+bit_not(T x) {
+ using BitType = typename FXRep<T>::StorageType;
+ static_assert(sizeof(BitType) * CHAR_BIT == sizeof(T) * CHAR_BIT);
+ BitType x_bit = cpp::bit_cast<BitType>(x);
+ return cpp::bit_cast<T, BitType>(~x_bit);
+}
+
template <typename T> LIBC_INLINE constexpr T abs(T x) {
using FXRep = FXRep<T>;
if constexpr (FXRep::SIGN_LEN == 0)
@@ -30,6 +62,27 @@ template <typename T> LIBC_INLINE constexpr T abs(T x) {
}
}
+// Round-to-nearest, tie-to-(+Inf)
+template <typename T> LIBC_INLINE constexpr T round(T x, int n) {
+ using FXRep = FXRep<T>;
+ if (LIBC_UNLIKELY(n < 0))
+ n = 0;
+ if (LIBC_UNLIKELY(n >= FXRep::FRACTION_LEN))
+ return x;
+
+ T round_bit = FXRep::EPS() << (FXRep::FRACTION_LEN - n - 1);
+ // Check for overflow.
+ if (LIBC_UNLIKELY(FXRep::MAX() - round_bit < x))
+ return FXRep::MAX();
+
+ T all_ones = bit_not(FXRep::ZERO());
+
+ int shift = FXRep::FRACTION_LEN - n;
+ T rounding_mask =
+ (shift == FXRep::TOTAL_LEN) ? FXRep::ZERO() : (all_ones << shift);
+ return bit_and((x + round_bit), rounding_mask);
+}
+
} // namespace LIBC_NAMESPACE::fixed_point
#endif // LIBC_COMPILER_HAS_FIXED_POINT
diff --git a/libc/src/stdfix/CMakeLists.txt b/libc/src/stdfix/CMakeLists.txt
index 1eaf64bb82a21b..6e2ed1bdfeafe7 100644
--- a/libc/src/stdfix/CMakeLists.txt
+++ b/libc/src/stdfix/CMakeLists.txt
@@ -16,3 +16,18 @@ foreach(suffix IN ITEMS hr r lr hk k lk)
libc.src.__support.fixed_point.fx_bits
)
endforeach()
+
+foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
+ add_entrypoint_object(
+ round${suffix}
+ HDRS
+ round${suffix}.h
+ SRCS
+ round${suffix}.cpp
+ COMPILE_OPTIONS
+ -O3
+ -ffixed-point
+ DEPENDS
+ libc.src.__support.fixed_point.fx_bits
+ )
+endforeach()
diff --git a/libc/src/stdfix/roundhk.cpp b/libc/src/stdfix/roundhk.cpp
new file mode 100644
index 00000000000000..a4f459ea0d6538
--- /dev/null
+++ b/libc/src/stdfix/roundhk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundhk function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "roundhk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(short accum, roundhk, (short accum x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/roundhk.h b/libc/src/stdfix/roundhk.h
new file mode 100644
index 00000000000000..9a5c874cc030db
--- /dev/null
+++ b/libc/src/stdfix/roundhk.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundhk -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_ROUNDHK_H
+#define LLVM_LIBC_SRC_STDFIX_ROUNDHK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE {
+
+short accum roundhk(short accum x, int n);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDFIX_ROUNDHK_H
diff --git a/libc/src/stdfix/roundhr.cpp b/libc/src/stdfix/roundhr.cpp
new file mode 100644
index 00000000000000..7757d1cc735f4c
--- /dev/null
+++ b/libc/src/stdfix/roundhr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundhr function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "roundhr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(short fract, roundhr, (short fract x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/roundhr.h b/libc/src/stdfix/roundhr.h
new file mode 100644
index 00000000000000..ba5a67945d6c3b
--- /dev/null
+++ b/libc/src/stdfix/roundhr.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundhr -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_ROUNDHR_H
+#define LLVM_LIBC_SRC_STDFIX_ROUNDHR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE {
+
+short fract roundhr(short fract x, int n);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDFIX_ROUNDHR_H
diff --git a/libc/src/stdfix/roundk.cpp b/libc/src/stdfix/roundk.cpp
new file mode 100644
index 00000000000000..bf47dd9898d8f1
--- /dev/null
+++ b/libc/src/stdfix/roundk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundk function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "roundk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(accum, roundk, (accum x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/roundk.h b/libc/src/stdfix/roundk.h
new file mode 100644
index 00000000000000..e9fa6d8f9c3b8c
--- /dev/null
+++ b/libc/src/stdfix/roundk.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundk ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_ROUNDK_H
+#define LLVM_LIBC_SRC_STDFIX_ROUNDK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE {
+
+accum roundk(accum x, int n);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDFIX_ROUNDK_H
diff --git a/libc/src/stdfix/roundlk.cpp b/libc/src/stdfix/roundlk.cpp
new file mode 100644
index 00000000000000..d2ffe8ab037852
--- /dev/null
+++ b/libc/src/stdfix/roundlk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundlk function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "roundlk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long accum, roundlk, (long accum x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/roundlk.h b/libc/src/stdfix/roundlk.h
new file mode 100644
index 00000000000000..5fa0e90e855a64
--- /dev/null
+++ b/libc/src/stdfix/roundlk.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundlk -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_ROUNDLK_H
+#define LLVM_LIBC_SRC_STDFIX_ROUNDLK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE {
+
+long accum roundlk(long accum x, int n);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDFIX_ROUNDLK_H
diff --git a/libc/src/stdfix/roundlr.cpp b/libc/src/stdfix/roundlr.cpp
new file mode 100644
index 00000000000000..cd4c911ffe68eb
--- /dev/null
+++ b/libc/src/stdfix/roundlr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundlr function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "roundlr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long fract, roundlr, (long fract x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/roundlr.h b/libc/src/stdfix/roundlr.h
new file mode 100644
index 00000000000000..c015292e8f3f28
--- /dev/null
+++ b/libc/src/stdfix/roundlr.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundlr -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_ROUNDLR_H
+#define LLVM_LIBC_SRC_STDFIX_ROUNDLR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE {
+
+long fract roundlr(long fract x, int n);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDFIX_ROUNDLR_H
diff --git a/libc/src/stdfix/roundr.cpp b/libc/src/stdfix/roundr.cpp
new file mode 100644
index 00000000000000..24216936d5f9bc
--- /dev/null
+++ b/libc/src/stdfix/roundr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of roundr function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "roundr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(fract, roundr, (fract x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/roundr.h b/libc/src/stdfix/roundr.h
new file mode 100644
index 00000000000000..b5b1375c882e03
--- /dev/null
+++ b/libc/src/stdfix/roundr.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for roundr ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_ROUNDR_H
+#define LLVM_LIBC_SRC_STDFIX_ROUNDR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE {
+
+fract roundr(fract x, int n);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDFIX_ROUNDR_H
diff --git a/libc/src/stdfix/rounduhk.cpp b/libc/src/stdfix/rounduhk.cpp
new file mode 100644
index 00000000000000..22561588e03361
--- /dev/null
+++ b/libc/src/stdfix/rounduhk.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of rounduhk function -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "rounduhk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned short accum, rounduhk,
+ (unsigned short accum x, int n)) {
+ return fixed_point::round(x, n);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdfix/rounduhk.h b/libc/src/stdfix/rounduhk.h
new file mode 100644
index 00000000000000..85ebf2903ec7e9
--- /dev/null
+++ b/libc/src/stdfix/rounduhk.h
@@ -0,0 +1,20 @@
+//===-- Impl...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/81994
More information about the libc-commits
mailing list