zig/lib/compiler_rt/addvsi3.zig
Matthew Lugg 20bd5e8018
compiler-rt: remove dead code
`__addosi4`, `__addodi4`, `__addoti4`, `__subosi4`, `__subodi4`, and
`__suboti4` were all functions which we invented for no apparent reason.
Neither LLVM, nor GCC, nor the Zig compiler use these functions. It
appears the functions were created in a kind of misunderstanding of an
old language proposal; see https://github.com/ziglang/zig/pull/10824.

There is no benefit to these functions existing; if a Zig compiler
backend needs this operation, it is trivial to implement, and *far*
simpler than calling a compiler-rt routine. Therefore, this commit
deletes them. A small amount of that code was used by other parts of
compiler-rt; the logic is trivial so has just been inlined where needed.
I also chose to quickly implement `__addvdi3` (a standard function)
because it is trivial and we already implement the `sub` parallel.
2025-11-12 16:00:16 +00:00

27 lines
981 B
Zig

const common = @import("./common.zig");
const testing = @import("std").testing;
pub const panic = common.panic;
comptime {
@export(&__addvsi3, .{ .name = "__addvsi3", .linkage = common.linkage, .visibility = common.visibility });
}
pub fn __addvsi3(a: i32, b: i32) callconv(.c) i32 {
const sum = a +% b;
// Overflow occurred iff both operands have the same sign, and the sign of the sum does
// not match it. In other words, iff the sum sign is not the sign of either operand.
if (((sum ^ a) & (sum ^ b)) < 0) @panic("compiler-rt: integer overflow");
return sum;
}
test "addvsi3" {
// const min: i32 = -2147483648
// const max: i32 = 2147483647
// TODO write panic handler for testing panics
// try test__addvsi3(-2147483648, -1, -1); // panic
// try test__addvsi3(2147483647, 1, 1); // panic
try testing.expectEqual(-2147483648, __addvsi3(-2147483647, -1));
try testing.expectEqual(2147483647, __addvsi3(2147483646, 1));
}