On 2026-07-26 17:13:04+0700, Ammar Faizi wrote:
(...)

> Cc: Alviro Iskandar Setiawan <[email protected]>
> Signed-off-by: Ammar Faizi <[email protected]>
> ---
>  tools/testing/selftests/nolibc/nolibc-test.c | 47 ++++++++++++++++++++
>  1 file changed, 47 insertions(+)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c 
> b/tools/testing/selftests/nolibc/nolibc-test.c
> index 996e8d13508e..4dc2ffea63bd 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -1728,6 +1728,52 @@ int test_alloca(void)
>       return *x - 0x1234;
>  }
>  
> +/* abs(), labs() and llabs() over the whole range of their argument type */
> +int test_abs_range(void)
> +{
> +     int i, ri;
> +     long l, rl;
> +     long long ll, rll;

Reverse xmas?

> +
> +     /*
> +      * Both the inputs and the results have to stay opaque: the compiler
> +      * knows abs() and friends never return a negative value and would
> +      * otherwise fold the comparisons below at build time, which would also
> +      * hide the undefined behavior that is being tested for.
> +      */
> +     i = INT_MIN; l = LONG_MIN; ll = LLONG_MIN;
> +     __asm__ ("" : "+r" (i), "+r" (l), "+r" (ll));

We have _NOLIBC_OPTIMIZER_HIDE_VAR() for this.
Maybe it can be made variadic.

> +     ri = abs(i); rl = labs(l); rll = llabs(ll);
> +     __asm__ ("" : "+r" (ri), "+r" (rl), "+r" (rll));
> +     /* the absolute value is not representable, the input is returned */
> +     if (ri != INT_MIN || rl != LONG_MIN || rll != LLONG_MIN)
> +             return 1;

(...)

Reply via email to