From 23e1363d55ba0921473b195af104edce5c0d1ba0 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 12 Jul 2020 00:07:14 +0100 Subject: [PATCH] Cast away signedness of built-ins in BitSet. Most of the built-in functions return signed values but BitSet deals only in unsigned values. We can safely cast these away though as they're always indices, which can't be negative. Bug: 160010896 Test: atest inputflinger_tests, atest libinput_tests Change-Id: I6e0b33972fabcd41ad1c269ea0e2a07b13b33c12 --- libutils/include/utils/BitSet.h | 38 +++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/libutils/include/utils/BitSet.h b/libutils/include/utils/BitSet.h index 8abfb1a07..1df46264f 100644 --- a/libutils/include/utils/BitSet.h +++ b/libutils/include/utils/BitSet.h @@ -46,7 +46,9 @@ struct BitSet32 { // Returns the number of marked bits in the set. inline uint32_t count() const { return count(value); } - static inline uint32_t count(uint32_t value) { return __builtin_popcountl(value); } + static inline uint32_t count(uint32_t value) { + return static_cast(__builtin_popcountl(value)); + } // Returns true if the bit set does not contain any marked bits. inline bool isEmpty() const { return isEmpty(value); } @@ -128,7 +130,7 @@ struct BitSet32 { } static inline uint32_t getIndexOfBit(uint32_t value, uint32_t n) { - return __builtin_popcountl(value & ~(0xffffffffUL >> n)); + return static_cast(__builtin_popcountl(value & ~(0xffffffffUL >> n))); } inline bool operator== (const BitSet32& other) const { return value == other.value; } @@ -153,17 +155,17 @@ private: // input, which is only guaranteed to be 16b, not 32. The compiler should optimize this away. static inline uint32_t clz_checked(uint32_t value) { if (sizeof(unsigned int) == sizeof(uint32_t)) { - return __builtin_clz(value); + return static_cast(__builtin_clz(value)); } else { - return __builtin_clzl(value); + return static_cast(__builtin_clzl(value)); } } static inline uint32_t ctz_checked(uint32_t value) { if (sizeof(unsigned int) == sizeof(uint32_t)) { - return __builtin_ctz(value); + return static_cast(__builtin_ctz(value)); } else { - return __builtin_ctzl(value); + return static_cast(__builtin_ctzl(value)); } } }; @@ -188,7 +190,9 @@ struct BitSet64 { // Returns the number of marked bits in the set. inline uint32_t count() const { return count(value); } - static inline uint32_t count(uint64_t value) { return __builtin_popcountll(value); } + static inline uint32_t count(uint64_t value) { + return static_cast(__builtin_popcountll(value)); + } // Returns true if the bit set does not contain any marked bits. inline bool isEmpty() const { return isEmpty(value); } @@ -219,26 +223,32 @@ struct BitSet64 { // Result is undefined if all bits are unmarked. inline uint32_t firstMarkedBit() const { return firstMarkedBit(value); } - static inline uint32_t firstMarkedBit(uint64_t value) { return __builtin_clzll(value); } + static inline uint32_t firstMarkedBit(uint64_t value) { + return static_cast(__builtin_clzll(value)); + } // Finds the first unmarked bit in the set. // Result is undefined if all bits are marked. inline uint32_t firstUnmarkedBit() const { return firstUnmarkedBit(value); } - static inline uint32_t firstUnmarkedBit(uint64_t value) { return __builtin_clzll(~ value); } + static inline uint32_t firstUnmarkedBit(uint64_t value) { + return static_cast(__builtin_clzll(~value)); + } // Finds the last marked bit in the set. // Result is undefined if all bits are unmarked. inline uint32_t lastMarkedBit() const { return lastMarkedBit(value); } - static inline uint32_t lastMarkedBit(uint64_t value) { return 63 - __builtin_ctzll(value); } + static inline uint32_t lastMarkedBit(uint64_t value) { + return static_cast(63 - __builtin_ctzll(value)); + } // Finds the first marked bit in the set and clears it. Returns the bit index. // Result is undefined if all bits are unmarked. inline uint32_t clearFirstMarkedBit() { return clearFirstMarkedBit(value); } static inline uint32_t clearFirstMarkedBit(uint64_t& value) { - uint64_t n = firstMarkedBit(value); + uint32_t n = firstMarkedBit(value); clearBit(value, n); return n; } @@ -248,7 +258,7 @@ struct BitSet64 { inline uint32_t markFirstUnmarkedBit() { return markFirstUnmarkedBit(value); } static inline uint32_t markFirstUnmarkedBit(uint64_t& value) { - uint64_t n = firstUnmarkedBit(value); + uint32_t n = firstUnmarkedBit(value); markBit(value, n); return n; } @@ -258,7 +268,7 @@ struct BitSet64 { inline uint32_t clearLastMarkedBit() { return clearLastMarkedBit(value); } static inline uint32_t clearLastMarkedBit(uint64_t& value) { - uint64_t n = lastMarkedBit(value); + uint32_t n = lastMarkedBit(value); clearBit(value, n); return n; } @@ -268,7 +278,7 @@ struct BitSet64 { inline uint32_t getIndexOfBit(uint32_t n) const { return getIndexOfBit(value, n); } static inline uint32_t getIndexOfBit(uint64_t value, uint32_t n) { - return __builtin_popcountll(value & ~(0xffffffffffffffffULL >> n)); + return static_cast(__builtin_popcountll(value & ~(0xffffffffffffffffULL >> n))); } inline bool operator== (const BitSet64& other) const { return value == other.value; }