master
1const common = @import("./common.zig");
2const testing = @import("std").testing;
3
4pub const panic = common.panic;
5
6comptime {
7 @export(&__subvsi3, .{ .name = "__subvsi3", .linkage = common.linkage, .visibility = common.visibility });
8}
9
10pub fn __subvsi3(a: i32, b: i32) callconv(.c) i32 {
11 const sum = a -% b;
12 // Overflow occurred iff the operands have opposite signs, and the sign of the
13 // sum is the opposite of the lhs sign.
14 if (((a ^ b) & (sum ^ a)) < 0) @panic("compiler-rt: integer overflow");
15 return sum;
16}
17
18test "subvsi3" {
19 // min i32 = -2147483648
20 // max i32 = 2147483647
21 // TODO write panic handler for testing panics
22 // try test__subvsi3(-2147483648, -1, -1); // panic
23 // try test__subvsi3(2147483647, 1, 1); // panic
24 try testing.expectEqual(-2147483648, __subvsi3(-2147483647, 1));
25 try testing.expectEqual(2147483647, __subvsi3(2147483646, -1));
26}