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

stage2-wasm: pass compiler_rt test suite
This commit is contained in:
Andrew Kelley
2024-07-23 20:42:25 -07:00
committed by GitHub
14 changed files with 257 additions and 49 deletions

View File

@@ -48,14 +48,14 @@ pub fn ceilf(x: f32) callconv(.C) f32 {
if (u & m == 0) {
return x;
}
mem.doNotOptimizeAway(x + 0x1.0p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
if (u >> 31 == 0) {
u += m;
}
u &= ~m;
return @bitCast(u);
} else {
mem.doNotOptimizeAway(x + 0x1.0p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
if (u >> 31 != 0) {
return -0.0;
} else {
@@ -82,7 +82,7 @@ pub fn ceil(x: f64) callconv(.C) f64 {
}
if (e <= 0x3FF - 1) {
mem.doNotOptimizeAway(y);
if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
if (u >> 63 != 0) {
return -0.0;
} else {
@@ -116,7 +116,7 @@ pub fn ceilq(x: f128) callconv(.C) f128 {
}
if (e <= 0x3FFF - 1) {
mem.doNotOptimizeAway(y);
if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
if (u >> 127 != 0) {
return -0.0;
} else {

View File

@@ -24,6 +24,8 @@ pub const want_aeabi = switch (builtin.abi) {
};
pub const want_ppc_abi = builtin.cpu.arch.isPPC() or builtin.cpu.arch.isPPC64();
pub const want_float_exceptions = !builtin.cpu.arch.isWasm();
// Libcalls that involve u128 on Windows x86-64 are expected by LLVM to use the
// calling convention of @Vector(2, u64), rather than what's standard.
pub const want_windows_v2u64_abi = builtin.os.tag == .windows and builtin.cpu.arch == .x86_64 and @import("builtin").object_format != .c;

View File

@@ -41,7 +41,7 @@ pub fn cosf(x: f32) callconv(.C) f32 {
if (ix <= 0x3f490fda) { // |x| ~<= pi/4
if (ix < 0x39800000) { // |x| < 2**-12
// raise inexact if x != 0
mem.doNotOptimizeAway(x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
return 1.0;
}
return trig.__cosdf(x);
@@ -92,7 +92,7 @@ pub fn cos(x: f64) callconv(.C) f64 {
if (ix <= 0x3fe921fb) {
if (ix < 0x3e46a09e) { // |x| < 2**-27 * sqrt(2)
// raise inexact if x!=0
mem.doNotOptimizeAway(x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
return 1.0;
}
return trig.__cos(x, 0);

View File

@@ -59,7 +59,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {
return x * 0x1.0p127;
}
if (sign != 0) {
mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow
if (common.want_float_exceptions) mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow
// x <= -103.972084
if (hx >= 0x42CFF1B5) {
return 0;
@@ -91,7 +91,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {
hi = x;
lo = 0;
} else {
mem.doNotOptimizeAway(0x1.0p127 + x); // inexact
if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p127 + x); // inexact
return 1 + x;
}
@@ -142,7 +142,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {
}
if (x < -708.39641853226410622) {
// underflow if x != -inf
// mem.doNotOptimizeAway(@as(f32, -0x1.0p-149 / x));
// if (common.want_float_exceptions) mem.doNotOptimizeAway(@as(f32, -0x1.0p-149 / x));
if (x < -745.13321910194110842) {
return 0;
}
@@ -175,7 +175,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {
lo = 0;
} else {
// inexact if x != 0
// mem.doNotOptimizeAway(0x1.0p1023 + x);
// if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p1023 + x);
return 1 + x;
}

View File

@@ -55,7 +55,7 @@ pub fn exp2f(x: f32) callconv(.C) f32 {
// x < -126
if (u >= 0x80000000) {
if (u >= 0xC3160000 or u & 0x000FFFF != 0) {
mem.doNotOptimizeAway(-0x1.0p-149 / x);
if (common.want_float_exceptions) mem.doNotOptimizeAway(-0x1.0p-149 / x);
}
// x <= -150
if (u >= 0x3160000) {
@@ -120,7 +120,7 @@ pub fn exp2(x: f64) callconv(.C) f64 {
if (ux >> 63 != 0) {
// underflow
if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {
mem.doNotOptimizeAway(@as(f32, @floatCast(-0x1.0p-149 / x)));
if (common.want_float_exceptions) mem.doNotOptimizeAway(@as(f32, @floatCast(-0x1.0p-149 / x)));
}
if (x <= -1075) {
return 0;

View File

@@ -45,13 +45,13 @@ pub fn __floorh(x: f16) callconv(.C) f16 {
if (u & m == 0) {
return x;
}
mem.doNotOptimizeAway(x + 0x1.0p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
if (u >> 15 != 0) {
u += m;
}
return @bitCast(u & ~m);
} else {
mem.doNotOptimizeAway(x + 0x1.0p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
if (u >> 15 == 0) {
return 0.0;
} else {
@@ -79,13 +79,13 @@ pub fn floorf(x: f32) callconv(.C) f32 {
if (u & m == 0) {
return x;
}
mem.doNotOptimizeAway(x + 0x1.0p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
if (u >> 31 != 0) {
u += m;
}
return @bitCast(u & ~m);
} else {
mem.doNotOptimizeAway(x + 0x1.0p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
if (u >> 31 == 0) {
return 0.0;
} else {
@@ -112,7 +112,7 @@ pub fn floor(x: f64) callconv(.C) f64 {
}
if (e <= 0x3FF - 1) {
mem.doNotOptimizeAway(y);
if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
if (u >> 63 != 0) {
return -1.0;
} else {
@@ -146,7 +146,7 @@ pub fn floorq(x: f128) callconv(.C) f128 {
}
if (e <= 0x3FFF - 1) {
mem.doNotOptimizeAway(y);
if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
if (u >> 127 != 0) {
return -1.0;
} else {

View File

@@ -46,7 +46,7 @@ pub fn roundf(x_: f32) callconv(.C) f32 {
x = -x;
}
if (e < 0x7F - 1) {
mem.doNotOptimizeAway(x + f32_toint);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f32_toint);
return 0 * @as(f32, @bitCast(u));
}
@@ -81,7 +81,7 @@ pub fn round(x_: f64) callconv(.C) f64 {
x = -x;
}
if (e < 0x3ff - 1) {
mem.doNotOptimizeAway(x + f64_toint);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f64_toint);
return 0 * @as(f64, @bitCast(u));
}
@@ -121,7 +121,7 @@ pub fn roundq(x_: f128) callconv(.C) f128 {
x = -x;
}
if (e < 0x3FFF - 1) {
mem.doNotOptimizeAway(x + f128_toint);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f128_toint);
return 0 * @as(f128, @bitCast(u));
}

View File

@@ -49,7 +49,7 @@ pub fn sinf(x: f32) callconv(.C) f32 {
if (ix <= 0x3f490fda) { // |x| ~<= pi/4
if (ix < 0x39800000) { // |x| < 2**-12
// raise inexact if x!=0 and underflow if subnormal
mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
return x;
}
return trig.__sindf(x);
@@ -98,7 +98,7 @@ pub fn sin(x: f64) callconv(.C) f64 {
if (ix <= 0x3fe921fb) {
if (ix < 0x3e500000) { // |x| < 2**-26
// raise inexact if x != 0 and underflow if subnormal
mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
return x;
}
return trig.__sin(x, 0.0, 0);

View File

@@ -46,7 +46,7 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.C) void {
// |x| < 2**-12
if (ix < 0x39800000) {
// raise inexact if x!=0 and underflow if subnormal
mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
r_sin.* = x;
r_cos.* = 1.0;
return;
@@ -134,7 +134,7 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void {
// if |x| < 2**-27 * sqrt(2)
if (ix < 0x3e46a09e) {
// raise inexact if x != 0 and underflow if subnormal
mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
r_sin.* = x;
r_cos.* = 1.0;
return;
@@ -232,7 +232,7 @@ inline fn sincos_generic(comptime F: type, x: F, r_sin: *F, r_cos: *F) void {
if (se < 0x3fff - math.floatFractionalBits(F) - 1) {
// raise underflow if subnormal
if (se == 0) {
mem.doNotOptimizeAway(x * 0x1p-120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x * 0x1p-120);
}
r_sin.* = x;
// raise inexact if x!=0

View File

@@ -51,7 +51,7 @@ pub fn tanf(x: f32) callconv(.C) f32 {
if (ix <= 0x3f490fda) { // |x| ~<= pi/4
if (ix < 0x39800000) { // |x| < 2**-12
// raise inexact if x!=0 and underflow if subnormal
mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00800000) x / 0x1p120 else x + 0x1p120);
return x;
}
return kernel.__tandf(x, false);
@@ -89,7 +89,7 @@ pub fn tan(x: f64) callconv(.C) f64 {
if (ix <= 0x3fe921fb) {
if (ix < 0x3e400000) { // |x| < 2**-27
// raise inexact if x!=0 and underflow if subnormal
mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(if (ix < 0x00100000) x / 0x1p120 else x + 0x1p120);
return x;
}
return kernel.__tan(x, 0.0, false);

View File

@@ -47,7 +47,7 @@ pub fn truncf(x: f32) callconv(.C) f32 {
if (u & m == 0) {
return x;
} else {
mem.doNotOptimizeAway(x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
return @bitCast(u & ~m);
}
}
@@ -68,7 +68,7 @@ pub fn trunc(x: f64) callconv(.C) f64 {
if (u & m == 0) {
return x;
} else {
mem.doNotOptimizeAway(x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
return @bitCast(u & ~m);
}
}
@@ -94,7 +94,7 @@ pub fn truncq(x: f128) callconv(.C) f128 {
if (u & m == 0) {
return x;
} else {
mem.doNotOptimizeAway(x + 0x1p120);
if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
return @bitCast(u & ~m);
}
}

View File

@@ -130,6 +130,7 @@ pub fn __umodei4(r_p: [*]u32, u_p: [*]const u32, v_p: [*]const u32, bits: usize)
test "__udivei4/__umodei4" {
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
const RndGen = std.Random.DefaultPrng;
var rnd = RndGen.init(42);

View File

@@ -1837,6 +1837,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
.sub_sat => func.airSatBinOp(inst, .sub),
.sub_wrap => func.airWrapBinOp(inst, .sub),
.mul => func.airBinOp(inst, .mul),
.mul_sat => func.airSatMul(inst),
.mul_wrap => func.airWrapBinOp(inst, .mul),
.div_float, .div_exact => func.airDiv(inst),
.div_trunc => func.airDivTrunc(inst),
@@ -2002,7 +2003,6 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
.error_set_has_value => func.airErrorSetHasValue(inst),
.frame_addr => func.airFrameAddress(inst),
.mul_sat,
.assembly,
.is_err_ptr,
.is_non_err_ptr,
@@ -2666,8 +2666,8 @@ fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) Inner
switch (op) {
.mul => return func.callIntrinsic("__multi3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
.div => switch (int_info.signedness) {
.signed => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
.unsigned => return func.callIntrinsic("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
.signed => return func.callIntrinsic("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
.unsigned => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
},
.rem => switch (int_info.signedness) {
.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 {
else
try func.intcast(operand, operand_ty, ty);
return func.finishAir(inst, result, &.{});
return func.finishAir(inst, result, &.{ty_op.operand});
}
/// 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 {
const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
const operand = try func.resolveInst(ty_op.operand);
const wanted_ty = ty_op.ty.toType();
const wanted_ty: Type = ty_op.ty.toType();
const op_ty = func.typeOf(ty_op.operand);
const pt = func.pt;
const mod = pt.zcu;
if (wanted_ty.zigTypeTag(mod) == .Vector or op_ty.zigTypeTag(mod) == .Vector) {
return func.fail("TODO: trunc for vectors", .{});
}
const result = if (op_ty.bitSize(pt) == wanted_ty.bitSize(pt))
func.reuseOperand(ty_op.operand, operand)
else
try func.trunc(operand, wanted_ty, op_ty);
const result = try func.trunc(operand, wanted_ty, op_ty);
return func.finishAir(inst, result, &.{ty_op.operand});
}
@@ -6783,6 +6793,106 @@ fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs });
}
fn airSatMul(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
const pt = func.pt;
const mod = pt.zcu;
const ty = func.typeOfIndex(inst);
const int_info = ty.intInfo(mod);
const is_signed = int_info.signedness == .signed;
const lhs = try func.resolveInst(bin_op.lhs);
const rhs = try func.resolveInst(bin_op.rhs);
const wasm_bits = toWasmBits(int_info.bits) orelse {
return func.fail("TODO: mul_sat for {}", .{ty.fmt(pt)});
};
switch (wasm_bits) {
32 => {
const upcast_ty: Type = if (is_signed) Type.i64 else Type.u64;
const lhs_up = try func.intcast(lhs, ty, upcast_ty);
const rhs_up = try func.intcast(rhs, ty, upcast_ty);
var mul_res = try (try func.binOp(lhs_up, rhs_up, upcast_ty, .mul)).toLocal(func, upcast_ty);
defer mul_res.free(func);
if (is_signed) {
const imm_max: WValue = .{ .imm64 = ~@as(u64, 0) >> @intCast(64 - (int_info.bits - 1)) };
try func.emitWValue(mul_res);
try func.emitWValue(imm_max);
_ = try func.cmp(mul_res, imm_max, upcast_ty, .lt);
try func.addTag(.select);
var tmp = try func.allocLocal(upcast_ty);
defer tmp.free(func);
try func.addLabel(.local_set, tmp.local.value);
const imm_min: WValue = .{ .imm64 = ~@as(u64, 0) << @intCast(int_info.bits - 1) };
try func.emitWValue(tmp);
try func.emitWValue(imm_min);
_ = try func.cmp(tmp, imm_min, upcast_ty, .gt);
try func.addTag(.select);
} else {
const imm_max: WValue = .{ .imm64 = ~@as(u64, 0) >> @intCast(64 - int_info.bits) };
try func.emitWValue(mul_res);
try func.emitWValue(imm_max);
_ = try func.cmp(mul_res, imm_max, upcast_ty, .lt);
try func.addTag(.select);
}
try func.addTag(.i32_wrap_i64);
},
64 => {
if (!(int_info.bits == 64 and int_info.signedness == .signed)) {
return func.fail("TODO: mul_sat for {}", .{ty.fmt(pt)});
}
const overflow_ret = try func.allocStack(Type.i32);
_ = try func.callIntrinsic(
"__mulodi4",
&[_]InternPool.Index{ .i64_type, .i64_type, .usize_type },
Type.i64,
&.{ lhs, rhs, overflow_ret },
);
const xor = try func.binOp(lhs, rhs, Type.i64, .xor);
const sign_v = try func.binOp(xor, .{ .imm64 = 63 }, Type.i64, .shr);
_ = try func.binOp(sign_v, .{ .imm64 = ~@as(u63, 0) }, Type.i64, .xor);
_ = try func.load(overflow_ret, Type.i32, 0);
try func.addTag(.i32_eqz);
try func.addTag(.select);
},
128 => {
if (!(int_info.bits == 128 and int_info.signedness == .signed)) {
return func.fail("TODO: mul_sat for {}", .{ty.fmt(pt)});
}
const overflow_ret = try func.allocStack(Type.i32);
const ret = try func.callIntrinsic(
"__muloti4",
&[_]InternPool.Index{ .i128_type, .i128_type, .usize_type },
Type.i128,
&.{ lhs, rhs, overflow_ret },
);
try func.lowerToStack(ret);
const xor = try func.binOp(lhs, rhs, Type.i128, .xor);
const sign_v = try func.binOp(xor, .{ .imm32 = 127 }, Type.i128, .shr);
// xor ~@as(u127, 0)
try func.emitWValue(sign_v);
const lsb = try func.load(sign_v, Type.u64, 0);
_ = try func.binOp(lsb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor);
try func.store(.stack, .stack, Type.u64, sign_v.offset());
try func.emitWValue(sign_v);
const msb = try func.load(sign_v, Type.u64, 8);
_ = try func.binOp(msb, .{ .imm64 = ~@as(u63, 0) }, Type.u64, .xor);
try func.store(.stack, .stack, Type.u64, sign_v.offset() + 8);
try func.lowerToStack(sign_v);
_ = try func.load(overflow_ret, Type.i32, 0);
try func.addTag(.i32_eqz);
try func.addTag(.select);
},
else => unreachable,
}
return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs });
}
fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
assert(op == .add or op == .sub);
const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;

View File

@@ -154,6 +154,109 @@ test "saturating subtraction 128bit" {
try comptime S.doTheTest();
}
fn testSatMul(comptime T: type, a: T, b: T, expected: T) !void {
const res: T = a *| b;
try expect(res == expected);
}
test "saturating multiplication <= 32 bits" {
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_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_c and comptime builtin.cpu.arch.isArmOrThumb()) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .wasm32) {
// https://github.com/ziglang/zig/issues/9660
return error.SkipZigTest;
}
try testSatMul(u8, 0, maxInt(u8), 0);
try testSatMul(u8, 1 << 7, 1 << 7, maxInt(u8));
try testSatMul(u8, maxInt(u8) - 1, 2, maxInt(u8));
try testSatMul(u8, 1 << 4, 1 << 4, maxInt(u8));
try testSatMul(u8, 1 << 4, 1 << 3, 1 << 7);
try testSatMul(u8, 1 << 5, 1 << 3, maxInt(u8));
try testSatMul(u8, 10, 20, 200);
try testSatMul(u16, 0, maxInt(u16), 0);
try testSatMul(u16, 1 << 15, 1 << 15, maxInt(u16));
try testSatMul(u16, maxInt(u16) - 1, 2, maxInt(u16));
try testSatMul(u16, 1 << 8, 1 << 8, maxInt(u16));
try testSatMul(u16, 1 << 12, 1 << 3, 1 << 15);
try testSatMul(u16, 1 << 13, 1 << 3, maxInt(u16));
try testSatMul(u16, 10, 20, 200);
try testSatMul(u32, 0, maxInt(u32), 0);
try testSatMul(u32, 1 << 31, 1 << 31, maxInt(u32));
try testSatMul(u32, maxInt(u32) - 1, 2, maxInt(u32));
try testSatMul(u32, 1 << 16, 1 << 16, maxInt(u32));
try testSatMul(u32, 1 << 28, 1 << 3, 1 << 31);
try testSatMul(u32, 1 << 29, 1 << 3, maxInt(u32));
try testSatMul(u32, 10, 20, 200);
try testSatMul(i8, 0, maxInt(i8), 0);
try testSatMul(i8, 0, minInt(i8), 0);
try testSatMul(i8, 1 << 6, 1 << 6, maxInt(i8));
try testSatMul(i8, minInt(i8), minInt(i8), maxInt(i8));
try testSatMul(i8, maxInt(i8) - 1, 2, maxInt(i8));
try testSatMul(i8, minInt(i8) + 1, 2, minInt(i8));
try testSatMul(i8, 1 << 4, 1 << 4, maxInt(i8));
try testSatMul(i8, minInt(i4), 1 << 4, minInt(i8));
try testSatMul(i8, 10, 12, 120);
try testSatMul(i8, 10, -12, -120);
try testSatMul(i16, 0, maxInt(i16), 0);
try testSatMul(i16, 0, minInt(i16), 0);
try testSatMul(i16, 1 << 14, 1 << 14, maxInt(i16));
try testSatMul(i16, minInt(i16), minInt(i16), maxInt(i16));
try testSatMul(i16, maxInt(i16) - 1, 2, maxInt(i16));
try testSatMul(i16, minInt(i16) + 1, 2, minInt(i16));
try testSatMul(i16, 1 << 8, 1 << 8, maxInt(i16));
try testSatMul(i16, minInt(i8), 1 << 8, minInt(i16));
try testSatMul(i16, 10, 12, 120);
try testSatMul(i16, 10, -12, -120);
try testSatMul(i32, 0, maxInt(i32), 0);
try testSatMul(i32, 0, minInt(i32), 0);
try testSatMul(i32, 1 << 30, 1 << 30, maxInt(i32));
try testSatMul(i32, minInt(i32), minInt(i32), maxInt(i32));
try testSatMul(i32, maxInt(i32) - 1, 2, maxInt(i32));
try testSatMul(i32, minInt(i32) + 1, 2, minInt(i32));
try testSatMul(i32, 1 << 16, 1 << 16, maxInt(i32));
try testSatMul(i32, minInt(i16), 1 << 16, minInt(i32));
try testSatMul(i32, 10, 12, 120);
try testSatMul(i32, 10, -12, -120);
}
// TODO: remove this test, integrate into general test
test "saturating mul i64, i128, wasm only" {
if (builtin.zig_backend != .stage2_wasm) return error.SkipZigTest;
try testSatMul(i64, 0, maxInt(i64), 0);
try testSatMul(i64, 0, minInt(i64), 0);
try testSatMul(i64, 1 << 62, 1 << 62, maxInt(i64));
try testSatMul(i64, minInt(i64), minInt(i64), maxInt(i64));
try testSatMul(i64, maxInt(i64) - 1, 2, maxInt(i64));
try testSatMul(i64, minInt(i64) + 1, 2, minInt(i64));
try testSatMul(i64, 1 << 32, 1 << 32, maxInt(i64));
try testSatMul(i64, minInt(i32), 1 << 32, minInt(i64));
try testSatMul(i64, 10, 12, 120);
try testSatMul(i64, 10, -12, -120);
try testSatMul(i128, 0, maxInt(i128), 0);
try testSatMul(i128, 0, minInt(i128), 0);
try testSatMul(i128, 1 << 126, 1 << 126, maxInt(i128));
try testSatMul(i128, minInt(i128), minInt(i128), maxInt(i128));
try testSatMul(i128, maxInt(i128) - 1, 2, maxInt(i128));
try testSatMul(i128, minInt(i128) + 1, 2, minInt(i128));
try testSatMul(i128, 1 << 64, 1 << 64, maxInt(i128));
try testSatMul(i128, minInt(i64), 1 << 64, minInt(i128));
try testSatMul(i128, 10, 12, 120);
try testSatMul(i128, 10, -12, -120);
}
test "saturating multiplication" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
@@ -183,23 +286,15 @@ test "saturating multiplication" {
try testSatMul(u8, 2, 255, 255);
try testSatMul(u128, maxInt(u128), maxInt(u128), maxInt(u128));
}
fn testSatMul(comptime T: type, lhs: T, rhs: T, expected: T) !void {
try expect((lhs *| rhs) == expected);
var x = lhs;
x *|= rhs;
try expect(x == expected);
}
};
try S.doTheTest();
try comptime S.doTheTest();
try comptime S.testSatMul(comptime_int, 0, 0, 0);
try comptime S.testSatMul(comptime_int, 3, 2, 6);
try comptime S.testSatMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);
try comptime S.testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);
try comptime testSatMul(comptime_int, 0, 0, 0);
try comptime testSatMul(comptime_int, 3, 2, 6);
try comptime testSatMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);
try comptime testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);
}
test "saturating shift-left" {