| author | |
| committer | |
| log | fa95c89a717f283215e9fd3ea0b8ebcd1599a69b |
| tree | 8f2a312f2dddb0ddd91b1abd4419f51deb4da0bd |
| parent | 399f4fe7d69723eb94db7e13d2e8ffdc0ce4345b |
| parent | ba8522e6c79b5c93cfbd8bdfbecabf8255848624 |
| signature |
stage2-wasm: pass compiler_rt test suite14 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 { |
| 48 | 48 | if (u & m == 0) { |
| 49 | 49 | return x; |
| 50 | 50 | } |
| 51 | mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 51 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 52 | 52 | if (u >> 31 == 0) { |
| 53 | 53 | u += m; |
| 54 | 54 | } |
| 55 | 55 | u &= ~m; |
| 56 | 56 | return @bitCast(u); |
| 57 | 57 | } else { |
| 58 | mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 58 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 59 | 59 | if (u >> 31 != 0) { |
| 60 | 60 | return -0.0; |
| 61 | 61 | } else { |
| ... | ... | @@ -82,7 +82,7 @@ pub fn ceil(x: f64) callconv(.C) f64 { |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | if (e <= 0x3FF - 1) { |
| 85 | mem.doNotOptimizeAway(y); | |
| 85 | if (common.want_float_exceptions) mem.doNotOptimizeAway(y); | |
| 86 | 86 | if (u >> 63 != 0) { |
| 87 | 87 | return -0.0; |
| 88 | 88 | } else { |
| ... | ... | @@ -116,7 +116,7 @@ pub fn ceilq(x: f128) callconv(.C) f128 { |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | if (e <= 0x3FFF - 1) { |
| 119 | mem.doNotOptimizeAway(y); | |
| 119 | if (common.want_float_exceptions) mem.doNotOptimizeAway(y); | |
| 120 | 120 | if (u >> 127 != 0) { |
| 121 | 121 | return -0.0; |
| 122 | 122 | } else { |
lib/compiler_rt/common.zig+2| ... | ... | @@ -24,6 +24,8 @@ pub const want_aeabi = switch (builtin.abi) { |
| 24 | 24 | }; |
| 25 | 25 | pub const want_ppc_abi = builtin.cpu.arch.isPPC() or builtin.cpu.arch.isPPC64(); |
| 26 | 26 | |
| 27 | pub const want_float_exceptions = !builtin.cpu.arch.isWasm(); | |
| 28 | ||
| 27 | 29 | // Libcalls that involve u128 on Windows x86-64 are expected by LLVM to use the |
| 28 | 30 | // calling convention of @Vector(2, u64), rather than what's standard. |
| 29 | 31 | pub 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 { |
| 41 | 41 | if (ix <= 0x3f490fda) { // |x| ~<= pi/4 |
| 42 | 42 | if (ix < 0x39800000) { // |x| < 2**-12 |
| 43 | 43 | // raise inexact if x != 0 |
| 44 | mem.doNotOptimizeAway(x + 0x1p120); | |
| 44 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120); | |
| 45 | 45 | return 1.0; |
| 46 | 46 | } |
| 47 | 47 | return trig.__cosdf(x); |
| ... | ... | @@ -92,7 +92,7 @@ pub fn cos(x: f64) callconv(.C) f64 { |
| 92 | 92 | if (ix <= 0x3fe921fb) { |
| 93 | 93 | if (ix < 0x3e46a09e) { // |x| < 2**-27 * sqrt(2) |
| 94 | 94 | // raise inexact if x!=0 |
| 95 | mem.doNotOptimizeAway(x + 0x1p120); | |
| 95 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120); | |
| 96 | 96 | return 1.0; |
| 97 | 97 | } |
| 98 | 98 | return trig.__cos(x, 0); |
lib/compiler_rt/exp.zig+4-4| ... | ... | @@ -59,7 +59,7 @@ pub fn expf(x_: f32) callconv(.C) f32 { |
| 59 | 59 | return x * 0x1.0p127; |
| 60 | 60 | } |
| 61 | 61 | if (sign != 0) { |
| 62 | mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow | |
| 62 | if (common.want_float_exceptions) mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow | |
| 63 | 63 | // x <= -103.972084 |
| 64 | 64 | if (hx >= 0x42CFF1B5) { |
| 65 | 65 | return 0; |
| ... | ... | @@ -91,7 +91,7 @@ pub fn expf(x_: f32) callconv(.C) f32 { |
| 91 | 91 | hi = x; |
| 92 | 92 | lo = 0; |
| 93 | 93 | } else { |
| 94 | mem.doNotOptimizeAway(0x1.0p127 + x); // inexact | |
| 94 | if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p127 + x); // inexact | |
| 95 | 95 | return 1 + x; |
| 96 | 96 | } |
| 97 | 97 | |
| ... | ... | @@ -142,7 +142,7 @@ pub fn exp(x_: f64) callconv(.C) f64 { |
| 142 | 142 | } |
| 143 | 143 | if (x < -708.39641853226410622) { |
| 144 | 144 | // 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)); | |
| 146 | 146 | if (x < -745.13321910194110842) { |
| 147 | 147 | return 0; |
| 148 | 148 | } |
| ... | ... | @@ -175,7 +175,7 @@ pub fn exp(x_: f64) callconv(.C) f64 { |
| 175 | 175 | lo = 0; |
| 176 | 176 | } else { |
| 177 | 177 | // inexact if x != 0 |
| 178 | // mem.doNotOptimizeAway(0x1.0p1023 + x); | |
| 178 | // if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p1023 + x); | |
| 179 | 179 | return 1 + x; |
| 180 | 180 | } |
| 181 | 181 |
lib/compiler_rt/exp2.zig+2-2| ... | ... | @@ -55,7 +55,7 @@ pub fn exp2f(x: f32) callconv(.C) f32 { |
| 55 | 55 | // x < -126 |
| 56 | 56 | if (u >= 0x80000000) { |
| 57 | 57 | 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); | |
| 59 | 59 | } |
| 60 | 60 | // x <= -150 |
| 61 | 61 | if (u >= 0x3160000) { |
| ... | ... | @@ -120,7 +120,7 @@ pub fn exp2(x: f64) callconv(.C) f64 { |
| 120 | 120 | if (ux >> 63 != 0) { |
| 121 | 121 | // underflow |
| 122 | 122 | 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))); | |
| 124 | 124 | } |
| 125 | 125 | if (x <= -1075) { |
| 126 | 126 | return 0; |
lib/compiler_rt/floor.zig+6-6| ... | ... | @@ -45,13 +45,13 @@ pub fn __floorh(x: f16) callconv(.C) f16 { |
| 45 | 45 | if (u & m == 0) { |
| 46 | 46 | return x; |
| 47 | 47 | } |
| 48 | mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 48 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 49 | 49 | if (u >> 15 != 0) { |
| 50 | 50 | u += m; |
| 51 | 51 | } |
| 52 | 52 | return @bitCast(u & ~m); |
| 53 | 53 | } else { |
| 54 | mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 54 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 55 | 55 | if (u >> 15 == 0) { |
| 56 | 56 | return 0.0; |
| 57 | 57 | } else { |
| ... | ... | @@ -79,13 +79,13 @@ pub fn floorf(x: f32) callconv(.C) f32 { |
| 79 | 79 | if (u & m == 0) { |
| 80 | 80 | return x; |
| 81 | 81 | } |
| 82 | mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 82 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 83 | 83 | if (u >> 31 != 0) { |
| 84 | 84 | u += m; |
| 85 | 85 | } |
| 86 | 86 | return @bitCast(u & ~m); |
| 87 | 87 | } else { |
| 88 | mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 88 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120); | |
| 89 | 89 | if (u >> 31 == 0) { |
| 90 | 90 | return 0.0; |
| 91 | 91 | } else { |
| ... | ... | @@ -112,7 +112,7 @@ pub fn floor(x: f64) callconv(.C) f64 { |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | if (e <= 0x3FF - 1) { |
| 115 | mem.doNotOptimizeAway(y); | |
| 115 | if (common.want_float_exceptions) mem.doNotOptimizeAway(y); | |
| 116 | 116 | if (u >> 63 != 0) { |
| 117 | 117 | return -1.0; |
| 118 | 118 | } else { |
| ... | ... | @@ -146,7 +146,7 @@ pub fn floorq(x: f128) callconv(.C) f128 { |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | if (e <= 0x3FFF - 1) { |
| 149 | mem.doNotOptimizeAway(y); | |
| 149 | if (common.want_float_exceptions) mem.doNotOptimizeAway(y); | |
| 150 | 150 | if (u >> 127 != 0) { |
| 151 | 151 | return -1.0; |
| 152 | 152 | } else { |
lib/compiler_rt/round.zig+3-3| ... | ... | @@ -46,7 +46,7 @@ pub fn roundf(x_: f32) callconv(.C) f32 { |
| 46 | 46 | x = -x; |
| 47 | 47 | } |
| 48 | 48 | if (e < 0x7F - 1) { |
| 49 | mem.doNotOptimizeAway(x + f32_toint); | |
| 49 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f32_toint); | |
| 50 | 50 | return 0 * @as(f32, @bitCast(u)); |
| 51 | 51 | } |
| 52 | 52 | |
| ... | ... | @@ -81,7 +81,7 @@ pub fn round(x_: f64) callconv(.C) f64 { |
| 81 | 81 | x = -x; |
| 82 | 82 | } |
| 83 | 83 | if (e < 0x3ff - 1) { |
| 84 | mem.doNotOptimizeAway(x + f64_toint); | |
| 84 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f64_toint); | |
| 85 | 85 | return 0 * @as(f64, @bitCast(u)); |
| 86 | 86 | } |
| 87 | 87 | |
| ... | ... | @@ -121,7 +121,7 @@ pub fn roundq(x_: f128) callconv(.C) f128 { |
| 121 | 121 | x = -x; |
| 122 | 122 | } |
| 123 | 123 | if (e < 0x3FFF - 1) { |
| 124 | mem.doNotOptimizeAway(x + f128_toint); | |
| 124 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f128_toint); | |
| 125 | 125 | return 0 * @as(f128, @bitCast(u)); |
| 126 | 126 | } |
| 127 | 127 |
lib/compiler_rt/sin.zig+2-2| ... | ... | @@ -49,7 +49,7 @@ pub fn sinf(x: f32) callconv(.C) f32 { |
| 49 | 49 | if (ix <= 0x3f490fda) { // |x| ~<= pi/4 |
| 50 | 50 | if (ix < 0x39800000) { // |x| < 2**-12 |
| 51 | 51 | // 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); | |
| 53 | 53 | return x; |
| 54 | 54 | } |
| 55 | 55 | return trig.__sindf(x); |
| ... | ... | @@ -98,7 +98,7 @@ pub fn sin(x: f64) callconv(.C) f64 { |
| 98 | 98 | if (ix <= 0x3fe921fb) { |
| 99 | 99 | if (ix < 0x3e500000) { // |x| < 2**-26 |
| 100 | 100 | // 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); | |
| 102 | 102 | return x; |
| 103 | 103 | } |
| 104 | 104 | 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 { |
| 46 | 46 | // |x| < 2**-12 |
| 47 | 47 | if (ix < 0x39800000) { |
| 48 | 48 | // 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); | |
| 50 | 50 | r_sin.* = x; |
| 51 | 51 | r_cos.* = 1.0; |
| 52 | 52 | return; |
| ... | ... | @@ -134,7 +134,7 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void { |
| 134 | 134 | // if |x| < 2**-27 * sqrt(2) |
| 135 | 135 | if (ix < 0x3e46a09e) { |
| 136 | 136 | // 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); | |
| 138 | 138 | r_sin.* = x; |
| 139 | 139 | r_cos.* = 1.0; |
| 140 | 140 | return; |
| ... | ... | @@ -232,7 +232,7 @@ inline fn sincos_generic(comptime F: type, x: F, r_sin: *F, r_cos: *F) void { |
| 232 | 232 | if (se < 0x3fff - math.floatFractionalBits(F) - 1) { |
| 233 | 233 | // raise underflow if subnormal |
| 234 | 234 | if (se == 0) { |
| 235 | mem.doNotOptimizeAway(x * 0x1p-120); | |
| 235 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x * 0x1p-120); | |
| 236 | 236 | } |
| 237 | 237 | r_sin.* = x; |
| 238 | 238 | // raise inexact if x!=0 |
lib/compiler_rt/tan.zig+2-2| ... | ... | @@ -51,7 +51,7 @@ pub fn tanf(x: f32) callconv(.C) f32 { |
| 51 | 51 | if (ix <= 0x3f490fda) { // |x| ~<= pi/4 |
| 52 | 52 | if (ix < 0x39800000) { // |x| < 2**-12 |
| 53 | 53 | // 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); | |
| 55 | 55 | return x; |
| 56 | 56 | } |
| 57 | 57 | return kernel.__tandf(x, false); |
| ... | ... | @@ -89,7 +89,7 @@ pub fn tan(x: f64) callconv(.C) f64 { |
| 89 | 89 | if (ix <= 0x3fe921fb) { |
| 90 | 90 | if (ix < 0x3e400000) { // |x| < 2**-27 |
| 91 | 91 | // 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); | |
| 93 | 93 | return x; |
| 94 | 94 | } |
| 95 | 95 | 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 { |
| 47 | 47 | if (u & m == 0) { |
| 48 | 48 | return x; |
| 49 | 49 | } else { |
| 50 | mem.doNotOptimizeAway(x + 0x1p120); | |
| 50 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120); | |
| 51 | 51 | return @bitCast(u & ~m); |
| 52 | 52 | } |
| 53 | 53 | } |
| ... | ... | @@ -68,7 +68,7 @@ pub fn trunc(x: f64) callconv(.C) f64 { |
| 68 | 68 | if (u & m == 0) { |
| 69 | 69 | return x; |
| 70 | 70 | } else { |
| 71 | mem.doNotOptimizeAway(x + 0x1p120); | |
| 71 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120); | |
| 72 | 72 | return @bitCast(u & ~m); |
| 73 | 73 | } |
| 74 | 74 | } |
| ... | ... | @@ -94,7 +94,7 @@ pub fn truncq(x: f128) callconv(.C) f128 { |
| 94 | 94 | if (u & m == 0) { |
| 95 | 95 | return x; |
| 96 | 96 | } else { |
| 97 | mem.doNotOptimizeAway(x + 0x1p120); | |
| 97 | if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120); | |
| 98 | 98 | return @bitCast(u & ~m); |
| 99 | 99 | } |
| 100 | 100 | } |
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) |
| 130 | 130 | |
| 131 | 131 | test "__udivei4/__umodei4" { |
| 132 | 132 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; |
| 133 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; | |
| 133 | 134 | |
| 134 | 135 | const RndGen = std.Random.DefaultPrng; |
| 135 | 136 | 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 { |
| 1837 | 1837 | .sub_sat => func.airSatBinOp(inst, .sub), |
| 1838 | 1838 | .sub_wrap => func.airWrapBinOp(inst, .sub), |
| 1839 | 1839 | .mul => func.airBinOp(inst, .mul), |
| 1840 | .mul_sat => func.airSatMul(inst), | |
| 1840 | 1841 | .mul_wrap => func.airWrapBinOp(inst, .mul), |
| 1841 | 1842 | .div_float, .div_exact => func.airDiv(inst), |
| 1842 | 1843 | .div_trunc => func.airDivTrunc(inst), |
| ... | ... | @@ -2002,7 +2003,6 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2002 | 2003 | .error_set_has_value => func.airErrorSetHasValue(inst), |
| 2003 | 2004 | .frame_addr => func.airFrameAddress(inst), |
| 2004 | 2005 | |
| 2005 | .mul_sat, | |
| 2006 | 2006 | .assembly, |
| 2007 | 2007 | .is_err_ptr, |
| 2008 | 2008 | .is_non_err_ptr, |
| ... | ... | @@ -2666,8 +2666,8 @@ fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) Inner |
| 2666 | 2666 | switch (op) { |
| 2667 | 2667 | .mul => return func.callIntrinsic("__multi3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), |
| 2668 | 2668 | .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 }), | |
| 2671 | 2671 | }, |
| 2672 | 2672 | .rem => switch (int_info.signedness) { |
| 2673 | 2673 | .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 { |
| 4378 | 4378 | else |
| 4379 | 4379 | try func.intcast(operand, operand_ty, ty); |
| 4380 | 4380 | |
| 4381 | return func.finishAir(inst, result, &.{}); | |
| 4381 | return func.finishAir(inst, result, &.{ty_op.operand}); | |
| 4382 | 4382 | } |
| 4383 | 4383 | |
| 4384 | 4384 | /// 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 { |
| 4677 | 4677 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4678 | 4678 | |
| 4679 | 4679 | 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(); | |
| 4681 | 4681 | 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); | |
| 4682 | 4693 | |
| 4683 | const result = try func.trunc(operand, wanted_ty, op_ty); | |
| 4684 | 4694 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 4685 | 4695 | } |
| 4686 | 4696 | |
| ... | ... | @@ -6783,6 +6793,106 @@ fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6783 | 6793 | return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs }); |
| 6784 | 6794 | } |
| 6785 | 6795 | |
| 6796 | fn 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 | ||
| 6786 | 6896 | fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6787 | 6897 | assert(op == .add or op == .sub); |
| 6788 | 6898 | 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" { |
| 154 | 154 | try comptime S.doTheTest(); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | fn testSatMul(comptime T: type, a: T, b: T, expected: T) !void { | |
| 158 | const res: T = a *| b; | |
| 159 | try expect(res == expected); | |
| 160 | } | |
| 161 | ||
| 162 | test "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 | |
| 234 | test "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 | ||
| 157 | 260 | test "saturating multiplication" { |
| 158 | 261 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO |
| 159 | 262 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO |
| ... | ... | @@ -183,23 +286,15 @@ test "saturating multiplication" { |
| 183 | 286 | try testSatMul(u8, 2, 255, 255); |
| 184 | 287 | try testSatMul(u128, maxInt(u128), maxInt(u128), maxInt(u128)); |
| 185 | 288 | } |
| 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 | } | |
| 194 | 289 | }; |
| 195 | 290 | |
| 196 | 291 | try S.doTheTest(); |
| 197 | 292 | try comptime S.doTheTest(); |
| 198 | 293 | |
| 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); | |
| 203 | 298 | } |
| 204 | 299 | |
| 205 | 300 | test "saturating shift-left" { |