From patchwork Thu Sep 19 14:06:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11152537 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 868C976 for ; Thu, 19 Sep 2019 14:06:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 645D32067B for ; Thu, 19 Sep 2019 14:06:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="OCn3gIYj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388712AbfISOGb (ORCPT ); Thu, 19 Sep 2019 10:06:31 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:45592 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388639AbfISOGa (ORCPT ); Thu, 19 Sep 2019 10:06:30 -0400 Received: by mail-lf1-f68.google.com with SMTP id r134so2438134lff.12 for ; Thu, 19 Sep 2019 07:06:29 -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:mime-version :content-transfer-encoding; bh=9jGZhci6ir2QQxaMaZ4RPKwsW6VmlML93zQ3UVVvLVc=; b=OCn3gIYj+wUYxAtXas8eOyf4fSEWhP2JZywuNcvWhoXd2re/NpOYXHVgx+ktYyP5cV f+T9+6Yleq+jPzmdljsDSTYYkv0hRJ4OXI6VngwNHBQhZ+MPKG8XvWun3Tnm4NO4agx6 jyhuQWbNOO26WlFgyizU4iA2+osQUAPnhKYiE= 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:mime-version :content-transfer-encoding; bh=9jGZhci6ir2QQxaMaZ4RPKwsW6VmlML93zQ3UVVvLVc=; b=ui4BOAHc2L+ZkjW/L2XULpu9pAt05HFML9EBbH3Qix/akYEKS7B17Sex7UGNjVGm88 OO74OOnAjzv+4VqQzMOf2YezpnoBMs0a15fNABYF+WdDgUfZSMdDR7l/PAC1RLV9xsEO NCj07WwlZs+nsj2yGRWX+yCQM5OiyrPi3zF5rt9MszQncSEhHsFyl20j6G2MJbwwCmFE yF+U70xwLGXNyct11HAvrSJPgV4sUhkwPMTGMqWxkXq+2aYVGn3WOMWT9Xi2sxDhVo/+ OQ3n747TrzNkzXUPQaO8VAFVrQ1+uggZYnRD0My9ZoWKbOMHClO9CMdIyrOD+/qpjlG1 uYow== X-Gm-Message-State: APjAAAUD+lqutWwQE2eQS/9YNmeBPDF6zTmj5j1vidFH3RxqTJIFqVeO KL94EO7yx6cubexWkZ/kbrdu9w== X-Google-Smtp-Source: APXvYqx94tejJIy0k7sD7BxbdRz2nLUSrH5aFo9cmoMdLo9lK0ILNVQtax9BnK0daUvVanbqCyRpiw== X-Received: by 2002:ac2:51a7:: with SMTP id f7mr5064133lfk.119.1568901989001; Thu, 19 Sep 2019 07:06:29 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id h3sm1687886ljf.12.2019.09.19.07.06.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Sep 2019 07:06:28 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , 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 1/5] backlight: pwm_bl: fix cie1913 comments and constant Date: Thu, 19 Sep 2019 16:06:16 +0200 Message-Id: <20190919140620.32407-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-fbdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@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. Signed-off-by: Rasmus Villemoes Reviewed-by: Daniel Thompson --- 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 2201b8c78641..be36be1cacb7 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 Thu Sep 19 14:06:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11152543 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1F7AD76 for ; Thu, 19 Sep 2019 14:06:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F286A20882 for ; Thu, 19 Sep 2019 14:06:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="A48WL858" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389656AbfISOGn (ORCPT ); Thu, 19 Sep 2019 10:06:43 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:43041 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388605AbfISOGd (ORCPT ); Thu, 19 Sep 2019 10:06:33 -0400 Received: by mail-lj1-f193.google.com with SMTP id d5so3701112lja.10 for ; Thu, 19 Sep 2019 07:06:31 -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=zBTcMpOyl2bLXBXPvlZrHA8WBZZACi7gNqiyseLnytg=; b=A48WL858gPJVbFagBB2BTpTgHdTGMt1R9BC01trDu5z3ENfmhWS7RiqjnfWx20RoUY E0iQrrx0deYrkinYfpODZNFzuVCQBajUDYXy09Qkmb+GV1iPmOdfGkZktLbOEyqsuOiz 8Rwokwielo8W6w7zis9sx6uJ5V5kHfsVwrS8I= 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=zBTcMpOyl2bLXBXPvlZrHA8WBZZACi7gNqiyseLnytg=; b=CQxJxN9zxbMOuT+EDLifCvr2v+/qDRHmNDsiwC/H4MxSKRJXBWUi4grG+EBkHdvYND QPhUHV5sByRnYQ+T8I4jiXRTl46b9VIJWQFlqw5tDxAq20/D62To6ZX1bLOnU75gFboH PKoCpFQWX2wubK51JJQjZUiBv50KCvseQEe6CaVjZ19J68GmMayRi4B5yQKRSaUo7PZh HMdBKnfczEhr7L1vB/B1lXLPDWLW2NaGkc+pl8rqnwW1z9kf2z0jyjWlN9GB7rqA4oZt 5PAvaJco34EROsdTTOpKJ2IxYP5rUeCsc3OMfoQSBL3WLsgf+6zDX/S0loucxCpaY/Nw RZAQ== X-Gm-Message-State: APjAAAVmKljbinySqOUQY9SHdt4G4EflEcz4nlXy1SACrB19yFhd6D9L D4T0/ujvXvvSRZmjR1NeLDaA5A== X-Google-Smtp-Source: APXvYqz80XnBinuoIhjkWosIvpazzODW2CaBBl25zB2SkJzfe+uW+0Y8qIL/RSBMCTZSD3oY/92ueA== X-Received: by 2002:a2e:6e18:: with SMTP id j24mr5534397ljc.158.1568901990286; Thu, 19 Sep 2019 07:06:30 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id h3sm1687886ljf.12.2019.09.19.07.06.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Sep 2019 07:06:29 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , 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 2/5] backlight: pwm_bl: eliminate a 64/32 division Date: Thu, 19 Sep 2019 16:06:17 +0200 Message-Id: <20190919140620.32407-2-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190919140620.32407-1-linux@rasmusvillemoes.dk> References: <20190919140620.32407-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-fbdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@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(). Signed-off-by: Rasmus Villemoes Reviewed-by: Daniel Thompson --- 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 be36be1cacb7..9252d51f31b9 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 Thu Sep 19 14:06:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11152541 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9544D1745 for ; Thu, 19 Sep 2019 14:06:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7361720882 for ; Thu, 19 Sep 2019 14:06:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="W4FhrPJA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389489AbfISOGf (ORCPT ); Thu, 19 Sep 2019 10:06:35 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:46389 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389372AbfISOGe (ORCPT ); Thu, 19 Sep 2019 10:06:34 -0400 Received: by mail-lj1-f196.google.com with SMTP id e17so3683178ljf.13 for ; Thu, 19 Sep 2019 07:06:32 -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=k4SJ+eSfjSAP63ushly3GU0Efl4ebkKBMuWTSOkSqS8=; b=W4FhrPJAR9Dqgs5qFA5Pl22WVgeXGbokdSlE6Xjl8ev+AwkvtlovQkmnKhocbnMFdS 81B7iYoNdsvMs2yQlsvxAdJGvvAKDR0FvjYPLiSIbpwUt9AMKu8kvddB2vIg68Gs3TZA XspHxRzEPQhGFNCyXRy2eUNIWhGekZOWx5Sfw= 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=k4SJ+eSfjSAP63ushly3GU0Efl4ebkKBMuWTSOkSqS8=; b=mi8TqKbbVfMFz2tfVWQM2n6VNMlsDAQ2c4TGNBCazhd4GFjoKU+Sl1uJptU0KkhJG1 kHWs9eNeuYNPW5WdGVQmf25hFDyT3aBgMiXW3b6nNelzVZeJGoCkhCmISPKMF00Evbcz TM+IW0yqpA+/de+yGre7YS0HzhbhnlZbydI3f70Iknz1MEQKXaR+g+lBMNrPRetEiRrM X4sOYfDEQMfn0DQmONoxdEEMkSy/VLbTVB0+kDkjJSbt+kmDPCRHVuCEyzv2IUYztazf FkqvIpuexdbvBfRpPJouoOwm2271m8DAEfT9yccAoQQb+ar06ZNi3WBcYZM/JtwVcmyi CkrA== X-Gm-Message-State: APjAAAUXx+I9gfrLl2g6N2Z6FfVO7fH1WP2AdoUoS0ZqfTkzPCQwhTH5 zlbuT2rIJ1vTInLjWV0uHohuCw== X-Google-Smtp-Source: APXvYqzlHJRhLqL82K2xKbtjAXAMpinhzWwpvMBA7qVfJ7tNQ26aw9X0BviaTA3hpMKKf3q8cAewtQ== X-Received: by 2002:a2e:934f:: with SMTP id m15mr5586940ljh.101.1568901991526; Thu, 19 Sep 2019 07:06:31 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id h3sm1687886ljf.12.2019.09.19.07.06.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Sep 2019 07:06:31 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , 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 3/5] backlight: pwm_bl: drop use of int_pow() Date: Thu, 19 Sep 2019 16:06:18 +0200 Message-Id: <20190919140620.32407-3-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190919140620.32407-1-linux@rasmusvillemoes.dk> References: <20190919140620.32407-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-fbdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org The scheduler uses a (currently private) fixed_power_int() in its load average computation for computing powers of numbers 0 < x < 1 expressed as fixed-point numbers, which is also what we want here. But that requires the scale to be a power-of-2. We could (and a following patch will) change to use a power-of-2 scale, but for a fixed small exponent of 3, there's no advantage in using repeated squaring. Signed-off-by: Rasmus Villemoes Reviewed-by: Daniel Thompson --- 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 9252d51f31b9..aee6839e024a 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 Thu Sep 19 14:06:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 11152539 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0DB0116B1 for ; Thu, 19 Sep 2019 14:06:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E07E020882 for ; Thu, 19 Sep 2019 14:06:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="MZKdSEpO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389501AbfISOGf (ORCPT ); Thu, 19 Sep 2019 10:06:35 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:38025 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389448AbfISOGe (ORCPT ); Thu, 19 Sep 2019 10:06:34 -0400 Received: by mail-lj1-f194.google.com with SMTP id y23so3741712ljn.5 for ; Thu, 19 Sep 2019 07:06:33 -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=Jg7JcvREMiiX5x2HEJEjAcDv3XByVb0RNmStAp9nahQ=; b=MZKdSEpOEV+5iG16oW7TXFRd1eg5H9XeUG6b1t8t2jgOvEG6oGHacyhtyg3NPIEGY/ uKiTYPItaGHpX/90K1aP/bPkpA8b+8k/Rldl4zxvx2B9det3NTU55vbdyWW1nSSp9eg3 YRFw3EAaRrmgYhPjv9gwnV6JQyEwF2erlvjsw= 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=Jg7JcvREMiiX5x2HEJEjAcDv3XByVb0RNmStAp9nahQ=; b=YCv6z0+/LfYMKPJEs9smrUsnCLnKwpssoSLLJT9S3wX0+oG0JcTpR1Ji7Bng3Rsi0n gB3wdOcJxcT08ffhzACliE6vcTF1eJSLZzGg5HYh5crB+Y06b+8+05i2x8rBqWiOUujH dCZIyjVObm9tyg24zw1Syx7zNV/wgwMQ9XwkAs/SOCzV0DrQpVBZ3K2/ba66tSIOBXLR ZsiPTwCKLgeU8heHx3JKCI2WsxqrLGr0+fux/sJzkBfZ+McDacWAuqtreF1CkrhOavKY 87+KHIl9C0R7UyJcfUBVxHLhYST7TPGwZ42A2M7LsenZeZGBdLOi+8uapvJfa7P4lEUB 1wqA== X-Gm-Message-State: APjAAAVJ+FkjehRptVEegbAtmyalfOPrDX4wh2xEEwDjMb3UJh4aW6oR HLvgtaaKE/UoPN2sKpNvRE/wYQ== X-Google-Smtp-Source: APXvYqwT7aVA83Vb4qPr4tBmMd1vi6mmQP81J4Kl4kTliuCxLy+GI3jRKm2Szplr4kWj55N1eonI9A== X-Received: by 2002:a05:651c:c9:: with SMTP id 9mr107103ljr.29.1568901992739; Thu, 19 Sep 2019 07:06:32 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id h3sm1687886ljf.12.2019.09.19.07.06.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Sep 2019 07:06:32 -0700 (PDT) From: Rasmus Villemoes To: Thierry Reding , 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 4/5] backlight: pwm_bl: switch to power-of-2 base for fixed-point math Date: Thu, 19 Sep 2019 16:06:19 +0200 Message-Id: <20190919140620.32407-4-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190919140620.32407-1-linux@rasmusvillemoes.dk> References: <20190919140620.32407-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: linux-fbdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@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^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 Reviewed-by: Daniel Thompson --- 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;