Re: [PATCH v12 05/11] iio: core: add decimal value formatting into 64-bit value

From: Rodrigo Alencar

Date: Tue May 12 2026 - 15:05:54 EST


On 26/05/12 08:49PM, Andy Shevchenko wrote:
> On Tue, May 12, 2026 at 05:09:32PM +0100, Rodrigo Alencar wrote:
> > On 26/05/12 05:35PM, Andy Shevchenko wrote:
> > > On Sun, May 10, 2026 at 01:42:23PM +0100, Rodrigo Alencar via B4 Relay wrote:
> > >
> > > > Create new format types for iio values (IIO_VAL_DECIMAL64_*), which
> > > > defines the representation of fixed decimal point values into a single
> > > > 64-bit number. This new format increases the range of represented values,
> > > > allowing for integer parts greater than 2^32, as bits are not "wasted"
> > > > in the fractional part, which can be seen in IIO_VAL_INT_PLUS_MICRO and
> > > > IIO_VAL_INT_PLUS_NANO. Helpers are created to compose and decompose 64-bit
> > > > decimals into integer values used in IIO formatting interfaces, which
> > > > creates consistency and avoid error-prone manual assignments when using
> > > > wordpart macros. When doing the parsing, kstrtodec64() is used with the
> > > > scale defined by the specific decimal format type.
>
> ...
>
> > > > + tmp2 = div64_s64_rem(iio_val_s64_from_array(vals),
> > > > + int_pow(10, scale), &frac);
> > > > + if (tmp2 == 0 && frac < 0)
> > > > + return sysfs_emit_at(buf, offset, "-0.%0*lld", scale,
> > > > + abs(frac));
> > > > + else
> > > > + return sysfs_emit_at(buf, offset, "%lld.%0*lld", tmp2,
> > > > + scale, abs(frac));
> > > > + }
> > >
> > > What about
> > >
> > > /* Print a leading '-' for negative fractions */
> > > if (tmp2 == 0 && frac < 0)
> > > offset += sysfs_emit_at(buf, offset, "-");
> > >
> > > return sysfs_emit_at(buf, offset, "%lld.%0*lld", tmp2, scale, abs(frac));
> > >
> > > Also note this won't work with the frac that are == S64_MIN. It's UB (undefined
> > > behaviour), see the comment at abs() implementation. Maybe a time to add abs()
> > > corner case tests...
> >
> > frac cannot be S64_MIN, it is always and remainder of a power of 10 modulus.
>
> Okay, but what about input of -0.9999999999999999999 ? Will it fit the signed
> frac type?

For the scales considered here it would not be a problem (*_PICO = 12 + *_BASE).
For the max scale of 19 it would probably fail the parsing of the fractional part
with overflow.

--
Kind regards,

Rodrigo Alencar