From: Guenter Roeck <linux@roeck-us.net>
To: Robert Lippert <roblip@gmail.com>
Cc: linux-hwmon@vger.kernel.org, jdelvare@suse.com,
linux-kernel@vger.kernel.org, xow@google.com,
Robert Lippert <rlippert@google.com>
Subject: Re: [PATCH] hwmon: (pmbus) Use 64bit math for DIRECT format values
Date: Wed, 22 Nov 2017 14:28:09 -0800 [thread overview]
Message-ID: <20171122222809.GA2951@roeck-us.net> (raw)
In-Reply-To: <20171122220843.18309-1-rlippert@google.com>
On Wed, Nov 22, 2017 at 02:08:43PM -0800, Robert Lippert wrote:
> Power values in the 100s of watt range can easily blow past
> 32bit math limits when processing everything in microwatts.
>
> Use 64bit math instead to avoid these issues on common 32bit ARM
> BMC platforms.
>
> Signed-off-by: Robert Lippert <rlippert@google.com>
> ---
> drivers/hwmon/pmbus/pmbus_core.c | 21 ++++++++++++---------
> 1 file changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
> index 52a58b8b6e1b..f4efea9f282e 100644
> --- a/drivers/hwmon/pmbus/pmbus_core.c
> +++ b/drivers/hwmon/pmbus/pmbus_core.c
> @@ -499,8 +499,8 @@ static long pmbus_reg2data_linear(struct pmbus_data *data,
> static long pmbus_reg2data_direct(struct pmbus_data *data,
> struct pmbus_sensor *sensor)
> {
> - long val = (s16) sensor->data;
> - long m, b, R;
> + s64 val = (s16) sensor->data;
> + s64 m, b, R;
>
> m = data->info->m[sensor->class];
> b = data->info->b[sensor->class];
> @@ -528,11 +528,13 @@ static long pmbus_reg2data_direct(struct pmbus_data *data,
> R--;
> }
> while (R < 0) {
> - val = DIV_ROUND_CLOSEST(val, 10);
> + do_div(val, 10);
Any reason to not use DIV_ROUND_CLOSEST_ULL ?
> R++;
> }
>
> - return (val - b) / m;
> + val = val - b;
> + do_div(val, m);
> + return val;
Can this overflow ?
> }
>
> /*
> @@ -656,7 +658,8 @@ static u16 pmbus_data2reg_linear(struct pmbus_data *data,
> static u16 pmbus_data2reg_direct(struct pmbus_data *data,
> struct pmbus_sensor *sensor, long val)
> {
> - long m, b, R;
> + s64 m, b, R;
> + s64 val64 = val;
>
> m = data->info->m[sensor->class];
> b = data->info->b[sensor->class];
> @@ -673,18 +676,18 @@ static u16 pmbus_data2reg_direct(struct pmbus_data *data,
> R -= 3; /* Adjust R and b for data in milli-units */
> b *= 1000;
> }
> - val = val * m + b;
> + val64 = val64 * m + b;
>
> while (R > 0) {
> - val *= 10;
> + val64 *= 10;
> R--;
> }
> while (R < 0) {
> - val = DIV_ROUND_CLOSEST(val, 10);
> + do_div(val64, 10);
Same here.
> R++;
> }
>
> - return val;
> + return (u16) val64;
Can this overflow ?
> }
>
> static u16 pmbus_data2reg_vid(struct pmbus_data *data,
> --
> 2.15.0.448.gf294e3d99a-goog
>
next prev parent reply other threads:[~2017-11-22 22:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-22 22:08 [PATCH] hwmon: (pmbus) Use 64bit math for DIRECT format values Robert Lippert
2017-11-22 22:28 ` Guenter Roeck [this message]
2017-11-27 21:42 ` Rob Lippert
2017-11-25 20:28 ` kbuild test robot
2017-11-27 21:39 ` [PATCH v2] " Robert Lippert
2017-11-27 22:11 ` Guenter Roeck
2017-11-27 23:51 ` Rob Lippert
2017-11-27 23:51 ` [PATCH v3] " Robert Lippert
2017-11-28 4:12 ` [v3] " Guenter Roeck
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=20171122222809.GA2951@roeck-us.net \
--to=linux@roeck-us.net \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rlippert@google.com \
--cc=roblip@gmail.com \
--cc=xow@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox