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 {...@@ -48,14 +48,14 @@ pub fn ceilf(x: f32) callconv(.C) f32 {
48 if (u & m == 0) {48 if (u & m == 0) {
49 return x;49 return x;
50 }50 }
51 mem.doNotOptimizeAway(x + 0x1.0p120);51 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
52 if (u >> 31 == 0) {52 if (u >> 31 == 0) {
53 u += m;53 u += m;
54 }54 }
55 u &= ~m;55 u &= ~m;
56 return @bitCast(u);56 return @bitCast(u);
57 } else {57 } else {
58 mem.doNotOptimizeAway(x + 0x1.0p120);58 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
59 if (u >> 31 != 0) {59 if (u >> 31 != 0) {
60 return -0.0;60 return -0.0;
61 } else {61 } else {
...@@ -82,7 +82,7 @@ pub fn ceil(x: f64) callconv(.C) f64 {...@@ -82,7 +82,7 @@ pub fn ceil(x: f64) callconv(.C) f64 {
82 }82 }
8383
84 if (e <= 0x3FF - 1) {84 if (e <= 0x3FF - 1) {
85 mem.doNotOptimizeAway(y);85 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
86 if (u >> 63 != 0) {86 if (u >> 63 != 0) {
87 return -0.0;87 return -0.0;
88 } else {88 } else {
...@@ -116,7 +116,7 @@ pub fn ceilq(x: f128) callconv(.C) f128 {...@@ -116,7 +116,7 @@ pub fn ceilq(x: f128) callconv(.C) f128 {
116 }116 }
117117
118 if (e <= 0x3FFF - 1) {118 if (e <= 0x3FFF - 1) {
119 mem.doNotOptimizeAway(y);119 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
120 if (u >> 127 != 0) {120 if (u >> 127 != 0) {
121 return -0.0;121 return -0.0;
122 } else {122 } else {
lib/compiler_rt/common.zig+2
...@@ -24,6 +24,8 @@ pub const want_aeabi = switch (builtin.abi) {...@@ -24,6 +24,8 @@ pub const want_aeabi = switch (builtin.abi) {
24};24};
25pub const want_ppc_abi = builtin.cpu.arch.isPPC() or builtin.cpu.arch.isPPC64();25pub const want_ppc_abi = builtin.cpu.arch.isPPC() or builtin.cpu.arch.isPPC64();
2626
27pub const want_float_exceptions = !builtin.cpu.arch.isWasm();
28
27// Libcalls that involve u128 on Windows x86-64 are expected by LLVM to use the29// Libcalls that involve u128 on Windows x86-64 are expected by LLVM to use the
28// calling convention of @Vector(2, u64), rather than what's standard.30// calling convention of @Vector(2, u64), rather than what's standard.
29pub const want_windows_v2u64_abi = builtin.os.tag == .windows and builtin.cpu.arch == .x86_64 and @import("builtin").object_format != .c;31pub 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,7 +41,7 @@ pub fn cosf(x: f32) callconv(.C) f32 {
41 if (ix <= 0x3f490fda) { // |x| ~<= pi/441 if (ix <= 0x3f490fda) { // |x| ~<= pi/4
42 if (ix < 0x39800000) { // |x| < 2**-1242 if (ix < 0x39800000) { // |x| < 2**-12
43 // raise inexact if x != 043 // raise inexact if x != 0
44 mem.doNotOptimizeAway(x + 0x1p120);44 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
45 return 1.0;45 return 1.0;
46 }46 }
47 return trig.__cosdf(x);47 return trig.__cosdf(x);
...@@ -92,7 +92,7 @@ pub fn cos(x: f64) callconv(.C) f64 {...@@ -92,7 +92,7 @@ pub fn cos(x: f64) callconv(.C) f64 {
92 if (ix <= 0x3fe921fb) {92 if (ix <= 0x3fe921fb) {
93 if (ix < 0x3e46a09e) { // |x| < 2**-27 * sqrt(2)93 if (ix < 0x3e46a09e) { // |x| < 2**-27 * sqrt(2)
94 // raise inexact if x!=094 // raise inexact if x!=0
95 mem.doNotOptimizeAway(x + 0x1p120);95 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
96 return 1.0;96 return 1.0;
97 }97 }
98 return trig.__cos(x, 0);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,7 +59,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {
59 return x * 0x1.0p127;59 return x * 0x1.0p127;
60 }60 }
61 if (sign != 0) {61 if (sign != 0) {
62 mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow62 if (common.want_float_exceptions) mem.doNotOptimizeAway(-0x1.0p-149 / x); // overflow
63 // x <= -103.97208463 // x <= -103.972084
64 if (hx >= 0x42CFF1B5) {64 if (hx >= 0x42CFF1B5) {
65 return 0;65 return 0;
...@@ -91,7 +91,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {...@@ -91,7 +91,7 @@ pub fn expf(x_: f32) callconv(.C) f32 {
91 hi = x;91 hi = x;
92 lo = 0;92 lo = 0;
93 } else {93 } else {
94 mem.doNotOptimizeAway(0x1.0p127 + x); // inexact94 if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p127 + x); // inexact
95 return 1 + x;95 return 1 + x;
96 }96 }
9797
...@@ -142,7 +142,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {...@@ -142,7 +142,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {
142 }142 }
143 if (x < -708.39641853226410622) {143 if (x < -708.39641853226410622) {
144 // underflow if x != -inf144 // 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 if (x < -745.13321910194110842) {146 if (x < -745.13321910194110842) {
147 return 0;147 return 0;
148 }148 }
...@@ -175,7 +175,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {...@@ -175,7 +175,7 @@ pub fn exp(x_: f64) callconv(.C) f64 {
175 lo = 0;175 lo = 0;
176 } else {176 } else {
177 // inexact if x != 0177 // inexact if x != 0
178 // mem.doNotOptimizeAway(0x1.0p1023 + x);178 // if (common.want_float_exceptions) mem.doNotOptimizeAway(0x1.0p1023 + x);
179 return 1 + x;179 return 1 + x;
180 }180 }
181181
lib/compiler_rt/exp2.zig+2-2
...@@ -55,7 +55,7 @@ pub fn exp2f(x: f32) callconv(.C) f32 {...@@ -55,7 +55,7 @@ pub fn exp2f(x: f32) callconv(.C) f32 {
55 // x < -12655 // x < -126
56 if (u >= 0x80000000) {56 if (u >= 0x80000000) {
57 if (u >= 0xC3160000 or u & 0x000FFFF != 0) {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 // x <= -15060 // x <= -150
61 if (u >= 0x3160000) {61 if (u >= 0x3160000) {
...@@ -120,7 +120,7 @@ pub fn exp2(x: f64) callconv(.C) f64 {...@@ -120,7 +120,7 @@ pub fn exp2(x: f64) callconv(.C) f64 {
120 if (ux >> 63 != 0) {120 if (ux >> 63 != 0) {
121 // underflow121 // underflow
122 if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {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 if (x <= -1075) {125 if (x <= -1075) {
126 return 0;126 return 0;
lib/compiler_rt/floor.zig+6-6
...@@ -45,13 +45,13 @@ pub fn __floorh(x: f16) callconv(.C) f16 {...@@ -45,13 +45,13 @@ pub fn __floorh(x: f16) callconv(.C) f16 {
45 if (u & m == 0) {45 if (u & m == 0) {
46 return x;46 return x;
47 }47 }
48 mem.doNotOptimizeAway(x + 0x1.0p120);48 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
49 if (u >> 15 != 0) {49 if (u >> 15 != 0) {
50 u += m;50 u += m;
51 }51 }
52 return @bitCast(u & ~m);52 return @bitCast(u & ~m);
53 } else {53 } else {
54 mem.doNotOptimizeAway(x + 0x1.0p120);54 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
55 if (u >> 15 == 0) {55 if (u >> 15 == 0) {
56 return 0.0;56 return 0.0;
57 } else {57 } else {
...@@ -79,13 +79,13 @@ pub fn floorf(x: f32) callconv(.C) f32 {...@@ -79,13 +79,13 @@ pub fn floorf(x: f32) callconv(.C) f32 {
79 if (u & m == 0) {79 if (u & m == 0) {
80 return x;80 return x;
81 }81 }
82 mem.doNotOptimizeAway(x + 0x1.0p120);82 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
83 if (u >> 31 != 0) {83 if (u >> 31 != 0) {
84 u += m;84 u += m;
85 }85 }
86 return @bitCast(u & ~m);86 return @bitCast(u & ~m);
87 } else {87 } else {
88 mem.doNotOptimizeAway(x + 0x1.0p120);88 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1.0p120);
89 if (u >> 31 == 0) {89 if (u >> 31 == 0) {
90 return 0.0;90 return 0.0;
91 } else {91 } else {
...@@ -112,7 +112,7 @@ pub fn floor(x: f64) callconv(.C) f64 {...@@ -112,7 +112,7 @@ pub fn floor(x: f64) callconv(.C) f64 {
112 }112 }
113113
114 if (e <= 0x3FF - 1) {114 if (e <= 0x3FF - 1) {
115 mem.doNotOptimizeAway(y);115 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
116 if (u >> 63 != 0) {116 if (u >> 63 != 0) {
117 return -1.0;117 return -1.0;
118 } else {118 } else {
...@@ -146,7 +146,7 @@ pub fn floorq(x: f128) callconv(.C) f128 {...@@ -146,7 +146,7 @@ pub fn floorq(x: f128) callconv(.C) f128 {
146 }146 }
147147
148 if (e <= 0x3FFF - 1) {148 if (e <= 0x3FFF - 1) {
149 mem.doNotOptimizeAway(y);149 if (common.want_float_exceptions) mem.doNotOptimizeAway(y);
150 if (u >> 127 != 0) {150 if (u >> 127 != 0) {
151 return -1.0;151 return -1.0;
152 } else {152 } else {
lib/compiler_rt/round.zig+3-3
...@@ -46,7 +46,7 @@ pub fn roundf(x_: f32) callconv(.C) f32 {...@@ -46,7 +46,7 @@ pub fn roundf(x_: f32) callconv(.C) f32 {
46 x = -x;46 x = -x;
47 }47 }
48 if (e < 0x7F - 1) {48 if (e < 0x7F - 1) {
49 mem.doNotOptimizeAway(x + f32_toint);49 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f32_toint);
50 return 0 * @as(f32, @bitCast(u));50 return 0 * @as(f32, @bitCast(u));
51 }51 }
5252
...@@ -81,7 +81,7 @@ pub fn round(x_: f64) callconv(.C) f64 {...@@ -81,7 +81,7 @@ pub fn round(x_: f64) callconv(.C) f64 {
81 x = -x;81 x = -x;
82 }82 }
83 if (e < 0x3ff - 1) {83 if (e < 0x3ff - 1) {
84 mem.doNotOptimizeAway(x + f64_toint);84 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f64_toint);
85 return 0 * @as(f64, @bitCast(u));85 return 0 * @as(f64, @bitCast(u));
86 }86 }
8787
...@@ -121,7 +121,7 @@ pub fn roundq(x_: f128) callconv(.C) f128 {...@@ -121,7 +121,7 @@ pub fn roundq(x_: f128) callconv(.C) f128 {
121 x = -x;121 x = -x;
122 }122 }
123 if (e < 0x3FFF - 1) {123 if (e < 0x3FFF - 1) {
124 mem.doNotOptimizeAway(x + f128_toint);124 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + f128_toint);
125 return 0 * @as(f128, @bitCast(u));125 return 0 * @as(f128, @bitCast(u));
126 }126 }
127127
lib/compiler_rt/sin.zig+2-2
...@@ -49,7 +49,7 @@ pub fn sinf(x: f32) callconv(.C) f32 {...@@ -49,7 +49,7 @@ pub fn sinf(x: f32) callconv(.C) f32 {
49 if (ix <= 0x3f490fda) { // |x| ~<= pi/449 if (ix <= 0x3f490fda) { // |x| ~<= pi/4
50 if (ix < 0x39800000) { // |x| < 2**-1250 if (ix < 0x39800000) { // |x| < 2**-12
51 // raise inexact if x!=0 and underflow if subnormal51 // 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 return x;53 return x;
54 }54 }
55 return trig.__sindf(x);55 return trig.__sindf(x);
...@@ -98,7 +98,7 @@ pub fn sin(x: f64) callconv(.C) f64 {...@@ -98,7 +98,7 @@ pub fn sin(x: f64) callconv(.C) f64 {
98 if (ix <= 0x3fe921fb) {98 if (ix <= 0x3fe921fb) {
99 if (ix < 0x3e500000) { // |x| < 2**-2699 if (ix < 0x3e500000) { // |x| < 2**-26
100 // raise inexact if x != 0 and underflow if subnormal100 // 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 return x;102 return x;
103 }103 }
104 return trig.__sin(x, 0.0, 0);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,7 +46,7 @@ pub fn sincosf(x: f32, r_sin: *f32, r_cos: *f32) callconv(.C) void {
46 // |x| < 2**-1246 // |x| < 2**-12
47 if (ix < 0x39800000) {47 if (ix < 0x39800000) {
48 // raise inexact if x!=0 and underflow if subnormal48 // 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 r_sin.* = x;50 r_sin.* = x;
51 r_cos.* = 1.0;51 r_cos.* = 1.0;
52 return;52 return;
...@@ -134,7 +134,7 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void {...@@ -134,7 +134,7 @@ pub fn sincos(x: f64, r_sin: *f64, r_cos: *f64) callconv(.C) void {
134 // if |x| < 2**-27 * sqrt(2)134 // if |x| < 2**-27 * sqrt(2)
135 if (ix < 0x3e46a09e) {135 if (ix < 0x3e46a09e) {
136 // raise inexact if x != 0 and underflow if subnormal136 // 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 r_sin.* = x;138 r_sin.* = x;
139 r_cos.* = 1.0;139 r_cos.* = 1.0;
140 return;140 return;
...@@ -232,7 +232,7 @@ inline fn sincos_generic(comptime F: type, x: F, r_sin: *F, r_cos: *F) void {...@@ -232,7 +232,7 @@ inline fn sincos_generic(comptime F: type, x: F, r_sin: *F, r_cos: *F) void {
232 if (se < 0x3fff - math.floatFractionalBits(F) - 1) {232 if (se < 0x3fff - math.floatFractionalBits(F) - 1) {
233 // raise underflow if subnormal233 // raise underflow if subnormal
234 if (se == 0) {234 if (se == 0) {
235 mem.doNotOptimizeAway(x * 0x1p-120);235 if (common.want_float_exceptions) mem.doNotOptimizeAway(x * 0x1p-120);
236 }236 }
237 r_sin.* = x;237 r_sin.* = x;
238 // raise inexact if x!=0238 // 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,7 +51,7 @@ pub fn tanf(x: f32) callconv(.C) f32 {
51 if (ix <= 0x3f490fda) { // |x| ~<= pi/451 if (ix <= 0x3f490fda) { // |x| ~<= pi/4
52 if (ix < 0x39800000) { // |x| < 2**-1252 if (ix < 0x39800000) { // |x| < 2**-12
53 // raise inexact if x!=0 and underflow if subnormal53 // 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 return x;55 return x;
56 }56 }
57 return kernel.__tandf(x, false);57 return kernel.__tandf(x, false);
...@@ -89,7 +89,7 @@ pub fn tan(x: f64) callconv(.C) f64 {...@@ -89,7 +89,7 @@ pub fn tan(x: f64) callconv(.C) f64 {
89 if (ix <= 0x3fe921fb) {89 if (ix <= 0x3fe921fb) {
90 if (ix < 0x3e400000) { // |x| < 2**-2790 if (ix < 0x3e400000) { // |x| < 2**-27
91 // raise inexact if x!=0 and underflow if subnormal91 // 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 return x;93 return x;
94 }94 }
95 return kernel.__tan(x, 0.0, false);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,7 +47,7 @@ pub fn truncf(x: f32) callconv(.C) f32 {
47 if (u & m == 0) {47 if (u & m == 0) {
48 return x;48 return x;
49 } else {49 } else {
50 mem.doNotOptimizeAway(x + 0x1p120);50 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
51 return @bitCast(u & ~m);51 return @bitCast(u & ~m);
52 }52 }
53}53}
...@@ -68,7 +68,7 @@ pub fn trunc(x: f64) callconv(.C) f64 {...@@ -68,7 +68,7 @@ pub fn trunc(x: f64) callconv(.C) f64 {
68 if (u & m == 0) {68 if (u & m == 0) {
69 return x;69 return x;
70 } else {70 } else {
71 mem.doNotOptimizeAway(x + 0x1p120);71 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
72 return @bitCast(u & ~m);72 return @bitCast(u & ~m);
73 }73 }
74}74}
...@@ -94,7 +94,7 @@ pub fn truncq(x: f128) callconv(.C) f128 {...@@ -94,7 +94,7 @@ pub fn truncq(x: f128) callconv(.C) f128 {
94 if (u & m == 0) {94 if (u & m == 0) {
95 return x;95 return x;
96 } else {96 } else {
97 mem.doNotOptimizeAway(x + 0x1p120);97 if (common.want_float_exceptions) mem.doNotOptimizeAway(x + 0x1p120);
98 return @bitCast(u & ~m);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,6 +130,7 @@ pub fn __umodei4(r_p: [*]u32, u_p: [*]const u32, v_p: [*]const u32, bits: usize)
130130
131test "__udivei4/__umodei4" {131test "__udivei4/__umodei4" {
132 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;132 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
133 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
133134
134 const RndGen = std.Random.DefaultPrng;135 const RndGen = std.Random.DefaultPrng;
135 var rnd = RndGen.init(42);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,6 +1837,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
1837 .sub_sat => func.airSatBinOp(inst, .sub),1837 .sub_sat => func.airSatBinOp(inst, .sub),
1838 .sub_wrap => func.airWrapBinOp(inst, .sub),1838 .sub_wrap => func.airWrapBinOp(inst, .sub),
1839 .mul => func.airBinOp(inst, .mul),1839 .mul => func.airBinOp(inst, .mul),
1840 .mul_sat => func.airSatMul(inst),
1840 .mul_wrap => func.airWrapBinOp(inst, .mul),1841 .mul_wrap => func.airWrapBinOp(inst, .mul),
1841 .div_float, .div_exact => func.airDiv(inst),1842 .div_float, .div_exact => func.airDiv(inst),
1842 .div_trunc => func.airDivTrunc(inst),1843 .div_trunc => func.airDivTrunc(inst),
...@@ -2002,7 +2003,6 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -2002,7 +2003,6 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2002 .error_set_has_value => func.airErrorSetHasValue(inst),2003 .error_set_has_value => func.airErrorSetHasValue(inst),
2003 .frame_addr => func.airFrameAddress(inst),2004 .frame_addr => func.airFrameAddress(inst),
20042005
2005 .mul_sat,
2006 .assembly,2006 .assembly,
2007 .is_err_ptr,2007 .is_err_ptr,
2008 .is_non_err_ptr,2008 .is_non_err_ptr,
...@@ -2666,8 +2666,8 @@ fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) Inner...@@ -2666,8 +2666,8 @@ fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) Inner
2666 switch (op) {2666 switch (op) {
2667 .mul => return func.callIntrinsic("__multi3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),2667 .mul => return func.callIntrinsic("__multi3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
2668 .div => switch (int_info.signedness) {2668 .div => switch (int_info.signedness) {
2669 .signed => return func.callIntrinsic("__udivti3", &.{ 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("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),2670 .unsigned => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),
2671 },2671 },
2672 .rem => switch (int_info.signedness) {2672 .rem => switch (int_info.signedness) {
2673 .signed => return func.callIntrinsic("__modti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }),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,7 +4378,7 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4378 else4378 else
4379 try func.intcast(operand, operand_ty, ty);4379 try func.intcast(operand, operand_ty, ty);
43804380
4381 return func.finishAir(inst, result, &.{});4381 return func.finishAir(inst, result, &.{ty_op.operand});
4382}4382}
43834383
4384/// Upcasts or downcasts an integer based on the given and wanted types,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,10 +4677,20 @@ fn airTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4677 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;4677 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
46784678
4679 const operand = try func.resolveInst(ty_op.operand);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 const op_ty = func.typeOf(ty_op.operand);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);
46824693
4683 const result = try func.trunc(operand, wanted_ty, op_ty);
4684 return func.finishAir(inst, result, &.{ty_op.operand});4694 return func.finishAir(inst, result, &.{ty_op.operand});
4685}4695}
46864696
...@@ -6783,6 +6793,106 @@ fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {...@@ -6783,6 +6793,106 @@ fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6783 return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs });6793 return func.finishAir(inst, .stack, &.{ bin_op.lhs, bin_op.rhs });
6784}6794}
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
6786fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {6896fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
6787 assert(op == .add or op == .sub);6897 assert(op == .add or op == .sub);
6788 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;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,6 +154,109 @@ test "saturating subtraction 128bit" {
154 try comptime S.doTheTest();154 try comptime S.doTheTest();
155}155}
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
157test "saturating multiplication" {260test "saturating multiplication" {
158 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO261 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
159 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO262 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
...@@ -183,23 +286,15 @@ test "saturating multiplication" {...@@ -183,23 +286,15 @@ test "saturating multiplication" {
183 try testSatMul(u8, 2, 255, 255);286 try testSatMul(u8, 2, 255, 255);
184 try testSatMul(u128, maxInt(u128), maxInt(u128), maxInt(u128));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 };
195290
196 try S.doTheTest();291 try S.doTheTest();
197 try comptime S.doTheTest();292 try comptime S.doTheTest();
198293
199 try comptime S.testSatMul(comptime_int, 0, 0, 0);294 try comptime testSatMul(comptime_int, 0, 0, 0);
200 try comptime S.testSatMul(comptime_int, 3, 2, 6);295 try comptime testSatMul(comptime_int, 3, 2, 6);
201 try comptime S.testSatMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);296 try comptime testSatMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);
202 try comptime S.testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);297 try comptime testSatMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);
203}298}
204299
205test "saturating shift-left" {300test "saturating shift-left" {