[PATCH] D144218: [Clang] [AVR] Fix USHRT_MAX for 16-bit int.

Daniel Thornburgh via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 22 15:40:15 PST 2023


mysterymath marked an inline comment as done.
mysterymath added inline comments.


================
Comment at: clang/lib/Headers/limits.h:55
 #define UCHAR_MAX (__SCHAR_MAX__*2  +1)
-#define USHRT_MAX (__SHRT_MAX__ *2  +1)
+#define USHRT_MAX (__SHRT_MAX__ * 2U + 1U)
 #define UINT_MAX  (__INT_MAX__  *2U +1U)
----------------
aaron.ballman wrote:
> It's worth double-checking that this still gives the correct type for the macro:
> 
> C2x 5.2.4.2.1p2: For all unsigned integer types for which <limits.h> or <stdint.h> define a macro with suffix _WIDTH holding its width N, there is a macro with suffix _MAX holding the maximal value 2N − 1 that is representable by the type and that has the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. ...
Ah, thanks; it hadn't occurred to me that the type of the expression would be specified in the standard. It could be either `unsigned int` or `int`, depending on the target.

The most straightforward approach I could think of to produce the right type is:
1) Perform the arithmetic in `unsigned int` to produce the right value
2) Cast to `unsigned short` to produce the right type
3) Directly perform integer promotion using unary plus

The use of unary plus is a bit odd here, but it seems like the most direct way to express the logic, and the overall expression seems less fragile than the `#if` alternative. I've added a comment to explain this as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144218/new/

https://reviews.llvm.org/D144218



More information about the cfe-commits mailing list