[libc-commits] [PATCH] D125929: [libc] add printf base 10 integer conversion

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri May 20 11:16:40 PDT 2022

sivachandra added inline comments.

Comment at: libc/src/stdio/printf_core/int_converter.h:59
+  case LengthModifier::none:
+    num = num & type_mask<int>();
+    break;
michaelrj wrote:
> sivachandra wrote:
> > Why are these masking operations required? Also, for max values, we have: https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/Limits.h
> This is to handle the fact that the length modifiers allow the user to specify the size of the integer to be converted. The type in `num` is always `uintmax_t` since that can fit any valid type, but the user can specify that they want it to be treated as a `short`, in which case we have to limit the value of `num` to what can fit in a short. The way this is done here is by creating a mask of just the bits in the specified type.
Thanks for explaining. You can either use LLVM libc's own `NumericLimits` or directly use macros from the freestanding `limits.h` header. `NumericLimits` is probably more appropriate as you can get max values of `size_t` and `ptrdiff_t` is a straightforward way.

  rG LLVM Github Monorepo



More information about the libc-commits mailing list