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