-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[clang][bytecode] Implement bitcasts to floating-point values #114485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesFull diff: https://github.com/llvm/llvm-project/pull/114485.diff 2 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index ade6f7424b1fd6..52d3a19ca99593 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3062,7 +3062,8 @@ inline bool BitCast(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
return false;
if constexpr (std::is_same_v<T, Floating>) {
- assert(false && "Implement bitcasting to a floating type");
+ assert(Sem);
+ S.Stk.push<Floating>(T::bitcastFromMemory(Buff.data(), *Sem));
} else {
assert(!Sem);
S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 58cf486833412f..319405c60cbad0 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -103,7 +103,7 @@ namespace simple {
static_assert(check_round_trip<unsigned>((int)0x12345678));
static_assert(check_round_trip<unsigned>((int)0x87654321));
static_assert(check_round_trip<unsigned>((int)0x0C05FEFE));
- // static_assert(round_trip<float>((int)0x0C05FEFE));
+ static_assert(round_trip<float>((int)0x0C05FEFE));
/// This works in GCC and in the bytecode interpreter, but the current interpreter
@@ -449,3 +449,57 @@ struct ref_mem {
// both-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
// both-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
constexpr intptr_t run_ref_mem = __builtin_bit_cast(intptr_t, ref_mem{global_int});
+
+namespace test_long_double {
+#ifdef __x86_64
+#if 0
+constexpr __int128_t test_cast_to_int128 = bit_cast<__int128_t>((long double)0); // expected-error{{must be initialized by a constant expression}}\
+ // expected-note{{in call}}
+#endif
+constexpr long double ld = 3.1425926539;
+
+struct bytes {
+ unsigned char d[16];
+};
+
+// static_assert(round_trip<bytes>(ld), "");
+
+static_assert(round_trip<long double>(10.0L));
+
+#if 0
+constexpr bool f(bool read_uninit) {
+ bytes b = bit_cast<bytes>(ld);
+ unsigned char ld_bytes[10] = {
+ 0x0, 0x48, 0x9f, 0x49, 0xf0,
+ 0x3c, 0x20, 0xc9, 0x0, 0x40,
+ };
+
+ for (int i = 0; i != 10; ++i)
+ if (ld_bytes[i] != b.d[i])
+ return false;
+
+ if (read_uninit && b.d[10]) // expected-note{{read of uninitialized object is not allowed in a constant expression}}
+ return false;
+
+ return true;
+}
+
+static_assert(f(/*read_uninit=*/false), "");
+static_assert(f(/*read_uninit=*/true), ""); // expected-error{{static assertion expression is not an integral constant expression}} \
+ // expected-note{{in call to 'f(true)'}}
+#endif
+constexpr bytes ld539 = {
+ 0x0, 0x0, 0x0, 0x0,
+ 0x0, 0x0, 0xc0, 0x86,
+ 0x8, 0x40, 0x0, 0x0,
+ 0x0, 0x0, 0x0, 0x0,
+};
+
+constexpr long double fivehundredandthirtynine = 539.0;
+
+static_assert(bit_cast<long double>(ld539) == fivehundredandthirtynine, "");
+
+#else
+static_assert(round_trip<__int128_t>(34.0L));
+#endif
+}
|
ef34e3c
to
7fc9c53
Compare
7fc9c53
to
724a636
Compare
This broke the Solaris/sparcv9 bot. |
tbaederr
added a commit
that referenced
this pull request
Nov 2, 2024
Sorry, reverted. |
smallp-o-p
pushed a commit
to smallp-o-p/llvm-project
that referenced
this pull request
Nov 3, 2024
smallp-o-p
pushed a commit
to smallp-o-p/llvm-project
that referenced
this pull request
Nov 3, 2024
tbaederr
added a commit
to tbaederr/llvm-project
that referenced
this pull request
Nov 3, 2024
…14485) This time I tested on big-endian hosts.
tbaederr
added a commit
to tbaederr/llvm-project
that referenced
this pull request
Nov 3, 2024
…14485) This time I tested on big-endian hosts.
tbaederr
added a commit
that referenced
this pull request
Nov 3, 2024
… (#114712) This time I tested on big-endian hosts.
NoumanAmir657
pushed a commit
to NoumanAmir657/llvm-project
that referenced
this pull request
Nov 4, 2024
NoumanAmir657
pushed a commit
to NoumanAmir657/llvm-project
that referenced
this pull request
Nov 4, 2024
PhilippRados
pushed a commit
to PhilippRados/llvm-project
that referenced
this pull request
Nov 6, 2024
…14485) (llvm#114712) This time I tested on big-endian hosts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.