Bug 1432646 - Followup bustage fix for compilers that warn (error with -Werror-alike) when negating an unsigned expression. r=bustage in a CLOSED TREE

This commit is contained in:
Jeff Walden 2018-01-27 00:25:40 -08:00
parent 92624b2025
commit 51f84c53a8

View File

@ -80,6 +80,16 @@ TestIsPowerOfTwo()
static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX)), "0xffffffffffffffff isn't a power of two");
}
// NOTE: In places below |-FOO_MAX - 1| is used instead of |-FOO_MIN| because
// in C++ numeric literals are full expressions -- the |-| in a negative
// number is technically separate. So with most compilers that limit
// |int| to the signed 32-bit range, something like |-2147483648| is
// operator-() applied to an *unsigned* expression. And MSVC, at least,
// warns when you do that. (The operation is well-defined, but it likely
// doesn't do what was intended.) So we do the usual workaround for this
// (see your local copy of <stdint.h> for a likely demo of this), writing
// it out by negating the max value and subtracting 1.
static_assert(WrapToSigned(uint8_t(17)) == 17,
"no wraparound should work, 8-bit");
static_assert(WrapToSigned(uint8_t(128)) == -128,
@ -100,12 +110,12 @@ static_assert(WrapToSigned(uint16_t(32768 + 32767)) == -32768 + 32767,
static_assert(WrapToSigned(uint32_t(8675309)) == 8675309,
"no wraparound should work, 32-bit");
static_assert(WrapToSigned(uint32_t(2147483648)) == -2147483648,
static_assert(WrapToSigned(uint32_t(2147483648)) == -2147483647 - 1,
"works for 32-bit numbers, wraparound low end");
static_assert(WrapToSigned(uint32_t(2147483648 + 42)) == -2147483648 + 42,
static_assert(WrapToSigned(uint32_t(2147483648 + 42)) == -2147483647 - 1 + 42,
"works for 32-bit numbers, wraparound mid");
static_assert(WrapToSigned(uint32_t(2147483648 + 2147483647)) ==
-2147483648 + 2147483647,
-2147483647 - 1 + 2147483647,
"works for 32-bit numbers, wraparound high end");
static_assert(WrapToSigned(uint64_t(4152739164)) == 4152739164,