This is an archive of the discontinued LLVM Phabricator instance.

[libc] Define long long limits if not defined
ClosedPublic

Authored by abrachet on Aug 4 2023, 8:44 AM.

Details

Summary

Some older gcc toolchains don't define these on 32 bit platforms. This
is a problem for pigweed which uses an older gcc toolchain and targets
32 bit.

Diff Detail

Event Timeline

abrachet created this revision.Aug 4 2023, 8:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2023, 8:44 AM
abrachet requested review of this revision.Aug 4 2023, 8:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2023, 8:44 AM

Unfortunately that won't work for gcc whose limits.h https://gnu.googlesource.com/gcc/+/refs/heads/trunk/gcc/glimits.h doesn't use include_next. The compiler header will be chosen first.

alright, in that case since this is exclusively for internal use then I'm okay with having our own definitions for these constants. I'd prefer to use constexpr variables for this instead of macros, since those provide better type checking.

src/__support/CPP/limits.h
16 ↗(On Diff #547236)

While in almost every case this is safe, I'd still prefer not to assume that long long is 64 bits. Either (sizeof(long long) * 8) - 1 or some other way to calculate the size would be better.

abrachet updated this revision to Diff 554727.Aug 30 2023, 8:05 AM
abrachet marked an inline comment as done.
lntue added inline comments.Aug 30 2023, 8:25 AM
libc/src/__support/CPP/limits.h
19

This constant name in the header is a bit too generic. Maybe you can name it:

constexpr size_t LLONG_BIT_WIDTH = sizeof(long long) * 8;

and use LLONG_BIT_WIDTH - 1 instead of size_bits below.

Or we can add the templates BitWidth<T>::value = sizeof(T) * 8 and MostSignificantBitExponent<T>::value = BitWidth<T>::value - 1 or something like this, as they are actually used in many places.

abrachet updated this revision to Diff 554737.Aug 30 2023, 8:32 AM
abrachet marked an inline comment as done.
lntue accepted this revision.Aug 30 2023, 8:41 AM
This revision is now accepted and ready to land.Aug 30 2023, 8:41 AM
This revision was landed with ongoing or failed builds.Aug 30 2023, 4:04 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2023, 4:04 PM