Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp996458ybe; Thu, 19 Sep 2019 07:09:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqxV/flmcFTpjYmdLlM14dJG75fNXyaBFKX3+xDf+Fwuheaz9h5PP11zvcDgQpBXdPe/vzSk X-Received: by 2002:aa7:c6c6:: with SMTP id b6mr8133079eds.19.1568902162638; Thu, 19 Sep 2019 07:09:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568902162; cv=none; d=google.com; s=arc-20160816; b=Dy80dns/n5sozUJLNiG2WUer1ITCwWZiREw9FnlKY+eCZTHFQm7meesSChxXz4pwL5 gtxfQN2pkoP716yoZgniumAmw/6hmY8JHpOolV7i4VWvG+FhudixQRduZzEx3Ib5kYBk zISIucwr9OIkT8XwY3ddkVYw2wF6WuD9NoIaprxXmuD9xfYt7fErXApPVlJLlw4CDJ2x +9qYc1m6Hty0d82rzHSEAU0rI/t9sP6oRU0ocFuH883Bj5i2aUN+6kW+TqRbkYBYrTgF uiEWmUzXdffJPT9dRw11Yi7bRyztGits1AZldnTSBRTWMG+gH7pNDFtGEmhX7qh6mTuk 4ebg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Jg7JcvREMiiX5x2HEJEjAcDv3XByVb0RNmStAp9nahQ=; b=GeL+mDq0nQCDxA8f2AXkPcJ8AIumpip1UYBWdSWxC/zFItJj8M+A7BUYKwmxvIm1wx MF3fQYfGHLhG6kPFQTQB/r5P9kDPC2b222wnfoZk8aVkQ8GmJ669YaD8rFobsyod1gn1 sX6BfCLWf+0skXMgAVJFA11fmZxOALSWpow4WcYqrnM0z72Kg+vWWDDL+e1PPcvtf9iL MgtFZuerXZyAGCn1as1Wx2Bv3G23Z6Pl5JpVddD20K2k2H+HM0z4sb/3OoYEMJCaLjgZ nRgLdrocOYMIXiSHQGrlem0hiE6YclfAsyPqejtYIYIIfcJ/Ot8XD8JlmF+mBUV5epGt XSAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=MZKdSEpO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z16si5357794edb.420.2019.09.19.07.08.59; Thu, 19 Sep 2019 07:09:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=MZKdSEpO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389503AbfISOGf (ORCPT + 99 others); Thu, 19 Sep 2019 10:06:35 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:35689 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389439AbfISOGe (ORCPT ); Thu, 19 Sep 2019 10:06:34 -0400 Received: by mail-lj1-f195.google.com with SMTP id m7so3764228lji.2 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=eqh2iSv0BMSc3sFRvPAy5/374Dkw36+3gtrd7E/e8Sm/Iti3l3w+BHqPGqWIpzJEn0 v6cGUH3SIrEtwaRp7ain0kEvsRInI1GUGNg+T7XLF98m7B2o5mHaW/N4KKpsq/0JPLPL n8mCFADnsqk4JupJnsWqzzIfn3k+JOQbaH692zXQ/N72jDfdgPw7rBTEKadfH0CDYqmZ eM4pL8/avJ11gi1PGWMSm4vKwvJnWM0TXU4t7YvUSUKsPpfveSi+V666xTHWP4oaOYaL uu74le+g7Fotz5tOwRQb33Pc9KDqdO91dnnyG45ND+yKG45KalqiqjbHtCFLiZSOxQk0 CoGw== X-Gm-Message-State: APjAAAWB+IREkLJMvLLd0gWd48VRqGiA3KjiIrwt8YdkmJtPMIb1cPMw oA5s3h8RhmdMoJpRak4Tb8enLw== 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 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 --- 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