Commit 6026bbd0ad
Changed files (4)
src
arch
wasm
test
src/arch/wasm/CodeGen.zig
@@ -1841,7 +1841,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
.bit_or => func.airBinOp(inst, .@"or"),
.bool_and => func.airBinOp(inst, .@"and"),
.bool_or => func.airBinOp(inst, .@"or"),
- .rem => func.airBinOp(inst, .rem),
+ .rem => func.airRem(inst),
.mod => func.airMod(inst),
.shl => func.airWrapBinOp(inst, .shl),
.shl_exact => func.airBinOp(inst, .shl),
@@ -6917,13 +6917,54 @@ fn divSigned(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type) InnerError!WVal
try func.emitWValue(lhs);
try func.emitWValue(rhs);
}
- try func.addTag(.i32_div_s);
+ switch (wasm_bits) {
+ 32 => try func.addTag(.i32_div_s),
+ 64 => try func.addTag(.i64_div_s),
+ else => unreachable,
+ }
+ _ = try func.wrapOperand(.stack, ty);
const result = try func.allocLocal(ty);
try func.addLabel(.local_set, result.local.value);
return result;
}
+fn airRem(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
+ const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
+
+ const mod = func.bin_file.base.comp.module.?;
+ const ty = func.typeOfIndex(inst);
+ const lhs = try func.resolveInst(bin_op.lhs);
+ const rhs = try func.resolveInst(bin_op.rhs);
+
+ const result = if (ty.isSignedInt(mod)) result: {
+ const int_bits = ty.intInfo(mod).bits;
+ const wasm_bits = toWasmBits(int_bits) orelse {
+ return func.fail("TODO: `@rem` for signed integers larger than 128 bits ({d} bits requested)", .{int_bits});
+ };
+
+ if (wasm_bits > 64) {
+ return func.fail("TODO: `@rem` for signed integers larger than 64 bits ({d} bits requested)", .{int_bits});
+ }
+
+ const lhs_wasm = if (wasm_bits != int_bits)
+ try (try func.signExtendInt(lhs, ty)).toLocal(func, ty)
+ else
+ lhs;
+
+ const rhs_wasm = if (wasm_bits != int_bits)
+ try (try func.signExtendInt(rhs, ty)).toLocal(func, ty)
+ else
+ rhs;
+
+ _ = try func.binOp(lhs_wasm, rhs_wasm, ty, .rem);
+ break :result try func.wrapOperand(.stack, ty);
+ } else try func.binOp(lhs, rhs, ty, .rem);
+
+ const return_local = try result.toLocal(func, ty);
+ func.finishAir(inst, return_local, &.{ bin_op.lhs, bin_op.rhs });
+}
+
/// Remainder after floor division, defined by:
/// @divFloor(a, b) * b + @mod(a, b) = a
fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
test/behavior/int_div.zig
@@ -1,115 +0,0 @@
-const std = @import("std");
-const builtin = @import("builtin");
-const expect = std.testing.expect;
-
-test "integer division" {
- if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
- if (builtin.zig_backend == .stage2_riscv64) 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(divExact(u32, 55, 11) == 5);
- try expect(divExact(i32, -55, 11) == -5);
- try expect(divExact(i64, -55, 11) == -5);
- try expect(divExact(i16, -55, 11) == -5);
-
- 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(divTrunc(i32, 5, 3) == 1);
- try expect(divTrunc(i32, -5, 3) == -1);
- try expect(divTrunc(i32, 9, -10) == 0);
- try expect(divTrunc(i32, -9, 10) == 0);
- try expect(divTrunc(i32, 10, 12) == 0);
- try expect(divTrunc(i32, -14, 12) == -1);
- try expect(divTrunc(i32, -2, 12) == 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);
-
- comptime {
- try expect(
- 1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,
- );
- try expect(
- @rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,
- );
- try expect(
- 1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,
- );
- try expect(
- @divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,
- );
- try expect(
- @divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,
- );
- try expect(
- @divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,
- );
- try expect(
- 4126227191251978491697987544882340798050766755606969681711 % 10 == 1,
- );
- }
-}
-fn div(comptime T: type, a: T, b: T) T {
- return a / b;
-}
-fn divExact(comptime T: type, a: T, b: T) T {
- return @divExact(a, b);
-}
-fn divFloor(comptime T: type, a: T, b: T) T {
- return @divFloor(a, b);
-}
-fn divTrunc(comptime T: type, a: T, b: T) T {
- return @divTrunc(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);
-}
-
-test "large integer division" {
- if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
-
- {
- var numerator: u256 = 99999999999999999997315645440;
- var divisor: u256 = 10000000000000000000000000000;
- _ = .{ &numerator, &divisor };
- try expect(numerator / divisor == 9);
- }
- {
- var numerator: u256 = 99999999999999999999000000000000000000000;
- var divisor: u256 = 10000000000000000000000000000000000000000;
- _ = .{ &numerator, &divisor };
- try expect(numerator / divisor == 9);
- }
-}
test/behavior/math.zig
@@ -439,7 +439,7 @@ test "division" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
- if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
@@ -448,23 +448,25 @@ test "division" {
return error.SkipZigTest;
}
- try testDivision();
- try comptime testDivision();
+ try testIntDivision();
+ try comptime testIntDivision();
+
+ try testFloatDivision();
+ try comptime testFloatDivision();
}
-fn testDivision() !void {
+fn testIntDivision() !void {
try expect(div(u32, 13, 3) == 4);
- try expect(div(f32, 1.0, 2.0) == 0.5);
+ try expect(div(u64, 13, 3) == 4);
+ try expect(div(u8, 13, 3) == 4);
try expect(divExact(u32, 55, 11) == 5);
try expect(divExact(i32, -55, 11) == -5);
- try expect(divExact(f32, 55.0, 11.0) == 5.0);
- try expect(divExact(f32, -55.0, 11.0) == -5.0);
+ try expect(divExact(i64, -55, 11) == -5);
+ try expect(divExact(i16, -55, 11) == -5);
try expect(divFloor(i32, 5, 3) == 1);
try expect(divFloor(i32, -5, 3) == -2);
- try expect(divFloor(f32, 5.0, 3.0) == 1.0);
- try expect(divFloor(f32, -5.0, 3.0) == -2.0);
try expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
try expect(divFloor(i32, 0, -0x80000000) == 0);
try expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
@@ -473,18 +475,15 @@ fn testDivision() !void {
try expect(divFloor(i32, -14, 12) == -2);
try expect(divFloor(i32, -2, 12) == -1);
+ try expect(divFloor(i8, 5, 3) == 1);
+ try expect(divFloor(i16, -5, 3) == -2);
+ try expect(divFloor(i64, -0x80000000, -2) == 0x40000000);
+ try expect(divFloor(i64, -0x40000001, 0x40000000) == -2);
+
try expect(divTrunc(i32, 5, 3) == 1);
try expect(divTrunc(i32, -5, 3) == -1);
try expect(divTrunc(i32, 9, -10) == 0);
try expect(divTrunc(i32, -9, 10) == 0);
- try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
- try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
- try expect(divTrunc(f32, 9.0, -10.0) == 0.0);
- try expect(divTrunc(f32, -9.0, 10.0) == 0.0);
- try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
- try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
- try expect(divTrunc(f64, 9.0, -10.0) == 0.0);
- try expect(divTrunc(f64, -9.0, 10.0) == 0.0);
try expect(divTrunc(i32, 10, 12) == 0);
try expect(divTrunc(i32, -14, 12) == -1);
try expect(divTrunc(i32, -2, 12) == 0);
@@ -496,6 +495,19 @@ fn testDivision() !void {
try expect(mod(i32, -14, -12) == -2);
try expect(mod(i32, -2, -12) == -2);
+ try expect(mod(i64, -118, 12) == 2);
+ try expect(mod(u32, 10, 12) == 10);
+ try expect(mod(i64, -14, 12) == 10);
+ try expect(mod(i16, -2, 12) == 10);
+ try expect(mod(i16, -118, 12) == 2);
+ try expect(mod(i8, -2, 12) == 10); // TODO: fails in x86_64
+
+ try expect(rem(i64, -118, 12) == -10);
+ try expect(rem(i32, 10, 12) == 10);
+ try expect(rem(i32, -14, 12) == -2);
+ try expect(rem(i32, -2, 12) == -2);
+ try expect(rem(i16, -118, 12) == -10);
+
try expect(divTrunc(i20, 20, -5) == -4);
try expect(divTrunc(i20, -20, -4) == 5);
@@ -524,6 +536,52 @@ fn testDivision() !void {
}
}
+fn testFloatDivision() !void {
+ try expect(div(f32, 1.0, 2.0) == 0.5);
+
+ try expect(divExact(f32, 55.0, 11.0) == 5.0);
+ try expect(divExact(f32, -55.0, 11.0) == -5.0);
+
+ try expect(divFloor(f32, 5.0, 3.0) == 1.0);
+ try expect(divFloor(f32, -5.0, 3.0) == -2.0);
+ 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(divTrunc(f32, 5.0, 3.0) == 1.0);
+ try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
+ try expect(divTrunc(f32, 9.0, -10.0) == 0.0);
+ try expect(divTrunc(f32, -9.0, 10.0) == 0.0);
+ try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
+ try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
+ try expect(divTrunc(f64, 9.0, -10.0) == 0.0);
+ try expect(divTrunc(f64, -9.0, 10.0) == 0.0);
+}
+
+test "large integer division" {
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
+
+ {
+ var numerator: u256 = 99999999999999999997315645440;
+ var divisor: u256 = 10000000000000000000000000000;
+ _ = .{ &numerator, &divisor };
+ try expect(numerator / divisor == 9);
+ }
+ {
+ var numerator: u256 = 99999999999999999999000000000000000000000;
+ var divisor: u256 = 10000000000000000000000000000000000000000;
+ _ = .{ &numerator, &divisor };
+ try expect(numerator / divisor == 9);
+ }
+}
+
test "division half-precision floats" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
@@ -564,6 +622,9 @@ fn divTrunc(comptime T: type, a: T, b: T) T {
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);
+}
test "unsigned wrapping" {
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
test/behavior/stage2_wasm_div.zig
@@ -1,54 +0,0 @@
-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);
-}