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