| 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(&__subvsi3, "__subvsi3"); |
| 7 | } |
| 8 | |
| 9 | pub fn __subvsi3(a: i32, b: i32) callconv(.c) i32 { |
| 10 | const sum = a -% b; |
| 11 | // Overflow occurred iff the operands have opposite signs, and the sign of the |
| 12 | // sum is the opposite of the lhs sign. |
| 13 | if (((a ^ b) & (sum ^ a)) < 0) @panic("integer overflow"); |
| 14 | return sum; |
| 15 | } |
| 16 | |
| 17 | test "subvsi3" { |
| 18 | // min i32 = -2147483648 |
| 19 | // max i32 = 2147483647 |
| 20 | // TODO write panic handler for testing panics |
| 21 | // try test__subvsi3(-2147483648, -1, -1); // panic |
| 22 | // try test__subvsi3(2147483647, 1, 1); // panic |
| 23 | try testing.expectEqual(-2147483648, __subvsi3(-2147483647, 1)); |
| 24 | try testing.expectEqual(2147483647, __subvsi3(2147483646, -1)); |
| 25 | } |