[PATCH] stdlib: Implement C2Y uabs, ulabs, ullabs and uimaxabs

Andrew Pinski pinskia@gmail.com
Fri Mar 7 20:59:04 GMT 2025


On Fri, Mar 7, 2025, 12:48 PM Paul Eggert <eggert@cs.ucla.edu> wrote:

> On 2025-03-07 12:30, Lenard wrote:
> > The rationale for using ckd_mul in N3349 was portability.
>
> I see, but at least use ckd_sub as that's more natural than ckd_mul.
>
> Also, it's nicer to have the source code match what the machine code
> does, and as there is no conditional branch at the machine level there
> is no need for an "if" statement at the source level. Perhaps something
> like the following, which generates the same instructions:
>
>    unsigned int
>    uabs (int i)
>    {
>      unsigned int u;
>      __builtin_sub_overflow (0, i, &u);
>      return i < 0 ? u : i;
>    }
>

Not all targets have conditional moves.
So just do
unsigned int u = i;
return i < 0 ? - u : u;

Since unsigned is defined as wrapping and is always the same size as signed
and for glibc targets a power of 2 size, this is always correct.

Using sub_overflow seems overly complex for no gain and simple is better.


>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250307/ef4fbf7c/attachment-0001.htm>


More information about the Libc-alpha mailing list