Also, this adds unit tests to check that limits.h complies with the C
standard.
Details
- Reviewers
sivachandra - Commits
- rGb62d72f3c542: [libc] Add support for 128 bit ints in limits.h
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
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)? |
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. |
libc/utils/CPP/Limits.h | ||
---|---|---|
55 | Just need to add a new Test::test instantiation for __int128_t like in LibcTest.cpp:245 |
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? |
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. |
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); } |
libc/utils/CPP/Limits.h | ||
---|---|---|
63 | ah, yes that does work. |
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.