From 7c17f4c4b37804b8f7c01d85e0e4c54bdcf34e1d Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sun, 4 Oct 2026 03:38:42 -0400 Subject: [PATCH] math: simplify code --- lib/std/math/big/int.zig | 2 +- lib/std/math/log10.zig | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 5f33b91b52a95b0e699b5db8e247c9d1390083d1..ba0ea5807b95a6f4fe287373763b7260f08caac8 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -35,7 +35,7 @@ const constants: Constants = blk: { digits_per_limb[base] = @intCast(math.log(Limb, base, math.maxInt(Limb))); bases[base] = std.math.pow(Limb, base, digits_per_limb[base]); } - break :blk Constants{ .big_bases = bases, .digits_per_limb = digits_per_limb }; + break :blk .{ .big_bases = bases, .digits_per_limb = digits_per_limb }; }; /// Returns the number of limbs needed to store `scalar`, which must be a diff --git a/lib/std/math/log10.zig b/lib/std/math/log10.zig index 0dc5b2a8be90cd88276ec344c9de51a492b6430a..fac0e3b14b502be61902317a26729bc69b0194e5 100644 --- a/lib/std/math/log10.zig +++ b/lib/std/math/log10.zig @@ -58,9 +58,9 @@ pub fn log10_int(x: anytype) std.math.Log2Int(@TypeOf(x)) { var log: u32 = 0; inline for (0..11) |i| { + const num_digits = (1 << (11 - i)) * 5; // Unnecessary branches should be removed by the compiler - if (bit_size > (1 << (11 - i)) * 5 * @log2(10.0) and val >= pow10((1 << (11 - i)) * 5)) { - const num_digits = (1 << (11 - i)) * 5; + if (bit_size > num_digits * @log2(10.0) and val >= pow10(num_digits)) { val /= pow10(num_digits); log += num_digits; } -- 2.54.0