Received: by 10.223.164.202 with SMTP id h10csp2344963wrb; Mon, 27 Nov 2017 15:54:17 -0800 (PST) X-Google-Smtp-Source: AGs4zMbqc2q4ZA26fELd4As6AVA1B3PcOPN+rRk7BMowzjvwsi9hjzH5rwMt0SNSYoM+9kdu5Fpe X-Received: by 10.99.95.13 with SMTP id t13mr37948486pgb.448.1511826856895; Mon, 27 Nov 2017 15:54:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511826856; cv=none; d=google.com; s=arc-20160816; b=ZqZ5nvELoFcSL49X2oY4BeDqV0NjxExm1YOw8WsgBE/PPDLNQh+gob/2Ol7XmloO/R aO6b9rlpVip+BKgyFLyvJyEh3ioME/K4v80YFVgqPZTITj04cDNZaY9+N7KZjLQMJXT2 kmlwfRckZZrgK5+5POI/FWb+DmPPp77anxH8ByuZ2oUEHZSlHUGuCuggpygCRfcECo6x V9Kh2tFd1Xc6WBjmKMSDlLs3wX6ybwGqGa6ZeIHy4feXEZqvPHcijn3OfeT5tiQsVwzK ozqw4bJSWVpsVd8SD7Cqhd295uLMOvCk9+iBhwV2qVyM1K7lbzcLpf0epNQuCLZGkYYK 1+0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=d25U3uzJ2VqNjYY3XIdgoSk+unpfJS6pRLgag9daaUs=; b=uJZvUPq5FN3U9+SH4n0OKhljWifWTOdVG0e/kPLkYRPr++rlbW209Ksml1LMYgYkgo JVGjdrpfdNlnuWZHMM6TH1CR8pAVhqO90AB8/H7pMAvbqISrlZq+Ly3XUmyij06Ow7XP qTb6q2GW9Y9Bx5CASVr5Wr3Mxf8dO07XCr2gm0ogclFK+Gr3KF2z7e+/GJsYZXqhvp7A CPlAL5XD6kENa/21z/QAn9wx4Wqq3n37WsGbfqkDmSz/QFnBPaCDoaJhPXYA4ebb0pMQ 2vHU+6YFqsfbQNpQ+DzDhlTHethLdRxHWW7YZr+275u9Nq0+iJOFi8jFaScRUFOrtQH9 XCWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ct/17BeO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e10si23692604pgc.554.2017.11.27.15.54.05; Mon, 27 Nov 2017 15:54:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ct/17BeO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752849AbdK0XwI (ORCPT + 78 others); Mon, 27 Nov 2017 18:52:08 -0500 Received: from mail-qt0-f193.google.com ([209.85.216.193]:45891 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752357AbdK0XwG (ORCPT ); Mon, 27 Nov 2017 18:52:06 -0500 Received: by mail-qt0-f193.google.com with SMTP id r37so33415827qtj.12 for ; Mon, 27 Nov 2017 15:52:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=d25U3uzJ2VqNjYY3XIdgoSk+unpfJS6pRLgag9daaUs=; b=Ct/17BeOQlXJTF1rBeirxBEfpsIts2qt6MbzgBkQaJ122L9i4f8whpevSL1P2rc/N3 /XchehcXxoC1hCVJklukCkUURN7wsVp9gxBjrdLmpnevJj2SCMBcFZvk23PKM6PHLjaz CQaIxdPSrakKli0HQw96RMlBhd0TlJibKJhRkqI6xLFi99KvcBZtW2pUYh+EK0XM1tYi +zYuoEJ8uUYsTUeoGJV+30P2BAgLix2EypzTHRipvgGul/Gzad2cD6aQENDmj6IZdIyW ivHWM3kzqgwnvlgXrCa0LRNID9Yzy11XKb1oPU4wvJblMZMfZkiuLzIEET/kvdEuW9vD 6LhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=d25U3uzJ2VqNjYY3XIdgoSk+unpfJS6pRLgag9daaUs=; b=AWjzWKdeU4cAPA9ZKCcaTzmFCIwYulmHltpQgOFgMrsLOym8slwHnqQS2hMFg9iSzK os1RswEXr4AH/8o/z+Mgk9ExboNCw0/GdsxzZno4ER95r6LLEdAmMpm95Fc9IaiICkzD wG6BWb8/jdp5vmm1VrfIpMt9dG62DIWv/11426JFXz3HG6FEttbXAiTsEv7d1k9Ldcdt XruzOeAfxEJ9FH3enhAAO6fOm48lp9LAb07pQyJgljOqmIBrQd3rXf+dqmJwlX5BL01X QoGqP61GtYPCANQsAaPOai9kPPEqwbNYSP8SgiKXljbtxOvHDu23NcT+3M1Ez3qo+/Er ZGOw== X-Gm-Message-State: AJaThX4h/OXTvJ3Ds7CP2vbjq5zVibY+kEfcUxQ6Z4xFmJwJ4uvm54B8 L3KUigti+n8FG07DiPSM41b/8wF85HBLwlvMZcfS+A== X-Received: by 10.200.25.134 with SMTP id u6mr58723374qtj.221.1511826725785; Mon, 27 Nov 2017 15:52:05 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.35.17 with HTTP; Mon, 27 Nov 2017 15:51:45 -0800 (PST) In-Reply-To: <20171127221135.GB3848@roeck-us.net> References: <20171122220843.18309-1-rlippert@google.com> <20171127213914.141687-1-rlippert@google.com> <20171127221135.GB3848@roeck-us.net> From: Rob Lippert Date: Mon, 27 Nov 2017 15:51:45 -0800 Message-ID: Subject: Re: [PATCH v2] hwmon: (pmbus) Use 64bit math for DIRECT format values To: Guenter Roeck Cc: Robert Lippert , linux-hwmon@vger.kernel.org, jdelvare@suse.com, linux-kernel@vger.kernel.org, Xo Wang Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 27, 2017 at 2:11 PM, Guenter Roeck wrote: > On Mon, Nov 27, 2017 at 01:39:14PM -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 >> --- >> drivers/hwmon/pmbus/pmbus_core.c | 23 ++++++++++++++--------- >> 1 file changed, 14 insertions(+), 9 deletions(-) >> >> diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c >> index 52a58b8b6e1b..ff701502974a 100644 >> --- a/drivers/hwmon/pmbus/pmbus_core.c >> +++ b/drivers/hwmon/pmbus/pmbus_core.c >> @@ -21,6 +21,7 @@ >> >> #include >> #include >> +#include >> #include >> #include >> #include >> @@ -499,8 +500,9 @@ 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 b, R; > > Do b and R have to be s64 ? Seems excessive. b has a range of -32767 to 32768 and is multiplied in the code below by 1,000,000 for a power sensor so I think needs to be 64bit to avoid overflow. R has limited range so changed in v3 to s32 type. > >> + s32 m; >> >> m = data->info->m[sensor->class]; >> b = data->info->b[sensor->class]; >> @@ -528,11 +530,12 @@ static long pmbus_reg2data_direct(struct pmbus_data *data, >> R--; >> } >> while (R < 0) { >> - val = DIV_ROUND_CLOSEST(val, 10); >> + val = div_s64(val + 5LL, 10L); // round closest (also fixed up in v3 this C++ style comment that slipped in...) >> R++; >> } >> >> - return (val - b) / m; >> + val = div_s64(val - b, m); >> + return clamp_val(val, LONG_MIN, LONG_MAX); >> } >> >> /* >> @@ -656,7 +659,9 @@ 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 b, R; > > Same question as above - do those have to be s64 ? > >> + s32 m; >> + s64 val64 = val; >> >> m = data->info->m[sensor->class]; >> b = data->info->b[sensor->class]; >> @@ -673,18 +678,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); >> + val64 = div_s64(val64 + 5LL, 10L); // round closest >> R++; >> } >> >> - return val; >> + return (u16) clamp_val(val64, S16_MIN, S16_MAX); >> } >> >> static u16 pmbus_data2reg_vid(struct pmbus_data *data, >> -- >> 2.15.0.417.g466bffb3ac-goog >> >> -- >> To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in >> the body of a message to majordomo@vger.kernel.org >> More majordomo info at http://vger.kernel.org/majordomo-info.html From 1585265302697284605@xxx Mon Nov 27 23:53:23 +0000 2017 X-GM-THRID: 1584805833119531257 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread