[libc-commits] [libc] [libc][stdfix] Implement fixed point bitsfx functions in llvm libc (PR #128413)

via libc-commits libc-commits at lists.llvm.org
Wed Feb 26 15:00:13 PST 2025


================
@@ -0,0 +1,94 @@
+//===-- Utility class to test bitsfx 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 "src/__support/fixed_point/fx_rep.h"
+
+template <typename T, typename XType>
+class BitsFxTest : public LIBC_NAMESPACE::testing::Test {
+
+  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
+  static constexpr T zero = FXRep::ZERO();
+  static constexpr T max = FXRep::MAX();
+  static constexpr T min = FXRep::MIN();
+  static constexpr T one_half = FXRep::ONE_HALF();
+  static constexpr T one_fourth = FXRep::ONE_FOURTH();
+  static constexpr T eps = FXRep::EPS();
+
+  static constexpr T zero_point_six_eight_seven_five_t = 0.6875;
+
+  static constexpr T negative_zero_point_six_eight_seven_five_t = -0.6875;
+
+  // an arbitrarily chosen special number
+  static constexpr T special_num_t = 10.71875;
+
+  static constexpr T negative_special_num_t = -10.71875;
+
+public:
+  typedef XType (*BitsFxFunc)(T);
+
+  void testSpecialNumbers(BitsFxFunc func) {
+    EXPECT_EQ(static_cast<XType>(0), func(zero));
+    EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 1)),
+              func(one_half));
+    EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 2)),
+              func(one_fourth));
+    EXPECT_EQ(static_cast<XType>(1), func(eps));
+
+    // (0.6875)_10 = (0.1011)_2
+    EXPECT_EQ(static_cast<XType>(11ULL << (FXRep::FRACTION_LEN - 4)),
+              func(zero_point_six_eight_seven_five_t));
+
+    if constexpr (FXRep::SIGN_LEN > 0)
+      EXPECT_EQ(static_cast<XType>(-(11ULL << (FXRep::FRACTION_LEN - 4))),
+                func(negative_zero_point_six_eight_seven_five_t));
+
+    if constexpr (FXRep::INTEGRAL_LEN > 0) {
+      if (static_cast<int>(max) >= 11)
+        switch (FXRep::FRACTION_LEN) {
+        case 7:
+          EXPECT_EQ(static_cast<XType>(1372), func(special_num_t));
+          break;
+        case 15:
+          EXPECT_EQ(static_cast<XType>(351232), func(special_num_t));
+          break;
+        case 23:
+          EXPECT_EQ(static_cast<XType>(89915392), func(special_num_t));
+          break;
+        default:
+          break;
----------------
PiJoules wrote:

Hmm ok rather than having the switch, perhaps we could do something like have a preset number for the lowest value we know and do the shifts. That is, we have something like

```
constexpr size_t kMinFbits = 7;
if (max >= 11 && FXRep::FRACTION_LEN >= kMinFbits) {
  constexpr XType kExpected = 1372;  // 10.71875 with a scale of 7 equals this.
  EXPECT_EQ(XType << (FXRep::FRACTION_LEN - kMinFbits), func(special_num_t));
}
```

This way we can always have these tests even if the fractional bits aren't one of 7, 15, or 23.

Sorry I probably should've said this instead of accepting the switch from your comment earlier. I just though of this now.

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


More information about the libc-commits mailing list