mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
[priority_deque] simplify & optimize isMinLayer
LLVM has trouble compiling the old implementation, (presumably) because `leading_zeros` is thought to be a `u7` rather than a `u6`, which means `63 - clz` is not equivalent to `63 ^ clz`, which means it can't deduce that the final condition can simply be flipped. (I am assuming `usize` is a `u64` here for ease of understanding, but it's the same for any power of 2) https://zig.godbolt.org/z/Pbj4P7ob3 The new version is slightly better too because `isMinLayer(maxInt(usize))` is now well-defined behavior.
This commit is contained in:
parent
991e00c270
commit
ff5850183e
@ -69,9 +69,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
|
||||
// The first element is on a min layer;
|
||||
// next two are on a max layer;
|
||||
// next four are on a min layer, and so on.
|
||||
const leading_zeros = @clz(index + 1);
|
||||
const highest_set_bit = @bitSizeOf(usize) - 1 - leading_zeros;
|
||||
return (highest_set_bit & 1) == 0;
|
||||
return 1 == @clz(index +% 1) & 1;
|
||||
}
|
||||
|
||||
fn nextIsMinLayer(self: Self) bool {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user