All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch] iio: imu: adis16400: Fix sign extension
@ 2015-01-30  8:30 ` Dan Carpenter
  0 siblings, 0 replies; 13+ messages in thread
From: Dan Carpenter @ 2015-01-30  8:30 UTC (permalink / raw)
  To: Lars-Peter Clausen
  Cc: Michael Hennerich, Jonathan Cameron, Hartmut Knaack,
	Peter Meerwald, linux-iio, kernel-janitors, Rasmus Villemoes

Because of C's type promotion rules, the code shifts in
"((val16 & 0xFFF) << 4) >> 4;" cancel each other out and they're a
no-op.

The intention here was to sign-extend then 11th bit so we can use the
sign_extend32() function.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
Please review this carefully.  I'm pretty sure it's correct but this is
the first time I've ever used the sign_extend32() function.

diff --git a/drivers/iio/imu/adis16400_core.c b/drivers/iio/imu/adis16400_core.c
index b70873d..ed636c9 100644
--- a/drivers/iio/imu/adis16400_core.c
+++ b/drivers/iio/imu/adis16400_core.c
@@ -414,7 +414,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
 		mutex_unlock(&indio_dev->mlock);
 		if (ret)
 			return ret;
-		val16 = ((val16 & 0xFFF) << 4) >> 4;
+		val16 = sign_extend32(val16, 11);
 		*val = val16;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OFFSET:

^ permalink raw reply related	[flat|nested] 13+ messages in thread
* [PATCH] iio: imu: adis16400: Fix sign extension
@ 2015-01-22 23:34 Rasmus Villemoes
  2015-01-23  8:56 ` Lars-Peter Clausen
  0 siblings, 1 reply; 13+ messages in thread
From: Rasmus Villemoes @ 2015-01-22 23:34 UTC (permalink / raw)
  To: Lars-Peter Clausen, Michael Hennerich, Jonathan Cameron,
	Hartmut Knaack, Peter Meerwald
  Cc: Rasmus Villemoes, linux-iio, linux-kernel

The intention is obviously to sign-extend a 12 bit quantity. But
because of C's promotion rules, the assignment is equivalent to "val16
&= 0xfff;". Use the proper API for this.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/iio/imu/adis16400_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/imu/adis16400_core.c b/drivers/iio/imu/adis16400_core.c
index b70873de04ea..fa795dcd5f75 100644
--- a/drivers/iio/imu/adis16400_core.c
+++ b/drivers/iio/imu/adis16400_core.c
@@ -26,6 +26,7 @@
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/debugfs.h>
+#include <linux/bitops.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -414,7 +415,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
 		mutex_unlock(&indio_dev->mlock);
 		if (ret)
 			return ret;
-		val16 = ((val16 & 0xFFF) << 4) >> 4;
+		val16 = sign_extend32(val16, 11);
 		*val = val16;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OFFSET:
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-01-30 10:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-30  8:30 [patch] iio: imu: adis16400: Fix sign extension Dan Carpenter
2015-01-30  8:30 ` Dan Carpenter
2015-01-30  8:34 ` Lars-Peter Clausen
2015-01-30  8:34   ` Lars-Peter Clausen
2015-01-30  9:22   ` Dan Carpenter
2015-01-30  9:22     ` Dan Carpenter
2015-01-30  9:31     ` Rasmus Villemoes
2015-01-30  9:31       ` Rasmus Villemoes
2015-01-30 10:23       ` Dan Carpenter
2015-01-30 10:23         ` Dan Carpenter
  -- strict thread matches above, loose matches on Subject: below --
2015-01-22 23:34 [PATCH] " Rasmus Villemoes
2015-01-23  8:56 ` Lars-Peter Clausen
2015-01-26 21:07   ` Jonathan Cameron

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.