diff mbox series

iio: accel: sca3000: Use sign_extend32() instead of opencoding sign extension.

Message ID 20210603164729.3584702-1-jic23@kernel.org (mailing list archive)
State Superseded
Headers show
Series iio: accel: sca3000: Use sign_extend32() instead of opencoding sign extension. | expand

Commit Message

Jonathan Cameron June 3, 2021, 4:47 p.m. UTC
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Whilst nice to get rid of this non obvious code, this also clears a
static checker warning:

drivers/iio/accel/sca3000.c:734 sca3000_read_raw()
warn: no-op. '((*val) << 19) >> 19'

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/iio/accel/sca3000.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Jonathan Cameron July 18, 2021, 3:16 p.m. UTC | #1
On Thu,  3 Jun 2021 17:47:29 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
> Whilst nice to get rid of this non obvious code, this also clears a
> static checker warning:
> 
> drivers/iio/accel/sca3000.c:734 sca3000_read_raw()
> warn: no-op. '((*val) << 19) >> 19'
> 
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

If someone could give this a quick sanity check that would be great.

Thanks,

Jonathan

> ---
>  drivers/iio/accel/sca3000.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
> index cb753a43533c..0692ccb80293 100644
> --- a/drivers/iio/accel/sca3000.c
> +++ b/drivers/iio/accel/sca3000.c
> @@ -731,8 +731,7 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
>  				return ret;
>  			}
>  			*val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
> -			*val = ((*val) << (sizeof(*val) * 8 - 13)) >>
> -				(sizeof(*val) * 8 - 13);
> +			*val = sign_extend32(*val, 13);
>  		} else {
>  			/* get the temperature when available */
>  			ret = sca3000_read_data_short(st,
Jonathan Cameron Oct. 3, 2021, 4:06 p.m. UTC | #2
On Sun, 18 Jul 2021 16:16:12 +0100
Jonathan Cameron <jic23@kernel.org> wrote:

> On Thu,  3 Jun 2021 17:47:29 +0100
> Jonathan Cameron <jic23@kernel.org> wrote:
> 
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > 
> > Whilst nice to get rid of this non obvious code, this also clears a
> > static checker warning:
> > 
> > drivers/iio/accel/sca3000.c:734 sca3000_read_raw()
> > warn: no-op. '((*val) << 19) >> 19'
> > 
> > Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> > Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>  
> 
> If someone could give this a quick sanity check that would be great.
> 

This one is still in the queue because I really don't like applying
my own patches without anyone having sanity checked them...

So if anyone has time to for a quick look that would be great.

> Thanks,
> 
> Jonathan
> 
> > ---
> >  drivers/iio/accel/sca3000.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> > 
> > diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
> > index cb753a43533c..0692ccb80293 100644
> > --- a/drivers/iio/accel/sca3000.c
> > +++ b/drivers/iio/accel/sca3000.c
> > @@ -731,8 +731,7 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
> >  				return ret;
> >  			}
> >  			*val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
> > -			*val = ((*val) << (sizeof(*val) * 8 - 13)) >>
> > -				(sizeof(*val) * 8 - 13);
> > +			*val = sign_extend32(*val, 13);
> >  		} else {
> >  			/* get the temperature when available */
> >  			ret = sca3000_read_data_short(st,  
>
Lars-Peter Clausen Oct. 3, 2021, 6:03 p.m. UTC | #3
On 6/3/21 6:47 PM, Jonathan Cameron wrote:
> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>
> Whilst nice to get rid of this non obvious code, this also clears a
> static checker warning:
>
> drivers/iio/accel/sca3000.c:734 sca3000_read_raw()
> warn: no-op. '((*val) << 19) >> 19'
>
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>   drivers/iio/accel/sca3000.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
> index cb753a43533c..0692ccb80293 100644
> --- a/drivers/iio/accel/sca3000.c
> +++ b/drivers/iio/accel/sca3000.c
> @@ -731,8 +731,7 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
>   				return ret;
>   			}
>   			*val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
> -			*val = ((*val) << (sizeof(*val) * 8 - 13)) >>
> -				(sizeof(*val) * 8 - 13);
> +			*val = sign_extend32(*val, 13);
Should be 12 I think. The index is 0 based.
>   		} else {
>   			/* get the temperature when available */
>   			ret = sca3000_read_data_short(st,
Jonathan Cameron Oct. 10, 2021, 4:29 p.m. UTC | #4
On Sun, 3 Oct 2021 20:03:10 +0200
Lars-Peter Clausen <lars@metafoo.de> wrote:

> On 6/3/21 6:47 PM, Jonathan Cameron wrote:
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> >
> > Whilst nice to get rid of this non obvious code, this also clears a
> > static checker warning:
> >
> > drivers/iio/accel/sca3000.c:734 sca3000_read_raw()
> > warn: no-op. '((*val) << 19) >> 19'
> >
> > Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> > Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > ---
> >   drivers/iio/accel/sca3000.c | 3 +--
> >   1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
> > index cb753a43533c..0692ccb80293 100644
> > --- a/drivers/iio/accel/sca3000.c
> > +++ b/drivers/iio/accel/sca3000.c
> > @@ -731,8 +731,7 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
> >   				return ret;
> >   			}
> >   			*val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
> > -			*val = ((*val) << (sizeof(*val) * 8 - 13)) >>
> > -				(sizeof(*val) * 8 - 13);
> > +			*val = sign_extend32(*val, 13);  
> Should be 12 I think. The index is 0 based.

Fine example of why everything needs a sanity check.  Indeed, should be 12
to sign extend a 13bit value.

Jonathan

> >   		} else {
> >   			/* get the temperature when available */
> >   			ret = sca3000_read_data_short(st,  
> 
>
diff mbox series

Patch

diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
index cb753a43533c..0692ccb80293 100644
--- a/drivers/iio/accel/sca3000.c
+++ b/drivers/iio/accel/sca3000.c
@@ -731,8 +731,7 @@  static int sca3000_read_raw(struct iio_dev *indio_dev,
 				return ret;
 			}
 			*val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
-			*val = ((*val) << (sizeof(*val) * 8 - 13)) >>
-				(sizeof(*val) * 8 - 13);
+			*val = sign_extend32(*val, 13);
 		} else {
 			/* get the temperature when available */
 			ret = sca3000_read_data_short(st,