1const testing = @import("std").testing;
2const compiler_rt = @import("../compiler_rt.zig");
3const symbol = compiler_rt.symbol;
4
5comptime {
6 symbol(&__subvdi3, "__subvdi3");
7}
8
9pub fn __subvdi3(a: i64, b: i64) callconv(.c) i64 {
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
17test "subvdi3" {
18 // min i64 = -9223372036854775808
19 // max i64 = 9223372036854775807
20 // TODO write panic handler for testing panics
21 // try test__subvdi3(-9223372036854775808, -1, -1); // panic
22 // try test__addvdi3(9223372036854775807, 1, 1); // panic
23 try testing.expectEqual(-9223372036854775808, __subvdi3(-9223372036854775807, 1));
24 try testing.expectEqual(9223372036854775807, __subvdi3(9223372036854775806, -1));
25}