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