From 49d802862d5edf762928cb2ee159614f31eb2364 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Mon, 2 Apr 2018 16:50:54 +0000 Subject: [Core] Grab-bag of improvements for Scalar. Remove Scalar::Cast. It was noted on the list that this method is unused. So, this patch removes it. Fix Scalar::Promote for most integer types This fixes promotion of most integer types (128- and 256-bit types are handled in a subsequent patch) to floating-point types. Previously promotion was done bitwise, where value preservation is correct. Fix Scalar::Promote for 128- and 256-bit integer types This patch fixes the behavior of Scalar::Promote when trying to perform a binary operation involving a 128- or 256-bit integer type and a floating-point type. Now, the integer is cast to the floating point type for the operation. Patch by Tom Tromey! Differential Revision: https://reviews.llvm.org/D44907 llvm-svn: 328985 --- lldb/unittests/Core/ScalarTest.cpp | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'lldb/unittests') diff --git a/lldb/unittests/Core/ScalarTest.cpp b/lldb/unittests/Core/ScalarTest.cpp index 47fa0215ead..6ccc3907064 100644 --- a/lldb/unittests/Core/ScalarTest.cpp +++ b/lldb/unittests/Core/ScalarTest.cpp @@ -73,6 +73,12 @@ TEST(ScalarTest, CastOperations) { ASSERT_EQ((unsigned long)a, a_scalar.ULong()); ASSERT_EQ((signed long long)a, a_scalar.SLongLong()); ASSERT_EQ((unsigned long long)a, a_scalar.ULongLong()); + + int a2 = 23; + Scalar a2_scalar(a2); + ASSERT_EQ((float)a2, a2_scalar.Float()); + ASSERT_EQ((double)a2, a2_scalar.Double()); + ASSERT_EQ((long double)a2, a2_scalar.LongDouble()); } TEST(ScalarTest, ExtractBitfield) { @@ -140,3 +146,42 @@ TEST(ScalarTest, Division) { EXPECT_TRUE(r.IsValid()); EXPECT_EQ(r, Scalar(2.5)); } + +TEST(ScalarTest, Promotion) { + static Scalar::Type int_types[] = { + Scalar::e_sint, Scalar::e_uint, Scalar::e_slong, + Scalar::e_ulong, Scalar::e_slonglong, Scalar::e_ulonglong, + Scalar::e_sint128, Scalar::e_uint128, Scalar::e_sint256, + Scalar::e_uint256, + Scalar::e_void // sentinel + }; + + static Scalar::Type float_types[] = { + Scalar::e_float, Scalar::e_double, Scalar::e_long_double, + Scalar::e_void // sentinel + }; + + for (int i = 0; int_types[i] != Scalar::e_void; ++i) { + for (int j = 0; float_types[j] != Scalar::e_void; ++j) { + Scalar lhs(2); + EXPECT_TRUE(lhs.Promote(int_types[i])) << "int promotion #" << i; + Scalar rhs(0.5f); + EXPECT_TRUE(rhs.Promote(float_types[j])) << "float promotion #" << j; + Scalar x(2.5f); + EXPECT_TRUE(x.Promote(float_types[j])); + EXPECT_EQ(lhs + rhs, x); + } + } + + for (int i = 0; float_types[i] != Scalar::e_void; ++i) { + for (int j = 0; float_types[j] != Scalar::e_void; ++j) { + Scalar lhs(2); + EXPECT_TRUE(lhs.Promote(float_types[i])) << "float promotion #" << i; + Scalar rhs(0.5f); + EXPECT_TRUE(rhs.Promote(float_types[j])) << "float promotion #" << j; + Scalar x(2.5f); + EXPECT_TRUE(x.Promote(float_types[j])); + EXPECT_EQ(lhs + rhs, x); + } + } +} -- cgit v1.2.1