This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add support for 128 bit ints in limits.h
ClosedPublic

Authored by michaelrj on Sep 28 2021, 11:27 AM.

Details

Summary

Also, this adds unit tests to check that limits.h complies with the C
standard.

Diff Detail

Event Timeline

michaelrj created this revision.Sep 28 2021, 11:27 AM
michaelrj requested review of this revision.Sep 28 2021, 11:27 AM
sivachandra added inline comments.Sep 28 2021, 11:40 AM
libc/test/utils/CPP/limits_test.cpp
16

Instead of literals, we should use the macros like INT_MAX etc. Then, you can make these to be equality checks. The test might seem trivial at that point - we are probably only testing for copy-paste errors. But, the macros make it more robust against platform differences.

libc/utils/CPP/Limits.h
55

For completeness, should we add the limits of the signed __int128_t type?

58

Can this be ~__uint128_t(0)?

michaelrj marked an inline comment as done.

change max and add support for signed ints, although it's commented out currently due to the trest framework not supporting it.

libc/test/utils/CPP/limits_test.cpp
16

right now this is comparing against the C spec, so if they fail then the platform has not implemented its integers correctly. I can change it to use the macros, but then it's not really testing anything, other than that the macros equal themselves.

libc/utils/CPP/Limits.h
55

That would be non-trivial since the test framework doesn't support __int128_t right now.

abrachet added inline comments.
libc/utils/CPP/Limits.h
55

Just need to add a new Test::test instantiation for __int128_t like in LibcTest.cpp:245

sivachandra accepted this revision.Sep 28 2021, 1:13 PM
sivachandra added inline comments.
libc/test/utils/CPP/limits_test.cpp
16

I don't disagree with your reasoning. My point is that this test is really protecting us against copy-paste errors. For example, check for int limits is satisfied by all other signed integer types. So, we are not really protecting ourselves from copy-paste errors. I can totally see the other way round problem also - the test itself has copy paste errors. So, up to you.

Whether the platform is implementing the C spec around integer sizes is beyond the scope of the libc I would think.

libc/utils/CPP/Limits.h
55

You will have to add a describeValue overload also like this: https://github.com/llvm/llvm-project/blob/main/libc/utils/UnitTest/LibcTest.cpp#L47

I will leave it up to you if you want to do it all in this change or just add a TODO in this change along with the commented out code.

63

May be just ~__uint128_t(0) - 1?

This revision is now accepted and ready to land.Sep 28 2021, 1:13 PM
michaelrj updated this revision to Diff 375683.Sep 28 2021, 1:30 PM
michaelrj marked 6 inline comments as done.

get signed 128 bit integers working.

libc/test/utils/CPP/limits_test.cpp
16

Fair enough, I've changed it to use the macros.

libc/utils/CPP/Limits.h
55

Done, and I also had to add __int128_t to TypeTraits.h.

63

That doesn't work as int max, since it comes out to 0xfffffffffffffffffffffffffffffffe, as opposed to the 0x7fffffffffffffffffffffffffffffff. I've changed it to be the most logical way I can think of that doesn't rely on overflow/underflow.

sivachandra accepted this revision.Sep 28 2021, 2:09 PM
sivachandra added inline comments.
libc/utils/CPP/Limits.h
63

Uh, sorry! Not sure why I put a - instead of a >>. I wanted to say this:

return ~__uint128_t(0) >> 1;
libc/utils/UnitTest/LibcTest.cpp
57

Can we extract into a common function like this:

template <typename T128>
cpp::EnableIfType<cpp::IsSame<T128, __int128_t> || cpp::IsSame<T128, __uint128_t>, std::string>
describeValue128(...) {
  ...
}

template <> std::string describeValue<__int128_t>(__int128_t Value) { return describeValue128(Value); }
template <> std::string describeValue<__uint128_t>(__uint128_t Value) { return describeValue128(Value); }
michaelrj updated this revision to Diff 375717.Sep 28 2021, 3:08 PM
michaelrj marked an inline comment as done.

clean up a few remaining things

michaelrj marked an inline comment as done.Sep 28 2021, 3:08 PM
michaelrj added inline comments.
libc/utils/CPP/Limits.h
63

ah, yes that does work.

This revision was landed with ongoing or failed builds.Sep 28 2021, 4:50 PM
This revision was automatically updated to reflect the committed changes.