Skip to contentSkip to conversation

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

PATCH Superseded Jonathan Cameron 2 participants 5 messages Jun 3, 2021 – Oct 10, 2021 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

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

From: Jonathan Cameron <[email protected]>

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'

---
 drivers/iio/accel/sca3000.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/accel/sca3000.c +1 -2
index cb753a43533c..0692ccb80293 100644
@@ -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,
Signature · 1 line
2.31.1
author maintainer
#1 lore raw
Quoted 10 lines · On Thu, 3 Jun 2021 17:47:29 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

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'

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

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/accel/sca3000.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/accel/sca3000.c +1 -2
index cb753a43533c..0692ccb80293 100644
@@ -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,
author maintainer
#2 lore raw
Quoted 8 lines · On Sun, 18 Jul 2021 16:16:12 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 5 lines · On Thu, 3 Jun 2021 17:47:29 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

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

Quoted 8 lines
Quoted 5 lines

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

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.

Quoted 23 lines

Thanks,

Jonathan

Quoted 18 lines
---
 drivers/iio/accel/sca3000.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/accel/sca3000.c +1 -2
index cb753a43533c..0692ccb80293 100644
@@ -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,  
#3 lore raw
In reply to Jonathan Cameron
Quoted 19 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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'

---
  drivers/iio/accel/sca3000.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/accel/sca3000.c +0 -0
index cb753a43533c..0692ccb80293 100644

drivers/iio/accel/sca3000.c static int sca3000_read_raw(struct iio_dev *indio_dev, · line 734

Quoted 6 lines
drivers/iio/accel/sca3000.c +1 -2
@@ -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.

Quoted 3 lines
drivers/iio/accel/sca3000.c +0 -0
  		} else {
  			/* get the temperature when available */
  			ret = sca3000_read_data_short(st,
author maintainer
#4 lore raw
Quoted 15 lines · On Sun, 3 Oct 2021 20:03:10 +0200 Lars-Peter Clausen <[email protected]> wrote:
Quoted 14 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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'

---
  drivers/iio/accel/sca3000.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 12 lines

drivers/iio/accel/sca3000.c static int sca3000_read_raw(struct iio_dev *indio_dev, · line 734

Quoted 11 lines
drivers/iio/accel/sca3000.c +1 -2
index cb753a43533c..0692ccb80293 100644
@@ -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

Quoted 5 lines
Quoted 3 lines
drivers/iio/accel/sca3000.c +0 -0
  		} else {
  			/* get the temperature when available */
  			ret = sca3000_read_data_short(st,