authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-23 20:42:25-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-23 20:42:25-07:00
logfa95c89a717f283215e9fd3ea0b8ebcd1599a69b
tree8f2a312f2dddb0ddd91b1abd4419f51deb4da0bd
parent399f4fe7d69723eb94db7e13d2e8ffdc0ce4345b
parentba8522e6c79b5c93cfbd8bdfbecabf8255848624
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #20758 from pavelverigo/stage2-wasm-compiler-rt-test-pass

stage2-wasm: pass compiler_rt test suite

14 files changed, 257 insertions(+), 49 deletions(-)

lib/compiler_rt/ceil.zig+4-4
......@@ -48,14 +48,14 @@ pub fn ceilf(x: f32) callconv(.C) f32 {
4848 if (u & m == 0) {
4949 return x;
5050 }
51 mem.doNotOptimizeAway(x + 0x1.0p120);
51 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
5252 if (u >> 31 == 0) {
5353 u += m;
5454 }
5555 u &= ~m;
5656 return @bitCast(u);
5757 } else {
58 mem.doNotOptimizeAway(x + 0x1.0p120);
58 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
5959 if (u >> 31 != 0) {
6060 return -0.0;
6161 } else {
......@@ -82,7 +82,7 @@ pub fn ceil(x: f64) callconv(.C) f64 {
8282 }
8383
8484 if (e <= 0x3FF - 1) {
85 mem.doNotOptimizeAway(y);
85 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
8686 if (u >> 63 != 0) {
8787 return -0.0;
8888 } else {
......@@ -116,7 +116,7 @@ pub fn ceilq(x: f128) callconv(.C) f128 {
116116 }
117117
118118 if (e <= 0x3FFF - 1) {
119 mem.doNotOptimizeAway(y);
119 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
120120 if (u >> 127 != 0) {
121121 return -0.0;
122122 } else {
lib/compiler_rt/common.zig+2
......@@ -24,6 +24,8 @@ pub const want_aeabi = switch (builtin.abi) {
2424};
2525pub const want_ppc_abi = builtin.cpu.arch.isPPC() or builtin.cpu.arch.isPPC64();
2626
27pub const want_float_exceptions = !builtin.cpu.arch.isWasm();
28
2729// Libcalls that involve u128 on Windows x86-64 are expected by LLVM to use the
2830// calling convention of @Vector(2, u64), rather than what's standard.
2931pub const want_windows_v2u64_abi = builtin.os.tag == .windows and builtin.cpu.arch == .x86_64 and @import("builtin").object_format != .c;
lib/compiler_rt/cos.zig+2-2
......@@ -41,7 +41,7 @@ pub fn cosf(x: f32) callconv(.C) f32 {
4141 if (ix <= 0x3f490fda) { // |x| ~<= pi/4
4242 if (ix < 0x39800000) { // |x| < 2**-12
4343 // raise inexact if x != 0
44 mem.doNotOptimizeAway(x + 0x1p120);
44 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
4545 return 1.0;
4646 }
4747 return trig.__cosdf(x);
......@@ -92,7 +92,7 @@ pub fn cos(x: f64) callconv(.C) f64 {
9292 if (ix <= 0x3fe921fb) {
9393 if (ix < 0x3e46a09e) { // |x| < 2**-27 * sqrt(2)
9494 // raise inexact if x!=0
95 mem.doNotOptimizeAway(x + 0x1p120);
95 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
9696 return 1.0;
9797 }
9898 return trig.__cos(x, 0);
lib/compiler_rt/exp.zig+4-4
......@@ -59,7 +59,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {
5959 return x * 0x1.0p127;
6060 }
6161 if (sign != 0) {
62 mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow
62 if (common.want_float_exceptions) mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow
6363 // x <= -103.972084
6464 if (hx >= 0x42CFF1B5) {
6565 return 0;
......@@ -91,7 +91,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {
9191 hi = x;
9292 lo = 0;
9393 } else {
94 mem.doNotOptimizeAway(0x1.0p127 + x); // inexact
94 if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p127 + x); // inexact
9595 return 1 + x;
9696 }
9797
......@@ -142,7 +142,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {
142142 }
143143 if (x < -708.39641853226410622) {
144144 // underflow if x != -inf
145 // mem.doNotOptimizeAway(@as(f32, -0x1.0p-149 / x));
145 // if (common.want_float_exceptions) mem.doNotOptimizeAway(@as(f32, -0x1.0p-149 / x));
146146 if (x < -745.13321910194110842) {
147147 return 0;
148148 }
......@@ -175,7 +175,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {
175175 lo = 0;
176176 } else {
177177 // inexact if x != 0
178 // mem.doNotOptimizeAway(0x1.0p1023 + x);
178 // if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p1023 + x);
179179 return 1 + x;
180180 }
181181
lib/compiler_rt/exp2.zig+2-2
......@@ -55,7 +55,7 @@ pub fn exp2f(x: f32) callconv(.C) f32 {
5555 // x < -126
5656 if (u >= 0x80000000) {
5757 if (u >= 0xC3160000 or u & 0x000FFFF != 0) {
58 mem.doNotOptimizeAway(-0x1.0p-149 / x);
58 if (common.want_float_exceptions) mem.doNotOptimizeAway(-0x1.0p-149 / x);
5959 }
6060 // x <= -150
6161 if (u >= 0x3160000) {
......@@ -120,7 +120,7 @@ pub fn exp2(x: f64) callconv(.C) f64 {
120120 if (ux >> 63 != 0) {
121121 // underflow
122122 if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {
123 mem.doNotOptimizeAway(@as(f32, @floatCast(-0x1.0p-149 / x)));
123 if (common.want_float_exceptions) mem.doNotOptimizeAway(@as(f32, @floatCast(-0x1.0p-149 / x)));
124124 }
125125 if (x <= -1075) {
126126 return 0;
lib/compiler_rt/floor.zig+6-6
......@@ -45,13 +45,13 @@ pub fn __floorh(x: f16) callconv(.C) f16 {
4545 if (u & m == 0) {
4646 return x;
4747 }
48 mem.doNotOptimizeAway(x + 0x1.0p120);
48 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
4949 if (u >> 15 != 0) {
5050 u += m;
5151 }
5252 return @bitCast(u & ~m);
5353 } else {
54 mem.doNotOptimizeAway(x + 0x1.0p120);
54 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
5555 if (u >> 15 == 0) {
5656 return 0.0;
5757 } else {
......@@ -79,13 +79,13 @@ pub fn floorf(x: f32) callconv(.C) f32 {
7979 if (u & m == 0) {
8080 return x;
8181 }
82 mem.doNotOptimizeAway(x + 0x1.0p120);
82 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
8383 if (u >> 31 != 0) {
8484 u += m;
8585 }
8686 return @bitCast(u & ~m);
8787 } else {
88 mem.doNotOptimizeAway(x + 0x1.0p120);
88 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
8989 if (u >> 31 == 0) {
9090 return 0.0;
9191 } else {
......@@ -112,7 +112,7 @@ pub fn floor(x: f64) callconv(.C) f64 {
112112 }
113113
114114 if (e <= 0x3FF - 1) {
115 mem.doNotOptimizeAway(y);
115 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
116116 if (u >> 63 != 0) {
117117 return -1.0;
118118 } else {
......@@ -146,7 +146,7 @@ pub fn floorq(x: f128) callconv(.C) f128 {
146146 }
147147
148148 if (e <= 0x3FFF - 1) {
149 mem.doNotOptimizeAway(y);
149 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
150150 if (u >> 127 != 0) {
151151 return -1.0;
152152 } else {
lib/compiler_rt/round.zig+3-3
......@@ -46,7 +46,7 @@ pub fn roundf(x_: f32) callconv(.C) f32 {
4646 x = -x;
4747 }
4848 if (e < 0x7F - 1) {
49 mem.doNotOptimizeAway(x + f32_toint);
49 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f32_toint);
5050 return 0 * @as(f32, @bitCast(u));
5151 }
5252
......@@ -81,7 +81,7 @@ pub fn round(x_: f64) callconv(.C) f64 {
8181 x = -x;
8282 }
8383 if (e < 0x3ff - 1) {
84 mem.doNotOptimizeAway(x + f64_toint);
84 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f64_toint);
8585 return 0 * @as(f64, @bitCast(u));
8686 }
8787
......@@ -121,7 +121,7 @@ pub fn roundq(x_: f128) callconv(.C) f128 {
121121 x = -x;
122122 }
123123 if (e < 0x3FFF - 1) {
124 mem.doNotOptimizeAway(x + f128_toint);
124 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f128_toint);
125125 return 0 * @as(f128, @bitCast(u));
126126 }
127127
lib/compiler_rt/sin.zig+2-2
......@@ -49,7 +49,7 @@ pub fn sinf(x: f32) callconv(.C) f32 {
4949 if (ix <= 0x3f490fda) { // |x| ~<= pi/4
5050 if (ix < 0x39800000) { // |x| < 2**-12
5151 // raise inexact if x!=0 and underflow if subnormal
52 mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
52 if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
5353 return x;
5454 }
5555 return trig.__sindf(x);
......@@ -98,7 +98,7 @@ pub fn sin(x: f64) callconv(.C) f64 {
9898 if (ix <= 0x3fe921fb) {
9999 if (ix < 0x3e500000) { // |x| < 2**-26
100100 // raise inexact if x != 0 and underflow if subnormal
101 mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
101 if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
102102 return x;
103103 }
104104 return trig.__sin(x, 0.0, 0);
lib/compiler_rt/sincos.zig+3-3
......@@ -46,7 +46,7 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.C) void {
4646 // |x| < 2**-12
4747 if (ix < 0x39800000) {
4848 // raise inexact if x!=0 and underflow if subnormal
49 mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
49 if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
5050 r_sin.* = x;
5151 r_cos.* = 1.0;
5252 return;
......@@ -134,7 +134,7 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void {
134134 // if |x| < 2**-27 * sqrt(2)
135135 if (ix < 0x3e46a09e) {
136136 // raise inexact if x != 0 and underflow if subnormal
137 mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
137 if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
138138 r_sin.* = x;
139139 r_cos.* = 1.0;
140140 return;
......@@ -232,7 +232,7 @@ inline fn sincos_generic(comptime F: type, x: F, r_sin: *F, r_cos: *F) void {
232232 if (se < 0x3fff - math.floatFractionalBits(F) - 1) {
233233 // raise underflow if subnormal
234234 if (se == 0) {
235 mem.doNotOptimizeAway(x * 0x1p-120);
235 if (common.want_float_exceptions) mem.doNotOptimizeAway(x * 0x1p-120);
236236 }
237237 r_sin.* = x;
238238 // raise inexact if x!=0
lib/compiler_rt/tan.zig+2-2
......@@ -51,7 +51,7 @@ pub fn tanf(x: f32) callconv(.C) f32 {
5151 if (ix <= 0x3f490fda) { // |x| ~<= pi/4
5252 if (ix < 0x39800000) { // |x| < 2**-12
5353 // raise inexact if x!=0 and underflow if subnormal
54 mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
54 if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
5555 return x;
5656 }
5757 return kernel.__tandf(x, false);
......@@ -89,7 +89,7 @@ pub fn tan(x: f64) callconv(.C) f64 {
8989 if (ix <= 0x3fe921fb) {
9090 if (ix < 0x3e400000) { // |x| < 2**-27
9191 // raise inexact if x!=0 and underflow if subnormal
92 mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
92 if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
9393 return x;
9494 }
9595 return kernel.__tan(x, 0.0, false);
lib/compiler_rt/trunc.zig+3-3
......@@ -47,7 +47,7 @@ pub fn truncf(x: f32) callconv(.C) f32 {
4747 if (u & m == 0) {
4848 return x;
4949 } else {
50 mem.doNotOptimizeAway(x + 0x1p120);
50 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
5151 return @bitCast(u & ~m);
5252 }
5353}
......@@ -68,7 +68,7 @@ pub fn trunc(x: f64) callconv(.C) f64 {
6868 if (u & m == 0) {
6969 return x;
7070 } else {
71 mem.doNotOptimizeAway(x + 0x1p120);
71 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
7272 return @bitCast(u & ~m);
7373 }
7474}
......@@ -94,7 +94,7 @@ pub fn truncq(x: f128) callconv(.C) f128 {
9494 if (u & m == 0) {
9595 return x;
9696 } else {
97 mem.doNotOptimizeAway(x + 0x1p120);
97 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
9898 return @bitCast(u & ~m);
9999 }
100100}
lib/compiler_rt/udivmodei4.zig+1
......@@ -130,6 +130,7 @@ pub fn __umodei4(r_p: [*]u32, u_p: [*]const u32, v_p: [*]const u32, bits: usize)
130130
131131test "__udivei4/__umodei4" {
132132 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
133 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
133134
134135 const RndGen = std.Random.DefaultPrng;
135136 var rnd = RndGen.init(42);
src/arch/wasm/CodeGen.zig+116-6
......@@ -1837,6 +1837,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
18371837 .sub_sat => func.airSatBinOp(inst, .sub),
18381838 .sub_wrap => func.airWrapBinOp(inst, .sub),
18391839 .mul => func.airBinOp(inst, .mul),
1840 .mul_sat => func.airSatMul(inst),
18401841 .mul_wrap => func.airWrapBinOp(inst, .mul),
18411842 .div_float, .div_exact => func.airDiv(inst),
18421843 .div_trunc => func.airDivTrunc(inst),
......@@ -2002,7 +2003,6 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
20022003 .error_set_has_value => func.airErrorSetHasValue(inst),
20032004 .frame_addr => func.airFrameAddress(inst),
20042005
2005 .mul_sat,
20062006 .assembly,
20072007 .is_err_ptr,
20082008 .is_non_err_ptr,
......@@ -2666,8 +2666,8 @@ fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) Inner
26662666 switch (op) {
26672667 .mul => return func.callIntrinsic("__multi3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
26682668 .div => switch (int_info.signedness) {
2669 .signed => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
2670 .unsigned => return func.callIntrinsic("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
2669 .signed => return func.callIntrinsic("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
2670 .unsigned => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
26712671 },
26722672 .rem => switch (int_info.signedness) {
26732673 .signed => return func.callIntrinsic("__modti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
......@@ -4378,7 +4378,7 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
43784378 else
43794379 try func.intcast(operand, operand_ty, ty);
43804380
4381 return func.finishAir(inst, result, &.{});
4381 return func.finishAir(inst, result, &.{ty_op.operand});
43824382}
43834383
43844384/// Upcasts or downcasts an integer based on the given and wanted types,
......@@ -4677,10 +4677,20 @@ fn airTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
46774677 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
46784678
46794679 const operand = try func.resolveInst(ty_op.operand);
4680 const wanted_ty = ty_op.ty.toType();
4680 const wanted_ty: Type = ty_op.ty.toType();
46814681 const op_ty = func.typeOf(ty_op.operand);
4682 const pt = func.pt;
4683 const mod = pt.zcu;
4684
4685 if (wanted_ty.zigTypeTag(mod) == .Vector or op_ty.zigTypeTag(mod) == .Vector) {
4686 return func.fail("TODO: trunc for vectors", .{});
4687 }
4688
4689 const result = if (op_ty.bitSize(pt) == wanted_ty.bitSize(pt))
4690 func.reuseOperand(ty_op.operand, operand)
4691 else
4692 try func.trunc(operand, wanted_ty, op_ty);
46824693
4683 const result = try func.trunc(operand, wanted_ty, op_ty);
46844694 return func.finishAir(inst, result, &.{ty_op.operand});
46854695}
46864696
......@@ -6783,6 +6793,106 @@ fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
67836793 return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs });
67846794}
67856795
6796fn airSatMul(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6797 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
6798
6799 const pt = func.pt;
6800 const mod = pt.zcu;
6801 const ty = func.typeOfIndex(inst);
6802 const int_info = ty.intInfo(mod);
6803 const is_signed = int_info.signedness == .signed;
6804
6805 const lhs = try func.resolveInst(bin_op.lhs);
6806 const rhs = try func.resolveInst(bin_op.rhs);
6807 const wasm_bits = toWasmBits(int_info.bits) orelse {
6808 return func.fail("TODO: mul_sat for {}", .{ty.fmt(pt)});
6809 };
6810
6811 switch (wasm_bits) {
6812 32 => {
6813 const upcast_ty: Type = if (is_signed) Type.i64 else Type.u64;
6814 const lhs_up = try func.intcast(lhs, ty, upcast_ty);
6815 const rhs_up = try func.intcast(rhs, ty, upcast_ty);
6816 var mul_res = try (try func.binOp(lhs_up, rhs_up, upcast_ty, .mul)).toLocal(func, upcast_ty);
6817 defer mul_res.free(func);
6818 if (is_signed) {
6819 const imm_max: WValue = .{ .imm64 = ~@as(u64, 0) >> @intCast(64 - (int_info.bits - 1)) };
6820 try func.emitWValue(mul_res);
6821 try func.emitWValue(imm_max);
6822 _ = try func.cmp(mul_res, imm_max, upcast_ty, .lt);
6823 try func.addTag(.select);
6824
6825 var tmp = try func.allocLocal(upcast_ty);
6826 defer tmp.free(func);
6827 try func.addLabel(.local_set, tmp.local.value);
6828
6829 const imm_min: WValue = .{ .imm64 = ~@as(u64, 0) << @intCast(int_info.bits - 1) };
6830 try func.emitWValue(tmp);
6831 try func.emitWValue(imm_min);
6832 _ = try func.cmp(tmp, imm_min, upcast_ty, .gt);
6833 try func.addTag(.select);
6834 } else {
6835 const imm_max: WValue = .{ .imm64 = ~@as(u64, 0) >> @intCast(64 - int_info.bits) };
6836 try func.emitWValue(mul_res);
6837 try func.emitWValue(imm_max);
6838 _ = try func.cmp(mul_res, imm_max, upcast_ty, .lt);
6839 try func.addTag(.select);
6840 }
6841 try func.addTag(.i32_wrap_i64);
6842 },
6843 64 => {
6844 if (!(int_info.bits == 64 and int_info.signedness == .signed)) {
6845 return func.fail("TODO: mul_sat for {}", .{ty.fmt(pt)});
6846 }
6847 const overflow_ret = try func.allocStack(Type.i32);
6848 _ = try func.callIntrinsic(
6849 "__mulodi4",
6850 &[_]InternPool.Index{ .i64_type, .i64_type, .usize_type },
6851 Type.i64,
6852 &.{ lhs, rhs, overflow_ret },
6853 );
6854 const xor = try func.binOp(lhs, rhs, Type.i64, .xor);
6855 const sign_v = try func.binOp(xor, .{ .imm64 = 63 }, Type.i64, .shr);
6856 _ = try func.binOp(sign_v, .{ .imm64 = ~@as(u63, 0) }, Type.i64, .xor);
6857 _ = try func.load(overflow_ret, Type.i32, 0);
6858 try func.addTag(.i32_eqz);
6859 try func.addTag(.select);
6860 },
6861 128 => {
6862 if (!(int_info.bits == 128 and int_info.signedness == .signed)) {
6863 return func.fail("TODO: mul_sat for {}", .{ty.fmt(pt)});
6864 }
6865 const overflow_ret = try func.allocStack(Type.i32);
6866 const ret = try func.callIntrinsic(
6867 "__muloti4",
6868 &[_]InternPool.Index{ .i128_type, .i128_type, .usize_type },
6869 Type.i128,
6870 &.{ lhs, rhs, overflow_ret },
6871 );
6872 try func.lowerToStack(ret);
6873 const xor = try func.binOp(lhs, rhs, Type.i128, .xor);
6874 const sign_v = try func.binOp(xor, .{ .imm32 = 127 }, Type.i128, .shr);
6875
6876 // xor ~@as(u127, 0)
6877 try func.emitWValue(sign_v);
6878 const lsb = try func.load(sign_v, Type.u64, 0);
6879 _ = try func.binOp(lsb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor);
6880 try func.store(.stack, .stack, Type.u64, sign_v.offset());
6881 try func.emitWValue(sign_v);
6882 const msb = try func.load(sign_v, Type.u64, 8);
6883 _ = try func.binOp(msb, .{ .imm64 = ~@as(u63, 0) }, Type.u64, .xor);
6884 try func.store(.stack, .stack, Type.u64, sign_v.offset() + 8);
6885
6886 try func.lowerToStack(sign_v);
6887 _ = try func.load(overflow_ret, Type.i32, 0);
6888 try func.addTag(.i32_eqz);
6889 try func.addTag(.select);
6890 },
6891 else => unreachable,
6892 }
6893 return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs });
6894}
6895
67866896fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
67876897 assert(op == .add or op == .sub);
67886898 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
test/behavior/saturating_arithmetic.zig+107-12
......@@ -154,6 +154,109 @@ test "saturating subtraction 128bit" {
154154 try comptime S.doTheTest();
155155}
156156
157fn testSatMul(comptime T: type, a: T, b: T, expected: T) !void {
158 const res: T = a *| b;
159 try expect(res == expected);
160}
161
162test "saturating multiplication <= 32 bits" {
163 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
164 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
165 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
166 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
167 if (builtin.zig_backend == .stage2_c and comptime builtin.cpu.arch.isArmOrThumb()) return error.SkipZigTest;
168 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
169
170 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .wasm32) {
171 // https://github.com/ziglang/zig/issues/9660
172 return error.SkipZigTest;
173 }
174
175 try testSatMul(u8, 0, maxInt(u8), 0);
176 try testSatMul(u8, 1 << 7, 1 << 7, maxInt(u8));
177 try testSatMul(u8, maxInt(u8) - 1, 2, maxInt(u8));
178 try testSatMul(u8, 1 << 4, 1 << 4, maxInt(u8));
179 try testSatMul(u8, 1 << 4, 1 << 3, 1 << 7);
180 try testSatMul(u8, 1 << 5, 1 << 3, maxInt(u8));
181 try testSatMul(u8, 10, 20, 200);
182
183 try testSatMul(u16, 0, maxInt(u16), 0);
184 try testSatMul(u16, 1 << 15, 1 << 15, maxInt(u16));
185 try testSatMul(u16, maxInt(u16) - 1, 2, maxInt(u16));
186 try testSatMul(u16, 1 << 8, 1 << 8, maxInt(u16));
187 try testSatMul(u16, 1 << 12, 1 << 3, 1 << 15);
188 try testSatMul(u16, 1 << 13, 1 << 3, maxInt(u16));
189 try testSatMul(u16, 10, 20, 200);
190
191 try testSatMul(u32, 0, maxInt(u32), 0);
192 try testSatMul(u32, 1 << 31, 1 << 31, maxInt(u32));
193 try testSatMul(u32, maxInt(u32) - 1, 2, maxInt(u32));
194 try testSatMul(u32, 1 << 16, 1 << 16, maxInt(u32));
195 try testSatMul(u32, 1 << 28, 1 << 3, 1 << 31);
196 try testSatMul(u32, 1 << 29, 1 << 3, maxInt(u32));
197 try testSatMul(u32, 10, 20, 200);
198
199 try testSatMul(i8, 0, maxInt(i8), 0);
200 try testSatMul(i8, 0, minInt(i8), 0);
201 try testSatMul(i8, 1 << 6, 1 << 6, maxInt(i8));
202 try testSatMul(i8, minInt(i8), minInt(i8), maxInt(i8));
203 try testSatMul(i8, maxInt(i8) - 1, 2, maxInt(i8));
204 try testSatMul(i8, minInt(i8) + 1, 2, minInt(i8));
205 try testSatMul(i8, 1 << 4, 1 << 4, maxInt(i8));
206 try testSatMul(i8, minInt(i4), 1 << 4, minInt(i8));
207 try testSatMul(i8, 10, 12, 120);
208 try testSatMul(i8, 10, -12, -120);
209
210 try testSatMul(i16, 0, maxInt(i16), 0);
211 try testSatMul(i16, 0, minInt(i16), 0);
212 try testSatMul(i16, 1 << 14, 1 << 14, maxInt(i16));
213 try testSatMul(i16, minInt(i16), minInt(i16), maxInt(i16));
214 try testSatMul(i16, maxInt(i16) - 1, 2, maxInt(i16));
215 try testSatMul(i16, minInt(i16) + 1, 2, minInt(i16));
216 try testSatMul(i16, 1 << 8, 1 << 8, maxInt(i16));
217 try testSatMul(i16, minInt(i8), 1 << 8, minInt(i16));
218 try testSatMul(i16, 10, 12, 120);
219 try testSatMul(i16, 10, -12, -120);
220
221 try testSatMul(i32, 0, maxInt(i32), 0);
222 try testSatMul(i32, 0, minInt(i32), 0);
223 try testSatMul(i32, 1 << 30, 1 << 30, maxInt(i32));
224 try testSatMul(i32, minInt(i32), minInt(i32), maxInt(i32));
225 try testSatMul(i32, maxInt(i32) - 1, 2, maxInt(i32));
226 try testSatMul(i32, minInt(i32) + 1, 2, minInt(i32));
227 try testSatMul(i32, 1 << 16, 1 << 16, maxInt(i32));
228 try testSatMul(i32, minInt(i16), 1 << 16, minInt(i32));
229 try testSatMul(i32, 10, 12, 120);
230 try testSatMul(i32, 10, -12, -120);
231}
232
233// TODO: remove this test, integrate into general test
234test "saturating mul i64, i128, wasm only" {
235 if (builtin.zig_backend != .stage2_wasm) return error.SkipZigTest;
236
237 try testSatMul(i64, 0, maxInt(i64), 0);
238 try testSatMul(i64, 0, minInt(i64), 0);
239 try testSatMul(i64, 1 << 62, 1 << 62, maxInt(i64));
240 try testSatMul(i64, minInt(i64), minInt(i64), maxInt(i64));
241 try testSatMul(i64, maxInt(i64) - 1, 2, maxInt(i64));
242 try testSatMul(i64, minInt(i64) + 1, 2, minInt(i64));
243 try testSatMul(i64, 1 << 32, 1 << 32, maxInt(i64));
244 try testSatMul(i64, minInt(i32), 1 << 32, minInt(i64));
245 try testSatMul(i64, 10, 12, 120);
246 try testSatMul(i64, 10, -12, -120);
247
248 try testSatMul(i128, 0, maxInt(i128), 0);
249 try testSatMul(i128, 0, minInt(i128), 0);
250 try testSatMul(i128, 1 << 126, 1 << 126, maxInt(i128));
251 try testSatMul(i128, minInt(i128), minInt(i128), maxInt(i128));
252 try testSatMul(i128, maxInt(i128) - 1, 2, maxInt(i128));
253 try testSatMul(i128, minInt(i128) + 1, 2, minInt(i128));
254 try testSatMul(i128, 1 << 64, 1 << 64, maxInt(i128));
255 try testSatMul(i128, minInt(i64), 1 << 64, minInt(i128));
256 try testSatMul(i128, 10, 12, 120);
257 try testSatMul(i128, 10, -12, -120);
258}
259
157260test "saturating multiplication" {
158261 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
159262 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
......@@ -183,23 +286,15 @@ test "saturating multiplication" {
183286 try testSatMul(u8, 2, 255, 255);
184287 try testSatMul(u128, maxInt(u128), maxInt(u128), maxInt(u128));
185288 }
186
187 fn testSatMul(comptime T: type, lhs: T, rhs: T, expected: T) !void {
188 try expect((lhs *| rhs) == expected);
189
190 var x = lhs;
191 x *|= rhs;
192 try expect(x == expected);
193 }
194289 };
195290
196291 try S.doTheTest();
197292 try comptime S.doTheTest();
198293
199 try comptime S.testSatMul(comptime_int, 0, 0, 0);
200 try comptime S.testSatMul(comptime_int, 3, 2, 6);
201 try comptime S.testSatMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);
202 try comptime S.testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);
294 try comptime testSatMul(comptime_int, 0, 0, 0);
295 try comptime testSatMul(comptime_int, 3, 2, 6);
296 try comptime testSatMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);
297 try comptime testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);
203298}
204299
205300test "saturating shift-left" {