diff options
author | 2017-10-09 13:37:50 -0700 | |
---|---|---|
committer | 2017-10-09 13:40:53 -0700 | |
commit | 2f366292b51789bcecb1bde53e07d7f78802bc4f (patch) | |
tree | d18ad8237837f1716706a074e738076842f9b212 /runtime/base/bit_utils.h | |
parent | 797e6d4d2a2786da42c20a718723a72038f7a01c (diff) |
base: Fix integer conversion in MaskLeastSignificant, add more asserts
T x = (1 << bits) was being truncated for sizeof(T) > sizeof(bits).
Also add more static_asserts to BITSTRUCT_DEFINE_END to make it more
error-proof.
Test: make test-art-{host,target}-gtest-bit_{struct,utils}_test{32,64}
Change-Id: Ifedf53c1211b4a9492ebd785c321a39d906dc87a
Diffstat (limited to 'runtime/base/bit_utils.h')
-rw-r--r-- | runtime/base/bit_utils.h | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/runtime/base/bit_utils.h b/runtime/base/bit_utils.h index da3c7048b6..5d836545e9 100644 --- a/runtime/base/bit_utils.h +++ b/runtime/base/bit_utils.h @@ -388,7 +388,8 @@ inline static constexpr std::make_unsigned_t<T> MaskLeastSignificant(size_t bits if (bits >= BitSizeOf<T>()) { return std::numeric_limits<unsigned_T>::max(); } else { - return static_cast<unsigned_T>((1 << bits) - 1); + auto kOne = static_cast<unsigned_T>(1); // Do not truncate for T>size_t. + return static_cast<unsigned_T>((kOne << bits) - kOne); } } |