Skip to contentSkip to conversation

erofs-utils: fix bitops fls_long()

PATCH In review Hongzhen Luo 2 participants 2 messages Jul 9, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 erofs-utils: fix bitops fls_long()

The __builtin_clz is for unsigned int, while it is applied to unsigned long. This fixes it by using __builtin_clzl.

It does not impact any existing use cases in the whole codebase, since the only caller of fls_long is roundup_pow_of_two in erofs_init_devices, and the default compile optimization level is O2. At this level, the argument passed to roundup_pow_of_two is optimized into a constant, bypassing the logic of fls_long.

---
v3: Update the commit message.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

---
 include/erofs/defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/erofs/defs.h +1 -1
index e0798c8..310a6ab 100644
@@ -288,7 +288,7 @@ static inline u32 get_unaligned_le64(const void *p)
 
 static inline unsigned int fls_long(unsigned long x)
 {
-	return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
+	return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
 }
 
 static inline unsigned long lowbit(unsigned long n)
Signature · 1 line
2.43.5
#1 lore raw
Quoted 8 lines · Hongzhen Luo

The __builtin_clz is for unsigned int, while it is applied to unsigned long. This fixes it by using __builtin_clzl.

It does not impact any existing use cases in the whole codebase, since the only caller of fls_long is roundup_pow_of_two in erofs_init_devices, and the default compile optimization level is O2. At this level, the argument passed to roundup_pow_of_two is optimized into a constant, bypassing the logic of fls_long.

roundup_pow_of_two() in erofs_init_devices() could give wrong results although the current compiler optimization level "-O2" covers it up.

Quoted 2 lines

No need to resend, I will update manually.

Thanks,
Gao Xiang

Quoted 21 lines

---
v3: Update the commit message.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

---
  include/erofs/defs.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/erofs/defs.h +1 -1
index e0798c8..310a6ab 100644
@@ -288,7 +288,7 @@ static inline u32 get_unaligned_le64(const void *p)
 
  static inline unsigned int fls_long(unsigned long x)
  {
-	return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
+	return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
  }
 
  static inline unsigned long lowbit(unsigned long n)