diff --git a/std/assembly/math.ts b/std/assembly/math.ts index 7407b708..c49c24f3 100644 --- a/std/assembly/math.ts +++ b/std/assembly/math.ts @@ -1137,7 +1137,6 @@ export namespace NativeMath { var ex = (ux >> 52 & 0x7FF); var ey = (uy >> 52 & 0x7FF); var sx = (ux >> 63); - var sy = (uy >> 63); if (uy << 1 == 0 || ex == 0x7FF || isNaN(y)) return (x * y) / (x * y); if (ux << 1 == 0) return x; var uxi = ux; @@ -1162,7 +1161,7 @@ export namespace NativeMath { return x; } let i: u64; - for (; ex > ey; ex--) { + for (; ex > ey; --ex) { i = uxi - uy; if (i >> 63 == 0) { uxi = i; @@ -1192,7 +1191,7 @@ export namespace NativeMath { uxi >>= -ex + 1; } x = reinterpret(uxi); - if (sy) y = -y; + y = builtin_abs(y); var x2 = x + x; if (ex == ey || (ex + 1 == ey && (x2 > y || (x2 == y && (q & 1))))) { x -= y; @@ -2191,7 +2190,6 @@ export namespace NativeMathf { var ex = (ux >> 23 & 0xFF); var ey = (uy >> 23 & 0xFF); var sx = (ux >> 31); - var sy = (uy >> 31); var uxi = ux; if (uy << 1 == 0 || ex == 0xFF || isNaN(y)) return (x * y) / (x * y); if (ux << 1 == 0) return x; @@ -2216,11 +2214,11 @@ export namespace NativeMathf { return x; } let i: u32; - for (; ex > ey; ex--) { + for (; ex > ey; --ex) { i = uxi - uy; if (i >> 31 == 0) { uxi = i; - q++; + ++q; } uxi <<= 1; q <<= 1; @@ -2228,11 +2226,11 @@ export namespace NativeMathf { i = uxi - uy; if (i >> 31 == 0) { uxi = i; - q++; + ++q; } if (uxi == 0) ex = -30; else { - let shift = builtin_clz(uxi << 8); + let shift = builtin_clz(uxi << 8); ex -= shift; uxi <<= shift; } @@ -2246,10 +2244,11 @@ export namespace NativeMathf { uxi >>= -ex + 1; } x = reinterpret(uxi); - if (sy) y = -y; - if (ex == ey || (ex + 1 == ey && (2 * x > y || (2 * x == y && (q & 1))))) { + y = builtin_abs(y); + var x2 = x + x; + if (ex == ey || (ex + 1 == ey && (x2 > y || (x2 == y && (q & 1))))) { x -= y; - q++; + // q++; } return sx ? -x : x; } diff --git a/tests/compiler/std/math.optimized.wat b/tests/compiler/std/math.optimized.wat index 945627b7..52bb7cd1 100644 --- a/tests/compiler/std/math.optimized.wat +++ b/tests/compiler/std/math.optimized.wat @@ -11754,7 +11754,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 40) - (i32.const 2023) + (i32.const 2022) (i32.const 24) ) (unreachable) @@ -11914,7 +11914,6 @@ (local $8 i64) (local $9 f64) (local $10 i32) - (local $11 i32) (set_local $3 (i64.and (i64.shr_u @@ -11928,10 +11927,10 @@ (i64.const 2047) ) ) - (set_local $6 + (set_local $5 (i64.and (i64.shr_u - (tee_local $5 + (tee_local $6 (i64.reinterpret/f64 (get_local $1) ) @@ -11949,20 +11948,12 @@ ) ) ) - (set_local $11 - (i32.wrap/i64 - (i64.shr_u - (get_local $5) - (i64.const 63) - ) - ) - ) (if (i32.eqz (tee_local $4 (i64.eq (i64.shl - (get_local $5) + (get_local $6) (i64.const 1) ) (i64.const 0) @@ -12046,22 +12037,22 @@ ) ) ) - (set_local $5 + (set_local $6 (if (result i64) (i64.eqz - (get_local $6) + (get_local $5) ) (i64.shl - (get_local $5) + (get_local $6) (i64.add (i64.sub (i64.const 0) - (tee_local $6 + (tee_local $5 (i64.sub - (get_local $6) + (get_local $5) (i64.clz (i64.shl - (get_local $5) + (get_local $6) (i64.const 12) ) ) @@ -12073,7 +12064,7 @@ ) (i64.or (i64.and - (get_local $5) + (get_local $6) (i64.const 4503599627370495) ) (i64.const 4503599627370496) @@ -12084,7 +12075,7 @@ (if (i64.lt_s (get_local $3) - (get_local $6) + (get_local $5) ) (block (br_if $break|0 @@ -12093,7 +12084,7 @@ (get_local $3) (i64.const 1) ) - (get_local $6) + (get_local $5) ) ) (return @@ -12106,7 +12097,7 @@ (br_if $break|1 (i64.le_s (get_local $3) - (get_local $6) + (get_local $5) ) ) (if @@ -12115,7 +12106,7 @@ (tee_local $8 (i64.sub (get_local $2) - (get_local $5) + (get_local $6) ) ) (i64.const 63) @@ -12161,7 +12152,7 @@ (tee_local $8 (i64.sub (get_local $2) - (get_local $5) + (get_local $6) ) ) (i64.const 63) @@ -12211,49 +12202,45 @@ ) ) ) - (set_local $0 - (f64.reinterpret/i64 - (tee_local $2 - (if (result i64) - (i64.gt_s - (get_local $3) - (i64.const 0) - ) - (i64.or - (i64.sub - (get_local $2) - (i64.const 4503599627370496) - ) - (i64.shl - (get_local $3) - (i64.const 52) - ) - ) - (i64.shr_u - (get_local $2) - (i64.add - (i64.sub - (i64.const 0) - (get_local $3) - ) - (i64.const 1) - ) - ) - ) - ) - ) - ) - (if - (get_local $11) - (set_local $1 - (f64.neg - (get_local $1) - ) + (set_local $1 + (f64.abs + (get_local $1) ) ) (set_local $9 (f64.add - (get_local $0) + (tee_local $0 + (f64.reinterpret/i64 + (tee_local $2 + (if (result i64) + (i64.gt_s + (get_local $3) + (i64.const 0) + ) + (i64.or + (i64.sub + (get_local $2) + (i64.const 4503599627370496) + ) + (i64.shl + (get_local $3) + (i64.const 52) + ) + ) + (i64.shr_u + (get_local $2) + (i64.add + (i64.sub + (i64.const 0) + (get_local $3) + ) + (i64.const 1) + ) + ) + ) + ) + ) + ) (get_local $0) ) ) @@ -12262,7 +12249,7 @@ (tee_local $4 (i64.eq (get_local $3) - (get_local $6) + (get_local $5) ) ) ) @@ -12273,7 +12260,7 @@ (get_local $3) (i64.const 1) ) - (get_local $6) + (get_local $5) ) ) (if @@ -12338,7 +12325,7 @@ (local $5 i32) (local $6 i32) (local $7 i32) - (local $8 i32) + (local $8 f32) (local $9 i32) (set_local $3 (i32.and @@ -12353,10 +12340,10 @@ (i32.const 255) ) ) - (set_local $6 + (set_local $5 (i32.and (i32.shr_u - (tee_local $5 + (tee_local $6 (i32.reinterpret/f32 (get_local $1) ) @@ -12366,15 +12353,9 @@ (i32.const 255) ) ) - (set_local $8 - (i32.shr_u - (get_local $2) - (i32.const 31) - ) - ) (set_local $9 (i32.shr_u - (get_local $5) + (get_local $2) (i32.const 31) ) ) @@ -12383,7 +12364,7 @@ (tee_local $4 (i32.eqz (i32.shl - (get_local $5) + (get_local $6) (i32.const 1) ) ) @@ -12460,26 +12441,26 @@ ) ) ) - (set_local $5 + (set_local $6 (if (result i32) - (get_local $6) + (get_local $5) (i32.or (i32.and - (get_local $5) + (get_local $6) (i32.const 8388607) ) (i32.const 8388608) ) (i32.shl - (get_local $5) + (get_local $6) (i32.sub (i32.const 1) - (tee_local $6 + (tee_local $5 (i32.sub - (get_local $6) + (get_local $5) (i32.clz (i32.shl - (get_local $5) + (get_local $6) (i32.const 9) ) ) @@ -12493,7 +12474,7 @@ (if (i32.lt_s (get_local $3) - (get_local $6) + (get_local $5) ) (block (br_if $break|0 @@ -12502,7 +12483,7 @@ (get_local $3) (i32.const 1) ) - (get_local $6) + (get_local $5) ) ) (return @@ -12515,7 +12496,7 @@ (br_if $break|1 (i32.le_s (get_local $3) - (get_local $6) + (get_local $5) ) ) (if @@ -12524,7 +12505,7 @@ (tee_local $4 (i32.sub (get_local $2) - (get_local $5) + (get_local $6) ) ) (i32.const 31) @@ -12569,7 +12550,7 @@ (tee_local $4 (i32.sub (get_local $2) - (get_local $5) + (get_local $6) ) ) (i32.const 31) @@ -12615,41 +12596,43 @@ ) ) ) - (set_local $0 - (f32.reinterpret/i32 - (tee_local $2 - (if (result i32) - (i32.gt_s - (get_local $3) - (i32.const 0) - ) - (i32.or - (i32.sub - (get_local $2) - (i32.const 8388608) - ) - (i32.shl - (get_local $3) - (i32.const 23) - ) - ) - (i32.shr_u - (get_local $2) - (i32.sub - (i32.const 1) - (get_local $3) + (set_local $1 + (f32.abs + (get_local $1) + ) + ) + (set_local $8 + (f32.add + (tee_local $0 + (f32.reinterpret/i32 + (tee_local $2 + (if (result i32) + (i32.gt_s + (get_local $3) + (i32.const 0) + ) + (i32.or + (i32.sub + (get_local $2) + (i32.const 8388608) + ) + (i32.shl + (get_local $3) + (i32.const 23) + ) + ) + (i32.shr_u + (get_local $2) + (i32.sub + (i32.const 1) + (get_local $3) + ) + ) ) ) ) ) - ) - ) - (if - (get_local $9) - (set_local $1 - (f32.neg - (get_local $1) - ) + (get_local $0) ) ) (if @@ -12657,7 +12640,7 @@ (tee_local $4 (i32.eq (get_local $3) - (get_local $6) + (get_local $5) ) ) ) @@ -12668,17 +12651,14 @@ (get_local $3) (i32.const 1) ) - (get_local $6) + (get_local $5) ) ) (if (i32.eqz (tee_local $4 (f32.gt - (f32.mul - (f32.const 2) - (get_local $0) - ) + (get_local $8) (get_local $1) ) ) @@ -12686,10 +12666,7 @@ (if (tee_local $4 (f32.eq - (f32.mul - (f32.const 2) - (get_local $0) - ) + (get_local $8) (get_local $1) ) ) @@ -12713,7 +12690,7 @@ ) ) (if - (get_local $8) + (get_local $9) (set_local $0 (f32.neg (get_local $0) diff --git a/tests/compiler/std/math.untouched.wat b/tests/compiler/std/math.untouched.wat index cf8850d3..abe2c9a0 100644 --- a/tests/compiler/std/math.untouched.wat +++ b/tests/compiler/std/math.untouched.wat @@ -13633,7 +13633,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 40) - (i32.const 2023) + (i32.const 2022) (i32.const 24) ) (unreachable) @@ -13817,12 +13817,11 @@ (local $5 i64) (local $6 i32) (local $7 i32) - (local $8 i32) - (local $9 i64) - (local $10 i32) + (local $8 i64) + (local $9 i32) + (local $10 i64) (local $11 i64) - (local $12 i64) - (local $13 f64) + (local $12 f64) (set_local $2 (i64.reinterpret/f64 (get_local $0) @@ -13859,19 +13858,11 @@ ) ) ) - (set_local $7 - (i32.wrap/i64 - (i64.shr_u - (get_local $3) - (i64.const 63) - ) - ) - ) (if (if (result i32) - (tee_local $8 + (tee_local $7 (if (result i32) - (tee_local $8 + (tee_local $7 (i64.eq (i64.shl (get_local $3) @@ -13880,14 +13871,14 @@ (i64.const 0) ) ) - (get_local $8) + (get_local $7) (i64.eq (get_local $4) (i64.const 2047) ) ) ) - (get_local $8) + (get_local $7) (call $~lib/builtins/isNaN (get_local $1) ) @@ -13917,7 +13908,7 @@ (get_local $0) ) ) - (set_local $9 + (set_local $8 (get_local $2) ) (if @@ -13930,15 +13921,15 @@ (get_local $4) (i64.clz (i64.shl - (get_local $9) + (get_local $8) (i64.const 12) ) ) ) ) - (set_local $9 + (set_local $8 (i64.shl - (get_local $9) + (get_local $8) (i64.add (i64.sub (i64.const 0) @@ -13950,18 +13941,18 @@ ) ) (block - (set_local $9 + (set_local $8 (i64.and - (get_local $9) + (get_local $8) (i64.shr_u (i64.const -1) (i64.const 12) ) ) ) - (set_local $9 + (set_local $8 (i64.or - (get_local $9) + (get_local $8) (i64.shl (i64.const 1) (i64.const 52) @@ -14020,7 +14011,7 @@ ) ) ) - (set_local $10 + (set_local $9 (i32.const 0) ) (block $break|0 @@ -14057,41 +14048,41 @@ ) ) (block - (set_local $11 + (set_local $10 (i64.sub - (get_local $9) + (get_local $8) (get_local $3) ) ) (if (i64.eq (i64.shr_u - (get_local $11) + (get_local $10) (i64.const 63) ) (i64.const 0) ) (block - (set_local $9 - (get_local $11) + (set_local $8 + (get_local $10) ) - (set_local $10 + (set_local $9 (i32.add - (get_local $10) + (get_local $9) (i32.const 1) ) ) ) ) - (set_local $9 + (set_local $8 (i64.shl - (get_local $9) + (get_local $8) (i64.const 1) ) ) - (set_local $10 + (set_local $9 (i32.shl - (get_local $10) + (get_local $9) (i32.const 1) ) ) @@ -14105,27 +14096,27 @@ (br $repeat|1) ) ) - (set_local $11 + (set_local $10 (i64.sub - (get_local $9) + (get_local $8) (get_local $3) ) ) (if (i64.eq (i64.shr_u - (get_local $11) + (get_local $10) (i64.const 63) ) (i64.const 0) ) (block - (set_local $9 - (get_local $11) + (set_local $8 + (get_local $10) ) - (set_local $10 + (set_local $9 (i32.add - (get_local $10) + (get_local $9) (i32.const 1) ) ) @@ -14133,17 +14124,17 @@ ) (if (i64.eq - (get_local $9) + (get_local $8) (i64.const 0) ) (set_local $4 (i64.const -60) ) (block - (set_local $12 + (set_local $11 (i64.clz (i64.shl - (get_local $9) + (get_local $8) (i64.const 11) ) ) @@ -14151,13 +14142,13 @@ (set_local $4 (i64.sub (get_local $4) - (get_local $12) + (get_local $11) ) ) - (set_local $9 + (set_local $8 (i64.shl - (get_local $9) - (get_local $12) + (get_local $8) + (get_local $11) ) ) ) @@ -14172,18 +14163,18 @@ (i64.const 0) ) (block - (set_local $9 + (set_local $8 (i64.sub - (get_local $9) + (get_local $8) (i64.shl (i64.const 1) (i64.const 52) ) ) ) - (set_local $9 + (set_local $8 (i64.or - (get_local $9) + (get_local $8) (i64.shl (get_local $4) (i64.const 52) @@ -14191,9 +14182,9 @@ ) ) ) - (set_local $9 + (set_local $8 (i64.shr_u - (get_local $9) + (get_local $8) (i64.add (i64.sub (i64.const 0) @@ -14206,18 +14197,15 @@ ) (set_local $0 (f64.reinterpret/i64 - (get_local $9) + (get_local $8) ) ) - (if - (get_local $7) - (set_local $1 - (f64.neg - (get_local $1) - ) + (set_local $1 + (f64.abs + (get_local $1) ) ) - (set_local $13 + (set_local $12 (f64.add (get_local $0) (get_local $0) @@ -14225,15 +14213,15 @@ ) (if (if (result i32) - (tee_local $8 + (tee_local $7 (i64.eq (get_local $4) (get_local $5) ) ) - (get_local $8) + (get_local $7) (if (result i32) - (tee_local $8 + (tee_local $7 (i64.eq (i64.add (get_local $4) @@ -14243,28 +14231,28 @@ ) ) (if (result i32) - (tee_local $8 + (tee_local $7 (f64.gt - (get_local $13) + (get_local $12) (get_local $1) ) ) - (get_local $8) + (get_local $7) (if (result i32) - (tee_local $8 + (tee_local $7 (f64.eq - (get_local $13) + (get_local $12) (get_local $1) ) ) (i32.and - (get_local $10) + (get_local $9) (i32.const 1) ) - (get_local $8) + (get_local $7) ) ) - (get_local $8) + (get_local $7) ) ) (set_local $0 @@ -14303,7 +14291,7 @@ (local $8 i32) (local $9 i32) (local $10 i32) - (local $11 i32) + (local $11 f32) (set_local $2 (i32.reinterpret/f32 (get_local $0) @@ -14339,19 +14327,13 @@ ) ) (set_local $7 - (i32.shr_u - (get_local $3) - (i32.const 31) - ) - ) - (set_local $8 (get_local $2) ) (if (if (result i32) - (tee_local $9 + (tee_local $8 (if (result i32) - (tee_local $9 + (tee_local $8 (i32.eq (i32.shl (get_local $3) @@ -14360,14 +14342,14 @@ (i32.const 0) ) ) - (get_local $9) + (get_local $8) (i32.eq (get_local $4) (i32.const 255) ) ) ) - (get_local $9) + (get_local $8) (call $~lib/builtins/isNaN (get_local $1) ) @@ -14407,15 +14389,15 @@ (get_local $4) (i32.clz (i32.shl - (get_local $8) + (get_local $7) (i32.const 9) ) ) ) ) - (set_local $8 + (set_local $7 (i32.shl - (get_local $8) + (get_local $7) (i32.add (i32.sub (i32.const 0) @@ -14427,18 +14409,18 @@ ) ) (block - (set_local $8 + (set_local $7 (i32.and - (get_local $8) + (get_local $7) (i32.shr_u (i32.const -1) (i32.const 9) ) ) ) - (set_local $8 + (set_local $7 (i32.or - (get_local $8) + (get_local $7) (i32.shl (i32.const 1) (i32.const 23) @@ -14497,7 +14479,7 @@ ) ) ) - (set_local $10 + (set_local $9 (i32.const 0) ) (block $break|0 @@ -14534,41 +14516,41 @@ ) ) (block - (set_local $9 + (set_local $8 (i32.sub - (get_local $8) + (get_local $7) (get_local $3) ) ) (if (i32.eq (i32.shr_u - (get_local $9) + (get_local $8) (i32.const 31) ) (i32.const 0) ) (block - (set_local $8 - (get_local $9) + (set_local $7 + (get_local $8) ) - (set_local $10 + (set_local $9 (i32.add - (get_local $10) + (get_local $9) (i32.const 1) ) ) ) ) - (set_local $8 + (set_local $7 (i32.shl - (get_local $8) + (get_local $7) (i32.const 1) ) ) - (set_local $10 + (set_local $9 (i32.shl - (get_local $10) + (get_local $9) (i32.const 1) ) ) @@ -14582,27 +14564,27 @@ (br $repeat|1) ) ) - (set_local $9 + (set_local $8 (i32.sub - (get_local $8) + (get_local $7) (get_local $3) ) ) (if (i32.eq (i32.shr_u - (get_local $9) + (get_local $8) (i32.const 31) ) (i32.const 0) ) (block - (set_local $8 - (get_local $9) + (set_local $7 + (get_local $8) ) - (set_local $10 + (set_local $9 (i32.add - (get_local $10) + (get_local $9) (i32.const 1) ) ) @@ -14610,17 +14592,17 @@ ) (if (i32.eq - (get_local $8) + (get_local $7) (i32.const 0) ) (set_local $4 (i32.const -30) ) (block - (set_local $11 + (set_local $10 (i32.clz (i32.shl - (get_local $8) + (get_local $7) (i32.const 8) ) ) @@ -14628,13 +14610,13 @@ (set_local $4 (i32.sub (get_local $4) - (get_local $11) + (get_local $10) ) ) - (set_local $8 + (set_local $7 (i32.shl - (get_local $8) - (get_local $11) + (get_local $7) + (get_local $10) ) ) ) @@ -14649,18 +14631,18 @@ (i32.const 0) ) (block - (set_local $8 + (set_local $7 (i32.sub - (get_local $8) + (get_local $7) (i32.shl (i32.const 1) (i32.const 23) ) ) ) - (set_local $8 + (set_local $7 (i32.or - (get_local $8) + (get_local $7) (i32.shl (get_local $4) (i32.const 23) @@ -14668,9 +14650,9 @@ ) ) ) - (set_local $8 + (set_local $7 (i32.shr_u - (get_local $8) + (get_local $7) (i32.add (i32.sub (i32.const 0) @@ -14683,28 +14665,31 @@ ) (set_local $0 (f32.reinterpret/i32 - (get_local $8) + (get_local $7) ) ) - (if - (get_local $7) - (set_local $1 - (f32.neg - (get_local $1) - ) + (set_local $1 + (f32.abs + (get_local $1) + ) + ) + (set_local $11 + (f32.add + (get_local $0) + (get_local $0) ) ) (if (if (result i32) - (tee_local $9 + (tee_local $8 (i32.eq (get_local $4) (get_local $5) ) ) - (get_local $9) + (get_local $8) (if (result i32) - (tee_local $9 + (tee_local $8 (i32.eq (i32.add (get_local $4) @@ -14714,48 +14699,34 @@ ) ) (if (result i32) - (tee_local $9 + (tee_local $8 (f32.gt - (f32.mul - (f32.const 2) - (get_local $0) - ) + (get_local $11) (get_local $1) ) ) - (get_local $9) + (get_local $8) (if (result i32) - (tee_local $9 + (tee_local $8 (f32.eq - (f32.mul - (f32.const 2) - (get_local $0) - ) + (get_local $11) (get_local $1) ) ) (i32.and - (get_local $10) + (get_local $9) (i32.const 1) ) - (get_local $9) + (get_local $8) ) ) - (get_local $9) + (get_local $8) ) ) - (block - (set_local $0 - (f32.sub - (get_local $0) - (get_local $1) - ) - ) - (set_local $10 - (i32.add - (get_local $10) - (i32.const 1) - ) + (set_local $0 + (f32.sub + (get_local $0) + (get_local $1) ) ) )