-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Fix definition of UINT_MAX
in limits.h
#95279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary: Currently we use `(~0U)` for this definition, however the ~ operator returns a different sign, meaning that preprocessor checks against this value will fail. See https://godbolt.org/z/TrjaY1d8q where the preprocessor thinks that it's not `0xffffffff` while the static assertion thinks it is. This is because the latter does implicit conversion but the preprocessor does not. This is now consistent with other headers.
@llvm/pr-subscribers-libc Author: Joseph Huber (jhuber6) ChangesSummary: Full diff: https://github.com/llvm/llvm-project/pull/95279.diff 1 Files Affected:
diff --git a/libc/include/llvm-libc-macros/limits-macros.h b/libc/include/llvm-libc-macros/limits-macros.h
index 95f0f5f0baa58..3fab996b61ac9 100644
--- a/libc/include/llvm-libc-macros/limits-macros.h
+++ b/libc/include/llvm-libc-macros/limits-macros.h
@@ -148,7 +148,7 @@
#endif // INT_MAX
#ifndef UINT_MAX
-#define UINT_MAX (~0U)
+#define UINT_MAX (INT_MAX * 2U + 1U)
#endif // UINT_MAX
#ifndef LONG_MAX
@@ -160,7 +160,7 @@
#endif // LONG_MAX
#ifndef ULONG_MAX
-#define ULONG_MAX (~0UL)
+#define ULONG_MAX (LONG_MAX * 2UL + 1UL)
#endif // ULONG_MAX
#ifndef LLONG_MAX
@@ -172,7 +172,7 @@
#endif // LLONG_MAX
#ifndef ULLONG_MAX
-#define ULLONG_MAX (~0ULL)
+#define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL)
#endif // ULLONG_MAX
// *_MIN macros
|
@@ -148,7 +148,7 @@ | |||
#endif // INT_MAX | |||
|
|||
#ifndef UINT_MAX | |||
#define UINT_MAX (~0U) | |||
#define UINT_MAX (INT_MAX * 2U + 1U) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we explicitly cast ((unsigned) INT_MAX)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be handled by the 2U
part, otherwise it would fail in the myriad templates we use in limits.h
I believe. This is how Clang's implementation does it at least.
What's the issue?
compiles. |
It's explained in the summary, |
I see. Perhaps just |
Some targets have |
Summary:
Currently we use
(~0U)
for this definition, however the ~ operatorreturns a different sign, meaning that preprocessor checks against this
value will fail. See https://godbolt.org/z/TrjaY1d8q where the
preprocessor thinks that it's not
0xffffffff
while the staticassertion thinks it is. This is because the latter does implicit
conversion but the preprocessor does not. This is now consistent with
other headers.