This is an archive of the discontinued LLVM Phabricator instance.

[libcxx][NFC] Tidy up calculation of __nbuf in num_put::do_put, and add comments
ClosedPublic

Authored by DanielMcIntosh-IBM on May 28 2021, 1:37 PM.

Details

Summary

In 07ef8e679621 and 3ed9f6ebdeeb, __nbuf started to diverge from the amount
of space that was actually needed for the buffer. For 32-bit longs for example,
we allocate a buffer that is one larger than needed. Moreover, it is no longer
clear exactly where the extra +1 or +2 comes from - they're just numbers pulled
from thin air. This PR cleans up how __nbuf is calculated, and adds comments
to further clarify where each part comes from.

Specifically, it corrects the underestimation of the max size buffer needed
that the above two commits had to compensate for. The root cause looks to be
the use of signed type parameters to numeric_limits<>::digits. Since digits
only counts non-sign bits, the calculation was acting as though (for a signed
64-bit type) the longest value we would print was 2^63 in octal. However,
printing in octal treats values as unsigned, so it is actually 2^64. Thus,
using unsigned types and changing the final +2 to a +1 is probably a better
option.

Diff Detail

Event Timeline

DanielMcIntosh-IBM requested review of this revision.May 28 2021, 1:37 PM
DanielMcIntosh-IBM created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptMay 28 2021, 1:37 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
DanielMcIntosh-IBM edited the summary of this revision. (Show Details)May 28 2021, 1:39 PM

Nice catch! Can you rebase the patch to rerun the CI?

libcxx/include/locale
1466

Wouldn't it look better to do the rounded up character determination in one step?
((numeric_limits<unsigned long>::digits + 2) / 3)

ldionne accepted this revision.May 31 2021, 1:45 PM
ldionne added a subscriber: ldionne.

Thanks for fixing this! The CI is failing due to something that has been fixed now. Please rebase onto main and re-upload your diff, that will run CI again and everything should be green.

libcxx/include/locale
1466

IMO it's easier to read as-is since the + 2 trick isn't immediately obvious (to me at least).

This revision is now accepted and ready to land.May 31 2021, 1:45 PM
Mordante added inline comments.May 31 2021, 11:12 PM
libcxx/include/locale
1466

I thought this was very common idiom for rounding up. But if it's not obvious for everybody, then I'm in favour of keeping the code as is.

Mordante accepted this revision.Jun 1 2021, 11:04 PM

rebase

Thanks, LGTM!