From patchwork Tue Oct 8 12:03:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 1173281 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="W0ddgUpZ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46nbds1hNzz9s4Y for ; Tue, 8 Oct 2019 23:05:37 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730317AbfJHMFV (ORCPT ); Tue, 8 Oct 2019 08:05:21 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:41138 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730710AbfJHMFU (ORCPT ); Tue, 8 Oct 2019 08:05:20 -0400 Received: by mail-lj1-f196.google.com with SMTP id f5so17229834ljg.8 for ; Tue, 08 Oct 2019 05:05:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kZ+v3Ie5jsyMpg1/r8HbbsX7sXk3b8d5gKwzOHbV1b8=; b=W0ddgUpZ8caX090N18rJXEGsy5RctbdFr4Uj8eEHV+WXpAquDOJGemg+ouB0b7rO2k wQfnFphLbbQKFY/9KqpuIJl8qs1h1g3Lae36ILXsc4WNO/eomzP41sRPtH5RcLggU2XK ai8CJyNSJdiOh5PVAUkZIqTEPEq2lsZk8iZhY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kZ+v3Ie5jsyMpg1/r8HbbsX7sXk3b8d5gKwzOHbV1b8=; b=A+en99yo8/6pLlAlh0aMLCJa51UShjxQ7LqEI3TGFA1V0+ve483eg/slJjBWAI9+qZ i/o+ja05C6mm75pvgjKffX7kSleL1rIsyfZK7WMYfc7swMWMZWOPWi0J/JK6oQH3ERG1 i7po/COJxFsLJLk5Ypwpvi15/V0NZHQOkyG9Ld/7Qbfb1Xj1E3J07ZLRgg7C8gWZHrge grQazBsMbX9I95bor8pmzZUdFiYRJdrPNrWSIwvtzMQmcSB++RdslZ2uOAc/HSQoiY+D fy0U3/wfTR3cCYgbyZNK304A7wEP/QbwxuwYRF+0+RZG2LbtKsYltaWYUUWfPxhwckjg OYYA== X-Gm-Message-State: APjAAAVJbzp1j+Z/0exulLOglLzJAxStfKAp19hRnEtJwxcbespxvaH+ lxN27qyfMmd0rnFLnfBDZp+NsA== X-Google-Smtp-Source: APXvYqxr3LSKZs/gnEdARVmL8Fu4FXziognliaJ/7eGB45xQLrMsXf20NWCHuBIyf534zW7ay+klcg== X-Received: by 2002:a2e:810e:: with SMTP id d14mr22480784ljg.160.1570536318667; Tue, 08 Oct 2019 05:05:18 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id z18sm3918033ljh.17.2019.10.08.05.05.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2019 05:05:17 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?b?w7ZuaWc=?= , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz Cc: Rasmus Villemoes , linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] backlight: pwm_bl: fix cie1913 comments and constant Date: Tue, 8 Oct 2019 14:03:24 +0200 Message-Id: <20191008120327.24208-2-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191008120327.24208-1-linux@rasmusvillemoes.dk> References: <20191008120327.24208-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-pwm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org The "break-even" point for the two formulas is L==8, which is also what the code actually implements. [Incidentally, at that point one has Y=0.008856, not 0.08856]. Moreover, all the sources I can find say the linear factor is 903.3 rather than 902.3, which makes sense since then the formulas agree at L==8, both yielding the 0.008856 figure to four significant digits. Reviewed-by: Daniel Thompson Signed-off-by: Rasmus Villemoes --- drivers/video/backlight/pwm_bl.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index 746eebc411df..cc44a02e95c7 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -155,8 +155,8 @@ static const struct backlight_ops pwm_backlight_ops = { * * The CIE 1931 lightness formula is what actually describes how we perceive * light: - * Y = (L* / 902.3) if L* ≤ 0.08856 - * Y = ((L* + 16) / 116)^3 if L* > 0.08856 + * Y = (L* / 903.3) if L* ≤ 8 + * Y = ((L* + 16) / 116)^3 if L* > 8 * * Where Y is the luminance, the amount of light coming out of the screen, and * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human @@ -169,9 +169,15 @@ static u64 cie1931(unsigned int lightness, unsigned int scale) { 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. + */ lightness *= 100; if (lightness <= (8 * scale)) { - retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9023); + retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9033); } else { retval = int_pow((lightness + (16 * scale)) / 116, 3); retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale)); From patchwork Tue Oct 8 12:03:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 1173278 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="ELuDRuvE"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46nbdd1bH7z9sPk for ; Tue, 8 Oct 2019 23:05:25 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730909AbfJHMFY (ORCPT ); Tue, 8 Oct 2019 08:05:24 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:41140 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730899AbfJHMFX (ORCPT ); Tue, 8 Oct 2019 08:05:23 -0400 Received: by mail-lj1-f193.google.com with SMTP id f5so17229990ljg.8 for ; Tue, 08 Oct 2019 05:05:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=t1XvNBeL5bYKnc6NbbptU+vm2vXQBBrTO687+/k8huo=; b=ELuDRuvEPbxHn+tPF2h2RxCruUdG8zM+LbvVMlIluH8Z/3Nz3rM48QruNf6Jjzt6Qx s+T3vGwTXw/HUazqjiUT8y6NaXwohlIowNQCdfwlwMsrwKqKmMOt9HlxSOwPJeCD4YOl uaNX5F5hIsDxJs3FYKG+NK0+p/NucTLWFjocY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=t1XvNBeL5bYKnc6NbbptU+vm2vXQBBrTO687+/k8huo=; b=rRjV3egkNVlnI1pHCLRMffb3GajrmyZOqCai/xd90l8TkkRd4L2dIGEE3yAM1wFHLv YwcnIzeIBxNSJzmTgh1Av+qIu8h3K1RjwpLdMC70kPJXQHlzVu+Rf3UsILckgFkZKReO 7e71ghDXN006wvb90YB0WWm4Sk561y18dsStiShaA2qYVnvpgjlEPz+lOT3oSd2qxwtN TWIi0GPP659I1DXLKvkcfLwbzXLK7eOXJR0N80AqXa2AmbB/pxbOvRQGf7jJB2wD/iky 1qP7Ph7JpveNCE7I5HvwD0nkugXQ6KVYb0yIBhGm/E7NAtcYSsXAlykCCL9Ce4hYrREV b8/w== X-Gm-Message-State: APjAAAUnULaBx/zhpg1UbDB6wQ+VIHx8NulO8/6IEXR1JprS55dfsDNe c5qH9k+kTQeDi+TGiBeKqFl75g== X-Google-Smtp-Source: APXvYqwr3eVj5VGcs9c4XwSP3Lzo4ODiUPJZt5VqFcpdr7xw+mtUtMYtWeZVNZS+BWY7vjjUDAVqgQ== X-Received: by 2002:a2e:9d16:: with SMTP id t22mr19624221lji.207.1570536321581; Tue, 08 Oct 2019 05:05:21 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id z18sm3918033ljh.17.2019.10.08.05.05.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2019 05:05:19 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?b?w7ZuaWc=?= , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz Cc: Rasmus Villemoes , linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] backlight: pwm_bl: eliminate a 64/32 division Date: Tue, 8 Oct 2019 14:03:25 +0200 Message-Id: <20191008120327.24208-3-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191008120327.24208-1-linux@rasmusvillemoes.dk> References: <20191008120327.24208-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-pwm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org lightness*1000 is nowhere near overflowing 32 bits, so we can just use an ordinary 32/32 division, which is much cheaper than the 64/32 done via do_div(). Reviewed-by: Daniel Thompson Signed-off-by: Rasmus Villemoes --- drivers/video/backlight/pwm_bl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index cc44a02e95c7..672c5e7cfcd1 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -177,7 +177,7 @@ static u64 cie1931(unsigned int lightness, unsigned int scale) */ lightness *= 100; if (lightness <= (8 * scale)) { - retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9033); + retval = DIV_ROUND_CLOSEST(lightness * 10, 9033); } else { retval = int_pow((lightness + (16 * scale)) / 116, 3); retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale)); From patchwork Tue Oct 8 12:03:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 1173279 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="iHfqd7In"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46nbdj2DVHz9sQq for ; Tue, 8 Oct 2019 23:05:29 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730904AbfJHMF2 (ORCPT ); Tue, 8 Oct 2019 08:05:28 -0400 Received: from mail-lf1-f67.google.com ([209.85.167.67]:41360 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730915AbfJHMF0 (ORCPT ); Tue, 8 Oct 2019 08:05:26 -0400 Received: by mail-lf1-f67.google.com with SMTP id r2so11721673lfn.8 for ; Tue, 08 Oct 2019 05:05:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GHqEF0WPzWee4xQdmsltBE/DbSJ8wdizxOO3R+g9Qfw=; b=iHfqd7InHpWSp9HtncYc/nGuUN2SQLNPz6EP4Z7c0utpCDEfBTHo79qRfdGdqnHjDc mUERS8N/Ezm1xJivvlU2FPSjziBCWfbM0Mp19xZKJ+DciElb5UKCU0Vr/UJTOEN7Raiq 9nMDdC6cEjkuK79xMF6I9qmtVilDUGJ2/wvCs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GHqEF0WPzWee4xQdmsltBE/DbSJ8wdizxOO3R+g9Qfw=; b=MZGLPjF69aBhWy9Ef30LbO367hQWOorUyQcRKRps2+kzaOWUc33MIqjZFAiyFXyLbl kxD0EkoAcvYB+o8rWxudah0FPjO8KjrGDLMbl00nwytIsJbjXKKqYhzboZNxuuj5ZfiE o1pH5m4g07M02EORAa8NHEUaOA5xfGVzbJKGcZHdqBACQOesKSNXbROZELX+ODH7bb6e 8hNjUZnDwklrkzzDIfwGyACKw0eaek5q2ln+Gnx3/xnfunHIU1uqKx8tVOY3LEguglui zxqrqbXN4yb/An0GCaINql4EePWkqFo7fgewUnTyca5K8Kolt8o+Dqro8Y18vJ4UgkRh 7++w== X-Gm-Message-State: APjAAAWlrOgX3+PY7EgdqokFymyU5SK4hH8Pg/CaNRUnPA2aV9TA7jLg jZn4aJUkG1tQMrOqFaLIfTR7XA== X-Google-Smtp-Source: APXvYqxCIRhO8rpvb/4039KpvxZ2HkirRtcoXz27yozPDPIZ8udJLy4ukMGlXb9inSVE+1v8hleq+g== X-Received: by 2002:a19:90:: with SMTP id 138mr19176383lfa.111.1570536324544; Tue, 08 Oct 2019 05:05:24 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id z18sm3918033ljh.17.2019.10.08.05.05.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2019 05:05:22 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?b?w7ZuaWc=?= , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz Cc: Rasmus Villemoes , linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/4] backlight: pwm_bl: drop use of int_pow() Date: Tue, 8 Oct 2019 14:03:26 +0200 Message-Id: <20191008120327.24208-4-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191008120327.24208-1-linux@rasmusvillemoes.dk> References: <20191008120327.24208-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-pwm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org For a fixed small exponent of 3, it is more efficient to simply use two explicit multiplications rather than calling the int_pow() library function: Aside from the function call overhead, its implementation using repeated squaring means it ends up doing four 64x64 multiplications. Reviewed-by: Daniel Thompson Signed-off-by: Rasmus Villemoes --- drivers/video/backlight/pwm_bl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index 672c5e7cfcd1..273d3fb628a0 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -179,7 +179,8 @@ static u64 cie1931(unsigned int lightness, unsigned int scale) if (lightness <= (8 * scale)) { retval = DIV_ROUND_CLOSEST(lightness * 10, 9033); } else { - retval = int_pow((lightness + (16 * scale)) / 116, 3); + retval = (lightness + (16 * scale)) / 116; + retval *= retval * retval; retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale)); } From patchwork Tue Oct 8 12:03:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 1173280 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-pwm-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="EQxxLJPu"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46nbdl4TP3z9sPV for ; Tue, 8 Oct 2019 23:05:31 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730941AbfJHMFa (ORCPT ); Tue, 8 Oct 2019 08:05:30 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:46881 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730932AbfJHMF3 (ORCPT ); Tue, 8 Oct 2019 08:05:29 -0400 Received: by mail-lj1-f194.google.com with SMTP id d1so17198309ljl.13 for ; Tue, 08 Oct 2019 05:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TptQKMAgu40zPvzhQ1Z31J/pJlahQ9zU95/IQSGlotQ=; b=EQxxLJPuv5kKROn32XBozgwjwLwg89RorArcZU/g8cItI4zFmL7g+GBxM/Ahg/onYr /Qq6eZw1P0LGp4IiWbUpBRj0wt8o58C+ixmMvDPdQyY9ckNxJ8fwVC+8j5r3oWi+ocMe bAPIcSJt4aI9G1QMFztBqZWK5BjdSrlZzfkn0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TptQKMAgu40zPvzhQ1Z31J/pJlahQ9zU95/IQSGlotQ=; b=j1N7IWNjYf3Rs0v2KbciqmC9BO8AtOpPgHbzA5vT5Fz3oDnoYZ4pKj2eX2U0q2nr54 TIrDhuUEeo1wQRI9NpKJ2VnTZmh8hBg275W2cpbboihCm1Tg/SA3z+XTlvYb4BBFVsuU FPE8SYWxKO8JpfUJwhDirCcRPk8Bd7m17JjWdCcJ0o91YM4NjrI5h2hUQ59119hkiS8i npvXnpb+hHpcB3N4qRIoc2hrCVh2yeffnTrh9mWWvWx38q7Yf2g4EAOqUHVSP2RXgLcw Ay2LU3ILvMEmcUdGMHM4MAzjc2/zqhrIvfE/9F1Etz0etqLYEA9fksnf6eQAiqXc81ET tZAA== X-Gm-Message-State: APjAAAX54vLYjnQUY7c1axFgPj69SF9qoQUS1SoDqWllsgrTb4+UY2qb 99goeewV7s73IWKx8L97rF3zjg== X-Google-Smtp-Source: APXvYqz1ZVV6abC07XSZS+ewKWoIdZcZT75MUkfMcioX26MZiIhkt3Y7dE8w+s+hfv7TsUNPdK5xaw== X-Received: by 2002:a2e:b045:: with SMTP id d5mr22159030ljl.105.1570536327398; Tue, 08 Oct 2019 05:05:27 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id z18sm3918033ljh.17.2019.10.08.05.05.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2019 05:05:25 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?b?w7ZuaWc=?= , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz Cc: Rasmus Villemoes , linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] backlight: pwm_bl: switch to power-of-2 base for fixed-point math Date: Tue, 8 Oct 2019 14:03:27 +0200 Message-Id: <20191008120327.24208-5-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191008120327.24208-1-linux@rasmusvillemoes.dk> References: <20191008120327.24208-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-pwm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org 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^32/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). Reviewed-by: Daniel Thompson Signed-off-by: Rasmus Villemoes --- 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 273d3fb628a0..a99c2210c935 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 += 1ULL << (2*PWM_LUMINANCE_SHIFT - 1); + 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;