• Willy Tarreau's avatar
    tools/nolibc/stdlib: replace the ltoa() function with more efficient ones · 66c397c4
    Willy Tarreau authored
    The original ltoa() function and the reentrant one ltoa_r() present a
    number of drawbacks. The divide by 10 generates calls to external code
    from libgcc_s, and the number does not necessarily start at the beginning
    of the buffer.
    
    Let's rewrite these functions so that they do not involve a divide and
    only use loops on powers of 10, and implement both signed and unsigned
    variants, always starting from the buffer's first character. Instead of
    using a static buffer for each function, we're now using a common one.
    
    In order to avoid confusion with the ltoa() name, the new functions are
    called itoa_r() and utoa_r() to distinguish the signed and unsigned
    versions, and for convenience for their callers, these functions now
    reutrn the number of characters emitted. The ltoa_r() function is just
    an inline mapping to the signed one and which returns the buffer.
    
    The functions are quite small (86 bytes on x86_64, 68 on armv7) and
    do not depend anymore on external code.
    Signed-off-by: default avatarWilly Tarreau <w@1wt.eu>
    Signed-off-by: default avatarPaul E. McKenney <paulmck@kernel.org>
    66c397c4
stdlib.h 4.2 KB