* [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication @ 2018-06-11 23:38 Karim Eshapa 2018-06-11 23:57 ` Joe Perches 0 siblings, 1 reply; 5+ messages in thread From: Karim Eshapa @ 2018-06-11 23:38 UTC (permalink / raw) To: lars Cc: Michael.Hennerich, jic23, knaack.h, pmeerw, gregkh, linux-iio, devel, linux-kernel, Karim Eshapa Use sign_extend32 kernel function instead of code duplication. Safe also for 16 bit. Signed-off-by: Karim Eshapa <karim.eshapa@gmail.com> --- drivers/staging/iio/accel/adis16240.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/staging/iio/accel/adis16240.c b/drivers/staging/iio/accel/adis16240.c index fff6d99089cc..9adb5badb915 100644 --- a/drivers/staging/iio/accel/adis16240.c +++ b/drivers/staging/iio/accel/adis16240.c @@ -292,9 +292,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, ret = adis_read_reg_16(st, addr, &val16); if (ret) return ret; - val16 &= (1 << bits) - 1; - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); - *val = val16; + *val = sign_extend32(val16, bits - 1); return IIO_VAL_INT; case IIO_CHAN_INFO_PEAK: bits = 10; @@ -302,9 +300,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, ret = adis_read_reg_16(st, addr, &val16); if (ret) return ret; - val16 &= (1 << bits) - 1; - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); - *val = val16; + *val = sign_extend32(val16, bits - 1); return IIO_VAL_INT; } return -EINVAL; -- 2.17.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication 2018-06-11 23:38 [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication Karim Eshapa @ 2018-06-11 23:57 ` Joe Perches 2018-06-12 0:14 ` karim eshapa 2018-06-12 0:57 ` Karim Eshapa 0 siblings, 2 replies; 5+ messages in thread From: Joe Perches @ 2018-06-11 23:57 UTC (permalink / raw) To: Karim Eshapa, lars Cc: Michael.Hennerich, jic23, knaack.h, pmeerw, gregkh, linux-iio, devel, linux-kernel On Tue, 2018-06-12 at 01:38 +0200, Karim Eshapa wrote: > Use sign_extend32 kernel function instead of code duplication. > Safe also for 16 bit. Perhaps remove the bits declaration and assignments and just use 9 directly. > diff --git a/drivers/staging/iio/accel/adis16240.c b/drivers/staging/iio/accel/adis16240.c [] > @@ -292,9 +292,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, > ret = adis_read_reg_16(st, addr, &val16); > if (ret) > return ret; > - val16 &= (1 << bits) - 1; > - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); > - *val = val16; > + *val = sign_extend32(val16, bits - 1); > return IIO_VAL_INT; > case IIO_CHAN_INFO_PEAK: > bits = 10; > @@ -302,9 +300,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, > ret = adis_read_reg_16(st, addr, &val16); > if (ret) > return ret; > - val16 &= (1 << bits) - 1; > - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); > - *val = val16; > + *val = sign_extend32(val16, bits - 1); > return IIO_VAL_INT; > } > return -EINVAL; ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication 2018-06-11 23:57 ` Joe Perches @ 2018-06-12 0:14 ` karim eshapa 2018-06-12 0:57 ` Karim Eshapa 1 sibling, 0 replies; 5+ messages in thread From: karim eshapa @ 2018-06-12 0:14 UTC (permalink / raw) To: Joe Perches Cc: lars, Michael.Hennerich, jic23, knaack.h, Peter Meerwald-Stadler, gregkh, linux-iio, devel, linux-kernel [-- Attachment #1: Type: text/plain, Size: 2812 bytes --] >On Tue, 2018-06-12 at 01:38 +0200, Karim Eshapa wrote: >> Use sign_extend32 kernel function instead of code duplication. >> Safe also for 16 bit. > >Perhaps remove the bits declaration and assignments >and just use 9 directly. I think it's more readable in that way(bits - 1) instead of dealing with magic numbers. for more readability we can replace the declaration and assignment with macro's represent the cases. Thanks, Karim >> @@ -292,9 +292,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, >> ret = adis_read_reg_16(st, addr, &val16); >> if (ret) >> return ret; >> - val16 &= (1 << bits) - 1; >> - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); >> - *val = val16; >> + *val = sign_extend32(val16, bits - 1); >> return IIO_VAL_INT; >> case IIO_CHAN_INFO_PEAK: >> bits = 10; >> @@ -302,9 +300,7 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, >> ret = adis_read_reg_16(st, addr, &val16); >> if (ret) >> return ret; >> - val16 &= (1 << bits) - 1; >> - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); >> - *val = val16; >> + *val = sign_extend32(val16, bits - 1); >> return IIO_VAL_INT; >> } >> return -EINVAL; On 12 June 2018 at 01:57, Joe Perches <joe@perches.com> wrote: > On Tue, 2018-06-12 at 01:38 +0200, Karim Eshapa wrote: > > Use sign_extend32 kernel function instead of code duplication. > > Safe also for 16 bit. > > Perhaps remove the bits declaration and assignments > and just use 9 directly. > > > diff --git a/drivers/staging/iio/accel/adis16240.c > b/drivers/staging/iio/accel/adis16240.c > [] > > @@ -292,9 +292,7 @@ static int adis16240_read_raw(struct iio_dev > *indio_dev, > > ret = adis_read_reg_16(st, addr, &val16); > > if (ret) > > return ret; > > - val16 &= (1 << bits) - 1; > > - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); > > - *val = val16; > > + *val = sign_extend32(val16, bits - 1); > > return IIO_VAL_INT; > > case IIO_CHAN_INFO_PEAK: > > bits = 10; > > @@ -302,9 +300,7 @@ static int adis16240_read_raw(struct iio_dev > *indio_dev, > > ret = adis_read_reg_16(st, addr, &val16); > > if (ret) > > return ret; > > - val16 &= (1 << bits) - 1; > > - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); > > - *val = val16; > > + *val = sign_extend32(val16, bits - 1); > > return IIO_VAL_INT; > > } > > return -EINVAL; > [-- Attachment #2: Type: text/html, Size: 11014 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication 2018-06-11 23:57 ` Joe Perches 2018-06-12 0:14 ` karim eshapa @ 2018-06-12 0:57 ` Karim Eshapa 2018-06-12 10:01 ` Jonathan Cameron 1 sibling, 1 reply; 5+ messages in thread From: Karim Eshapa @ 2018-06-12 0:57 UTC (permalink / raw) To: lars Cc: Michael.Hennerich, jic23, knaack.h, pmeerw, gregkh, linux-iio, devel, linux-kernel, Karim Eshapa Use sign_extend32 kernel function instead of code duplication. Safe also for 16 bit. Signed-off-by: Karim Eshapa <karim.eshapa@gmail.com> --- drivers/staging/iio/accel/adis16240.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/drivers/staging/iio/accel/adis16240.c b/drivers/staging/iio/accel/adis16240.c index fff6d99089cc..24e525f1ef25 100644 --- a/drivers/staging/iio/accel/adis16240.c +++ b/drivers/staging/iio/accel/adis16240.c @@ -250,7 +250,6 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, { struct adis *st = iio_priv(indio_dev); int ret; - int bits; u8 addr; s16 val16; @@ -287,24 +286,18 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */ return IIO_VAL_INT; case IIO_CHAN_INFO_CALIBBIAS: - bits = 10; addr = adis16240_addresses[chan->scan_index][0]; ret = adis_read_reg_16(st, addr, &val16); if (ret) return ret; - val16 &= (1 << bits) - 1; - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); - *val = val16; + *val = sign_extend32(val16, 9); return IIO_VAL_INT; case IIO_CHAN_INFO_PEAK: - bits = 10; addr = adis16240_addresses[chan->scan_index][1]; ret = adis_read_reg_16(st, addr, &val16); if (ret) return ret; - val16 &= (1 << bits) - 1; - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); - *val = val16; + *val = sign_extend32(val16, 9); return IIO_VAL_INT; } return -EINVAL; -- 2.17.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication 2018-06-12 0:57 ` Karim Eshapa @ 2018-06-12 10:01 ` Jonathan Cameron 0 siblings, 0 replies; 5+ messages in thread From: Jonathan Cameron @ 2018-06-12 10:01 UTC (permalink / raw) To: Karim Eshapa Cc: lars, Michael.Hennerich, jic23, knaack.h, pmeerw, gregkh, linux-iio, devel, linux-kernel On Tue, 12 Jun 2018 02:57:48 +0200 Karim Eshapa <karim.eshapa@gmail.com> wrote: > Use sign_extend32 kernel function instead of code duplication. > Safe also for 16 bit. > > Signed-off-by: Karim Eshapa <karim.eshapa@gmail.com> Please resend as a fresh patch marked at V2 with a change log below the --- so we can easily see what has changed. Thanks, Jonathan > --- > drivers/staging/iio/accel/adis16240.c | 11 ++--------- > 1 file changed, 2 insertions(+), 9 deletions(-) > > diff --git a/drivers/staging/iio/accel/adis16240.c b/drivers/staging/iio/accel/adis16240.c > index fff6d99089cc..24e525f1ef25 100644 > --- a/drivers/staging/iio/accel/adis16240.c > +++ b/drivers/staging/iio/accel/adis16240.c > @@ -250,7 +250,6 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, > { > struct adis *st = iio_priv(indio_dev); > int ret; > - int bits; > u8 addr; > s16 val16; > > @@ -287,24 +286,18 @@ static int adis16240_read_raw(struct iio_dev *indio_dev, > *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */ > return IIO_VAL_INT; > case IIO_CHAN_INFO_CALIBBIAS: > - bits = 10; > addr = adis16240_addresses[chan->scan_index][0]; > ret = adis_read_reg_16(st, addr, &val16); > if (ret) > return ret; > - val16 &= (1 << bits) - 1; > - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); > - *val = val16; > + *val = sign_extend32(val16, 9); > return IIO_VAL_INT; > case IIO_CHAN_INFO_PEAK: > - bits = 10; > addr = adis16240_addresses[chan->scan_index][1]; > ret = adis_read_reg_16(st, addr, &val16); > if (ret) > return ret; > - val16 &= (1 << bits) - 1; > - val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); > - *val = val16; > + *val = sign_extend32(val16, 9); > return IIO_VAL_INT; > } > return -EINVAL; ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-06-12 10:01 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2018-06-11 23:38 [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication Karim Eshapa 2018-06-11 23:57 ` Joe Perches 2018-06-12 0:14 ` karim eshapa 2018-06-12 0:57 ` Karim Eshapa 2018-06-12 10:01 ` Jonathan Cameron
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox