diff options
| author | Pavel Verigo <paul.verigo@gmail.com> | 2024-06-21 22:26:21 +0200 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2024-06-22 21:35:36 +0200 |
| commit | 6026bbd0adb445a0edd6defb0b378937dcce7a9b (patch) | |
| tree | bfd570b423f20b1f061bc04d8ee2d8339520f6f3 /test/behavior/math.zig | |
| parent | 9be9b8ca902bb7c250a6836c6090af8525464749 (diff) | |
| download | zig-6026bbd0adb445a0edd6defb0b378937dcce7a9b.tar.gz zig-6026bbd0adb445a0edd6defb0b378937dcce7a9b.zip | |
stage2-wasm: fix div and rem
Diffstat (limited to 'test/behavior/math.zig')
| -rw-r--r-- | test/behavior/math.zig | 95 |
1 files changed, 78 insertions, 17 deletions
diff --git a/test/behavior/math.zig b/test/behavior/math.zig index fefcf4b0e8..6ff4380a44 100644 --- a/test/behavior/math.zig +++ b/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; |
