Re: [PATCH] iio: hid-sensors: fix fractional part for unit exponents -7 and -8

From: Andy Shevchenko

Date: Tue Nov 03 2026 - 06:06:09 EST


On Mon, Nov 02, 2026 at 02:23:08PM -0800, Srinivas Pandruvada wrote:
> split_micro_fraction() scales the remainder to micro units by
> multiplying it by 10^(6 - exp). The unit exponent of a HID field goes
> down to -8, so exp can be 7 or 8 here. Then the argument of int_pow()
> is negative, which the unsigned parameter turns into a huge power of
> ten: 0 modulo 2^64. As a result, the fractional part of a value read
> from the hysteresis attributes is always reported as 0.
>
> Divide by 10^(exp - 6) in these two cases. This matches the reverse
> conversion in convert_to_vtf_format().

...

> *val1 = no / divisor;
> - *val2 = no % divisor * int_pow(10, 6 - exp);
> + if (exp > 6)
> + *val2 = no % divisor / int_pow(10, exp - 6);
> + else
> + *val2 = no % divisor * int_pow(10, 6 - exp);

Please, make no % divisor to be in a temporary variable. It may lead to

foo = var / div;
bar = var % div;

compiled to the single assembly instruction (on x86, for example).

--
With Best Regards,
Andy Shevchenko