This is an archive of the discontinued LLVM Phabricator instance.

[PPC64] Use INT32_MIN instead of std::numeric_limits<int32_t>::min()
ClosedPublic

Authored by MaskRay on Nov 7 2018, 12:10 AM.

Details

Summary

D53821 fixed the bogus MSVC (at least 2017) C4146 warning (unary minus applied on unsigned type)
by using std::numeric_limits<int32_t>::min().
The warning was because -2147483648 is incorrectly treated as unsigned long instead of long long)

Let's use INT32_MIN which is arguably more readable.
Note, on GCC or clang, -0x80000000 works fine (ILP64: long, LP64: long long).

Diff Detail

Repository
rL LLVM

Event Timeline

MaskRay created this revision.Nov 7 2018, 12:10 AM
MaskRay updated this revision to Diff 172903.Nov 7 2018, 12:21 AM
MaskRay edited the summary of this revision. (Show Details)

Update description

I'm honestly not really convinced that -0x80000000LL is more readable. I think it's a matter of personal taste. I think numeric_limits<int32_t>::min() is more explicit about what it's aiming to achieve. That being said, I really don't care that much, so if somebody else gives a LGTM, I don't mind.

I agree with James that numeric_limits is clearer. I've run into bugs before because there was a 0/f missing when trying to use max/min value integers.

Is the problem here that we are missing a cast to int64_t?

Is the problem here that we are missing a cast to int64_t?

Not sure if you're referring to the original problem, but I assume you are. The original issue was highlighting a bug in MSVC's C++11 compatibility, as the type of the (now replaced) integer literal should be a signed long long automatically (effectively an int64_t), but instead it became an unsigned int. Casting it to an int64_t explicitly would also have worked, I think.

ruiu added a comment.Nov 7 2018, 5:40 AM

My preference is INT32_MIN which works both in C and C++, but I don't think 0x80000000 is more readable than numeric_limits<int32_t>::min(). Honestly it looks a bit more like an exercise to see if you really understand how negative numbers are represented in the two's complement, I think.

My preference is INT32_MIN which works both in C and C++, but I don't think 0x80000000 is more readable than numeric_limits<int32_t>::min(). Honestly it looks a bit more like an exercise to see if you really understand how negative numbers are represented in the two's complement, I think.

I have no objection to INT32_MIN, as it's clear as to the purpose.

MaskRay updated this revision to Diff 172981.Nov 7 2018, 10:17 AM
MaskRay retitled this revision from [PPC64] Use -0x80000000LL instead of std::numeric_limits<int32_t>::min() to [PPC64] Use INT32_MIN instead of std::numeric_limits<int32_t>::min().
MaskRay edited the summary of this revision. (Show Details)

Use INT32_MIN

This revision is now accepted and ready to land.Nov 7 2018, 11:21 AM
This revision was automatically updated to reflect the committed changes.