From 99c4bfb8af72473d9df481fc2804aaaf193dc442 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 1 Sep 2022 11:01:01 +0300 Subject: [PATCH 1/6] improve portable copysign --- std/portable/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/std/portable/index.js b/std/portable/index.js index e156f7b0dd..48e246b253 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -153,7 +153,10 @@ if (typeof globalScope.ASC_TARGET === "undefined") { globalScope["trunc"] = Math.trunc; globalScope["copysign"] = function copysign(x, y) { - return Math.abs(x) * Math.sign(y); + F64[0] = y; y = U64[1] & 0x80000000; + F64[0] = x; + U64[1] = U64[1] & 0x7FFFFFFF | y; + return F64[0]; }; globalScope["bswap"] = function bswap(value) { From f04826395faff7b860d6eccbe92a5818f1c59e6e Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 1 Sep 2022 11:14:40 +0300 Subject: [PATCH 2/6] use copysign in nearest --- std/portable/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/portable/index.js b/std/portable/index.js index 48e246b253..931624c7ff 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -140,7 +140,7 @@ if (typeof globalScope.ASC_TARGET === "undefined") { const INV_EPS64 = 4503599627370496.0; const y = Math.abs(value); return y < INV_EPS64 - ? Math.abs(y + INV_EPS64 - INV_EPS64) * Math.sign(value) + ? globalScope.copysign(y + INV_EPS64 - INV_EPS64, value) : value; }; From 47158c99db24501308f31f06cad2591c25c8a36d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 5 Sep 2022 13:48:54 +0300 Subject: [PATCH 3/6] better --- std/portable/index.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 931624c7ff..8276f500f6 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -140,7 +140,7 @@ if (typeof globalScope.ASC_TARGET === "undefined") { const INV_EPS64 = 4503599627370496.0; const y = Math.abs(value); return y < INV_EPS64 - ? globalScope.copysign(y + INV_EPS64 - INV_EPS64, value) + ? Math.abs(y + INV_EPS64 - INV_EPS64) * Math.sign(value) : value; }; @@ -153,10 +153,15 @@ if (typeof globalScope.ASC_TARGET === "undefined") { globalScope["trunc"] = Math.trunc; globalScope["copysign"] = function copysign(x, y) { - F64[0] = y; y = U64[1] & 0x80000000; - F64[0] = x; - U64[1] = U64[1] & 0x7FFFFFFF | y; - return F64[0]; + if (y) { + // fast path + return Math.abs(x) * Math.sign(y); + } else { // 0, -0, +NaN, -NaN + F64[0] = y; y = U64[1] & 0x80000000; + F64[0] = x; + U64[1] = U64[1] & 0x7FFFFFFF | y; + return F64[0]; + } }; globalScope["bswap"] = function bswap(value) { From 4f213fc1900ac4b23fe848c34b03962ebd3c1911 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 5 Sep 2022 14:01:19 +0300 Subject: [PATCH 4/6] simplify --- std/portable/index.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 8276f500f6..54861b174e 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -153,15 +153,10 @@ if (typeof globalScope.ASC_TARGET === "undefined") { globalScope["trunc"] = Math.trunc; globalScope["copysign"] = function copysign(x, y) { - if (y) { - // fast path - return Math.abs(x) * Math.sign(y); - } else { // 0, -0, +NaN, -NaN - F64[0] = y; y = U64[1] & 0x80000000; - F64[0] = x; - U64[1] = U64[1] & 0x7FFFFFFF | y; - return F64[0]; - } + return Math.abs(x) * (y + ? Math.sign(y) + : (F64[0] = y, U64[1] >>> 31 ? -1 : 1) // +0, -0, -NaN, +NaN + ); }; globalScope["bswap"] = function bswap(value) { From 8fac6d5748069f72f7268eb1dd5af1dd31261dd8 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 5 Sep 2022 14:07:27 +0300 Subject: [PATCH 5/6] simplify nearest --- std/portable/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/portable/index.js b/std/portable/index.js index 54861b174e..78b4b8bbad 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -140,7 +140,7 @@ if (typeof globalScope.ASC_TARGET === "undefined") { const INV_EPS64 = 4503599627370496.0; const y = Math.abs(value); return y < INV_EPS64 - ? Math.abs(y + INV_EPS64 - INV_EPS64) * Math.sign(value) + ? (y + INV_EPS64 - INV_EPS64) * Math.sign(value) : value; }; From 8873241e209bfff735ee8d63939deeadb7ccef1d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 5 Sep 2022 14:08:50 +0300 Subject: [PATCH 6/6] simplify copysign even more --- std/portable/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 78b4b8bbad..793a21d1b0 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -153,10 +153,9 @@ if (typeof globalScope.ASC_TARGET === "undefined") { globalScope["trunc"] = Math.trunc; globalScope["copysign"] = function copysign(x, y) { - return Math.abs(x) * (y - ? Math.sign(y) - : (F64[0] = y, U64[1] >>> 31 ? -1 : 1) // +0, -0, -NaN, +NaN - ); + return y + ? Math.abs(x) * Math.sign(y) + : (F64[0] = y, U64[1] >>> 31 ? -1 : 1); // +0, -0, -NaN, +NaN }; globalScope["bswap"] = function bswap(value) {