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