From: Rasmus Villemoes <linux@rasmusvillemoes.dk> To: Thierry Reding <thierry.reding@gmail.com>, Lee Jones <lee.jones@linaro.org>, Daniel Thompson <daniel.thompson@linaro.org>, Jingoo Han <jingoohan1@gmail.com>, Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>, linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/5] backlight: pwm_bl: switch to power-of-2 base for fixed-point math Date: Thu, 19 Sep 2019 16:06:19 +0200 [thread overview] Message-ID: <20190919140620.32407-4-linux@rasmusvillemoes.dk> (raw) In-Reply-To: <20190919140620.32407-1-linux@rasmusvillemoes.dk> Using a power-of-2 instead of power-of-10 base makes the computations much cheaper. 2^16 is safe; retval never becomes more than 2^48 + 2^16/2. On a 32 bit platform, the very expensive 64/32 division at the end of cie1931() instead becomes essentially free (a shift by 32 is just a register rename). Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> --- drivers/video/backlight/pwm_bl.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index aee6839e024a..102bc191310f 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -148,7 +148,8 @@ static const struct backlight_ops pwm_backlight_ops = { }; #ifdef CONFIG_OF -#define PWM_LUMINANCE_SCALE 10000 /* luminance scale */ +#define PWM_LUMINANCE_SHIFT 16 +#define PWM_LUMINANCE_SCALE (1 << PWM_LUMINANCE_SHIFT) /* luminance scale */ /* * CIE lightness to PWM conversion. @@ -165,23 +166,25 @@ static const struct backlight_ops pwm_backlight_ops = { * The following function does the fixed point maths needed to implement the * above formula. */ -static u64 cie1931(unsigned int lightness, unsigned int scale) +static u64 cie1931(unsigned int lightness) { u64 retval; /* * @lightness is given as a number between 0 and 1, expressed - * as a fixed-point number in scale @scale. Convert to a - * percentage, still expressed as a fixed-point number, so the - * above formulas can be applied. + * as a fixed-point number in scale + * PWM_LUMINANCE_SCALE. Convert to a percentage, still + * expressed as a fixed-point number, so the above formulas + * can be applied. */ lightness *= 100; - if (lightness <= (8 * scale)) { + if (lightness <= (8 * PWM_LUMINANCE_SCALE)) { retval = DIV_ROUND_CLOSEST(lightness * 10, 9033); } else { - retval = (lightness + (16 * scale)) / 116; + retval = (lightness + (16 * PWM_LUMINANCE_SCALE)) / 116; retval *= retval * retval; - retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale)); + retval += PWM_LUMINANCE_SCALE/2; + retval >>= 2*PWM_LUMINANCE_SHIFT; } return retval; @@ -215,8 +218,7 @@ int pwm_backlight_brightness_default(struct device *dev, /* Fill the table using the cie1931 algorithm */ for (i = 0; i < data->max_brightness; i++) { retval = cie1931((i * PWM_LUMINANCE_SCALE) / - data->max_brightness, PWM_LUMINANCE_SCALE) * - period; + data->max_brightness) * period; retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE); if (retval > UINT_MAX) return -EINVAL; -- 2.20.1
WARNING: multiple messages have this Message-ID (diff)
From: Rasmus Villemoes <linux@rasmusvillemoes.dk> To: Thierry Reding <thierry.reding@gmail.com>, Lee Jones <lee.jones@linaro.org>, Daniel Thompson <daniel.thompson@linaro.org>, Jingoo Han <jingoohan1@gmail.com>, Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>, linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/5] backlight: pwm_bl: switch to power-of-2 base for fixed-point math Date: Thu, 19 Sep 2019 14:06:19 +0000 [thread overview] Message-ID: <20190919140620.32407-4-linux@rasmusvillemoes.dk> (raw) In-Reply-To: <20190919140620.32407-1-linux@rasmusvillemoes.dk> Using a power-of-2 instead of power-of-10 base makes the computations much cheaper. 2^16 is safe; retval never becomes more than 2^48 + 2^16/2. On a 32 bit platform, the very expensive 64/32 division at the end of cie1931() instead becomes essentially free (a shift by 32 is just a register rename). Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> --- drivers/video/backlight/pwm_bl.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index aee6839e024a..102bc191310f 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -148,7 +148,8 @@ static const struct backlight_ops pwm_backlight_ops = { }; #ifdef CONFIG_OF -#define PWM_LUMINANCE_SCALE 10000 /* luminance scale */ +#define PWM_LUMINANCE_SHIFT 16 +#define PWM_LUMINANCE_SCALE (1 << PWM_LUMINANCE_SHIFT) /* luminance scale */ /* * CIE lightness to PWM conversion. @@ -165,23 +166,25 @@ static const struct backlight_ops pwm_backlight_ops = { * The following function does the fixed point maths needed to implement the * above formula. */ -static u64 cie1931(unsigned int lightness, unsigned int scale) +static u64 cie1931(unsigned int lightness) { u64 retval; /* * @lightness is given as a number between 0 and 1, expressed - * as a fixed-point number in scale @scale. Convert to a - * percentage, still expressed as a fixed-point number, so the - * above formulas can be applied. + * as a fixed-point number in scale + * PWM_LUMINANCE_SCALE. Convert to a percentage, still + * expressed as a fixed-point number, so the above formulas + * can be applied. */ lightness *= 100; - if (lightness <= (8 * scale)) { + if (lightness <= (8 * PWM_LUMINANCE_SCALE)) { retval = DIV_ROUND_CLOSEST(lightness * 10, 9033); } else { - retval = (lightness + (16 * scale)) / 116; + retval = (lightness + (16 * PWM_LUMINANCE_SCALE)) / 116; retval *= retval * retval; - retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale)); + retval += PWM_LUMINANCE_SCALE/2; + retval >>= 2*PWM_LUMINANCE_SHIFT; } return retval; @@ -215,8 +218,7 @@ int pwm_backlight_brightness_default(struct device *dev, /* Fill the table using the cie1931 algorithm */ for (i = 0; i < data->max_brightness; i++) { retval = cie1931((i * PWM_LUMINANCE_SCALE) / - data->max_brightness, PWM_LUMINANCE_SCALE) * - period; + data->max_brightness) * period; retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE); if (retval > UINT_MAX) return -EINVAL; -- 2.20.1
next prev parent reply other threads:[~2019-09-19 14:06 UTC|newest] Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-09-19 14:06 [PATCH 1/5] backlight: pwm_bl: fix cie1913 comments and constant Rasmus Villemoes 2019-09-19 14:06 ` Rasmus Villemoes 2019-09-19 14:06 ` [PATCH 2/5] backlight: pwm_bl: eliminate a 64/32 division Rasmus Villemoes 2019-09-19 14:06 ` Rasmus Villemoes 2019-10-07 15:13 ` Daniel Thompson 2019-10-07 15:13 ` Daniel Thompson 2019-09-19 14:06 ` [PATCH 3/5] backlight: pwm_bl: drop use of int_pow() Rasmus Villemoes 2019-09-19 14:06 ` Rasmus Villemoes 2019-10-07 15:28 ` Daniel Thompson 2019-10-07 15:28 ` Daniel Thompson 2019-10-07 18:43 ` Rasmus Villemoes 2019-10-07 18:43 ` Rasmus Villemoes 2019-10-08 9:31 ` Daniel Thompson 2019-10-08 9:31 ` Daniel Thompson 2019-10-08 10:02 ` Rasmus Villemoes 2019-10-08 10:02 ` Rasmus Villemoes 2019-10-08 10:43 ` Daniel Thompson 2019-10-08 10:43 ` Daniel Thompson 2019-10-08 10:43 ` Daniel Thompson 2019-09-19 14:06 ` Rasmus Villemoes [this message] 2019-09-19 14:06 ` [PATCH 4/5] backlight: pwm_bl: switch to power-of-2 base for fixed-point math Rasmus Villemoes 2019-10-07 15:32 ` Daniel Thompson 2019-10-07 15:32 ` Daniel Thompson 2019-09-19 14:06 ` [PATCH 5/5] lib/math: remove int_pow() Rasmus Villemoes 2019-09-19 14:36 ` Andy Shevchenko 2019-09-21 15:19 ` kbuild test robot 2019-09-21 15:22 ` kbuild test robot 2019-09-20 10:14 ` [PATCH 1/5] backlight: pwm_bl: fix cie1913 comments and constant Daniel Thompson 2019-09-20 10:14 ` Daniel Thompson 2019-09-20 10:14 ` Daniel Thompson 2019-09-20 10:36 ` Rasmus Villemoes 2019-09-20 10:36 ` Rasmus Villemoes 2019-10-07 15:08 ` Daniel Thompson 2019-10-07 15:08 ` Daniel Thompson
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=20190919140620.32407-4-linux@rasmusvillemoes.dk \ --to=linux@rasmusvillemoes.dk \ --cc=b.zolnierkie@samsung.com \ --cc=daniel.thompson@linaro.org \ --cc=dri-devel@lists.freedesktop.org \ --cc=jingoohan1@gmail.com \ --cc=lee.jones@linaro.org \ --cc=linux-fbdev@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-pwm@vger.kernel.org \ --cc=thierry.reding@gmail.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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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.