| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | #include <bit> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "Common/BitUtils.h" |
| 10 | #include "Common/CommonTypes.h" |
| 11 | |
| 12 | TEST(BitUtils, BitSize) |
| 13 | { |
| 14 | EXPECT_EQ(Common::BitSize<s8>(), 8u); |
| 15 | EXPECT_EQ(Common::BitSize<s16>(), 16u); |
| 16 | EXPECT_EQ(Common::BitSize<s32>(), 32u); |
| 17 | EXPECT_EQ(Common::BitSize<s64>(), 64u); |
| 18 | |
| 19 | EXPECT_EQ(Common::BitSize<u8>(), 8u); |
| 20 | EXPECT_EQ(Common::BitSize<u16>(), 16u); |
| 21 | EXPECT_EQ(Common::BitSize<u32>(), 32u); |
| 22 | EXPECT_EQ(Common::BitSize<u64>(), 64u); |
| 23 | } |
| 24 | |
| 25 | TEST(BitUtils, ExtractBit) |
| 26 | { |
| 27 | constexpr s32 zero = 0; |
| 28 | EXPECT_EQ(Common::ExtractBit<0>(zero), 0); |
| 29 | |
| 30 | constexpr s32 one = 1; |
| 31 | EXPECT_EQ(Common::ExtractBit<0>(one), 1); |
| 32 | |
| 33 | constexpr s32 negative_one = -1; |
| 34 | EXPECT_EQ(Common::ExtractBit<31>(negative_one), 1); |
| 35 | |
| 36 | constexpr s32 one_hundred_twenty_eight = 0b10000000; |
| 37 | EXPECT_EQ(Common::ExtractBit<7>(one_hundred_twenty_eight), 1); |
| 38 | } |
| 39 | |
| 40 | TEST(BitUtils, ExtractBits) |
| 41 | { |
| 42 | // Note: Parenthesizing is necessary to prevent the macros from |
| 43 | // mangling the template function usages. |
| 44 | |
| 45 | constexpr s32 two_hundred_four_signed = 0b0011001100; |
| 46 | EXPECT_EQ((Common::ExtractBits<2, 3>(two_hundred_four_signed)), 3u); |
| 47 | EXPECT_EQ((Common::ExtractBits<2, 7>(two_hundred_four_signed)), 51u); |
| 48 | EXPECT_EQ((Common::ExtractBits<3, 6>(two_hundred_four_signed)), 9u); |
| 49 | |
| 50 | constexpr u32 two_hundred_four_unsigned = 0b0011001100; |
| 51 | EXPECT_EQ((Common::ExtractBits<2, 3>(two_hundred_four_unsigned)), 3u); |
| 52 | EXPECT_EQ((Common::ExtractBits<2, 7>(two_hundred_four_unsigned)), 51u); |
| 53 | EXPECT_EQ((Common::ExtractBits<3, 6>(two_hundred_four_unsigned)), 9u); |
| 54 | |
| 55 | // Ensure bit extraction remains sign-independent even when signed types are used. |
| 56 | constexpr s32 negative_one = -1; |
| 57 | EXPECT_EQ((Common::ExtractBits<0, 31>(negative_one)), 0xFFFFFFFFU); |
| 58 | |
| 59 | // Ensure bit extraction with type overriding works as expected |
| 60 | EXPECT_EQ((Common::ExtractBits<0, 31, s32, s32>(negative_one)), -1); |
| 61 | } |
| 62 | |
| 63 | TEST(BitUtils, IsValidLowMask) |
| 64 | { |
| 65 | EXPECT_TRUE(Common::IsValidLowMask(0b0u)); |
| 66 | EXPECT_TRUE(Common::IsValidLowMask(0b1u)); |
| 67 | EXPECT_FALSE(Common::IsValidLowMask(0b10u)); |
| 68 | EXPECT_TRUE(Common::IsValidLowMask(0b11u)); |
| 69 | EXPECT_FALSE(Common::IsValidLowMask(0b1110u)); |
| 70 | EXPECT_TRUE(Common::IsValidLowMask(0b1111u)); |
| 71 | EXPECT_FALSE(Common::IsValidLowMask(0b10000u)); |
| 72 | EXPECT_FALSE(Common::IsValidLowMask(0b101111u)); |
| 73 | |
| 74 | EXPECT_TRUE(Common::IsValidLowMask((u8)~0b0)); |
| 75 | EXPECT_FALSE(Common::IsValidLowMask((u8)(~0b0 - 1))); |
| 76 | EXPECT_FALSE(Common::IsValidLowMask((u8) ~(0b10000))); |
| 77 | EXPECT_FALSE(Common::IsValidLowMask((u8)(~((u8)(~0b0) >> 1) | 0b1111))); |
| 78 | |
| 79 | EXPECT_TRUE(Common::IsValidLowMask((u16)~0b0)); |
| 80 | EXPECT_FALSE(Common::IsValidLowMask((u16)(~0b0 - 1))); |
| 81 | EXPECT_FALSE(Common::IsValidLowMask((u16) ~(0b10000))); |
| 82 | EXPECT_FALSE(Common::IsValidLowMask((u16)(~((u16)(~0b0) >> 1) | 0b1111))); |
| 83 | |
| 84 | EXPECT_TRUE(Common::IsValidLowMask((u32)~0b0)); |
| 85 | EXPECT_FALSE(Common::IsValidLowMask((u32)(~0b0 - 1))); |
| 86 | EXPECT_FALSE(Common::IsValidLowMask((u32) ~(0b10000))); |
| 87 | EXPECT_FALSE(Common::IsValidLowMask((u32)(~((u32)(~0b0) >> 1) | 0b1111))); |
| 88 | |
| 89 | EXPECT_TRUE(Common::IsValidLowMask((u64)~0b0)); |
| 90 | EXPECT_FALSE(Common::IsValidLowMask((u64)(~0b0 - 1))); |
| 91 | EXPECT_FALSE(Common::IsValidLowMask((u64) ~(0b10000))); |
| 92 | EXPECT_FALSE(Common::IsValidLowMask((u64)(~((u64)(~0b0) >> 1) | 0b1111))); |
| 93 | } |
| 94 | |
| 95 | TEST(BitUtils, BitCastPtr) |
| 96 | { |
| 97 | EXPECT_EQ(std::endian::native, std::endian::little); |
| 98 | |
| 99 | std::vector<u8> data{0, 1, 0, 2, 0, 3, 0, 0xFF, 0xFF}; |
| 100 | u8* buffer = data.data(); |
| 101 | |
| 102 | EXPECT_EQ(s16(1), Common::BitCastPtr<s16>(buffer + 1)); |
| 103 | Common::BitCastPtr<s16>(buffer + 1) = s16(-1); |
| 104 | EXPECT_EQ(u16(0xFFFF), Common::BitCastPtr<u16>(buffer + 1)); |
| 105 | EXPECT_EQ(data, std::vector<u8>({0, 0xFF, 0xFF, 2, 0, 3, 0, 0xFF, 0xFF})); |
| 106 | |
| 107 | EXPECT_EQ(s32(0xFFFF0003), Common::BitCastPtr<s32>(buffer + 1)[1]); |
| 108 | Common::BitCastPtr<u32>(buffer + 1)[1] = u32(0xFFFFFFFF); |
| 109 | EXPECT_EQ(s32(-1), Common::BitCastPtr<s32>(buffer + 1)[1]); |
| 110 | EXPECT_EQ(data, std::vector<u8>({0, 0xFF, 0xFF, 2, 0, 0xFF, 0xFF, 0xFF, 0xFF})); |
| 111 | |
| 112 | #pragma pack(push, 1) |
| 113 | struct MyStruct |
| 114 | { |
| 115 | u16 v16; |
| 116 | u8 v8; |
| 117 | }; |
| 118 | #pragma pack(pop) |
| 119 | |
| 120 | MyStruct s1 = Common::BitCastPtr<MyStruct>(buffer + 1); |
| 121 | EXPECT_EQ(u16(0xFFFF), s1.v16); |
| 122 | EXPECT_EQ(u8(2), s1.v8); |
| 123 | s1.v16 = 4; |
| 124 | s1.v8 = 5; |
| 125 | Common::BitCastPtr<MyStruct>(buffer + 1) = s1; |
| 126 | EXPECT_EQ(s16(4), Common::BitCastPtr<s16>(buffer + 1)); |
| 127 | EXPECT_EQ(s8(5), Common::BitCastPtr<s8>(buffer + 3)); |
| 128 | EXPECT_EQ(data, std::vector<u8>({0, 4, 0, 5, 0, 0xFF, 0xFF, 0xFF, 0xFF})); |
| 129 | |
| 130 | auto struct_array = Common::BitCastPtr<MyStruct>(buffer + 1); |
| 131 | const MyStruct s1_again = struct_array[0]; |
| 132 | EXPECT_EQ(u16(4), s1_again.v16); |
| 133 | EXPECT_EQ(u8(5), s1_again.v8); |
| 134 | MyStruct s2 = struct_array[1]; |
| 135 | EXPECT_EQ(u16(0xFF00), s2.v16); |
| 136 | EXPECT_EQ(u8(0xFF), s2.v8); |
| 137 | struct_array[1] = s1_again; |
| 138 | s2 = struct_array[1]; |
| 139 | EXPECT_EQ(u16(4), s2.v16); |
| 140 | EXPECT_EQ(u8(5), s2.v8); |
| 141 | EXPECT_EQ(data, std::vector<u8>({0, 4, 0, 5, 4, 0, 5, 0xFF, 0xFF})); |
| 142 | } |