commit 74f361a5ce5212ce321fd0ebfa4c158468a161bb (tree)
parent da6d4e28eff7b65589b046d2772516a6d9fd25bc
Author: hemisputnik <hemisputnik@proton.me>
Date: Sun, 1 Mar 2026 14:05:22 +0200
std.math.big.int: address log2/log10 reviews
There were good reviews made after #31365 was merged, so this commit
addresses them separately.
1. Assert that the number is greater than zero
2. Use `constants` instead of calculating constants manually
3. Use `Const.bitCountAbs` for log2
Diffstat:
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig
@@ -2678,7 +2678,9 @@ pub const Const = struct {
/// Calculate the base 2 logarithm, rounded down.
pub fn log2(a: Const) Limb {
- return a.limbs.len * @bitSizeOf(Limb) - 1 - @clz(a.limbs[a.limbs.len - 1]);
+ assert(a.positive);
+ assert(!a.eqlZero());
+ return a.bitCountAbs() - 1;
}
/// Calculate the base 10 logarithm, rounded down.
@@ -2695,13 +2697,9 @@ pub const Const = struct {
///
/// `limbs_buffer` is used for temporary storage. The amount required is given by `calcLog10LimbsBufferLen`.
pub fn log10(a: Const, limbs_buffer: []Limb) Limb {
- const max_digits_per_limb = std.math.log10(std.math.maxInt(Limb));
- const limb_base = comptime calc: {
- var limb_base: comptime_int = 1;
- for (0..max_digits_per_limb) |_| limb_base *= 10;
- break :calc limb_base;
- };
- const limb_base_as_bigint: Const = .{ .limbs = &.{limb_base}, .positive = true };
+ assert(a.positive);
+ assert(!a.eqlZero());
+ const limb_base_as_bigint: Const = .{ .limbs = &.{constants.big_bases[10]}, .positive = true };
var q: Mutable = .{
.limbs = limbs_buffer[0 .. a.limbs.len + 2],
@@ -2721,7 +2719,7 @@ pub const Const = struct {
var num_digits: Limb = 0;
while (q.len >= 2) {
q.divTrunc(&remainder, q.toConst(), limb_base_as_bigint, division_buf);
- num_digits += max_digits_per_limb;
+ num_digits += constants.digits_per_limb[10];
}
var remaining_limb = q.limbs[0];
while (remaining_limb != 0) {