Re: [PATCH v2 1/4] staging: iio: adt7316: refactor temperature calculation logic

From: Andy Shevchenko

Date: Fri Mar 06 2026 - 10:00:52 EST


On Thu, Mar 05, 2026 at 11:16:58PM -0800, Michael Harris wrote:
> Replace the manual sign manipulation with sign_extend32() and change the
> affected variable from u16 to s32 to properly handle negative values.
>
> Resolve a logic error where the sign bit was being checked at bit 10
> instead of bit 9 for a 10-bit value.
>
> Convert the data variable to be in centidegrees celsius (0.25 C per bit)
> so we can use simple division and modulo for sysfs_emit() instead of the
> convoluted bit shifting and masking.

Sounds like a candidate for Fixes tag.

...

> - return sysfs_emit(buf, "%c%d.%.2d\n", sign,
> - (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
> - (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
> + return sysfs_emit(buf, "%s%d.%02u\n",
> + (data < 0 ? "-" : ""),
> + abs(data / 100),
> + abs(data % 100));

Hmm... If you create a temporary variable for abs(data), this might give
slightly different code generation, presumably better on (some) architectures.
/ % combined maybe a single assembly instruction.

Or keep sign in a char as it was in the original code and just move data to be
abs(data).

--
With Best Regards,
Andy Shevchenko