summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BitUtilsTest.cpp
diff options
context:
space:
mode:
authorLioncash <mathew1800@gmail.com>2018-05-10 09:54:35 -0400
committerLioncash <mathew1800@gmail.com>2018-05-10 12:28:05 -0400
commitb3292298c976279765926c9927f2e4cfe38b3ede (patch)
treecfb1d352c49fa6f289e85fe764548a64d20b7062 /Source/UnitTests/Common/BitUtilsTest.cpp
parentfd1ea63383f7d9189a14a99948e08fddc4e4d7ee (diff)
BitUtils: Add C++14/C++17 compatible equivalent of std::bit_cast from C++2a
Given bit conversions between types are quite common in emulation (particularly when it comes to floating-point among other things) it makes sense to provide a utility function that keeps all the boilerplate contained; especially considering it makes it harder to accidentally misuse std::memcpy (such as accidentally transposing arguments, etc). Another benefit of this function is that it doesn't require separating declarations from assignments, allowing variables to be declared const. This makes the scenario of of uninitialized variables being used less likely to occur.
Diffstat (limited to 'Source/UnitTests/Common/BitUtilsTest.cpp')
-rw-r--r--Source/UnitTests/Common/BitUtilsTest.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/BitUtilsTest.cpp b/Source/UnitTests/Common/BitUtilsTest.cpp
index 4785f9dcef..e847e908bd 100644
--- a/Source/UnitTests/Common/BitUtilsTest.cpp
+++ b/Source/UnitTests/Common/BitUtilsTest.cpp
@@ -127,3 +127,16 @@ TEST(BitUtils, IsValidLowMask)
EXPECT_FALSE(Common::IsValidLowMask((u64) ~(0b10000)));
EXPECT_FALSE(Common::IsValidLowMask((u64)(~((u64)(~0b0) >> 1) | 0b1111)));
}
+
+TEST(BitUtils, BitCast)
+{
+ EXPECT_EQ(0x00000000U, Common::BitCast<u32>(0.0f));
+ EXPECT_EQ(0x80000000U, Common::BitCast<u32>(-0.0f));
+ EXPECT_EQ(0x3F800000U, Common::BitCast<u32>(1.0f));
+ EXPECT_EQ(0xBF800000U, Common::BitCast<u32>(-1.0f));
+
+ EXPECT_EQ(0x0000000000000000ULL, Common::BitCast<u64>(0.0));
+ EXPECT_EQ(0x8000000000000000ULL, Common::BitCast<u64>(-0.0));
+ EXPECT_EQ(0x3FF0000000000000ULL, Common::BitCast<u64>(1.0));
+ EXPECT_EQ(0xBFF0000000000000ULL, Common::BitCast<u64>(-1.0));
+}