Re: [PATCH 3/4] lib: vsprintf: Optimize put_dec_trunc8

From: Michal Nazarewicz
Date: Mon Sep 24 2012 - 11:06:30 EST


On Mon, Sep 24 2012, George Spelvin wrote:
> Michal Nazarewicz <mpn@xxxxxxxxxx> wrote:
>> static noinline_for_stack
>> char *put_dec_trunc8(char *buf, unsigned r) {
>> unsigned q;
>>
>> if (r > 10000) {
>> do {
>> q = r + '0';
>> r = (r * (uint64_t)0x1999999a) >> 32;
>> *buf++ = q - 10 * r;
>> } while (r >= 10000);
>> if (r == 0)
>> return buf;
>> }
>>
>> q = (r * 0x199a) >> 16;
>> *buf++ = (r - 10 * q) + '0'; /* 6 */
[...]
>> return buf;
>> }
>
> Two bugs:
>
> 1) The initial "(r > 10000)" should be >=.
> If you let r == 10000 through to the remaining code, you'll get
> ":000".

Obviously... ;)

>
> 2) The "r == 0" test isn't necessary.
> Given that the loop divides r by 10 each time, r >= 10000 at the
> beginning implies r >= 1000 at the end, so 1000 <= r < 10000
> when the loop exits.

Yeah, I've just figured that out. :]
--
Best regards, _ _
.o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o
..o | Computer Science, MichaÅ âmina86â Nazarewicz (o o)
ooo +----<email/xmpp: mpn@xxxxxxxxxx>--------------ooO--(_)--Ooo--

Attachment: pgp00000.pgp
Description: PGP signature