The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Joshua Crofts <joshua.crofts1@gmail.com>
Cc: "Maxwell Doose" <m32285159@gmail.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"open list:IIO SUBSYSTEM AND DRIVERS" <linux-iio@vger.kernel.org>,
	"open list" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] iio: chemical: scd30: Cleanup initializations in scd30_float_to_fp()
Date: Tue, 26 May 2026 15:38:42 +0100	[thread overview]
Message-ID: <20260526153842.61e15dfd@jic23-huawei> (raw)
In-Reply-To: <CALoEA-w7i4wQR_k9Eb6KPQLiLk6ZVrFmFoY9wHaO+7=ng=H-tg@mail.gmail.com>

On Mon, 25 May 2026 08:49:17 +0200
Joshua Crofts <joshua.crofts1@gmail.com> wrote:

> On Sun, 24 May 2026 at 04:04, Maxwell Doose <m32285159@gmail.com> 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@gmail.com>  

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
> 
> Not really sure, but the title of the commit could be better? Perhaps something
> along the lines of "simplify floating point bit arithmetic". Usually you don't
> need to mention the function name in the title since it can be found
> in the diff.
> 
> > @@ -89,10 +96,15 @@ static int scd30_reset(struct scd30_state *state)
> >  /* simplified float to fixed point conversion with a scaling factor of 0.01 */
> >  static int scd30_float_to_fp(int float32)
> >  {
> > -       int fraction, shift,
> > -           mantissa = float32 & GENMASK(22, 0),
> > -           sign = (float32 & BIT(31)) ? -1 : 1,
> > -           exp = (float32 & ~BIT(31)) >> 23;
> > +       int fraction, shift, sign;
> > +       int mantissa = FIELD_GET(SCD30_FLOAT_MANTISSA_MSK, float32);
> > +       int exp = FIELD_GET(SCD30_FLOAT_EXP_MSK, float32);
> > +
> > +       /* Determine sign of received float based on IEEE 754 standard */  
> 
> IMO this is an unnecessary comment.
> 
> Codewise OK.
> 
> Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
> 


  reply	other threads:[~2026-05-26 14:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-24  2:03 [PATCH v2] iio: chemical: scd30: Cleanup initializations in scd30_float_to_fp() Maxwell Doose
2026-05-25  6:49 ` Joshua Crofts
2026-05-26 14:38   ` Jonathan Cameron [this message]
2026-05-26 20:54     ` Maxwell Doose

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=20260526153842.61e15dfd@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=joshua.crofts1@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m32285159@gmail.com \
    --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