LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] staging: iio: ad2s1200: Fix sign extension
@ 2015-01-22 23:09 Rasmus Villemoes
  2015-01-23  8:56 ` Lars-Peter Clausen
  0 siblings, 1 reply; 3+ messages in thread
From: Rasmus Villemoes @ 2015-01-22 23:09 UTC (permalink / raw)
  To: Lars-Peter Clausen, Jonathan Cameron, Greg Kroah-Hartman
  Cc: Rasmus Villemoes, Hartmut Knaack, Peter Meerwald, linux-iio,
	devel, linux-kernel

The line above makes vel a 12-bit quantity (st->rx[] is u8). The
intention is to sign-extend vel using bit 11 as the sign bit. But
because of C's promotion rules "vel = (vel << 4) >> 4;" is actually a
no-op, since vel is promoted to int before the inner
shift. sign_extend32 works equally well for 8 and 16 bits types, so
use that.

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

diff --git a/drivers/staging/iio/resolver/ad2s1200.c b/drivers/staging/iio/resolver/ad2s1200.c
index 017d2f8379b7..c17893b4918c 100644
--- a/drivers/staging/iio/resolver/ad2s1200.c
+++ b/drivers/staging/iio/resolver/ad2s1200.c
@@ -18,6 +18,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/module.h>
+#include <linux/bitops.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -68,7 +69,7 @@ static int ad2s1200_read_raw(struct iio_dev *indio_dev,
 		break;
 	case IIO_ANGL_VEL:
 		vel = (((s16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
-		vel = (vel << 4) >> 4;
+		vel = sign_extend32(vel, 11);
 		*val = vel;
 		break;
 	default:
-- 
2.1.3


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

* Re: [PATCH] staging: iio: ad2s1200: Fix sign extension
  2015-01-22 23:09 [PATCH] staging: iio: ad2s1200: Fix sign extension Rasmus Villemoes
@ 2015-01-23  8:56 ` Lars-Peter Clausen
  2015-01-26 21:05   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Lars-Peter Clausen @ 2015-01-23  8:56 UTC (permalink / raw)
  To: Rasmus Villemoes, Jonathan Cameron, Greg Kroah-Hartman
  Cc: Hartmut Knaack, Peter Meerwald, linux-iio, devel, linux-kernel

On 01/23/2015 12:09 AM, Rasmus Villemoes wrote:
> The line above makes vel a 12-bit quantity (st->rx[] is u8). The
> intention is to sign-extend vel using bit 11 as the sign bit. But
> because of C's promotion rules "vel = (vel << 4) >> 4;" is actually a
> no-op, since vel is promoted to int before the inner
> shift. sign_extend32 works equally well for 8 and 16 bits types, so
> use that.
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

Acked-by: Lars-Peter Clausen <lars@metafoo.de>

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

* Re: [PATCH] staging: iio: ad2s1200: Fix sign extension
  2015-01-23  8:56 ` Lars-Peter Clausen
@ 2015-01-26 21:05   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2015-01-26 21:05 UTC (permalink / raw)
  To: Lars-Peter Clausen, Rasmus Villemoes, Greg Kroah-Hartman
  Cc: Hartmut Knaack, Peter Meerwald, linux-iio, devel, linux-kernel

On 23/01/15 08:56, Lars-Peter Clausen wrote:
> On 01/23/2015 12:09 AM, Rasmus Villemoes wrote:
>> The line above makes vel a 12-bit quantity (st->rx[] is u8). The
>> intention is to sign-extend vel using bit 11 as the sign bit. But
>> because of C's promotion rules "vel = (vel << 4) >> 4;" is actually a
>> no-op, since vel is promoted to int before the inner
>> shift. sign_extend32 works equally well for 8 and 16 bits types, so
>> use that.
>>
>> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> 
> Acked-by: Lars-Peter Clausen <lars@metafoo.de>
Applied to fixes-togreg branch of iio.git.

Thanks,

Jonathan


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

end of thread, other threads:[~2015-01-26 21:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-22 23:09 [PATCH] staging: iio: ad2s1200: Fix sign extension Rasmus Villemoes
2015-01-23  8:56 ` Lars-Peter Clausen
2015-01-26 21:05   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).