HomePhabricator

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

Authored by DanielMcIntosh-IBM on Jun 2 2021, 8:44 AM.

Description

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

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.

Reviewed By: #libc, ldionne, Mordante

Differential Revision: https://reviews.llvm.org/D103339

Details

Committed
abhina.sreeskantharajanJun 2 2021, 8:46 AM
Reviewer
Restricted Project
Differential Revision
D103339: [libcxx][NFC] Tidy up calculation of __nbuf in num_put::do_put, and add comments
Parents
rG5ecfdb51230c: [lld/mac] try to fix tests after a5645513dba
Branches
Unknown
Tags
Unknown