Use intrinsics.count_leading_zeros internally for math.next_power_of_two

This commit is contained in:
gingerBill
2026-06-23 11:39:19 +01:00
parent 8afac0f32f
commit 38fa18023d

View File

@@ -1613,17 +1613,11 @@ is_power_of_two :: proc "contextless" (x: int) -> bool {
@(require_results)
next_power_of_two :: proc "contextless" (x: int) -> int {
k := x -1
when size_of(int) == 8 {
k = k | (k >> 32)
if x <= 1 {
return 1
}
k = k | (k >> 16)
k = k | (k >> 8)
k = k | (k >> 4)
k = k | (k >> 2)
k = k | (k >> 1)
k += 1 + int(x <= 0)
return k
n := uint(size_of(x) * 8) - uint(intrinsics.count_leading_zeros(uint(x) - 1))
return int(1) << n
}
@(require_results)