Skip to content

Commit 6026bbd

Browse files
pavelverigoLuukdegram
authored andcommitted
stage2-wasm: fix div and rem
1 parent 9be9b8c commit 6026bbd

File tree

4 files changed

+121
-188
lines changed

4 files changed

+121
-188
lines changed

src/arch/wasm/CodeGen.zig

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
18411841
.bit_or => func.airBinOp(inst, .@"or"),
18421842
.bool_and => func.airBinOp(inst, .@"and"),
18431843
.bool_or => func.airBinOp(inst, .@"or"),
1844-
.rem => func.airBinOp(inst, .rem),
1844+
.rem => func.airRem(inst),
18451845
.mod => func.airMod(inst),
18461846
.shl => func.airWrapBinOp(inst, .shl),
18471847
.shl_exact => func.airBinOp(inst, .shl),
@@ -6917,13 +6917,54 @@ fn divSigned(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type) InnerError!WVal
69176917
try func.emitWValue(lhs);
69186918
try func.emitWValue(rhs);
69196919
}
6920-
try func.addTag(.i32_div_s);
6920+
switch (wasm_bits) {
6921+
32 => try func.addTag(.i32_div_s),
6922+
64 => try func.addTag(.i64_div_s),
6923+
else => unreachable,
6924+
}
6925+
_ = try func.wrapOperand(.stack, ty);
69216926

69226927
const result = try func.allocLocal(ty);
69236928
try func.addLabel(.local_set, result.local.value);
69246929
return result;
69256930
}
69266931

6932+
fn airRem(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6933+
const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
6934+
6935+
const mod = func.bin_file.base.comp.module.?;
6936+
const ty = func.typeOfIndex(inst);
6937+
const lhs = try func.resolveInst(bin_op.lhs);
6938+
const rhs = try func.resolveInst(bin_op.rhs);
6939+
6940+
const result = if (ty.isSignedInt(mod)) result: {
6941+
const int_bits = ty.intInfo(mod).bits;
6942+
const wasm_bits = toWasmBits(int_bits) orelse {
6943+
return func.fail("TODO: `@rem` for signed integers larger than 128 bits ({d} bits requested)", .{int_bits});
6944+
};
6945+
6946+
if (wasm_bits > 64) {
6947+
return func.fail("TODO: `@rem` for signed integers larger than 64 bits ({d} bits requested)", .{int_bits});
6948+
}
6949+
6950+
const lhs_wasm = if (wasm_bits != int_bits)
6951+
try (try func.signExtendInt(lhs, ty)).toLocal(func, ty)
6952+
else
6953+
lhs;
6954+
6955+
const rhs_wasm = if (wasm_bits != int_bits)
6956+
try (try func.signExtendInt(rhs, ty)).toLocal(func, ty)
6957+
else
6958+
rhs;
6959+
6960+
_ = try func.binOp(lhs_wasm, rhs_wasm, ty, .rem);
6961+
break :result try func.wrapOperand(.stack, ty);
6962+
} else try func.binOp(lhs, rhs, ty, .rem);
6963+
6964+
const return_local = try result.toLocal(func, ty);
6965+
func.finishAir(inst, return_local, &.{ bin_op.lhs, bin_op.rhs });
6966+
}
6967+
69276968
/// Remainder after floor division, defined by:
69286969
/// @divFloor(a, b) * b + @mod(a, b) = a
69296970
fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {

test/behavior/int_div.zig

Lines changed: 0 additions & 115 deletions
This file was deleted.

test/behavior/math.zig

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ test "division" {
439439
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
440440
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
441441
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
442-
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
442+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
443443
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
444444
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
445445

@@ -448,23 +448,25 @@ test "division" {
448448
return error.SkipZigTest;
449449
}
450450

451-
try testDivision();
452-
try comptime testDivision();
451+
try testIntDivision();
452+
try comptime testIntDivision();
453+
454+
try testFloatDivision();
455+
try comptime testFloatDivision();
453456
}
454457

455-
fn testDivision() !void {
458+
fn testIntDivision() !void {
456459
try expect(div(u32, 13, 3) == 4);
457-
try expect(div(f32, 1.0, 2.0) == 0.5);
460+
try expect(div(u64, 13, 3) == 4);
461+
try expect(div(u8, 13, 3) == 4);
458462

459463
try expect(divExact(u32, 55, 11) == 5);
460464
try expect(divExact(i32, -55, 11) == -5);
461-
try expect(divExact(f32, 55.0, 11.0) == 5.0);
462-
try expect(divExact(f32, -55.0, 11.0) == -5.0);
465+
try expect(divExact(i64, -55, 11) == -5);
466+
try expect(divExact(i16, -55, 11) == -5);
463467

464468
try expect(divFloor(i32, 5, 3) == 1);
465469
try expect(divFloor(i32, -5, 3) == -2);
466-
try expect(divFloor(f32, 5.0, 3.0) == 1.0);
467-
try expect(divFloor(f32, -5.0, 3.0) == -2.0);
468470
try expect(divFloor(i32, -0x80000000, -2) == 0x40000000);
469471
try expect(divFloor(i32, 0, -0x80000000) == 0);
470472
try expect(divFloor(i32, -0x40000001, 0x40000000) == -2);
@@ -473,18 +475,15 @@ fn testDivision() !void {
473475
try expect(divFloor(i32, -14, 12) == -2);
474476
try expect(divFloor(i32, -2, 12) == -1);
475477

478+
try expect(divFloor(i8, 5, 3) == 1);
479+
try expect(divFloor(i16, -5, 3) == -2);
480+
try expect(divFloor(i64, -0x80000000, -2) == 0x40000000);
481+
try expect(divFloor(i64, -0x40000001, 0x40000000) == -2);
482+
476483
try expect(divTrunc(i32, 5, 3) == 1);
477484
try expect(divTrunc(i32, -5, 3) == -1);
478485
try expect(divTrunc(i32, 9, -10) == 0);
479486
try expect(divTrunc(i32, -9, 10) == 0);
480-
try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
481-
try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
482-
try expect(divTrunc(f32, 9.0, -10.0) == 0.0);
483-
try expect(divTrunc(f32, -9.0, 10.0) == 0.0);
484-
try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
485-
try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
486-
try expect(divTrunc(f64, 9.0, -10.0) == 0.0);
487-
try expect(divTrunc(f64, -9.0, 10.0) == 0.0);
488487
try expect(divTrunc(i32, 10, 12) == 0);
489488
try expect(divTrunc(i32, -14, 12) == -1);
490489
try expect(divTrunc(i32, -2, 12) == 0);
@@ -496,6 +495,19 @@ fn testDivision() !void {
496495
try expect(mod(i32, -14, -12) == -2);
497496
try expect(mod(i32, -2, -12) == -2);
498497

498+
try expect(mod(i64, -118, 12) == 2);
499+
try expect(mod(u32, 10, 12) == 10);
500+
try expect(mod(i64, -14, 12) == 10);
501+
try expect(mod(i16, -2, 12) == 10);
502+
try expect(mod(i16, -118, 12) == 2);
503+
try expect(mod(i8, -2, 12) == 10); // TODO: fails in x86_64
504+
505+
try expect(rem(i64, -118, 12) == -10);
506+
try expect(rem(i32, 10, 12) == 10);
507+
try expect(rem(i32, -14, 12) == -2);
508+
try expect(rem(i32, -2, 12) == -2);
509+
try expect(rem(i16, -118, 12) == -10);
510+
499511
try expect(divTrunc(i20, 20, -5) == -4);
500512
try expect(divTrunc(i20, -20, -4) == 5);
501513

@@ -524,6 +536,52 @@ fn testDivision() !void {
524536
}
525537
}
526538

539+
fn testFloatDivision() !void {
540+
try expect(div(f32, 1.0, 2.0) == 0.5);
541+
542+
try expect(divExact(f32, 55.0, 11.0) == 5.0);
543+
try expect(divExact(f32, -55.0, 11.0) == -5.0);
544+
545+
try expect(divFloor(f32, 5.0, 3.0) == 1.0);
546+
try expect(divFloor(f32, -5.0, 3.0) == -2.0);
547+
try expect(divFloor(f32, 56.0, 9.0) == 6.0);
548+
try expect(divFloor(f32, 1053.0, -41.0) == -26.0);
549+
try expect(divFloor(f16, -43.0, 12.0) == -4.0);
550+
try expect(divFloor(f64, -90.0, -9.0) == 10.0);
551+
552+
try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
553+
try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
554+
try expect(divTrunc(f32, 9.0, -10.0) == 0.0);
555+
try expect(divTrunc(f32, -9.0, 10.0) == 0.0);
556+
try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
557+
try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
558+
try expect(divTrunc(f64, 9.0, -10.0) == 0.0);
559+
try expect(divTrunc(f64, -9.0, 10.0) == 0.0);
560+
}
561+
562+
test "large integer division" {
563+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
564+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
565+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
566+
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
567+
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
568+
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
569+
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
570+
571+
{
572+
var numerator: u256 = 99999999999999999997315645440;
573+
var divisor: u256 = 10000000000000000000000000000;
574+
_ = .{ &numerator, &divisor };
575+
try expect(numerator / divisor == 9);
576+
}
577+
{
578+
var numerator: u256 = 99999999999999999999000000000000000000000;
579+
var divisor: u256 = 10000000000000000000000000000000000000000;
580+
_ = .{ &numerator, &divisor };
581+
try expect(numerator / divisor == 9);
582+
}
583+
}
584+
527585
test "division half-precision floats" {
528586
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
529587
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
@@ -564,6 +622,9 @@ fn divTrunc(comptime T: type, a: T, b: T) T {
564622
fn mod(comptime T: type, a: T, b: T) T {
565623
return @mod(a, b);
566624
}
625+
fn rem(comptime T: type, a: T, b: T) T {
626+
return @rem(a, b);
627+
}
567628

568629
test "unsigned wrapping" {
569630
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

test/behavior/stage2_wasm_div.zig

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)