Skip to content

Commit 0386730

Browse files
authored
compiler-rt: add __addvsi3, __subvsi3, __mulvsi3, and __subvdi3
1 parent c907866 commit 0386730

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ set(ZIG_STAGE2_SOURCES
205205
lib/compiler_rt/addo.zig
206206
lib/compiler_rt/addsf3.zig
207207
lib/compiler_rt/addtf3.zig
208+
lib/compiler_rt/addvsi3.zig
208209
lib/compiler_rt/addxf3.zig
209210
lib/compiler_rt/arm.zig
210211
lib/compiler_rt/atomics.zig
@@ -323,6 +324,7 @@ set(ZIG_STAGE2_SOURCES
323324
lib/compiler_rt/mulo.zig
324325
lib/compiler_rt/mulsf3.zig
325326
lib/compiler_rt/multf3.zig
327+
lib/compiler_rt/mulvsi3.zig
326328
lib/compiler_rt/mulxf3.zig
327329
lib/compiler_rt/negXi2.zig
328330
lib/compiler_rt/negdf2.zig
@@ -346,6 +348,8 @@ set(ZIG_STAGE2_SOURCES
346348
lib/compiler_rt/subo.zig
347349
lib/compiler_rt/subsf3.zig
348350
lib/compiler_rt/subtf3.zig
351+
lib/compiler_rt/subvdi3.zig
352+
lib/compiler_rt/subvsi3.zig
349353
lib/compiler_rt/subxf3.zig
350354
lib/compiler_rt/tan.zig
351355
lib/compiler_rt/trig.zig

lib/compiler_rt.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ comptime {
2727
_ = @import("compiler_rt/absvti2.zig");
2828
_ = @import("compiler_rt/negv.zig");
2929

30+
_ = @import("compiler_rt/addvsi3.zig");
31+
_ = @import("compiler_rt/subvsi3.zig");
32+
_ = @import("compiler_rt/subvdi3.zig");
33+
_ = @import("compiler_rt/mulvsi3.zig");
34+
3035
_ = @import("compiler_rt/addo.zig");
3136
_ = @import("compiler_rt/subo.zig");
3237
_ = @import("compiler_rt/mulo.zig");

lib/compiler_rt/addvsi3.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const addv = @import("addo.zig");
2+
const common = @import("./common.zig");
3+
const testing = @import("std").testing;
4+
5+
pub const panic = common.panic;
6+
7+
comptime {
8+
@export(&__addvsi3, .{ .name = "__addvsi3", .linkage = common.linkage, .visibility = common.visibility });
9+
}
10+
11+
pub fn __addvsi3(a: i32, b: i32) callconv(.c) i32 {
12+
var overflow: c_int = 0;
13+
const sum = addv.__addosi4(a, b, &overflow);
14+
if (overflow != 0) @panic("compiler-rt: integer overflow");
15+
return sum;
16+
}
17+
18+
test "addvsi3" {
19+
// const min: i32 = -2147483648
20+
// const max: i32 = 2147483647
21+
// TODO write panic handler for testing panics
22+
// try test__addvsi3(-2147483648, -1, -1); // panic
23+
// try test__addvsi3(2147483647, 1, 1); // panic
24+
try testing.expectEqual(-2147483648, __addvsi3(-2147483647, -1));
25+
try testing.expectEqual(2147483647, __addvsi3(2147483646, 1));
26+
}

lib/compiler_rt/mulvsi3.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const mulv = @import("mulo.zig");
2+
const common = @import("./common.zig");
3+
const testing = @import("std").testing;
4+
5+
pub const panic = common.panic;
6+
7+
comptime {
8+
@export(&__mulvsi3, .{ .name = "__mulvsi3", .linkage = common.linkage, .visibility = common.visibility });
9+
}
10+
11+
pub fn __mulvsi3(a: i32, b: i32) callconv(.c) i32 {
12+
var overflow: c_int = 0;
13+
const sum = mulv.__mulosi4(a, b, &overflow);
14+
if (overflow != 0) @panic("compiler-rt: integer overflow");
15+
return sum;
16+
}
17+
18+
test "mulvsi3" {
19+
// min i32 = -2147483648
20+
// max i32 = 2147483647
21+
// TODO write panic handler for testing panics
22+
// try test__mulvsi3(-2147483648, -1, -1); // panic
23+
// try test__mulvsi3(2147483647, 1, 1); // panic
24+
try testing.expectEqual(-2147483648, __mulvsi3(-1073741824, 2));
25+
try testing.expectEqual(2147483646, __mulvsi3(1073741823, 2)); // one too less for corner case 2147483647
26+
}

lib/compiler_rt/subvdi3.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const subv = @import("subo.zig");
2+
const common = @import("./common.zig");
3+
const testing = @import("std").testing;
4+
5+
pub const panic = common.panic;
6+
7+
comptime {
8+
@export(&__subvdi3, .{ .name = "__subvdi3", .linkage = common.linkage, .visibility = common.visibility });
9+
}
10+
11+
pub fn __subvdi3(a: i64, b: i64) callconv(.c) i64 {
12+
var overflow: c_int = 0;
13+
const sum = subv.__subodi4(a, b, &overflow);
14+
if (overflow != 0) @panic("compiler-rt: integer overflow");
15+
return sum;
16+
}
17+
18+
test "subvdi3" {
19+
// min i64 = -9223372036854775808
20+
// max i64 = 9223372036854775807
21+
// TODO write panic handler for testing panics
22+
// try test__subvdi3(-9223372036854775808, -1, -1); // panic
23+
// try test__addvdi3(9223372036854775807, 1, 1); // panic
24+
try testing.expectEqual(-9223372036854775808, __subvdi3(-9223372036854775807, 1));
25+
try testing.expectEqual(9223372036854775807, __subvdi3(9223372036854775806, -1));
26+
}

lib/compiler_rt/subvsi3.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const subv = @import("subo.zig");
2+
const common = @import("./common.zig");
3+
const testing = @import("std").testing;
4+
5+
pub const panic = common.panic;
6+
7+
comptime {
8+
@export(&__subvsi3, .{ .name = "__subvsi3", .linkage = common.linkage, .visibility = common.visibility });
9+
}
10+
11+
pub fn __subvsi3(a: i32, b: i32) callconv(.c) i32 {
12+
var overflow: c_int = 0;
13+
const sum = subv.__subosi4(a, b, &overflow);
14+
if (overflow != 0) @panic("compiler-rt: integer overflow");
15+
return sum;
16+
}
17+
18+
test "subvsi3" {
19+
// min i32 = -2147483648
20+
// max i32 = 2147483647
21+
// TODO write panic handler for testing panics
22+
// try test__subvsi3(-2147483648, -1, -1); // panic
23+
// try test__subvsi3(2147483647, 1, 1); // panic
24+
try testing.expectEqual(-2147483648, __subvsi3(-2147483647, 1));
25+
try testing.expectEqual(2147483647, __subvsi3(2147483646, -1));
26+
}

0 commit comments

Comments
 (0)