All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: "Rodrigo Alencar" <455.rodrigo.alencar@gmail.com>,
	"David Laight" <David.Laight@aculab.com>,
	"Victor Chernyakin" <chernyakin.victor.j@outlook.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/4] iio: prepare iio_format_value to handle higher-precision IIO_VAL_DECIMAL64_* formats
Date: Wed, 28 Oct 2026 10:31:05 +0000	[thread overview]
Message-ID: <20261028103105.0053c079@pumpkin> (raw)
In-Reply-To: <auGuGfSqa-D9sceB@ashevche-desk.local>

On Wed, 28 Oct 2026 10:11:05 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Tue, Oct 27, 2026 at 03:56:35PM -0300, Rodrigo Alencar wrote:
> > On 26/10/26 11:51, Victor Chernyakin wrote:  
> 
> +Cc: David Laight
> 
> ...
> 
> > > +/*
> > > + * Regular abs(LLONG_MIN) causes signed overflow, which is undefined
> > > + * behavior. This variant avoids that by performing the computation in an
> > > + * unsigned type, for which negation is always well-defined.
> > > + */
> > > +static inline u64 abs_s64_no_overflow(s64 x)
> > > +{
> > > +	return x < 0 ? -(u64)x : (u64)x;
> > > +}  
> > 
> > I think I've seen Andy doing mentions to a needed 'safe' abs() a couple of times.
> > Don't we have something in the tree already? if not, Why not adding a
> > 'unsigned' abs function to math.h? something like:  
> 
> Or maybe something clever using the s64/u64 domain for all.
> Let's gather David Laight's opinion.

Well, it might be UB, but that is probably mostly related to '1s compliment'
and 'sign overpunch' number schemes.
So, in practise (u64)(x < 0 ? -x : x) probably always gives the right answer.
The kernel is also compiled with -Wno-strict-overflow which also stops the
compiler doing unexpected things.
OTOH clang is know for 'silently just throwing away code' if it detects UB
behaviour, so code that is fine with variables can get miscompiled for
constants.
(All not helped by it being undefined behaviour, not undefined result.
And the 'behaviour' bit really being 'might trap'.)

This should work for all types (except s128 - not used in the kernel):
#define abs(x) ((x) >= 0 ? (x) : (u64)-((x) + 1) + 1)
or maybe the equivalent:
#define abs(x) ((x) >= 0 ? (x) : -((x) + 1) + 1ull)
which will generate the correct bit-pattern for s128.

The negate is always well defined and its result in non-negative.

gcc and clang also realise that the (u64) cast always zero extends
so can be optimised away for 32bit values (especially relevant for
32bit compiles).
Note that optimisation may not happen (I've not checked) if you use
a real function with an s64 argument.
(Which also has a downside if code passes a very large unsigned value.)

Oh, and the compilers realise the '+ 1' cancel out and throw them away.

David

> 
> > /**
> >  * uabs - return absolute value of an argument as an unsigned type
> >  * @x: the value.
> >  *
> >  * Unlike abs(), the result is well defined when @x is the minimum value of
> >  * a signed type, since the negation is done in the unsigned domain.
> >  *
> >  * Return: an absolute value of @x, as the unsigned counterpart of its type.
> >  */
> > #define uabs(x)	__uabs_choose_expr(x, long long,			\
> > 		__uabs_choose_expr(x, long,				\
> > 		__uabs_choose_expr(x, int,				\
> > 		__uabs_choose_expr(x, short,				\
> > 		__uabs_choose_expr(x, char,				\
> > 		__builtin_choose_expr(					\
> > 			__builtin_types_compatible_p(typeof(x), char),	\
> > 			({ signed char __x = (x);			\
> > 			   (unsigned char)(__x < 0 ? -(unsigned char)__x :	\
> > 						     (unsigned char)__x); }),	\
> > 			((void)0)))))))
> > 
> > #define __uabs_choose_expr(x, type, other) __builtin_choose_expr(	\
> > 	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
> > 	__builtin_types_compatible_p(typeof(x), unsigned type),		\
> > 	({ signed type __x = (x);					\
> > 	   (unsigned type)(__x < 0 ? -(unsigned type)__x :		\
> > 				     (unsigned type)__x); }), other)  
> 


  reply	other threads:[~2026-10-28 10:31 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26 16:51 [PATCH v3 0/4] iio: cdc: add Texas Instruments FDC1004 driver Victor Chernyakin
2026-10-26 16:51 ` [PATCH v3 1/4] iio: prepare iio_format_value to handle higher-precision IIO_VAL_DECIMAL64_* formats Victor Chernyakin
2026-10-26 20:17   ` Andy Shevchenko
2026-10-27  1:51     ` Victor Chernyakin
2026-10-27  7:29       ` Andy Shevchenko
2026-10-27  8:36         ` Victor Chernyakin
2026-10-27 18:56   ` Rodrigo Alencar
2026-10-28  8:11     ` Andy Shevchenko
2026-10-28 10:31       ` David Laight [this message]
2026-10-26 16:51 ` [PATCH v3 2/4] iio: add IIO_VAL_DECIMAL64_{ATTO,ZEPTO,YOCTO} formats Victor Chernyakin
2026-10-26 16:51 ` [PATCH v3 3/4] dt-bindings: iio: cdc: add Texas Instruments FDC1004 Victor Chernyakin
2026-10-26 16:51 ` [PATCH v3 4/4] iio: cdc: add Texas Instruments FDC1004 driver Victor Chernyakin
2026-10-26 20:13   ` Andy Shevchenko
2026-10-27  2:13     ` Victor Chernyakin
2026-10-27  7:33       ` Andy Shevchenko
2026-10-27 10:04         ` Victor Chernyakin
2026-10-27  8:17   ` Joshua Crofts
2026-10-27 10:06     ` Victor Chernyakin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261028103105.0053c079@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=455.rodrigo.alencar@gmail.com \
    --cc=David.Laight@aculab.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=chernyakin.victor.j@outlook.com \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.