diff options
| author | 2020-08-14 09:38:45 -0400 | |
|---|---|---|
| committer | 2020-08-15 17:17:52 -0400 | |
| commit | df7248039553b3ebd338380c3ef0428b0e046e79 (patch) | |
| tree | eca7153300e311ac7954f5c085fdada0c7295699 /src/common/bit_util.h | |
| parent | Merge pull request #4526 from lioncash/core-semi (diff) | |
| download | yuzu-df7248039553b3ebd338380c3ef0428b0e046e79.tar.gz yuzu-df7248039553b3ebd338380c3ef0428b0e046e79.tar.xz yuzu-df7248039553b3ebd338380c3ef0428b0e046e79.zip | |
common: Make use of [[nodiscard]] where applicable
Now that clang-format makes [[nodiscard]] attributes format sensibly, we
can apply them to several functions within the common library to allow
the compiler to complain about any misuses of the functions.
Diffstat (limited to 'src/common/bit_util.h')
| -rw-r--r-- | src/common/bit_util.h | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 6f7d5a947..29f59a9a3 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h | |||
| @@ -17,12 +17,12 @@ namespace Common { | |||
| 17 | 17 | ||
| 18 | /// Gets the size of a specified type T in bits. | 18 | /// Gets the size of a specified type T in bits. |
| 19 | template <typename T> | 19 | template <typename T> |
| 20 | constexpr std::size_t BitSize() { | 20 | [[nodiscard]] constexpr std::size_t BitSize() { |
| 21 | return sizeof(T) * CHAR_BIT; | 21 | return sizeof(T) * CHAR_BIT; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | #ifdef _MSC_VER | 24 | #ifdef _MSC_VER |
| 25 | inline u32 CountLeadingZeroes32(u32 value) { | 25 | [[nodiscard]] inline u32 CountLeadingZeroes32(u32 value) { |
| 26 | unsigned long leading_zero = 0; | 26 | unsigned long leading_zero = 0; |
| 27 | 27 | ||
| 28 | if (_BitScanReverse(&leading_zero, value) != 0) { | 28 | if (_BitScanReverse(&leading_zero, value) != 0) { |
| @@ -32,7 +32,7 @@ inline u32 CountLeadingZeroes32(u32 value) { | |||
| 32 | return 32; | 32 | return 32; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | inline u32 CountLeadingZeroes64(u64 value) { | 35 | [[nodiscard]] inline u32 CountLeadingZeroes64(u64 value) { |
| 36 | unsigned long leading_zero = 0; | 36 | unsigned long leading_zero = 0; |
| 37 | 37 | ||
| 38 | if (_BitScanReverse64(&leading_zero, value) != 0) { | 38 | if (_BitScanReverse64(&leading_zero, value) != 0) { |
| @@ -42,7 +42,7 @@ inline u32 CountLeadingZeroes64(u64 value) { | |||
| 42 | return 64; | 42 | return 64; |
| 43 | } | 43 | } |
| 44 | #else | 44 | #else |
| 45 | inline u32 CountLeadingZeroes32(u32 value) { | 45 | [[nodiscard]] inline u32 CountLeadingZeroes32(u32 value) { |
| 46 | if (value == 0) { | 46 | if (value == 0) { |
| 47 | return 32; | 47 | return 32; |
| 48 | } | 48 | } |
| @@ -50,7 +50,7 @@ inline u32 CountLeadingZeroes32(u32 value) { | |||
| 50 | return static_cast<u32>(__builtin_clz(value)); | 50 | return static_cast<u32>(__builtin_clz(value)); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | inline u32 CountLeadingZeroes64(u64 value) { | 53 | [[nodiscard]] inline u32 CountLeadingZeroes64(u64 value) { |
| 54 | if (value == 0) { | 54 | if (value == 0) { |
| 55 | return 64; | 55 | return 64; |
| 56 | } | 56 | } |
| @@ -60,7 +60,7 @@ inline u32 CountLeadingZeroes64(u64 value) { | |||
| 60 | #endif | 60 | #endif |
| 61 | 61 | ||
| 62 | #ifdef _MSC_VER | 62 | #ifdef _MSC_VER |
| 63 | inline u32 CountTrailingZeroes32(u32 value) { | 63 | [[nodiscard]] inline u32 CountTrailingZeroes32(u32 value) { |
| 64 | unsigned long trailing_zero = 0; | 64 | unsigned long trailing_zero = 0; |
| 65 | 65 | ||
| 66 | if (_BitScanForward(&trailing_zero, value) != 0) { | 66 | if (_BitScanForward(&trailing_zero, value) != 0) { |
| @@ -70,7 +70,7 @@ inline u32 CountTrailingZeroes32(u32 value) { | |||
| 70 | return 32; | 70 | return 32; |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | inline u32 CountTrailingZeroes64(u64 value) { | 73 | [[nodiscard]] inline u32 CountTrailingZeroes64(u64 value) { |
| 74 | unsigned long trailing_zero = 0; | 74 | unsigned long trailing_zero = 0; |
| 75 | 75 | ||
| 76 | if (_BitScanForward64(&trailing_zero, value) != 0) { | 76 | if (_BitScanForward64(&trailing_zero, value) != 0) { |
| @@ -80,7 +80,7 @@ inline u32 CountTrailingZeroes64(u64 value) { | |||
| 80 | return 64; | 80 | return 64; |
| 81 | } | 81 | } |
| 82 | #else | 82 | #else |
| 83 | inline u32 CountTrailingZeroes32(u32 value) { | 83 | [[nodiscard]] inline u32 CountTrailingZeroes32(u32 value) { |
| 84 | if (value == 0) { | 84 | if (value == 0) { |
| 85 | return 32; | 85 | return 32; |
| 86 | } | 86 | } |
| @@ -88,7 +88,7 @@ inline u32 CountTrailingZeroes32(u32 value) { | |||
| 88 | return static_cast<u32>(__builtin_ctz(value)); | 88 | return static_cast<u32>(__builtin_ctz(value)); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | inline u32 CountTrailingZeroes64(u64 value) { | 91 | [[nodiscard]] inline u32 CountTrailingZeroes64(u64 value) { |
| 92 | if (value == 0) { | 92 | if (value == 0) { |
| 93 | return 64; | 93 | return 64; |
| 94 | } | 94 | } |
| @@ -99,13 +99,13 @@ inline u32 CountTrailingZeroes64(u64 value) { | |||
| 99 | 99 | ||
| 100 | #ifdef _MSC_VER | 100 | #ifdef _MSC_VER |
| 101 | 101 | ||
| 102 | inline u32 MostSignificantBit32(const u32 value) { | 102 | [[nodiscard]] inline u32 MostSignificantBit32(const u32 value) { |
| 103 | unsigned long result; | 103 | unsigned long result; |
| 104 | _BitScanReverse(&result, value); | 104 | _BitScanReverse(&result, value); |
| 105 | return static_cast<u32>(result); | 105 | return static_cast<u32>(result); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | inline u32 MostSignificantBit64(const u64 value) { | 108 | [[nodiscard]] inline u32 MostSignificantBit64(const u64 value) { |
| 109 | unsigned long result; | 109 | unsigned long result; |
| 110 | _BitScanReverse64(&result, value); | 110 | _BitScanReverse64(&result, value); |
| 111 | return static_cast<u32>(result); | 111 | return static_cast<u32>(result); |
| @@ -113,30 +113,30 @@ inline u32 MostSignificantBit64(const u64 value) { | |||
| 113 | 113 | ||
| 114 | #else | 114 | #else |
| 115 | 115 | ||
| 116 | inline u32 MostSignificantBit32(const u32 value) { | 116 | [[nodiscard]] inline u32 MostSignificantBit32(const u32 value) { |
| 117 | return 31U - static_cast<u32>(__builtin_clz(value)); | 117 | return 31U - static_cast<u32>(__builtin_clz(value)); |
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | inline u32 MostSignificantBit64(const u64 value) { | 120 | [[nodiscard]] inline u32 MostSignificantBit64(const u64 value) { |
| 121 | return 63U - static_cast<u32>(__builtin_clzll(value)); | 121 | return 63U - static_cast<u32>(__builtin_clzll(value)); |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | #endif | 124 | #endif |
| 125 | 125 | ||
| 126 | inline u32 Log2Floor32(const u32 value) { | 126 | [[nodiscard]] inline u32 Log2Floor32(const u32 value) { |
| 127 | return MostSignificantBit32(value); | 127 | return MostSignificantBit32(value); |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | inline u32 Log2Ceil32(const u32 value) { | 130 | [[nodiscard]] inline u32 Log2Ceil32(const u32 value) { |
| 131 | const u32 log2_f = Log2Floor32(value); | 131 | const u32 log2_f = Log2Floor32(value); |
| 132 | return log2_f + ((value ^ (1U << log2_f)) != 0U); | 132 | return log2_f + ((value ^ (1U << log2_f)) != 0U); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | inline u32 Log2Floor64(const u64 value) { | 135 | [[nodiscard]] inline u32 Log2Floor64(const u64 value) { |
| 136 | return MostSignificantBit64(value); | 136 | return MostSignificantBit64(value); |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | inline u32 Log2Ceil64(const u64 value) { | 139 | [[nodiscard]] inline u32 Log2Ceil64(const u64 value) { |
| 140 | const u64 log2_f = static_cast<u64>(Log2Floor64(value)); | 140 | const u64 log2_f = static_cast<u64>(Log2Floor64(value)); |
| 141 | return static_cast<u32>(log2_f + ((value ^ (1ULL << log2_f)) != 0ULL)); | 141 | return static_cast<u32>(log2_f + ((value ^ (1ULL << log2_f)) != 0ULL)); |
| 142 | } | 142 | } |