[libc-commits] [libc] [libc][stdfix] Implement fixed point fxdivi functions in llvm-libc (PR #210020)
via libc-commits
libc-commits at lists.llvm.org
Tue Jul 28 20:52:44 PDT 2026
================
@@ -0,0 +1,189 @@
+//===-- Utility class to test fxdivi functions ------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/Test.h"
+
+#include "hdr/signal_macros.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/fixed_point/fx_rep.h"
+
+namespace cpp = LIBC_NAMESPACE::cpp;
+
+template <typename FXType, typename IntType>
+class FxDiviTest : public LIBC_NAMESPACE::testing::Test {
+ using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<FXType>;
+
+ static constexpr FXType fx_max = FXRep::MAX();
+ static constexpr FXType fx_min = FXRep::MIN();
+ static constexpr FXType fx_zero = FXRep::ZERO();
+ static constexpr FXType epsilon = FXRep::EPS();
+ static constexpr FXType one_half = FXRep::ONE_HALF();
+ static constexpr FXType one_fourth = FXRep::ONE_FOURTH();
+ static constexpr FXType one_eighth = FXRep::ONE_EIGHTH();
+
+ static constexpr bool is_signed = (FXRep::SIGN_LEN > 0);
+ static constexpr bool has_integral = (FXRep::INTEGRAL_LEN > 0);
+ static constexpr int F = FXRep::FRACTION_LEN;
+
+ static constexpr auto abs_diff = [](FXType a, FXType b) {
+ return (a > b) ? (a - b) : (b - a);
+ };
+
+public:
+ typedef FXType (*FxDiviFunc)(IntType, IntType);
+
+ void testBasicNumbers(FxDiviFunc func) {
+ EXPECT_TRUE(abs_diff(func(1, 3), static_cast<FXType>(
+ 0.33333333333333333333)) <= epsilon);
----------------
sohail103 wrote:
I was thinking of adding something like this
```cpp
template <typename FXType, typename IntType>
static FXType expected_quotient(IntType n, IntType d) {
using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<FXType>;
using StorageType = typename FXRep::StorageType;
constexpr int F = FXRep::FRACTION_LEN;
using WideType = cpp::conditional_t<cpp::is_signed_v<IntType>, LIBC_NAMESPACE::Int128, LIBC_NAMESPACE::UInt128>;
WideType scaled_n = static_cast<WideType>(n) << F;
WideType quotient = scaled_n / static_cast<WideType>(d);
return LIBC_NAMESPACE::fixed_point::FXBits<FXType>(static_cast<StorageType>(quotient)).get_val();
}
```
and then
```cpp
EXPECT_TRUE(abs_diff(func(1, 3), expected_quotient<FXType, IntType>(1, 3)) <= epsilon);
```
https://github.com/llvm/llvm-project/pull/210020
More information about the libc-commits
mailing list