2018-06-11 23:43:47

by Karim Eshapa

[permalink] [raw]
Subject: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication

Use sign_extend32 kernel function instead of code duplication.
Safe also for 16 bit.

Signed-off-by: Karim Eshapa <[email protected]>
---
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



2018-06-11 23:59:54

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication

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;

2018-06-12 01:07:54

by Karim Eshapa

[permalink] [raw]
Subject: Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication

Use sign_extend32 kernel function instead of code duplication.
Safe also for 16 bit.

Signed-off-by: Karim Eshapa <[email protected]>
---
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


2018-06-12 10:03:26

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH] staging:iio:accel:adis16240: sign extend function avoiding code duplication

On Tue, 12 Jun 2018 02:57:48 +0200
Karim Eshapa <[email protected]> wrote:

> Use sign_extend32 kernel function instead of code duplication.
> Safe also for 16 bit.
>
> Signed-off-by: Karim Eshapa <[email protected]>

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;