Re: [PATCH 2/4] tools/nolibc: stdlib: avoid signed overflow in abs() and friends

From: Thomas Weißschuh

Date: Mon Jul 27 2026 - 12:22:32 EST


On 2026-07-27 08:32:12+0100, David Laight wrote:
> On Sun, 26 Jul 2026 18:01:11 +0200
> Willy Tarreau <w@xxxxxx> wrote:
> > On Sun, Jul 26, 2026 at 03:13:34PM +0100, David Laight wrote:
> > > On Sun, 26 Jul 2026 17:13:03 +0700
> > > Ammar Faizi <ammarfaizi2@xxxxxxxxxxxxx> wrote:

(...)

> > but ~(j - 1) would work, just like
> > (~j + 1). However here the benefit of the casts in Ammar's
> > version is that it's obvious that it's only playing with same
> > size casts with no extra operation.
>
> They only work for 2s compliment.
> While that is normal (and required for the next? C version)
> it isn't actually required by C.
> (Not that gcc supports '1s compliment' or 'sign overpunch'.)

nolibc only supports Linux, which in turn only supports 2s
complement. We already have code in nolibc which relies on this.


Thomas