Skip to content

Commit a579782

Browse files
authored
[llvm] Add serialization to uint32_t for FixedPointSemantics (#110288)
FixedPointSemantics is exactly 32bits and this static_assert'ed after its declaration. Add support for converting it to and from a uint32_t.
1 parent 0ee5c86 commit a579782

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

llvm/include/llvm/ADT/APFixedPoint.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ class FixedPointSemantics {
114114
}
115115
bool operator!=(FixedPointSemantics Other) const { return !(*this == Other); }
116116

117+
/// Convert the semantics to a 32-bit unsigned integer.
118+
/// The result is dependent on the host endianness and not stable across LLVM
119+
/// versions. See getFromOpaqueInt() to convert it back to a
120+
/// FixedPointSemantics object.
121+
uint32_t toOpaqueInt() const;
122+
/// Create a FixedPointSemantics object from an integer created via
123+
/// toOpaqueInt().
124+
static FixedPointSemantics getFromOpaqueInt(uint32_t);
125+
117126
private:
118127
unsigned Width : WidthBitWidth;
119128
signed int LsbWeight : LsbWeightBitWidth;

llvm/lib/Support/APFixedPoint.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ void FixedPointSemantics::print(llvm::raw_ostream &OS) const {
2929
OS << "IsSaturated=" << IsSaturated;
3030
}
3131

32+
uint32_t FixedPointSemantics::toOpaqueInt() const {
33+
return llvm::bit_cast<uint32_t>(*this);
34+
}
35+
36+
FixedPointSemantics FixedPointSemantics::getFromOpaqueInt(uint32_t I) {
37+
FixedPointSemantics F(0, 0, false, false, false);
38+
std::memcpy(&F, &I, sizeof(F));
39+
return F;
40+
}
41+
3242
APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema,
3343
bool *Overflow) const {
3444
APSInt NewVal = Val;

llvm/unittests/ADT/APFixedPointTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,4 +1274,35 @@ TEST(FixedPoint, div) {
12741274
true, false, false)));
12751275
}
12761276

1277+
TEST(FixedPoint, semanticsSerialization) {
1278+
auto roundTrip = [](FixedPointSemantics FPS) -> bool {
1279+
uint32_t I = FPS.toOpaqueInt();
1280+
FixedPointSemantics FPS2 = FixedPointSemantics::getFromOpaqueInt(I);
1281+
return FPS == FPS2;
1282+
};
1283+
1284+
ASSERT_TRUE(roundTrip(getS32Pos2()));
1285+
ASSERT_TRUE(roundTrip(getU8Pos4()));
1286+
ASSERT_TRUE(roundTrip(getS16Neg18()));
1287+
ASSERT_TRUE(roundTrip(getU8Neg10()));
1288+
ASSERT_TRUE(roundTrip(getPadULFractSema()));
1289+
ASSERT_TRUE(roundTrip(getPadUFractSema()));
1290+
ASSERT_TRUE(roundTrip(getPadUSFractSema()));
1291+
ASSERT_TRUE(roundTrip(getPadULAccumSema()));
1292+
ASSERT_TRUE(roundTrip(getPadUAccumSema()));
1293+
ASSERT_TRUE(roundTrip(getPadUSAccumSema()));
1294+
ASSERT_TRUE(roundTrip(getULFractSema()));
1295+
ASSERT_TRUE(roundTrip(getUFractSema()));
1296+
ASSERT_TRUE(roundTrip(getUSFractSema()));
1297+
ASSERT_TRUE(roundTrip(getULAccumSema()));
1298+
ASSERT_TRUE(roundTrip(getUAccumSema()));
1299+
ASSERT_TRUE(roundTrip(getUSAccumSema()));
1300+
ASSERT_TRUE(roundTrip(getLFractSema()));
1301+
ASSERT_TRUE(roundTrip(getFractSema()));
1302+
ASSERT_TRUE(roundTrip(getSFractSema()));
1303+
ASSERT_TRUE(roundTrip(getLAccumSema()));
1304+
ASSERT_TRUE(roundTrip(getAccumSema()));
1305+
ASSERT_TRUE(roundTrip(getSAccumSema()));
1306+
}
1307+
12771308
} // namespace

0 commit comments

Comments
 (0)