diff mbox series

thermal: rcar: reduce inaccuracy in calculate rounding

Message ID 1535596744-16598-2-git-send-email-na-hoan@jinso.co.jp (mailing list archive)
State Under Review
Delegated to: Geert Uytterhoeven
Headers show
Series thermal: rcar: reduce inaccuracy in calculate rounding | expand

Commit Message

グェン・アン・ホァン Aug. 30, 2018, 2:39 a.m. UTC
From: Hoan Nguyen An <na-hoan@jinso.co.jp>

About the formula for temperature calculation
[reg] = [temp] * a + b <=> [temp] = ([reg] - b) / a

Using "(mcelsius * tsc-> coef.aX) / 1000" instead of "DIV_ROUND_CLOSEST(mcelsius, 1000) * tsc-> coef.aX"
to avoid and reduce inaccuracy due to rounding the integer division.

Signed-off-by: Hoan Nguyen An <na-hoan@jinso.co.jp>
---
 drivers/thermal/rcar_gen3_thermal.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

Geert Uytterhoeven Aug. 30, 2018, 7:48 a.m. UTC | #1
Hi Hoan,

On Thu, Aug 30, 2018 at 4:39 AM Nguyen An Hoan <na-hoan@jinso.co.jp> wrote:
> From: Hoan Nguyen An <na-hoan@jinso.co.jp>
>
> About the formula for temperature calculation
> [reg] = [temp] * a + b <=> [temp] = ([reg] - b) / a
>
> Using "(mcelsius * tsc-> coef.aX) / 1000" instead of "DIV_ROUND_CLOSEST(mcelsius, 1000) * tsc-> coef.aX"
> to avoid and reduce inaccuracy due to rounding the integer division.
>
> Signed-off-by: Hoan Nguyen An <na-hoan@jinso.co.jp>

Thanks for your patch!

> --- a/drivers/thermal/rcar_gen3_thermal.c
> +++ b/drivers/thermal/rcar_gen3_thermal.c
> @@ -185,11 +185,10 @@ static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
>  static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
>                                               int mcelsius)
>  {
> -       int celsius, val1, val2;
> +       int val1, val2;
>
> -       celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
> -       val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
> -       val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
> +       val1 = (mcelsius * tsc->coef.a1)/1000 + tsc->coef.b1;
> +       val2 = (mcelsius * tsc->coef.a2)/1000 + tsc->coef.b2;

No rounding anymoe?

    val1 = DIV_ROUND_CLOSEST(mcelsius * tsc->coef.a1, 1000) + tsc->coef.b1;
    val2 = DIV_ROUND_CLOSEST(mcelsius * tsc->coef.a2, 1000) + tsc->coef.b2;

Can the multiplication overflow?

I know mcelsius = -40000..120000/
What is the range of a1 and a2?

>
>         return INT_FIXPT((val1 + val2) / 2);

BTW, do we want to introduce rounding here, too?
(for INT_FIXPT(), it doesn't matter much for /2)?

Gr{oetje,eeting}s,

                        Geert
Sergei Shtylyov Aug. 30, 2018, 9:39 a.m. UTC | #2
Hello!

On 8/30/2018 5:39 AM, Nguyen An Hoan wrote:

> From: Hoan Nguyen An <na-hoan@jinso.co.jp>
> 
> About the formula for temperature calculation
> [reg] = [temp] * a + b <=> [temp] = ([reg] - b) / a
> 
> Using "(mcelsius * tsc-> coef.aX) / 1000" instead of "DIV_ROUND_CLOSEST(mcelsius, 1000) * tsc-> coef.aX"
> to avoid and reduce inaccuracy due to rounding the integer division.
> 
> Signed-off-by: Hoan Nguyen An <na-hoan@jinso.co.jp>
> ---
>   drivers/thermal/rcar_gen3_thermal.c | 7 +++----
>   1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/thermal/rcar_gen3_thermal.c b/drivers/thermal/rcar_gen3_thermal.c
> index 7aed533..dbb31b8 100644
> --- a/drivers/thermal/rcar_gen3_thermal.c
> +++ b/drivers/thermal/rcar_gen3_thermal.c
> @@ -185,11 +185,10 @@ static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
>   static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
>   					      int mcelsius)
>   {
> -	int celsius, val1, val2;
> +	int val1, val2;
>   
> -	celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
> -	val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
> -	val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
> +	val1 = (mcelsius * tsc->coef.a1)/1000 + tsc->coef.b1;
> +	val2 = (mcelsius * tsc->coef.a2)/1000 + tsc->coef.b2;

    Please be consistent and enclose / in sopaces the same as you do for other 
operators.

[...]

MBR, Sergei
diff mbox series

Patch

diff --git a/drivers/thermal/rcar_gen3_thermal.c b/drivers/thermal/rcar_gen3_thermal.c
index 7aed533..dbb31b8 100644
--- a/drivers/thermal/rcar_gen3_thermal.c
+++ b/drivers/thermal/rcar_gen3_thermal.c
@@ -185,11 +185,10 @@  static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
 static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
 					      int mcelsius)
 {
-	int celsius, val1, val2;
+	int val1, val2;
 
-	celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
-	val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
-	val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
+	val1 = (mcelsius * tsc->coef.a1)/1000 + tsc->coef.b1;
+	val2 = (mcelsius * tsc->coef.a2)/1000 + tsc->coef.b2;
 
 	return INT_FIXPT((val1 + val2) / 2);
 }