mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Rodrigo Alencar <455.rodrigo.alencar@gmail.com>,
	David Laight <David.Laight@aculab.com>
Cc: "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:11:05 +0200	[thread overview]
Message-ID: <auGuGfSqa-D9sceB@ashevche-desk.local> (raw)
In-Reply-To: <25g6fr7y4bmd6bzomvlvoejtps7wzbnbuuxxipmcrnozujno2l@klcfyrmqsifd>

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.

> /**
>  * 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)

-- 
With Best Regards,
Andy Shevchenko



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

Thread overview: 19+ 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 [this message]
2026-10-28 10:31       ` David Laight
2026-10-28 11:16       ` David Laight
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=auGuGfSqa-D9sceB@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=455.rodrigo.alencar@gmail.com \
    --cc=David.Laight@aculab.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®