[llvm] [llvm] Add serialization to uint32_t for FixedPointSemantics (PR #110288)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 27 09:00:44 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

FixedPointSemantics is exactly 32bits and this static_assert'ed after its declaration. Add support for converting it to and from a uint32_t.

---
Full diff: https://github.com/llvm/llvm-project/pull/110288.diff


3 Files Affected:

- (modified) llvm/include/llvm/ADT/APFixedPoint.h (+3) 
- (modified) llvm/lib/Support/APFixedPoint.cpp (+12) 
- (modified) llvm/unittests/ADT/APFixedPointTest.cpp (+31) 


``````````diff
diff --git a/llvm/include/llvm/ADT/APFixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h
index ae40db96e4818c..45c7b20facab59 100644
--- a/llvm/include/llvm/ADT/APFixedPoint.h
+++ b/llvm/include/llvm/ADT/APFixedPoint.h
@@ -114,6 +114,9 @@ class FixedPointSemantics {
   }
   bool operator!=(FixedPointSemantics Other) const { return !(*this == Other); }
 
+  uint32_t toOpaqueInt() const;
+  static FixedPointSemantics getFromOpaqueInt(uint32_t);
+
 private:
   unsigned Width          : WidthBitWidth;
   signed int LsbWeight    : LsbWeightBitWidth;
diff --git a/llvm/lib/Support/APFixedPoint.cpp b/llvm/lib/Support/APFixedPoint.cpp
index 249c4f1e2153da..3c289c9ad26030 100644
--- a/llvm/lib/Support/APFixedPoint.cpp
+++ b/llvm/lib/Support/APFixedPoint.cpp
@@ -29,6 +29,18 @@ void FixedPointSemantics::print(llvm::raw_ostream &OS) const {
   OS << "IsSaturated=" << IsSaturated;
 }
 
+uint32_t FixedPointSemantics::toOpaqueInt() const {
+  uint32_t Result;
+  std::memcpy(&Result, this, sizeof(uint32_t));
+  return Result;
+}
+
+FixedPointSemantics FixedPointSemantics::getFromOpaqueInt(uint32_t I) {
+  FixedPointSemantics F(0, 0, false, false, false);
+  std::memcpy(&F, &I, sizeof(F));
+  return F;
+}
+
 APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema,
                                    bool *Overflow) const {
   APSInt NewVal = Val;
diff --git a/llvm/unittests/ADT/APFixedPointTest.cpp b/llvm/unittests/ADT/APFixedPointTest.cpp
index ecb89fbf76c8bb..e7aa58a8325773 100644
--- a/llvm/unittests/ADT/APFixedPointTest.cpp
+++ b/llvm/unittests/ADT/APFixedPointTest.cpp
@@ -1274,4 +1274,35 @@ TEST(FixedPoint, div) {
                                               true, false, false)));
 }
 
+TEST(FixedPoint, semanticsSerialization) {
+  auto roundTrip = [](FixedPointSemantics FPS) -> bool {
+    uint32_t I = FPS.toOpaqueInt();
+    FixedPointSemantics FPS2 = FixedPointSemantics::getFromOpaqueInt(I);
+    return FPS == FPS2;
+  };
+
+  ASSERT_TRUE(roundTrip(getS32Pos2()));
+  ASSERT_TRUE(roundTrip(getU8Pos4()));
+  ASSERT_TRUE(roundTrip(getS16Neg18()));
+  ASSERT_TRUE(roundTrip(getU8Neg10()));
+  ASSERT_TRUE(roundTrip(getPadULFractSema()));
+  ASSERT_TRUE(roundTrip(getPadUFractSema()));
+  ASSERT_TRUE(roundTrip(getPadUSFractSema()));
+  ASSERT_TRUE(roundTrip(getPadULAccumSema()));
+  ASSERT_TRUE(roundTrip(getPadUAccumSema()));
+  ASSERT_TRUE(roundTrip(getPadUSAccumSema()));
+  ASSERT_TRUE(roundTrip(getULFractSema()));
+  ASSERT_TRUE(roundTrip(getUFractSema()));
+  ASSERT_TRUE(roundTrip(getUSFractSema()));
+  ASSERT_TRUE(roundTrip(getULAccumSema()));
+  ASSERT_TRUE(roundTrip(getUAccumSema()));
+  ASSERT_TRUE(roundTrip(getUSAccumSema()));
+  ASSERT_TRUE(roundTrip(getLFractSema()));
+  ASSERT_TRUE(roundTrip(getFractSema()));
+  ASSERT_TRUE(roundTrip(getSFractSema()));
+  ASSERT_TRUE(roundTrip(getLAccumSema()));
+  ASSERT_TRUE(roundTrip(getAccumSema()));
+  ASSERT_TRUE(roundTrip(getSAccumSema()));
+}
+
 } // namespace

``````````

</details>


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


More information about the llvm-commits mailing list