motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 83a3365bfd9dc17067de26b3cb4c9af55a34644e (tree)
parent 05b7ca6356e4f394c084de8e806c1c93440e0b6b
Author: mlugg <mlugg@mlugg.co.uk>
Date:   Sat, 13 Sep 2025 11:31:17 +0100

std.math.big.int: normalize zero result for small multiplications

Resolves: #25221

Diffstat:
Mlib/std/math/big/int.zig | 7+++----
Mtest/behavior/enum.zig | 7+++++++
2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig @@ -786,11 +786,10 @@ pub const Mutable = struct { assert(rma.limbs.ptr != b.limbs.ptr); // illegal aliasing if (a.limbs.len == 1 and b.limbs.len == 1) { - const ov = @mulWithOverflow(a.limbs[0], b.limbs[0]); - rma.limbs[0] = ov[0]; - if (ov[1] == 0) { + rma.limbs[0], const overflow_bit = @mulWithOverflow(a.limbs[0], b.limbs[0]); + if (overflow_bit == 0) { rma.len = 1; - rma.positive = (a.positive == b.positive); + rma.positive = (a.positive == b.positive) or rma.limbs[0] == 0; return; } } diff --git a/test/behavior/enum.zig b/test/behavior/enum.zig @@ -1325,3 +1325,10 @@ test "large enum field values" { try expect(@intFromEnum(e) == std.math.maxInt(i128)); } } + +test "comptime @enumFromInt with signed arithmetic" { + const E = enum(i8) { foo = -1, bar = 0 }; + const x: E = @enumFromInt(@as(i8, -1) * 0); + comptime assert(x == .bar); + comptime assert(@intFromEnum(x) == 0); +}