| ... | @@ -44,12 +44,11 @@ pub fn pdqContext(a: usize, b: usize, context: anytype) void { | ... | @@ -44,12 +44,11 @@ pub fn pdqContext(a: usize, b: usize, context: anytype) void { |
| 44 | // slices of up to this length get sorted using insertion sort. | 44 | // slices of up to this length get sorted using insertion sort. |
| 45 | const max_insertion = 24; | 45 | const max_insertion = 24; |
| 46 | // number of allowed imbalanced partitions before switching to heap sort. | 46 | // number of allowed imbalanced partitions before switching to heap sort. |
| 47 | const max_limit = std.math.floorPowerOfTwo(usize, b - a) + 1; | 47 | const max_limit = if (b > a) math.log2_int(usize, b - a) else 0; |
| 48 | | 48 | |
| 49 | // set upper bound on stack memory usage. | 49 | // set upper bound on stack memory usage. |
| 50 | const Range = struct { a: usize, b: usize, limit: usize, leftmost: bool }; | 50 | const Range = struct { a: usize, b: usize, limit: usize, leftmost: bool }; |
| 51 | const stack_size = math.log2(math.maxInt(usize) + 1); | 51 | var stack: [2 * @bitSizeOf(usize)]Range = undefined; |
| 52 | var stack: [stack_size]Range = undefined; | | |
| 53 | var range = Range{ .a = a, .b = b, .limit = max_limit, .leftmost = true }; | 52 | var range = Range{ .a = a, .b = b, .limit = max_limit, .leftmost = true }; |
| 54 | var top: usize = 0; | 53 | var top: usize = 0; |
| 55 | | 54 | |