Skip to contentSkip to conversation

drm/fixed: use fls64() in drm_fixp_msbset()

PATCH In review Oarora Etimis 2 participants 2 messages Nov 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/fixed: use fls64() in drm_fixp_msbset()

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.

---
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.

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(-)
include/drm/drm_fixed.h +3 -6
index 21d822aeed55..7dbe89ca30c6 100644
@@ -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,

Signature · 1 line
Oarora Etimis <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Oarora Etimis

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.

I did some quick testing, it seems to really improve the VKMS performance on some workload (up to 3x faster)

Quoted 57 lines

---
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/[email protected]

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(-)
include/drm/drm_fixed.h +3 -7
index 21d822aeed55..7dbe89ca30c6 100644
@@ -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 <[email protected]>