From 87557b37c6fb112d9608bb4b6ba860f42febf188 Mon Sep 17 00:00:00 2001 From: riverbl <94326797+riverbl@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:30:03 +0100 Subject: [PATCH] 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 --- src/arch/wasm/CodeGen.zig | 4 +-- test/behavior/stage2_wasm_div.zig | 54 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/behavior/stage2_wasm_div.zig diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index bad99e9f59..db6daa8c21 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -6429,7 +6429,7 @@ fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { const zero = switch (wasm_bits) { 32 => WValue{ .imm32 = 0 }, 64 => WValue{ .imm64 = 0 }, - else => @panic("Unexpected wasm integer width"), + else => unreachable, }; // 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 { try func.addTag(.i64_extend_i32_u); try func.addTag(.i64_sub); }, - else => @panic("Unexpected wasm integer width"), + else => unreachable, } _ = try func.binOp(lhs_wasm, rhs_wasm, ty, .rem); diff --git a/test/behavior/stage2_wasm_div.zig b/test/behavior/stage2_wasm_div.zig new file mode 100644 index 0000000000..ed09578fab --- /dev/null +++ b/test/behavior/stage2_wasm_div.zig @@ -0,0 +1,54 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const expect = std.testing.expect; + +test "wasm integer division" { + // This test is copied from int_div.zig, with additional test cases for @divFloor on floats. + // TODO: Remove this test once the division tests in math.zig and int_div.zig pass with the + // stage2 wasm backend. + if (builtin.zig_backend != .stage2_wasm) return error.SkipZigTest; + + try testDivision(); + try comptime testDivision(); +} +fn testDivision() !void { + try expect(div(u32, 13, 3) == 4); + try expect(div(u64, 13, 3) == 4); + try expect(div(u8, 13, 3) == 4); + + try expect(divFloor(i8, 5, 3) == 1); + try expect(divFloor(i16, -5, 3) == -2); + try expect(divFloor(i64, -0x80000000, -2) == 0x40000000); + try expect(divFloor(i32, 0, -0x80000000) == 0); + try expect(divFloor(i64, -0x40000001, 0x40000000) == -2); + try expect(divFloor(i32, -0x80000000, 1) == -0x80000000); + try expect(divFloor(i32, 10, 12) == 0); + try expect(divFloor(i32, -14, 12) == -2); + try expect(divFloor(i32, -2, 12) == -1); + try expect(divFloor(f32, 56.0, 9.0) == 6.0); + try expect(divFloor(f32, 1053.0, -41.0) == -26.0); + try expect(divFloor(f16, -43.0, 12.0) == -4.0); + try expect(divFloor(f64, -90.0, -9.0) == 10.0); + + try expect(mod(u32, 10, 12) == 10); + try expect(mod(i32, 10, 12) == 10); + try expect(mod(i64, -14, 12) == 10); + try expect(mod(i16, -2, 12) == 10); + try expect(mod(i8, -2, 12) == 10); + + try expect(rem(i32, 10, 12) == 10); + try expect(rem(i32, -14, 12) == -2); + try expect(rem(i32, -2, 12) == -2); +} +fn div(comptime T: type, a: T, b: T) T { + return a / b; +} +fn divFloor(comptime T: type, a: T, b: T) T { + return @divFloor(a, b); +} +fn mod(comptime T: type, a: T, b: T) T { + return @mod(a, b); +} +fn rem(comptime T: type, a: T, b: T) T { + return @rem(a, b); +}