* [PATCH v1] iio: hid-sensor-attributes: Fix divisions for 32-bit platforms
@ 2019-09-05 11:27 Andy Shevchenko
2019-09-06 1:35 ` Pandruvada, Srinivas
0 siblings, 1 reply; 2+ messages in thread
From: Andy Shevchenko @ 2019-09-05 11:27 UTC (permalink / raw)
To: Nathan Chancellor, linux-iio, Jonathan Cameron, Hartmut Knaack,
Lars-Peter Clausen, Peter Meerwald-Stadler, Srinivas Pandruvada,
Greg KH
Cc: Andy Shevchenko, kbuild test robot
The commit 473d12f7638c
("iio: hid-sensor-attributes: Convert to use int_pow()")
converted to use generic int_pow() helper. Though, the generic one returns
64-bit value and, in cases when it is used as divisor, it compels 64-bit
division from compiler.
In order to fix this, introduce a temporary 32-bit variable to hold the result
of int_pow() and use it as divisor afterwards.
In couple of cases, replace int_pow() with a predefined unit factors for time
and frequency.
Fixes: 473d12f7638c ("iio: hid-sensor-attributes: Convert to use int_pow()")
Reported-by: kbuild test robot <lkp@intel.com>
Reported-by: Nathan Chancellor <natechancellor@gmail.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
.../hid-sensors/hid-sensor-attributes.c | 42 ++++++++++++-------
1 file changed, 28 insertions(+), 14 deletions(-)
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index b9dd19b34267..442ff787f7af 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -10,10 +10,14 @@
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/slab.h>
+#include <linux/time.h>
+
#include <linux/hid-sensor-hub.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
+#define HZ_PER_MHZ 1000000L
+
static struct {
u32 usage_id;
int unit; /* 0 for default others from HID sensor spec */
@@ -93,8 +97,10 @@ static void simple_div(int dividend, int divisor, int *whole,
static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
{
- *val1 = no / int_pow(10, exp);
- *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
+ int divisor = int_pow(10, exp);
+
+ *val1 = no / divisor;
+ *val2 = no % divisor * int_pow(10, 6 - exp);
}
/*
@@ -129,6 +135,7 @@ static void convert_from_vtf_format(u32 value, int size, int exp,
static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
{
+ int divisor;
u32 value;
int sign = 1;
@@ -136,10 +143,13 @@ static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
sign = -1;
exp = hid_sensor_convert_exponent(exp);
if (exp < 0) {
+ divisor = int_pow(10, 6 + exp);
value = abs(val1) * int_pow(10, -exp);
- value += abs(val2) / int_pow(10, 6 + exp);
- } else
- value = abs(val1) / int_pow(10, exp);
+ value += abs(val2) / divisor;
+ } else {
+ divisor = int_pow(10, exp);
+ value = abs(val1) / divisor;
+ }
if (sign < 0)
value = ((1LL << (size * 8)) - value);
@@ -202,12 +212,12 @@ int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
if (val1 < 0 || val2 < 0)
return -EINVAL;
- value = val1 * int_pow(10, 6) + val2;
+ value = val1 * HZ_PER_MHZ + val2;
if (value) {
if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
- value = int_pow(10, 9) / value;
+ value = NSEC_PER_SEC / value;
else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
- value = int_pow(10, 6) / value;
+ value = USEC_PER_SEC / value;
else
value = 0;
}
@@ -296,6 +306,7 @@ EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
static void adjust_exponent_nano(int *val0, int *val1, int scale0,
int scale1, int exp)
{
+ int divisor;
int i;
int x;
int res;
@@ -309,9 +320,10 @@ static void adjust_exponent_nano(int *val0, int *val1, int scale0,
return;
}
for (i = 0; i < exp; ++i) {
- x = scale1 / int_pow(10, 8 - i);
+ divisor = int_pow(10, 8 - i);
+ x = scale1 / divisor;
res += int_pow(10, exp - 1 - i) * x;
- scale1 = scale1 % int_pow(10, 8 - i);
+ scale1 = scale1 % divisor;
}
*val0 += res;
*val1 = scale1 * int_pow(10, exp);
@@ -321,13 +333,15 @@ static void adjust_exponent_nano(int *val0, int *val1, int scale0,
*val0 = *val1 = 0;
return;
}
- *val0 = scale0 / int_pow(10, exp);
- rem = scale0 % int_pow(10, exp);
+ divisor = int_pow(10, exp);
+ *val0 = scale0 / divisor;
+ rem = scale0 % divisor;
res = 0;
for (i = 0; i < (9 - exp); ++i) {
- x = scale1 / int_pow(10, 8 - i);
+ divisor = int_pow(10, 8 - i);
+ x = scale1 / divisor;
res += int_pow(10, 8 - exp - i) * x;
- scale1 = scale1 % int_pow(10, 8 - i);
+ scale1 = scale1 % divisor;
}
*val1 = rem * int_pow(10, 9 - exp) + res;
} else {
--
2.23.0.rc1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v1] iio: hid-sensor-attributes: Fix divisions for 32-bit platforms
2019-09-05 11:27 [PATCH v1] iio: hid-sensor-attributes: Fix divisions for 32-bit platforms Andy Shevchenko
@ 2019-09-06 1:35 ` Pandruvada, Srinivas
0 siblings, 0 replies; 2+ messages in thread
From: Pandruvada, Srinivas @ 2019-09-06 1:35 UTC (permalink / raw)
To: lars, knaack.h, jic23, linux-iio, pmeerw, gregkh, natechancellor,
andriy.shevchenko
Cc: lkp
[-- Attachment #1: Type: text/plain, Size: 4918 bytes --]
On Thu, 2019-09-05 at 14:27 +0300, Andy Shevchenko wrote:
> The commit 473d12f7638c
>
> ("iio: hid-sensor-attributes: Convert to use int_pow()")
>
> converted to use generic int_pow() helper. Though, the generic one
> returns
> 64-bit value and, in cases when it is used as divisor, it compels 64-
> bit
> division from compiler.
>
> In order to fix this, introduce a temporary 32-bit variable to hold
> the result
> of int_pow() and use it as divisor afterwards.
>
> In couple of cases, replace int_pow() with a predefined unit factors
> for time
> and frequency.
>
> Fixes: 473d12f7638c ("iio: hid-sensor-attributes: Convert to use
> int_pow()")
> Reported-by: kbuild test robot <lkp@intel.com>
> Reported-by: Nathan Chancellor <natechancellor@gmail.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
> ---
> .../hid-sensors/hid-sensor-attributes.c | 42 ++++++++++++-----
> --
> 1 file changed, 28 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> index b9dd19b34267..442ff787f7af 100644
> --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> @@ -10,10 +10,14 @@
> #include <linux/irq.h>
> #include <linux/kernel.h>
> #include <linux/slab.h>
> +#include <linux/time.h>
> +
> #include <linux/hid-sensor-hub.h>
> #include <linux/iio/iio.h>
> #include <linux/iio/sysfs.h>
>
> +#define HZ_PER_MHZ 1000000L
> +
> static struct {
> u32 usage_id;
> int unit; /* 0 for default others from HID sensor spec */
> @@ -93,8 +97,10 @@ static void simple_div(int dividend, int divisor,
> int *whole,
>
> static void split_micro_fraction(unsigned int no, int exp, int
> *val1, int *val2)
> {
> - *val1 = no / int_pow(10, exp);
> - *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
> + int divisor = int_pow(10, exp);
> +
> + *val1 = no / divisor;
> + *val2 = no % divisor * int_pow(10, 6 - exp);
> }
>
> /*
> @@ -129,6 +135,7 @@ static void convert_from_vtf_format(u32 value,
> int size, int exp,
>
> static u32 convert_to_vtf_format(int size, int exp, int val1, int
> val2)
> {
> + int divisor;
> u32 value;
> int sign = 1;
>
> @@ -136,10 +143,13 @@ static u32 convert_to_vtf_format(int size, int
> exp, int val1, int val2)
> sign = -1;
> exp = hid_sensor_convert_exponent(exp);
> if (exp < 0) {
> + divisor = int_pow(10, 6 + exp);
> value = abs(val1) * int_pow(10, -exp);
> - value += abs(val2) / int_pow(10, 6 + exp);
> - } else
> - value = abs(val1) / int_pow(10, exp);
> + value += abs(val2) / divisor;
> + } else {
> + divisor = int_pow(10, exp);
> + value = abs(val1) / divisor;
> + }
> if (sign < 0)
> value = ((1LL << (size * 8)) - value);
>
> @@ -202,12 +212,12 @@ int hid_sensor_write_samp_freq_value(struct
> hid_sensor_common *st,
> if (val1 < 0 || val2 < 0)
> return -EINVAL;
>
> - value = val1 * int_pow(10, 6) + val2;
> + value = val1 * HZ_PER_MHZ + val2;
> if (value) {
> if (st->poll.units ==
> HID_USAGE_SENSOR_UNITS_MILLISECOND)
> - value = int_pow(10, 9) / value;
> + value = NSEC_PER_SEC / value;
> else if (st->poll.units ==
> HID_USAGE_SENSOR_UNITS_SECOND)
> - value = int_pow(10, 6) / value;
> + value = USEC_PER_SEC / value;
> else
> value = 0;
> }
> @@ -296,6 +306,7 @@ EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
> static void adjust_exponent_nano(int *val0, int *val1, int scale0,
> int scale1, int exp)
> {
> + int divisor;
> int i;
> int x;
> int res;
> @@ -309,9 +320,10 @@ static void adjust_exponent_nano(int *val0, int
> *val1, int scale0,
> return;
> }
> for (i = 0; i < exp; ++i) {
> - x = scale1 / int_pow(10, 8 - i);
> + divisor = int_pow(10, 8 - i);
> + x = scale1 / divisor;
> res += int_pow(10, exp - 1 - i) * x;
> - scale1 = scale1 % int_pow(10, 8 - i);
> + scale1 = scale1 % divisor;
> }
> *val0 += res;
> *val1 = scale1 * int_pow(10, exp);
> @@ -321,13 +333,15 @@ static void adjust_exponent_nano(int *val0, int
> *val1, int scale0,
> *val0 = *val1 = 0;
> return;
> }
> - *val0 = scale0 / int_pow(10, exp);
> - rem = scale0 % int_pow(10, exp);
> + divisor = int_pow(10, exp);
> + *val0 = scale0 / divisor;
> + rem = scale0 % divisor;
> res = 0;
> for (i = 0; i < (9 - exp); ++i) {
> - x = scale1 / int_pow(10, 8 - i);
> + divisor = int_pow(10, 8 - i);
> + x = scale1 / divisor;
> res += int_pow(10, 8 - exp - i) * x;
> - scale1 = scale1 % int_pow(10, 8 - i);
> + scale1 = scale1 % divisor;
> }
> *val1 = rem * int_pow(10, 9 - exp) + res;
> } else {
[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3290 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-09-06 1:35 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-05 11:27 [PATCH v1] iio: hid-sensor-attributes: Fix divisions for 32-bit platforms Andy Shevchenko
2019-09-06 1:35 ` Pandruvada, Srinivas
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).