authorgravatar for 94326797+riverbl@users.noreply.github.comriverbl <94326797+riverbl@users.noreply.github.com> 2023-08-23 20:30:03+01:00
committergravatar for 94326797+riverbl@users.noreply.github.comriverbl <94326797+riverbl@users.noreply.github.com> 2023-08-23 20:34:59+01:00
log87557b37c6fb112d9608bb4b6ba860f42febf188
treef92b0e4b2440c4ad4e04304cd21a524fa5fd34b3
parent383e6ffc7b9e22613f4c4b15ebdcd2bf487573d6

Replace `@panic` with `unreachable`, add test

Replace `@panic` with `unreachable` in stage2 wasm `@divFloor` implementation Add test for division and remainder operations for stage2 wasm

2 files changed, 56 insertions(+), 2 deletions(-)

src/arch/wasm/CodeGen.zig+2-2
......@@ -6429,7 +6429,7 @@ fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
64296429 const zero = switch (wasm_bits) {
64306430 32 => WValue{ .imm32 = 0 },
64316431 64 => WValue{ .imm64 = 0 },
6432 else => @panic("Unexpected wasm integer width"),
6432 else => unreachable,
64336433 };
64346434
64356435 // tee leaves the value on the stack and stores it in a local.
......@@ -6458,7 +6458,7 @@ fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
64586458 try func.addTag(.i64_extend_i32_u);
64596459 try func.addTag(.i64_sub);
64606460 },
6461 else => @panic("Unexpected wasm integer width"),
6461 else => unreachable,
64626462 }
64636463
64646464 _ = try func.binOp(lhs_wasm, rhs_wasm, ty, .rem);
test/behavior/stage2_wasm_div.zig created+54
......@@ -0,0 +1,54 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const expect = std.testing.expect;
4
5test "wasm integer division" {
6 // This test is copied from int_div.zig, with additional test cases for @divFloor on floats.
7 // TODO: Remove this test once the division tests in math.zig and int_div.zig pass with the
8 // stage2 wasm backend.
9 if (builtin.zig_backend != .stage2_wasm) return error.SkipZigTest;
10
11 try testDivision();
12 try comptime testDivision();
13}
14fn testDivision() !void {
15 try expect(div(u32, 13, 3) == 4);
16 try expect(div(u64, 13, 3) == 4);
17 try expect(div(u8, 13, 3) == 4);
18
19 try expect(divFloor(i8, 5, 3) == 1);
20 try expect(divFloor(i16, -5, 3) == -2);
21 try expect(divFloor(i64, -0x80000000, -2) == 0x40000000);
22 try expect(divFloor(i32, 0, -0x80000000) == 0);
23 try expect(divFloor(i64, -0x40000001, 0x40000000) == -2);
24 try expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
25 try expect(divFloor(i32, 10, 12) == 0);
26 try expect(divFloor(i32, -14, 12) == -2);
27 try expect(divFloor(i32, -2, 12) == -1);
28 try expect(divFloor(f32, 56.0, 9.0) == 6.0);
29 try expect(divFloor(f32, 1053.0, -41.0) == -26.0);
30 try expect(divFloor(f16, -43.0, 12.0) == -4.0);
31 try expect(divFloor(f64, -90.0, -9.0) == 10.0);
32
33 try expect(mod(u32, 10, 12) == 10);
34 try expect(mod(i32, 10, 12) == 10);
35 try expect(mod(i64, -14, 12) == 10);
36 try expect(mod(i16, -2, 12) == 10);
37 try expect(mod(i8, -2, 12) == 10);
38
39 try expect(rem(i32, 10, 12) == 10);
40 try expect(rem(i32, -14, 12) == -2);
41 try expect(rem(i32, -2, 12) == -2);
42}
43fn div(comptime T: type, a: T, b: T) T {
44 return a / b;
45}
46fn divFloor(comptime T: type, a: T, b: T) T {
47 return @divFloor(a, b);
48}
49fn mod(comptime T: type, a: T, b: T) T {
50 return @mod(a, b);
51}
52fn rem(comptime T: type, a: T, b: T) T {
53 return @rem(a, b);
54}