Re: [PATCH v2] iio: chemical: scd30: Cleanup initializations in scd30_float_to_fp()

From: Maxwell Doose

Date: Tue May 26 2026 - 16:55:10 EST


On Tue, May 26, 2026 at 9:38 AM Jonathan Cameron <jic23@xxxxxxxxxx> wrote:
>
> On Mon, 25 May 2026 08:49:17 +0200
> Joshua Crofts <joshua.crofts1@xxxxxxxxx> wrote:
>
> > On Sun, 24 May 2026 at 04:04, Maxwell Doose <m32285159@xxxxxxxxx> wrote:
> > >
> > > Include linux/bitfield.h for FIELD_GET().
> > >
> > > Create new macros for bit manipulation in combination with manual bit
> > > manipulation being replaced with FIELD_GET().
> > >
> > > The current variable declaration and initializations are barely readable
> > > and use comma separations across multiple lines. Refactor the
> > > initializations so that mantissa and exp have separate declarations and
> > > sign gets initialized later.
> >
> > > Signed-off-by: Maxwell Doose <m32285159@xxxxxxxxx>
>
> Sashiko review of this one is really interesting. If you fancy doing a bit
> of digging it would be good to verify that it is correct on the original
> code giving a very wrong answer on 64 bit platforms.
>
> I poked the compiler explorer (https://godbolt.org/) with and without -m32
> passed to 64bit gcc. Looks like sashiko is right to me. However given the way
> it is used I think we are in undefined behaviour territory so it might
> 'work' unless the compiler is feel malicious.
>
> Upshot, verify it for your own understanding and then add a fixes tag
> for wherever that issue came from.
>
> https://sashiko.dev/#/patchset/20260524020309.18618-1-m32285159%40gmail.com
>

Woof, this'll be quite a doozy to look into. I'm no good in terms of
understanding compilers (I ought to do a deeper dive into stuff like
that at some point), but I guess compiler explorer is sufficient for
now (note that I'm also no good at understanding assembly). I think
(and correct me if I'm wrong) that under the hood, BIT(n) does (1UL <<
n) and since an unsigned long is 64 bits on 64-bit architectures,
float32 gets sign extended to 0xFFFFFFFF7FFFFFFF which is a problem.

Regardless, will send a v3 with fixes, reported-by, and closes tags (I
guess sashiko technically reported it) as well as an explanation for
the fixes tag.

best regards,
max