Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5567779ybp; Tue, 8 Oct 2019 05:07:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzB2zvzLQq1oBzitskGyNNAgUrgbRuV+NAUXY6AYt9liBqm1/5ks1Ra2hXfrv9YUQ9p003p X-Received: by 2002:a50:eac4:: with SMTP id u4mr33981553edp.36.1570536432520; Tue, 08 Oct 2019 05:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570536432; cv=none; d=google.com; s=arc-20160816; b=BoPu6CwJ6iYo50joABL7Rjcp9dm5qjLioakLW8Z6hf0bduRoCrcDyHnDwd4K++O8Db qAYsxPQmHLRsnyGJ5FX5YxVdQIAUl5ve73TiQEvFRpzp3+YD4yXWEkkEZfhJa1MvmEAm 8KAEVJTskj0nRcB4r6YDdf4bZIXvpwuTID7vVpAr4ZUiEPr1ZYfUJ8PqjfMY6WIwZAli 7aM/F9VpE1m5NUcUN7wumb/H/p6HZgH4SwKEj/FD50skAJKJNkIMlBoCbUEx4hJpuXi1 c4kXS/MPq01ghYya4h3OzOD6XkAq6Xm9X9CSU+o4e/uUeVdVpvkmkwiOHa2Vq6D4bw/f VhZA== 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=TptQKMAgu40zPvzhQ1Z31J/pJlahQ9zU95/IQSGlotQ=; b=XnQjvixSkU2fwqQsTiALHmbxwkx+op1sEyoZqgwoQeAARk+4YIe6EOHjx4sjHwERCK G9sgz9MvEkIg9/VP1/phkwXyJdGAjMQa3hURu3ZFsB32ZDaSpqJ47Heb7fbi1DeCdLOw EBT7omTXHB+RES5HLJ7AKjjK1BXyXKmJHbBTJnvk0/HhUwNTZAdo9tA2I5MASHE4N09f M7wbF2qzWKea/ORcQqnjIfytbLKj8f0Yv5mkmqy8KMugNo+kXCY8xMZoE+skq19dNnIK CaWVErH5wk0cXEa/i3bByV57Hx5ZkDjwQ25tTj9SHJz0zMlIbjtTFjYBc0RcjbKRZusA rmHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=EQxxLJPu; 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 j35si12072777eda.144.2019.10.08.05.06.48; Tue, 08 Oct 2019 05:07:12 -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=EQxxLJPu; 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 S1730947AbfJHMFa (ORCPT + 99 others); Tue, 8 Oct 2019 08:05:30 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:42332 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730923AbfJHMF3 (ORCPT ); Tue, 8 Oct 2019 08:05:29 -0400 Received: by mail-lj1-f193.google.com with SMTP id y23so17214898lje.9 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=gT6n/s4hkYaiQYJbSYBfw1NVvhFyUU72ZOTV6SzmEfuaEbD0I1boRf3qynZNVoFWzu py1GDsznh9bQDNKt9m3oK3rchU1AhegshvNr8nMSzW39nC2Tz7ButAqGLULnlNcfrjBI drQmQvynHaCn5TIGG7YAAAnCN3gt4L653CWPsN3TDNG9JJCeo9IXwhr9szVeckhmn4Ai BDnjPlqfC7wVH00rEiiAlQOK5NLXnG+CgAqLQ9X1STbfIjuzb5SUcoFUo4r3yDe47xBa PzaiibCnbd3d13Kaf3jcJD8BMBUPtswkFQ8hSFRpH3KiO64SaPYEnqrsvRm8mwge2m7S PZSA== X-Gm-Message-State: APjAAAU0uxDmEKks4FvsZQnWCh2sZbcE5amscXxbUtfhlCn6UYeevX+z +aouW7dVDP8RA00O+XWaFKgHN/DRrp8WC19d 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=20Kleine-K=C3=B6nig?= , 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 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^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; -- 2.20.1