From 51f84c53a836830d5f3f561c17242b70f010b5ed Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Sat, 27 Jan 2018 00:25:40 -0800 Subject: [PATCH] Bug 1432646 - Followup bustage fix for compilers that warn (error with -Werror-alike) when negating an unsigned expression. r=bustage in a CLOSED TREE --- mfbt/tests/TestMathAlgorithms.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mfbt/tests/TestMathAlgorithms.cpp b/mfbt/tests/TestMathAlgorithms.cpp index a898bf3d1788..3dd26a5e7b59 100644 --- a/mfbt/tests/TestMathAlgorithms.cpp +++ b/mfbt/tests/TestMathAlgorithms.cpp @@ -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 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,