Skip to content

Commit ef34e3c

Browse files
committed
[clang][bytecode] Implement bitcasts to floating-point values
1 parent a1987be commit ef34e3c

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,8 @@ inline bool BitCast(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
30623062
return false;
30633063

30643064
if constexpr (std::is_same_v<T, Floating>) {
3065-
assert(false && "Implement bitcasting to a floating type");
3065+
assert(Sem);
3066+
S.Stk.push<Floating>(T::bitcastFromMemory(Buff.data(), *Sem));
30663067
} else {
30673068
assert(!Sem);
30683069
S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ namespace simple {
103103
static_assert(check_round_trip<unsigned>((int)0x12345678));
104104
static_assert(check_round_trip<unsigned>((int)0x87654321));
105105
static_assert(check_round_trip<unsigned>((int)0x0C05FEFE));
106-
// static_assert(round_trip<float>((int)0x0C05FEFE));
106+
static_assert(round_trip<float>((int)0x0C05FEFE));
107107

108108

109109
/// This works in GCC and in the bytecode interpreter, but the current interpreter
@@ -449,3 +449,57 @@ struct ref_mem {
449449
// both-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
450450
// both-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
451451
constexpr intptr_t run_ref_mem = __builtin_bit_cast(intptr_t, ref_mem{global_int});
452+
453+
namespace test_long_double {
454+
#ifdef __x86_64
455+
#if 0
456+
constexpr __int128_t test_cast_to_int128 = bit_cast<__int128_t>((long double)0); // expected-error{{must be initialized by a constant expression}}\
457+
// expected-note{{in call}}
458+
#endif
459+
constexpr long double ld = 3.1425926539;
460+
461+
struct bytes {
462+
unsigned char d[16];
463+
};
464+
465+
// static_assert(round_trip<bytes>(ld), "");
466+
467+
static_assert(round_trip<long double>(10.0L));
468+
469+
#if 0
470+
constexpr bool f(bool read_uninit) {
471+
bytes b = bit_cast<bytes>(ld);
472+
unsigned char ld_bytes[10] = {
473+
0x0, 0x48, 0x9f, 0x49, 0xf0,
474+
0x3c, 0x20, 0xc9, 0x0, 0x40,
475+
};
476+
477+
for (int i = 0; i != 10; ++i)
478+
if (ld_bytes[i] != b.d[i])
479+
return false;
480+
481+
if (read_uninit && b.d[10]) // expected-note{{read of uninitialized object is not allowed in a constant expression}}
482+
return false;
483+
484+
return true;
485+
}
486+
487+
static_assert(f(/*read_uninit=*/false), "");
488+
static_assert(f(/*read_uninit=*/true), ""); // expected-error{{static assertion expression is not an integral constant expression}} \
489+
// expected-note{{in call to 'f(true)'}}
490+
#endif
491+
constexpr bytes ld539 = {
492+
0x0, 0x0, 0x0, 0x0,
493+
0x0, 0x0, 0xc0, 0x86,
494+
0x8, 0x40, 0x0, 0x0,
495+
0x0, 0x0, 0x0, 0x0,
496+
};
497+
498+
constexpr long double fivehundredandthirtynine = 539.0;
499+
500+
static_assert(bit_cast<long double>(ld539) == fivehundredandthirtynine, "");
501+
502+
#else
503+
static_assert(round_trip<__int128_t>(34.0L));
504+
#endif
505+
}

0 commit comments

Comments
 (0)