* [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow()
@ 2019-06-19 14:07 Andy Shevchenko
2019-06-22 9:06 ` Jonathan Cameron
0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2019-06-19 14:07 UTC (permalink / raw)
To: Jonathan Cameron, linux-iio, Hartmut Knaack, Lars-Peter Clausen,
Peter Meerwald-Stadler
Cc: Andy Shevchenko
Instead of linear approach to calculate power of 10, use generic int_pow()
which does it better.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
.../hid-sensors/hid-sensor-attributes.c | 53 ++++++++-----------
1 file changed, 22 insertions(+), 31 deletions(-)
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index a8a3fe428d8d..b9dd19b34267 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -8,6 +8,7 @@
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
+#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/hid-sensor-hub.h>
#include <linux/iio/iio.h>
@@ -68,16 +69,6 @@ static struct {
{HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
};
-static int pow_10(unsigned power)
-{
- int i;
- int ret = 1;
- for (i = 0; i < power; ++i)
- ret = ret * 10;
-
- return ret;
-}
-
static void simple_div(int dividend, int divisor, int *whole,
int *micro_frac)
{
@@ -96,14 +87,14 @@ static void simple_div(int dividend, int divisor, int *whole,
rem *= 10;
exp++;
}
- *micro_frac = (rem / divisor) * pow_10(6-exp);
+ *micro_frac = (rem / divisor) * int_pow(10, 6 - exp);
}
}
static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
{
- *val1 = no/pow_10(exp);
- *val2 = no%pow_10(exp) * pow_10(6-exp);
+ *val1 = no / int_pow(10, exp);
+ *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
}
/*
@@ -125,7 +116,7 @@ static void convert_from_vtf_format(u32 value, int size, int exp,
}
exp = hid_sensor_convert_exponent(exp);
if (exp >= 0) {
- *val1 = sign * value * pow_10(exp);
+ *val1 = sign * value * int_pow(10, exp);
*val2 = 0;
} else {
split_micro_fraction(value, -exp, val1, val2);
@@ -145,10 +136,10 @@ 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) {
- value = abs(val1) * pow_10(-exp);
- value += abs(val2) / pow_10(6+exp);
+ value = abs(val1) * int_pow(10, -exp);
+ value += abs(val2) / int_pow(10, 6 + exp);
} else
- value = abs(val1) / pow_10(exp);
+ value = abs(val1) / int_pow(10, exp);
if (sign < 0)
value = ((1LL << (size * 8)) - value);
@@ -211,12 +202,12 @@ int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
if (val1 < 0 || val2 < 0)
return -EINVAL;
- value = val1 * pow_10(6) + val2;
+ value = val1 * int_pow(10, 6) + val2;
if (value) {
if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
- value = pow_10(9)/value;
+ value = int_pow(10, 9) / value;
else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
- value = pow_10(6)/value;
+ value = int_pow(10, 6) / value;
else
value = 0;
}
@@ -311,34 +302,34 @@ static void adjust_exponent_nano(int *val0, int *val1, int scale0,
int rem;
if (exp > 0) {
- *val0 = scale0 * pow_10(exp);
+ *val0 = scale0 * int_pow(10, exp);
res = 0;
if (exp > 9) {
*val1 = 0;
return;
}
for (i = 0; i < exp; ++i) {
- x = scale1 / pow_10(8 - i);
- res += (pow_10(exp - 1 - i) * x);
- scale1 = scale1 % pow_10(8 - i);
+ x = scale1 / int_pow(10, 8 - i);
+ res += int_pow(10, exp - 1 - i) * x;
+ scale1 = scale1 % int_pow(10, 8 - i);
}
*val0 += res;
- *val1 = scale1 * pow_10(exp);
+ *val1 = scale1 * int_pow(10, exp);
} else if (exp < 0) {
exp = abs(exp);
if (exp > 9) {
*val0 = *val1 = 0;
return;
}
- *val0 = scale0 / pow_10(exp);
- rem = scale0 % pow_10(exp);
+ *val0 = scale0 / int_pow(10, exp);
+ rem = scale0 % int_pow(10, exp);
res = 0;
for (i = 0; i < (9 - exp); ++i) {
- x = scale1 / pow_10(8 - i);
- res += (pow_10(8 - exp - i) * x);
- scale1 = scale1 % pow_10(8 - i);
+ x = scale1 / int_pow(10, 8 - i);
+ res += int_pow(10, 8 - exp - i) * x;
+ scale1 = scale1 % int_pow(10, 8 - i);
}
- *val1 = rem * pow_10(9 - exp) + res;
+ *val1 = rem * int_pow(10, 9 - exp) + res;
} else {
*val0 = scale0;
*val1 = scale1;
--
2.20.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow()
2019-06-19 14:07 [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow() Andy Shevchenko
@ 2019-06-22 9:06 ` Jonathan Cameron
2019-08-13 14:43 ` Andy Shevchenko
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2019-06-22 9:06 UTC (permalink / raw)
To: Andy Shevchenko
Cc: linux-iio, Hartmut Knaack, Lars-Peter Clausen,
Peter Meerwald-Stadler, Srinivas Pandruvada
On Wed, 19 Jun 2019 17:07:02 +0300
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> Instead of linear approach to calculate power of 10, use generic int_pow()
> which does it better.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Hi Andy,
Looks good to me, but I would like Srinivas to take a look and
preferably test this a bit just to check we aren't all missing something
subtle!
+CC Srinivas
Thanks,
Jonathan
> ---
> .../hid-sensors/hid-sensor-attributes.c | 53 ++++++++-----------
> 1 file changed, 22 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> index a8a3fe428d8d..b9dd19b34267 100644
> --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> @@ -8,6 +8,7 @@
> #include <linux/module.h>
> #include <linux/interrupt.h>
> #include <linux/irq.h>
> +#include <linux/kernel.h>
> #include <linux/slab.h>
> #include <linux/hid-sensor-hub.h>
> #include <linux/iio/iio.h>
> @@ -68,16 +69,6 @@ static struct {
> {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
> };
>
> -static int pow_10(unsigned power)
> -{
> - int i;
> - int ret = 1;
> - for (i = 0; i < power; ++i)
> - ret = ret * 10;
> -
> - return ret;
> -}
> -
> static void simple_div(int dividend, int divisor, int *whole,
> int *micro_frac)
> {
> @@ -96,14 +87,14 @@ static void simple_div(int dividend, int divisor, int *whole,
> rem *= 10;
> exp++;
> }
> - *micro_frac = (rem / divisor) * pow_10(6-exp);
> + *micro_frac = (rem / divisor) * int_pow(10, 6 - exp);
> }
> }
>
> static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
> {
> - *val1 = no/pow_10(exp);
> - *val2 = no%pow_10(exp) * pow_10(6-exp);
> + *val1 = no / int_pow(10, exp);
> + *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
> }
>
> /*
> @@ -125,7 +116,7 @@ static void convert_from_vtf_format(u32 value, int size, int exp,
> }
> exp = hid_sensor_convert_exponent(exp);
> if (exp >= 0) {
> - *val1 = sign * value * pow_10(exp);
> + *val1 = sign * value * int_pow(10, exp);
> *val2 = 0;
> } else {
> split_micro_fraction(value, -exp, val1, val2);
> @@ -145,10 +136,10 @@ 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) {
> - value = abs(val1) * pow_10(-exp);
> - value += abs(val2) / pow_10(6+exp);
> + value = abs(val1) * int_pow(10, -exp);
> + value += abs(val2) / int_pow(10, 6 + exp);
> } else
> - value = abs(val1) / pow_10(exp);
> + value = abs(val1) / int_pow(10, exp);
> if (sign < 0)
> value = ((1LL << (size * 8)) - value);
>
> @@ -211,12 +202,12 @@ int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
> if (val1 < 0 || val2 < 0)
> return -EINVAL;
>
> - value = val1 * pow_10(6) + val2;
> + value = val1 * int_pow(10, 6) + val2;
> if (value) {
> if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
> - value = pow_10(9)/value;
> + value = int_pow(10, 9) / value;
> else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
> - value = pow_10(6)/value;
> + value = int_pow(10, 6) / value;
> else
> value = 0;
> }
> @@ -311,34 +302,34 @@ static void adjust_exponent_nano(int *val0, int *val1, int scale0,
> int rem;
>
> if (exp > 0) {
> - *val0 = scale0 * pow_10(exp);
> + *val0 = scale0 * int_pow(10, exp);
> res = 0;
> if (exp > 9) {
> *val1 = 0;
> return;
> }
> for (i = 0; i < exp; ++i) {
> - x = scale1 / pow_10(8 - i);
> - res += (pow_10(exp - 1 - i) * x);
> - scale1 = scale1 % pow_10(8 - i);
> + x = scale1 / int_pow(10, 8 - i);
> + res += int_pow(10, exp - 1 - i) * x;
> + scale1 = scale1 % int_pow(10, 8 - i);
> }
> *val0 += res;
> - *val1 = scale1 * pow_10(exp);
> + *val1 = scale1 * int_pow(10, exp);
> } else if (exp < 0) {
> exp = abs(exp);
> if (exp > 9) {
> *val0 = *val1 = 0;
> return;
> }
> - *val0 = scale0 / pow_10(exp);
> - rem = scale0 % pow_10(exp);
> + *val0 = scale0 / int_pow(10, exp);
> + rem = scale0 % int_pow(10, exp);
> res = 0;
> for (i = 0; i < (9 - exp); ++i) {
> - x = scale1 / pow_10(8 - i);
> - res += (pow_10(8 - exp - i) * x);
> - scale1 = scale1 % pow_10(8 - i);
> + x = scale1 / int_pow(10, 8 - i);
> + res += int_pow(10, 8 - exp - i) * x;
> + scale1 = scale1 % int_pow(10, 8 - i);
> }
> - *val1 = rem * pow_10(9 - exp) + res;
> + *val1 = rem * int_pow(10, 9 - exp) + res;
> } else {
> *val0 = scale0;
> *val1 = scale1;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow()
2019-06-22 9:06 ` Jonathan Cameron
@ 2019-08-13 14:43 ` Andy Shevchenko
2019-08-18 19:22 ` Jonathan Cameron
0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2019-08-13 14:43 UTC (permalink / raw)
To: Jonathan Cameron
Cc: linux-iio, Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald-Stadler
On Sat, Jun 22, 2019 at 10:06:42AM +0100, Jonathan Cameron wrote:
> On Wed, 19 Jun 2019 17:07:02 +0300
> Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>
> > Instead of linear approach to calculate power of 10, use generic int_pow()
> > which does it better.
> >
> > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Hi Andy,
>
> Looks good to me, but I would like Srinivas to take a look and
> preferably test this a bit just to check we aren't all missing something
> subtle!
>
> +CC Srinivas
Srinivas, do you have any comments on this?
>
> Thanks,
>
> Jonathan
> > ---
> > .../hid-sensors/hid-sensor-attributes.c | 53 ++++++++-----------
> > 1 file changed, 22 insertions(+), 31 deletions(-)
> >
> > diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > index a8a3fe428d8d..b9dd19b34267 100644
> > --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > @@ -8,6 +8,7 @@
> > #include <linux/module.h>
> > #include <linux/interrupt.h>
> > #include <linux/irq.h>
> > +#include <linux/kernel.h>
> > #include <linux/slab.h>
> > #include <linux/hid-sensor-hub.h>
> > #include <linux/iio/iio.h>
> > @@ -68,16 +69,6 @@ static struct {
> > {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
> > };
> >
> > -static int pow_10(unsigned power)
> > -{
> > - int i;
> > - int ret = 1;
> > - for (i = 0; i < power; ++i)
> > - ret = ret * 10;
> > -
> > - return ret;
> > -}
> > -
> > static void simple_div(int dividend, int divisor, int *whole,
> > int *micro_frac)
> > {
> > @@ -96,14 +87,14 @@ static void simple_div(int dividend, int divisor, int *whole,
> > rem *= 10;
> > exp++;
> > }
> > - *micro_frac = (rem / divisor) * pow_10(6-exp);
> > + *micro_frac = (rem / divisor) * int_pow(10, 6 - exp);
> > }
> > }
> >
> > static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
> > {
> > - *val1 = no/pow_10(exp);
> > - *val2 = no%pow_10(exp) * pow_10(6-exp);
> > + *val1 = no / int_pow(10, exp);
> > + *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
> > }
> >
> > /*
> > @@ -125,7 +116,7 @@ static void convert_from_vtf_format(u32 value, int size, int exp,
> > }
> > exp = hid_sensor_convert_exponent(exp);
> > if (exp >= 0) {
> > - *val1 = sign * value * pow_10(exp);
> > + *val1 = sign * value * int_pow(10, exp);
> > *val2 = 0;
> > } else {
> > split_micro_fraction(value, -exp, val1, val2);
> > @@ -145,10 +136,10 @@ 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) {
> > - value = abs(val1) * pow_10(-exp);
> > - value += abs(val2) / pow_10(6+exp);
> > + value = abs(val1) * int_pow(10, -exp);
> > + value += abs(val2) / int_pow(10, 6 + exp);
> > } else
> > - value = abs(val1) / pow_10(exp);
> > + value = abs(val1) / int_pow(10, exp);
> > if (sign < 0)
> > value = ((1LL << (size * 8)) - value);
> >
> > @@ -211,12 +202,12 @@ int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
> > if (val1 < 0 || val2 < 0)
> > return -EINVAL;
> >
> > - value = val1 * pow_10(6) + val2;
> > + value = val1 * int_pow(10, 6) + val2;
> > if (value) {
> > if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
> > - value = pow_10(9)/value;
> > + value = int_pow(10, 9) / value;
> > else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
> > - value = pow_10(6)/value;
> > + value = int_pow(10, 6) / value;
> > else
> > value = 0;
> > }
> > @@ -311,34 +302,34 @@ static void adjust_exponent_nano(int *val0, int *val1, int scale0,
> > int rem;
> >
> > if (exp > 0) {
> > - *val0 = scale0 * pow_10(exp);
> > + *val0 = scale0 * int_pow(10, exp);
> > res = 0;
> > if (exp > 9) {
> > *val1 = 0;
> > return;
> > }
> > for (i = 0; i < exp; ++i) {
> > - x = scale1 / pow_10(8 - i);
> > - res += (pow_10(exp - 1 - i) * x);
> > - scale1 = scale1 % pow_10(8 - i);
> > + x = scale1 / int_pow(10, 8 - i);
> > + res += int_pow(10, exp - 1 - i) * x;
> > + scale1 = scale1 % int_pow(10, 8 - i);
> > }
> > *val0 += res;
> > - *val1 = scale1 * pow_10(exp);
> > + *val1 = scale1 * int_pow(10, exp);
> > } else if (exp < 0) {
> > exp = abs(exp);
> > if (exp > 9) {
> > *val0 = *val1 = 0;
> > return;
> > }
> > - *val0 = scale0 / pow_10(exp);
> > - rem = scale0 % pow_10(exp);
> > + *val0 = scale0 / int_pow(10, exp);
> > + rem = scale0 % int_pow(10, exp);
> > res = 0;
> > for (i = 0; i < (9 - exp); ++i) {
> > - x = scale1 / pow_10(8 - i);
> > - res += (pow_10(8 - exp - i) * x);
> > - scale1 = scale1 % pow_10(8 - i);
> > + x = scale1 / int_pow(10, 8 - i);
> > + res += int_pow(10, 8 - exp - i) * x;
> > + scale1 = scale1 % int_pow(10, 8 - i);
> > }
> > - *val1 = rem * pow_10(9 - exp) + res;
> > + *val1 = rem * int_pow(10, 9 - exp) + res;
> > } else {
> > *val0 = scale0;
> > *val1 = scale1;
>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow()
2019-08-13 14:43 ` Andy Shevchenko
@ 2019-08-18 19:22 ` Jonathan Cameron
2019-08-19 2:48 ` Pandruvada, Srinivas
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2019-08-18 19:22 UTC (permalink / raw)
To: Andy Shevchenko
Cc: linux-iio, Hartmut Knaack, Lars-Peter Clausen,
Peter Meerwald-Stadler, Srinivas Pandruvada
On Tue, 13 Aug 2019 17:43:15 +0300
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> On Sat, Jun 22, 2019 at 10:06:42AM +0100, Jonathan Cameron wrote:
> > On Wed, 19 Jun 2019 17:07:02 +0300
> > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> >
> > > Instead of linear approach to calculate power of 10, use generic int_pow()
> > > which does it better.
> > >
> > > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Hi Andy,
> >
> > Looks good to me, but I would like Srinivas to take a look and
> > preferably test this a bit just to check we aren't all missing something
> > subtle!
> >
> > +CC Srinivas
>
> Srinivas, do you have any comments on this?
I think Srinivas got dropped from your CC list for some reason.
Anyhow, it's been a while so let's conclude Srinivas is busy and
rely on it being as obviously correct as it seems to be ;)
Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to play with it.
Thanks,
Jonathan
>
> >
> > Thanks,
> >
> > Jonathan
> > > ---
> > > .../hid-sensors/hid-sensor-attributes.c | 53 ++++++++-----------
> > > 1 file changed, 22 insertions(+), 31 deletions(-)
> > >
> > > diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > > index a8a3fe428d8d..b9dd19b34267 100644
> > > --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > > +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > > @@ -8,6 +8,7 @@
> > > #include <linux/module.h>
> > > #include <linux/interrupt.h>
> > > #include <linux/irq.h>
> > > +#include <linux/kernel.h>
> > > #include <linux/slab.h>
> > > #include <linux/hid-sensor-hub.h>
> > > #include <linux/iio/iio.h>
> > > @@ -68,16 +69,6 @@ static struct {
> > > {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
> > > };
> > >
> > > -static int pow_10(unsigned power)
> > > -{
> > > - int i;
> > > - int ret = 1;
> > > - for (i = 0; i < power; ++i)
> > > - ret = ret * 10;
> > > -
> > > - return ret;
> > > -}
> > > -
> > > static void simple_div(int dividend, int divisor, int *whole,
> > > int *micro_frac)
> > > {
> > > @@ -96,14 +87,14 @@ static void simple_div(int dividend, int divisor, int *whole,
> > > rem *= 10;
> > > exp++;
> > > }
> > > - *micro_frac = (rem / divisor) * pow_10(6-exp);
> > > + *micro_frac = (rem / divisor) * int_pow(10, 6 - exp);
> > > }
> > > }
> > >
> > > static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
> > > {
> > > - *val1 = no/pow_10(exp);
> > > - *val2 = no%pow_10(exp) * pow_10(6-exp);
> > > + *val1 = no / int_pow(10, exp);
> > > + *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
> > > }
> > >
> > > /*
> > > @@ -125,7 +116,7 @@ static void convert_from_vtf_format(u32 value, int size, int exp,
> > > }
> > > exp = hid_sensor_convert_exponent(exp);
> > > if (exp >= 0) {
> > > - *val1 = sign * value * pow_10(exp);
> > > + *val1 = sign * value * int_pow(10, exp);
> > > *val2 = 0;
> > > } else {
> > > split_micro_fraction(value, -exp, val1, val2);
> > > @@ -145,10 +136,10 @@ 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) {
> > > - value = abs(val1) * pow_10(-exp);
> > > - value += abs(val2) / pow_10(6+exp);
> > > + value = abs(val1) * int_pow(10, -exp);
> > > + value += abs(val2) / int_pow(10, 6 + exp);
> > > } else
> > > - value = abs(val1) / pow_10(exp);
> > > + value = abs(val1) / int_pow(10, exp);
> > > if (sign < 0)
> > > value = ((1LL << (size * 8)) - value);
> > >
> > > @@ -211,12 +202,12 @@ int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
> > > if (val1 < 0 || val2 < 0)
> > > return -EINVAL;
> > >
> > > - value = val1 * pow_10(6) + val2;
> > > + value = val1 * int_pow(10, 6) + val2;
> > > if (value) {
> > > if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
> > > - value = pow_10(9)/value;
> > > + value = int_pow(10, 9) / value;
> > > else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
> > > - value = pow_10(6)/value;
> > > + value = int_pow(10, 6) / value;
> > > else
> > > value = 0;
> > > }
> > > @@ -311,34 +302,34 @@ static void adjust_exponent_nano(int *val0, int *val1, int scale0,
> > > int rem;
> > >
> > > if (exp > 0) {
> > > - *val0 = scale0 * pow_10(exp);
> > > + *val0 = scale0 * int_pow(10, exp);
> > > res = 0;
> > > if (exp > 9) {
> > > *val1 = 0;
> > > return;
> > > }
> > > for (i = 0; i < exp; ++i) {
> > > - x = scale1 / pow_10(8 - i);
> > > - res += (pow_10(exp - 1 - i) * x);
> > > - scale1 = scale1 % pow_10(8 - i);
> > > + x = scale1 / int_pow(10, 8 - i);
> > > + res += int_pow(10, exp - 1 - i) * x;
> > > + scale1 = scale1 % int_pow(10, 8 - i);
> > > }
> > > *val0 += res;
> > > - *val1 = scale1 * pow_10(exp);
> > > + *val1 = scale1 * int_pow(10, exp);
> > > } else if (exp < 0) {
> > > exp = abs(exp);
> > > if (exp > 9) {
> > > *val0 = *val1 = 0;
> > > return;
> > > }
> > > - *val0 = scale0 / pow_10(exp);
> > > - rem = scale0 % pow_10(exp);
> > > + *val0 = scale0 / int_pow(10, exp);
> > > + rem = scale0 % int_pow(10, exp);
> > > res = 0;
> > > for (i = 0; i < (9 - exp); ++i) {
> > > - x = scale1 / pow_10(8 - i);
> > > - res += (pow_10(8 - exp - i) * x);
> > > - scale1 = scale1 % pow_10(8 - i);
> > > + x = scale1 / int_pow(10, 8 - i);
> > > + res += int_pow(10, 8 - exp - i) * x;
> > > + scale1 = scale1 % int_pow(10, 8 - i);
> > > }
> > > - *val1 = rem * pow_10(9 - exp) + res;
> > > + *val1 = rem * int_pow(10, 9 - exp) + res;
> > > } else {
> > > *val0 = scale0;
> > > *val1 = scale1;
> >
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow()
2019-08-18 19:22 ` Jonathan Cameron
@ 2019-08-19 2:48 ` Pandruvada, Srinivas
0 siblings, 0 replies; 5+ messages in thread
From: Pandruvada, Srinivas @ 2019-08-19 2:48 UTC (permalink / raw)
To: andriy.shevchenko, jic23; +Cc: knaack.h, lars, linux-iio, pmeerw
[-- Attachment #1: Type: text/plain, Size: 6993 bytes --]
On Sun, 2019-08-18 at 20:22 +0100, Jonathan Cameron wrote:
> On Tue, 13 Aug 2019 17:43:15 +0300
> Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>
> > On Sat, Jun 22, 2019 at 10:06:42AM +0100, Jonathan Cameron wrote:
> > > On Wed, 19 Jun 2019 17:07:02 +0300
> > > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > >
> > > > Instead of linear approach to calculate power of 10, use
> > > > generic int_pow()
> > > > which does it better.
> > > > */
> > > > u64 int_pow(u64 base, unsigned int exp)
> > > > {
> > > > u64 result = 1;
> > > >
> > > > while (exp) {
> > > > if (exp & 1)
> > > > result *= base;
> > > > exp >>= 1;
> > > > base *= base;
> > > > }
> > > >
> > > > return result;
> > > > }
> > > > Signed-off-by: Andy Shevchenko <
> > > > andriy.shevchenko@linux.intel.com>
> > >
Hi Jonathan,
> > > Hi Andy,
> > >
> > > Looks good to me, but I would like Srinivas to take a look and
> > > preferably test this a bit just to check we aren't all missing
> > > something
> > > subtle!
> > >
> > > +CC Srinivas
> >
> > Srinivas, do you have any comments on this?
>
> I think Srinivas got dropped from your CC list for some reason.
>
> Anyhow, it's been a while so let's conclude Srinivas is busy and
> rely on it being as obviously correct as it seems to be ;)
Looks correct to me.
I will be LPC, so hope to see you if you are there.
Thanks,
Srinivas
>
> Applied to the togreg branch of iio.git and pushed out as testing for
> the autobuilders to play with it.
>
> Thanks,
>
> Jonathan
>
> >
> > >
> > > Thanks,
> > >
> > > Jonathan
> > > > ---
> > > > .../hid-sensors/hid-sensor-attributes.c | 53 ++++++++---
> > > > --------
> > > > 1 file changed, 22 insertions(+), 31 deletions(-)
> > > >
> > > > diff --git a/drivers/iio/common/hid-sensors/hid-sensor-
> > > > attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-
> > > > attributes.c
> > > > index a8a3fe428d8d..b9dd19b34267 100644
> > > > --- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > > > +++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
> > > > @@ -8,6 +8,7 @@
> > > > #include <linux/module.h>
> > > > #include <linux/interrupt.h>
> > > > #include <linux/irq.h>
> > > > +#include <linux/kernel.h>
> > > > #include <linux/slab.h>
> > > > #include <linux/hid-sensor-hub.h>
> > > > #include <linux/iio/iio.h>
> > > > @@ -68,16 +69,6 @@ static struct {
> > > > {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
> > > > };
> > > >
> > > > -static int pow_10(unsigned power)
> > > > -{
> > > > - int i;
> > > > - int ret = 1;
> > > > - for (i = 0; i < power; ++i)
> > > > - ret = ret * 10;
> > > > -
> > > > - return ret;
> > > > -}
> > > > -
> > > > static void simple_div(int dividend, int divisor, int *whole,
> > > > int *micro_frac)
> > > > {
> > > > @@ -96,14 +87,14 @@ static void simple_div(int dividend, int
> > > > divisor, int *whole,
> > > > rem *= 10;
> > > > exp++;
> > > > }
> > > > - *micro_frac = (rem / divisor) * pow_10(6-exp);
> > > > + *micro_frac = (rem / divisor) * int_pow(10, 6 -
> > > > exp);
> > > > }
> > > > }
> > > >
> > > > static void split_micro_fraction(unsigned int no, int exp, int
> > > > *val1, int *val2)
> > > > {
> > > > - *val1 = no/pow_10(exp);
> > > > - *val2 = no%pow_10(exp) * pow_10(6-exp);
> > > > + *val1 = no / int_pow(10, exp);
> > > > + *val2 = no % int_pow(10, exp) * int_pow(10, 6 - exp);
> > > > }
> > > >
> > > > /*
> > > > @@ -125,7 +116,7 @@ static void convert_from_vtf_format(u32
> > > > value, int size, int exp,
> > > > }
> > > > exp = hid_sensor_convert_exponent(exp);
> > > > if (exp >= 0) {
> > > > - *val1 = sign * value * pow_10(exp);
> > > > + *val1 = sign * value * int_pow(10, exp);
> > > > *val2 = 0;
> > > > } else {
> > > > split_micro_fraction(value, -exp, val1, val2);
> > > > @@ -145,10 +136,10 @@ 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) {
> > > > - value = abs(val1) * pow_10(-exp);
> > > > - value += abs(val2) / pow_10(6+exp);
> > > > + value = abs(val1) * int_pow(10, -exp);
> > > > + value += abs(val2) / int_pow(10, 6 + exp);
> > > > } else
> > > > - value = abs(val1) / pow_10(exp);
> > > > + value = abs(val1) / int_pow(10, exp);
> > > > if (sign < 0)
> > > > value = ((1LL << (size * 8)) - value);
> > > >
> > > > @@ -211,12 +202,12 @@ int
> > > > hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
> > > > if (val1 < 0 || val2 < 0)
> > > > return -EINVAL;
> > > >
> > > > - value = val1 * pow_10(6) + val2;
> > > > + value = val1 * int_pow(10, 6) + val2;
> > > > if (value) {
> > > > if (st->poll.units ==
> > > > HID_USAGE_SENSOR_UNITS_MILLISECOND)
> > > > - value = pow_10(9)/value;
> > > > + value = int_pow(10, 9) / value;
> > > > else if (st->poll.units ==
> > > > HID_USAGE_SENSOR_UNITS_SECOND)
> > > > - value = pow_10(6)/value;
> > > > + value = int_pow(10, 6) / value;
> > > > else
> > > > value = 0;
> > > > }
> > > > @@ -311,34 +302,34 @@ static void adjust_exponent_nano(int
> > > > *val0, int *val1, int scale0,
> > > > int rem;
> > > >
> > > > if (exp > 0) {
> > > > - *val0 = scale0 * pow_10(exp);
> > > > + *val0 = scale0 * int_pow(10, exp);
> > > > res = 0;
> > > > if (exp > 9) {
> > > > *val1 = 0;
> > > > return;
> > > > }
> > > > for (i = 0; i < exp; ++i) {
> > > > - x = scale1 / pow_10(8 - i);
> > > > - res += (pow_10(exp - 1 - i) * x);
> > > > - scale1 = scale1 % pow_10(8 - i);
> > > > + x = scale1 / int_pow(10, 8 - i);
> > > > + res += int_pow(10, exp - 1 - i) * x;
> > > > + scale1 = scale1 % int_pow(10, 8 - i);
> > > > }
> > > > *val0 += res;
> > > > - *val1 = scale1 * pow_10(exp);
> > > > + *val1 = scale1 * int_pow(10, exp);
> > > > } else if (exp < 0) {
> > > > exp = abs(exp);
> > > > if (exp > 9) {
> > > > *val0 = *val1 = 0;
> > > > return;
> > > > }
> > > > - *val0 = scale0 / pow_10(exp);
> > > > - rem = scale0 % pow_10(exp);
> > > > + *val0 = scale0 / int_pow(10, exp);
> > > > + rem = scale0 % int_pow(10, exp);
> > > > res = 0;
> > > > for (i = 0; i < (9 - exp); ++i) {
> > > > - x = scale1 / pow_10(8 - i);
> > > > - res += (pow_10(8 - exp - i) * x);
> > > > - scale1 = scale1 % pow_10(8 - i);
> > > > + x = scale1 / int_pow(10, 8 - i);
> > > > + res += int_pow(10, 8 - exp - i) * x;
> > > > + scale1 = scale1 % int_pow(10, 8 - i);
> > > > }
> > > > - *val1 = rem * pow_10(9 - exp) + res;
> > > > + *val1 = rem * int_pow(10, 9 - exp) + res;
> > > > } else {
> > > > *val0 = scale0;
> > > > *val1 = scale1;
> > >
> > >
>
>
[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3290 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-08-19 2:48 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-19 14:07 [PATCH v1] iio: hid-sensor-attributes: Convert to use int_pow() Andy Shevchenko
2019-06-22 9:06 ` Jonathan Cameron
2019-08-13 14:43 ` Andy Shevchenko
2019-08-18 19:22 ` Jonathan Cameron
2019-08-19 2:48 ` 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).