All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Nathan Chancellor <natechancellor@gmail.com>,
	linux-iio@vger.kernel.org, Jonathan Cameron <jic23@kernel.org>,
	Hartmut Knaack <knaack.h@gmx.de>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald-Stadler <pmeerw@pmeerw.net>,
	Srinivas Pandruvada <srinivas.pandruvada@intel.com>,
	Greg KH <gregkh@linuxfoundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	kbuild test robot <lkp@intel.com>
Subject: [PATCH v1] iio: hid-sensor-attributes: Fix divisions for 32-bit platforms
Date: Thu,  5 Sep 2019 14:27:59 +0300	[thread overview]
Message-ID: <20190905112759.13035-1-andriy.shevchenko@linux.intel.com> (raw)

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


             reply	other threads:[~2019-09-05 11:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05 11:27 Andy Shevchenko [this message]
2019-09-06  1:35 ` [PATCH v1] iio: hid-sensor-attributes: Fix divisions for 32-bit platforms Pandruvada, Srinivas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190905112759.13035-1-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=natechancellor@gmail.com \
    --cc=pmeerw@pmeerw.net \
    --cc=srinivas.pandruvada@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.