All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] thermal: rcar: reduce inaccuracy in the calculate rounding for temperature conversion
@ 2018-08-30  2:39 Nguyen An Hoan
  2018-08-30  2:39 ` [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding Nguyen An Hoan
  0 siblings, 1 reply; 4+ messages in thread
From: Nguyen An Hoan @ 2018-08-30  2:39 UTC (permalink / raw)
  To: broonie, linux-renesas-soc, geert+renesas
  Cc: wsa, niklas.soderlund+renesas, kuninori.morimoto.gx,
	yoshihiro.shimoda.uh, magnus.damm, h-inayoshi, nv-dung, cv-dong,
	na-hoan

From: Hoan Nguyen An <na-hoan@jinso.co.jp>

This patch to avoid and reduce the inaccuracy in the calculation when DIV_ROUND() and then calculates the conversion, I think, should change the order: calculate the conversion and then DIV_ROUND().
Although I'm not sure there will be an error, but I have encountered problems when troubleshooting interrupts not counting up.

Hoan Nguyen An (1):
  thermal: rcar: reduce inaccuracy in calculate rounding

 drivers/thermal/rcar_gen3_thermal.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

-- 
2.7.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding
  2018-08-30  2:39 [PATCH] thermal: rcar: reduce inaccuracy in the calculate rounding for temperature conversion Nguyen An Hoan
@ 2018-08-30  2:39 ` Nguyen An Hoan
  2018-08-30  7:48   ` Geert Uytterhoeven
  2018-08-30  9:39   ` Sergei Shtylyov
  0 siblings, 2 replies; 4+ messages in thread
From: Nguyen An Hoan @ 2018-08-30  2:39 UTC (permalink / raw)
  To: broonie, linux-renesas-soc, geert+renesas
  Cc: wsa, niklas.soderlund+renesas, kuninori.morimoto.gx,
	yoshihiro.shimoda.uh, magnus.damm, h-inayoshi, nv-dung, cv-dong,
	na-hoan

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;
 
 	return INT_FIXPT((val1 + val2) / 2);
 }
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding
  2018-08-30  2:39 ` [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding Nguyen An Hoan
@ 2018-08-30  7:48   ` Geert Uytterhoeven
  2018-08-30  9:39   ` Sergei Shtylyov
  1 sibling, 0 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2018-08-30  7:48 UTC (permalink / raw)
  To: Hoan Nguyen An
  Cc: Mark Brown, Linux-Renesas, Geert Uytterhoeven, Wolfram Sang,
	Niklas Söderlund, Kuninori Morimoto, Yoshihiro Shimoda,
	Magnus Damm, 稲吉, Dung:人ソ,
	カオ・ヴァン・ドン

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

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding
  2018-08-30  2:39 ` [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding Nguyen An Hoan
  2018-08-30  7:48   ` Geert Uytterhoeven
@ 2018-08-30  9:39   ` Sergei Shtylyov
  1 sibling, 0 replies; 4+ messages in thread
From: Sergei Shtylyov @ 2018-08-30  9:39 UTC (permalink / raw)
  To: Nguyen An Hoan, broonie, linux-renesas-soc, geert+renesas
  Cc: wsa, niklas.soderlund+renesas, kuninori.morimoto.gx,
	yoshihiro.shimoda.uh, magnus.damm, h-inayoshi, nv-dung, cv-dong

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-08-30 13:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-30  2:39 [PATCH] thermal: rcar: reduce inaccuracy in the calculate rounding for temperature conversion Nguyen An Hoan
2018-08-30  2:39 ` [PATCH] thermal: rcar: reduce inaccuracy in calculate rounding Nguyen An Hoan
2018-08-30  7:48   ` Geert Uytterhoeven
2018-08-30  9:39   ` Sergei Shtylyov

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.