Staging: iio: Change data type in adis16220_read_16bit to u16
Subsystems (4)
PATCH 1/1 Staging: iio: Change data type in adis16220_read_16bit to u16
In the adis16220_read16bit() function we earlier used a s16 value 'val' which is used by the adis_read_reg_16 function to read data and takes a u16 value as a parameter.
So, this patch changes the data type of 'val' from s16 to u16 for further simplification of code and thereby avoiding unnecessary typecast.
--- drivers/staging/iio/accel/adis16220_core.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/staging/iio/accel/adis16220_core.c +3 -4
@@ -28,16 +28,15 @@ static ssize_t adis16220_read_16bit(struct device *dev,struct iio_dev *indio_dev =dev_to_iio_dev (dev);struct adis16220_state *st =iio_priv (indio_dev);ssize_t ret; -s16 val =0 ; +u16 val =0 ;/* Take the iio_dev status lock */ mutex_lock (&indio_dev->mlock); - ret =adis_read_reg_16 (&st->adis, this_attr->address, - (u16 *)&val); + ret =adis_read_reg_16 (&st->adis, this_attr->address, &val);mutex_unlock (&indio_dev->mlock);if (ret)return ret; -return sprintf (buf,"%d\n" , val); +return sprintf (buf,"%u\n" , val); } static ssize_t adis16220_write_16bit(struct device *dev,
Signature · 1 line
1.9.1
Hi Somya,
Quoted 8 lines · Somya Anand
In the
adis16220_read16bit()function we earlier used a s16 value 'val' which is used by theadis_read_reg_16function to read data and takes a u16 value as a parameter.So, this patch changes the data type of 'val' from s16 to u16 for further simplification of code and thereby avoiding unnecessary typecast.
Signed-off-by Somya Anand <[email protected]>
Ideally, subject should tell us "Why?" the patch is needed, instead of "What?" the patch does.
The latter should be easy to understand reading the code.
e.g:
staging: iio: adis16220: Avoid unnecessary typecast
Quoted 18 lines
--- drivers/staging/iio/accel/adis16220_core.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)drivers/staging/iio/accel/adis16220_core.c +1
-2@@ -28,16 +28,15 @@ static ssize_t adis16220_read_16bit(struct device *dev,struct iio_dev *indio_dev =dev_to_iio_dev (dev);struct adis16220_state *st =iio_priv (indio_dev);ssize_t ret; -s16 val =0 ; +u16 val =0 ;/* Take the iio_dev status lock */ mutex_lock (&indio_dev->mlock); - ret = adis_read_reg_16(&st->adis, this_attr->address,
drivers/staging/iio/accel/adis16220_core.c static ssize_t adis16220_read_16bit(struct device *dev, · line 42
Quoted 10 lines
drivers/staging/iio/accel/adis16220_core.c +2
-2- (u16 *)&val); + ret =adis_read_reg_16 (&st->adis, this_attr->address, &val);mutex_unlock (&indio_dev->mlock);if (ret)return ret; -return sprintf (buf,"%d\n" , val); +return sprintf (buf,"%u\n" , val); } static ssize_t adis16220_write_16bit(struct device *dev,
Other than that it looks good to me. We will need an ACK from Jonathan / Lars on this.
thanks,
Daniel.
Quoted 11 lines · Daniel Baluta
Hi Somya,
Quoted 8 lines · Somya Anand
In the
adis16220_read16bit()function we earlier used a s16 value 'val' which is used by theadis_read_reg_16function to read data and takes a u16 value as a parameter.So, this patch changes the data type of 'val' from s16 to u16 for further simplification of code and thereby avoiding unnecessary typecast.
Signed-off-by Somya Anand <[email protected]>
Patch looks ok. But it changes the semantics of the function, the reason why this is a s16 instead of a u16 is because at some point the function was used to read register that contained signed 16 bit values. The code is essentially the short version of:
u16 uval;
adis_read_reg_16(&st->adis, this_attr->address, &uval);
sprintf("%d\n", sign_extend32(uval, 15));This patch removes the extra sign extension. Which is ok, since the only user of the function uses it to read a 10 unsigned value which will lead to the same result in both cases. But the commit message should mention this, since it is not just the removal of a unnecessary type cast.
- Lars
Quoted 17 lines · Lars-Peter Clausen
Quoted 15 lines · Daniel Baluta
Hi Somya,
Quoted 9 lines · Somya Anand
In the
adis16220_read16bit()function we earlier used a s16 value 'val' which is used by theadis_read_reg_16function to read data and takes a u16 value as a parameter.So, this patch changes the data type of 'val' from s16 to u16 for further simplification of code and thereby avoiding unnecessary typecast.
Signed-off-by Somya Anand <[email protected]>
Quoted 9 lines
Patch looks ok. But it changes the semantics of the function, the reason why this is a s16 instead of a u16 is because at some point the function was used to read register that contained signed 16 bit values. The code is essentially the short version of:
u16 uval; adis_read_reg_16(&st->adis, this_attr->address, &uval); sprintf("%d\n", sign_extend32(uval, 15));
I have asked Daniel regarding this earlier because while reading the
code, I could not get the exact purpose of using s16. Thanks for clearing my doubt.
Quoted 7 lines
This patch removes the extra sign extension. Which is ok, since the only user of the function uses it to read a 10 unsigned value which will lead to the same result in both cases. But the commit message should mention this, since it is not just the removal of a unnecessary type cast.
Sure, I will write a better commit message so that it would be more
descriptive.
Quoted 2 lines
- Lars
Somya
Quoted 9 lines
Somya Anand <[email protected]> hat am 6. März 2015 um 09:36 geschrieben:
drivers/staging/iio/accel/adis16220_core.c +0
-0
drivers/staging/iio/accel/adis16220_core.c static ssize_t adis16220_read_16bit(struct device *dev, · line 34
Quoted 8 lines
drivers/staging/iio/accel/adis16220_core.c +1
-1@@ -28,16 +28,15 @@ static ssize_t adis16220_read_16bit(struct device *dev,struct iio_dev *indio_dev =dev_to_iio_dev (dev);struct adis16220_state *st =iio_priv (indio_dev);ssize_t ret; -s16 val =0 ; +u16 val =0 ;/* Take the iio_dev status lock */ mutex_lock (&indio_dev->mlock);
In a patch like this, it would be best to also drop the initialization to zero, which is
not used here. Not initializing it helps the compiler provide useful warnings if the code gets changed to return an otherwise uninitialized result.
drivers/staging/iio/accel/adis16220_core.c static ssize_t adis16220_read_16bit(struct device *dev, · line 41
Quoted 10 lines
drivers/staging/iio/accel/adis16220_core.c +2
-3- ret =adis_read_reg_16 (&st->adis, this_attr->address, - (u16 *)&val); + ret =adis_read_reg_16 (&st->adis, this_attr->address, &val);mutex_unlock (&indio_dev->mlock);if (ret)return ret; -return sprintf (buf,"%d\n" , val); +return sprintf (buf,"%u\n" , val); }
This looks wrong: Unless you know better, you should assume that the sprintf format
string is correct and that the value is indeed signed for the purpose of this file.
The reason why adis_read_reg_16() takes a u16 argument instead of s16 is that it accesses a register that doesn't have a concept of signedness. I would just leave
the code as it is, alternatively you could have two variables and explicitly cast from
u16 to s16 with a comment.
Arnd