[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 Jul 24 03:36:27 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);
+    EXPECT_TRUE(abs_diff(func(2, 3), static_cast<FXType>(
+                                         0.66666666666666666667)) <= epsilon);
+    EXPECT_TRUE(abs_diff(func(3, 4), 3 * one_fourth) <= epsilon);
----------------
sohail103 wrote:

Thanks, updated in latest push

https://github.com/llvm/llvm-project/pull/210020


More information about the libc-commits mailing list