Skip to content

Commit 1e19f0f

Browse files
authored
[clang][bytecode] Implement IntegralAP bitcasting (llvm#114471)
Only for full-byte bitwidths for now.
1 parent 8cb6b65 commit 1e19f0f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

clang/lib/AST/ByteCode/IntegralAP.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ template <bool Signed> class IntegralAP final {
171171
return IntegralAP<false>(Copy);
172172
}
173173

174-
void bitcastToMemory(std::byte *Dest) const { assert(false); }
174+
void bitcastToMemory(std::byte *Dest) const {
175+
llvm::StoreIntToMemory(V, (uint8_t *)Dest, bitWidth() / 8);
176+
}
175177

176178
static IntegralAP bitcastFromMemory(const std::byte *Src, unsigned BitWidth) {
177-
return IntegralAP();
179+
APInt V(BitWidth, static_cast<uint64_t>(0), Signed);
180+
llvm::LoadIntFromMemory(V, (const uint8_t *)Src, BitWidth / 8);
181+
return IntegralAP(V);
178182
}
179183

180184
ComparisonCategoryResult compare(const IntegralAP &RHS) const {

clang/test/AST/ByteCode/builtin-bit-cast.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ constexpr bool operator==(const struct bits<N, T, P>& lhs, const struct bits<N,
7171
return lhs.bits == rhs.bits;
7272
}
7373

74+
#ifdef __SIZEOF_INT128__
75+
static_assert(check_round_trip<__int128_t>((__int128_t)34));
76+
static_assert(check_round_trip<__int128_t>((__int128_t)-34));
77+
#endif
78+
7479

7580
namespace simple {
7681
constexpr int A = __builtin_bit_cast(int, 10);

0 commit comments

Comments
 (0)