[libc-commits] [libc] [libc][stdfix] Implement fixed point fxdivi functions in llvm-libc (PR #210020)
via libc-commits
libc-commits at lists.llvm.org
Thu Aug 13 22:32:02 PDT 2026
================
@@ -0,0 +1,215 @@
+//===-- 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_EQ(func(3, 4), 3 * one_fourth);
+ EXPECT_TRUE(abs_diff(func(5, 7), static_cast<FXType>(
+ 0.71428571428571428571)) <= epsilon);
+ if constexpr (is_signed) {
+ EXPECT_TRUE(
+ abs_diff(func(-5, 7), static_cast<FXType>(-0.71428571428571428571)) <=
+ epsilon);
+ }
+ EXPECT_TRUE(abs_diff(func(1043, 2764),
+ static_cast<FXType>(0.37735166425470332851)) <=
+ epsilon);
+ EXPECT_TRUE(abs_diff(func(60000, 720293),
+ static_cast<FXType>(0.08329943509099769122)) <=
+ epsilon);
+
+ EXPECT_EQ(func(128, 256), one_half);
+ EXPECT_EQ(func(1, 2), one_half);
+ EXPECT_EQ(func(1, 4), one_fourth);
+ EXPECT_EQ(func(1, 8), one_eighth);
+ EXPECT_EQ(func(1, 16), static_cast<FXType>(0.0625));
+ if constexpr (is_signed) {
+ EXPECT_EQ(func(-1, 2), -one_half);
+ EXPECT_EQ(func(1, -4), -one_fourth);
+ EXPECT_EQ(func(-1, 8), -one_eighth);
+ EXPECT_EQ(func(1, -16), static_cast<FXType>(-0.0625));
+ }
+
+ if constexpr (has_integral) {
+ EXPECT_TRUE(
+ abs_diff(func(27, 23), static_cast<FXType>(1.17391304347826086957)) <=
+ epsilon);
+ }
+ }
+
+ void testEdgeCases(FxDiviFunc func) {
----------------
sohail103 wrote:
Thanks, updated in latest push. I think saturation tests for positive overflow on accum types was the one that was missing so added that.
https://github.com/llvm/llvm-project/pull/210020
More information about the libc-commits
mailing list