mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Oarora Etimis <oaroraetimis@gmail.com>
To: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	 Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	 David Airlie <airlied@gmail.com>,
	Simona Vetter <simona@ffwll.ch>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	 Oarora Etimis <oaroraetimis@gmail.com>
Subject: [PATCH] drm/fixed: use fls64() in drm_fixp_msbset()
Date: Tue, 17 Nov 2026 12:32:19 +0100	[thread overview]
Message-ID: <20261117-fixp-msbset-v1-1-60526519ae2d@gmail.com> (raw)

Replace the loop with fls64() on the value with the sign folded away.

Output is unchanged. The loop runs once per bit it has to skip, so a
channel value takes it about 47 iterations and 347 executed
instructions. The replacement is 6, and the work is one bit scan: bsr on
x86-64, two bsrl on i386, clz on arm and riscv.

Signed-off-by: Oarora Etimis <oaroraetimis@gmail.com>
---
drm_fixp_msbset() walks from bit 62 down, one bit at a time, to find the
highest bit that differs from the sign bit. fls64() returns the same
value in one step.

In the other users it is only called a few times, so the loop does not
show. In vkms it runs for every channel of every pixel of every frame,
and a channel value is 16 bits wide, so each run starts 47 compares from
its answer.

  Link: https://lore.kernel.org/20261108-lut-v1-0-f762d70ae4cd@gmail.com

Fold the sign away and let the CPU find the highest set bit. The range
drm_fixp_div() shifts by is unchanged, and the output is bit-identical.

Counted with callgrind, gcc 16.2.1 at -O2 on x86-64, function body only.
---
 include/drm/drm_fixed.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/include/drm/drm_fixed.h b/include/drm/drm_fixed.h
index 21d822aeed55..7dbe89ca30c6 100644
--- a/include/drm/drm_fixed.h
+++ b/include/drm/drm_fixed.h
@@ -28,6 +28,7 @@
 #include <linux/math64.h>
 #include <linux/types.h>
 #include <linux/wordpart.h>
+#include <linux/bitops.h>
 
 typedef union dfixed {
 	u32 full;
@@ -121,13 +122,9 @@ static inline int drm_fixp2int_ceil(s64 a)
 
 static inline unsigned drm_fixp_msbset(s64 a)
 {
-	unsigned shift, sign = (a >> 63) & 1;
+	u64 v = (a < 0) ? ~(u64)a : (u64)a;
 
-	for (shift = 62; shift > 0; --shift)
-		if (((a >> shift) & 1) != sign)
-			return shift;
-
-	return 0;
+	return fls64(v | 1) - 1;
 }
 
 static inline s64 drm_fixp_mul(s64 a, s64 b)

---
base-commit: fc72cd52fb284f3707fa36a68ea5a22543a51d71
change-id: 20261113-fixp-msbset-5965c14bcb9b

Best regards,
--  
Oarora Etimis <oaroraetimis@gmail.com>


             reply	other threads:[~2026-11-17 11:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:32 Oarora Etimis [this message]
2026-11-17 15:25 ` Louis Chauvet

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=20261117-fixp-msbset-v1-1-60526519ae2d@gmail.com \
    --to=oaroraetimis@gmail.com \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tzimmermann@suse.de \
    /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: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®