[libc-commits] [libc] [libc][stdfix] Implement fixed point fxdivi functions in llvm-libc (PR #210020)
via libc-commits
libc-commits at lists.llvm.org
Fri Aug 21 06:38:51 PDT 2026
================
@@ -0,0 +1,223 @@
+//===-- 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 "src/__support/CPP/type_traits/conditional.h"
+#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);
+ };
+
+ using CompType =
+ cpp::conditional_t<is_signed, long accum, unsigned long accum>;
+
+ // Here, expected() uses CompType's own division operator as a reference which
+ // has its own independent error bounds (division operator on CompType can
+ // have almost 2 ulp of error as per ISO/IEC TR 18037:2008(E),
+ // clause 4.1.6.2.1). The fxdivi implementation being tested here has an exact
+ // fast path when the denominator is a power of 2, so do not use this helper
+ // for such denominators. Instead, use the actual expected fixed-point
+ // literal.
+ static constexpr auto expected = [](IntType n, IntType d) -> FXType {
+ return static_cast<FXType>(static_cast<CompType>(n) /
+ static_cast<CompType>(d));
+ };
+
+public:
+ typedef FXType (*FxDiviFunc)(IntType, IntType);
+
+ void testBasicNumbers(FxDiviFunc func) {
+ EXPECT_TRUE(abs_diff(func(1, 3), expected(1, 3)) <= epsilon);
----------------
sohail103 wrote:
Do you think it would be helpful to allow the tolerance to be passed as an argument as well? That way tests for different functions could reuse the same macro with different tolerances.
https://github.com/llvm/llvm-project/pull/210020
More information about the libc-commits
mailing list