Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 16 Apr 2024 16:29:12 +0200
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Cc: Viktor Reznov <yann.collet.is.not.a.perfectionist@...il.com>
Subject: Re: [PATCH] Decreasing the number of divisions

Am Tue, Apr 16, 2024 at 04:29:05PM +0300 schrieb Viktor Reznov:
> diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
> index 497c5e19..0f9a1e6a 100644
> --- a/src/stdio/vfprintf.c
> +++ b/src/stdio/vfprintf.c
> @@ -165,8 +165,10 @@ static char *fmt_o(uintmax_t x, char *s)
>  static char *fmt_u(uintmax_t x, char *s)
>  {
>         unsigned long y;
> +       if (x == 0) return s;
>         for (   ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
> -       for (y=x;           y; y/=10) *--s = '0' + y%10;
> +       for (y=x;       y>=10; y/=10) *--s = '0' + y%10;
> +       *--s = '0' + y;
>         return s;
>  }

I played around with this change on godbolt: https://godbolt.org/z/9PoGK9zae

Seems to me like the version with OPTIMIZE=1 is longer and more
complicated.

OK, let's take a step back: What is the point of this patch? It makes
the code longer and less readable. It does not fix a logic bug. The only
reason I can see is that it makes the code "faster". In that case, I
would like to see a benchmark.

On x86-64, the loop condition on the first loop is always false, so the
unchanged function becomes a single loop. The changed function becomes a
selection statement, a loop, and an assignment. Don't see how that could
possibly be faster. But am willing to be convinced otherwise.

Ciao,
Markus

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.