authorgravatar for Validark@pm.meNiles Salter <Validark@pm.me> 2023-06-20 17:32:07-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-22 01:57:28-07:00
logff5850183eee854fdfe0d3f7b7242b9ff56c2116
tree715ed346487b0f221f92e26280ea5e5b52a9254a
parent991e00c2702b31dffb7b5ada26c7b19a552502a7

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

1 files changed, 1 insertions(+), 3 deletions(-)

lib/std/priority_dequeue.zig+1-3
...@@ -69,9 +69,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar...@@ -69,9 +69,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
69 // The first element is on a min layer;69 // The first element is on a min layer;
70 // next two are on a max layer;70 // next two are on a max layer;
71 // next four are on a min layer, and so on.71 // next four are on a min layer, and so on.
72 const leading_zeros = @clz(index + 1);72 return 1 == @clz(index +% 1) & 1;
73 const highest_set_bit = @bitSizeOf(usize) - 1 - leading_zeros;
74 return (highest_set_bit & 1) == 0;
75 }73 }
7674
77 fn nextIsMinLayer(self: Self) bool {75 fn nextIsMinLayer(self: Self) bool {