diff --git a/lib/std/sort/pdq.zig b/lib/std/sort/pdq.zig index 85d3a62c9c5233441edb013b6f6d611d7159b9b9..5b0da14156ae1365f7a677c84453d0b07d84a774 100644 --- a/lib/std/sort/pdq.zig +++ b/lib/std/sort/pdq.zig @@ -44,12 +44,11 @@ pub fn pdqContext(a: usize, b: usize, context: anytype) void { // slices of up to this length get sorted using insertion sort. const max_insertion = 24; // number of allowed imbalanced partitions before switching to heap sort. - const max_limit = std.math.floorPowerOfTwo(usize, b - a) + 1; + const max_limit = if (b > a) math.log2_int(usize, b - a) else 0; // set upper bound on stack memory usage. const Range = struct { a: usize, b: usize, limit: usize, leftmost: bool }; - const stack_size = math.log2(math.maxInt(usize) + 1); - var stack: [stack_size]Range = undefined; + var stack: [2 * @bitSizeOf(usize)]Range = undefined; var range = Range{ .a = a, .b = b, .limit = max_limit, .leftmost = true }; var top: usize = 0;