From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_2 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C238C07E95 for ; Sun, 4 Jul 2021 16:11:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2403613DA for ; Sun, 4 Jul 2021 16:11:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229557AbhGDQOW (ORCPT ); Sun, 4 Jul 2021 12:14:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:40590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229537AbhGDQOW (ORCPT ); Sun, 4 Jul 2021 12:14:22 -0400 Received: from jic23-huawei (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2E0EE61363; Sun, 4 Jul 2021 16:11:44 +0000 (UTC) Date: Sun, 4 Jul 2021 17:14:06 +0100 From: Jonathan Cameron To: Linus Walleij Cc: Jean Delvare , Guenter Roeck , linux-hwmon@vger.kernel.org, Peter Rosin , Chris Lesiak , linux-iio@vger.kernel.org Subject: Re: [PATCH] hwmon: (ntc_thermistor): Use library interpolation Message-ID: <20210704171406.61235987@jic23-huawei> In-Reply-To: <20210703180131.4036589-1-linus.walleij@linaro.org> References: <20210703180131.4036589-1-linus.walleij@linaro.org> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org On Sat, 3 Jul 2021 20:01:31 +0200 Linus Walleij wrote: > The kernel has a helper function for linear interpolation so > use it. It incidentally makes the code easier to read as well. > > Cc: Peter Rosin > Cc: Chris Lesiak > Cc: linux-iio@vger.kernel.org > Signed-off-by: Linus Walleij Potential precision problem because the multiplication by 1000 is now done post division whereas before it was before it? I'm only eyeballing the code so no idea if it makes a practical difference. > --- > drivers/hwmon/ntc_thermistor.c | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c > index 8587189c7f15..61bd0e074ec9 100644 > --- a/drivers/hwmon/ntc_thermistor.c > +++ b/drivers/hwmon/ntc_thermistor.c > @@ -14,6 +14,7 @@ > #include > #include > #include > +#include > > #include > > @@ -557,10 +558,12 @@ static int get_temp_mc(struct ntc_data *data, unsigned int ohm) > /* Unable to use linear approximation */ > temp = data->comp[low].temp_c * 1000; > } else { > - temp = data->comp[low].temp_c * 1000 + > - ((data->comp[high].temp_c - data->comp[low].temp_c) * > - 1000 * ((int)ohm - (int)data->comp[low].ohm)) / > - ((int)data->comp[high].ohm - (int)data->comp[low].ohm); > + temp = fixp_linear_interpolate(data->comp[low].ohm, > + data->comp[low].temp_c, > + data->comp[high].ohm, > + data->comp[high].temp_c, > + ohm); > + temp *= 1000; > } > return temp; > }