From 4247a469b9ce21d5e40ad06df96d70f580679337 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 15:53:23 +0300 Subject: [PATCH 1/9] FIx MIN_POSITIVE_VALUE -> MIN_NORMAL_VALUE for definitions --- std/assembly/index.d.ts | 4 ++-- std/portable/index.d.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/std/assembly/index.d.ts b/std/assembly/index.d.ts index 1a1534de31..e9988d15be 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -506,7 +506,7 @@ declare namespace f32 { /** Largest representable value. */ export const MAX_VALUE: f32; /** Smallest normalized positive value. */ - export const MIN_POSITIVE_VALUE: f32; + export const MIN_NORMAL_VALUE: f32; /** Smallest safely representable integer value. */ export const MIN_SAFE_INTEGER: f32; /** Largest safely representable integer value. */ @@ -526,7 +526,7 @@ declare namespace f64 { /** Largest representable value. */ export const MAX_VALUE: f64; /** Smallest normalized positive value. */ - export const MIN_POSITIVE_VALUE: f64; + export const MIN_NORMAL_VALUE: f64; /** Smallest safely representable integer value. */ export const MIN_SAFE_INTEGER: f64; /** Largest safely representable integer value. */ diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index bc4587db3d..ecc600e969 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -230,7 +230,7 @@ declare namespace f32 { /** Largest representable value. */ export const MAX_VALUE: f32; /** Smallest normalized positive value. */ - export const MIN_POSITIVE_VALUE: f32; + export const MIN_NORMAL_VALUE: f32; /** Smallest safely representable integer value. */ export const MIN_SAFE_INTEGER: f32; /** Largest safely representable integer value. */ @@ -258,7 +258,7 @@ declare namespace f64 { /** Largest representable value. */ export const MAX_VALUE: f64; /** Smallest normalized positive value. */ - export const MIN_POSITIVE_VALUE: f64; + export const MIN_NORMAL_VALUE: f64; /** Smallest safely representable integer value. */ export const MIN_SAFE_INTEGER: f64; /** Largest safely representable integer value. */ From d50c7fc9f61e326228c4d5a2d8d2e6209c33f643 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 16:12:55 +0300 Subject: [PATCH 2/9] also add (f32|f64).POSITIVE/NEGATIVE_INFINITY --- std/assembly/builtins.ts | 18 +++++++++++++++++- std/assembly/index.d.ts | 8 ++++++++ std/portable/index.d.ts | 8 ++++++++ std/portable/index.js | 8 ++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 34626cff90..24301e11e9 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -168,7 +168,7 @@ export declare function assert(isTrueish: T, message?: string): T; @unsafe @builtin export declare function unchecked(expr: T): T; -// @ts-ignore: decorator +// @ts-ignore: decorator @unsafe @builtin export declare function call_indirect(index: u32, ...args: auto[]): T; @@ -846,6 +846,14 @@ export namespace f32 { @lazy export const MAX_SAFE_INTEGER: f32 = 16777215; + // @ts-ignore: decorator + @lazy + export const POSITIVE_INFINITY: f32 = Infinity; + + // @ts-ignore: decorator + @lazy + export const NEGATIVE_INFINITY: f32 = -Infinity; + // @ts-ignore: decorator @builtin export declare function abs(value: f32): f32; @@ -925,6 +933,14 @@ export namespace f64 { @lazy export const MAX_SAFE_INTEGER: f64 = 9007199254740991; + // @ts-ignore: decorator + @lazy + export const POSITIVE_INFINITY: f64 = Infinity; + + // @ts-ignore: decorator + @lazy + export const NEGATIVE_INFINITY: f64 = -Infinity; + // @ts-ignore: decorator @builtin export declare function abs(value: f64): f64; diff --git a/std/assembly/index.d.ts b/std/assembly/index.d.ts index e9988d15be..65cbf0f60c 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -511,6 +511,10 @@ declare namespace f32 { export const MIN_SAFE_INTEGER: f32; /** Largest safely representable integer value. */ export const MAX_SAFE_INTEGER: f32; + /** Positive infinity value. */ + export const POSITIVE_INFINITY: f32; + /** Negative infinity value. */ + export const NEGATIVE_INFINITY: f32; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f32; /** Loads a 32-bit float from memory. */ @@ -531,6 +535,10 @@ declare namespace f64 { export const MIN_SAFE_INTEGER: f64; /** Largest safely representable integer value. */ export const MAX_SAFE_INTEGER: f64; + /** Positive infinity value. */ + export const POSITIVE_INFINITY: f64; + /** Negative infinity value. */ + export const NEGATIVE_INFINITY: f64; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f64; /** Loads a 64-bit float from memory. */ diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index ecc600e969..afda48dfde 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -235,6 +235,10 @@ declare namespace f32 { export const MIN_SAFE_INTEGER: f32; /** Largest safely representable integer value. */ export const MAX_SAFE_INTEGER: f32; + /** Positive infinity value. */ + export const POSITIVE_INFINITY: f32; + /** Negative infinity value. */ + export const NEGATIVE_INFINITY: f32; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f32; /** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */ @@ -263,6 +267,10 @@ declare namespace f64 { export const MIN_SAFE_INTEGER: f64; /** Largest safely representable integer value. */ export const MAX_SAFE_INTEGER: f64; + /** Positive infinity value. */ + export const POSITIVE_INFINITY: f64; + /** Negative infinity value. */ + export const NEGATIVE_INFINITY: f64; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f64; /** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */ diff --git a/std/portable/index.js b/std/portable/index.js index 9271173249..ee7f402ff8 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -80,7 +80,9 @@ Object.defineProperties( "MAX_VALUE": { value: Math.fround(3.4028235e+38), writable: false }, "MIN_NORMAL_VALUE": { value: Math.fround(1.17549435e-38), writable: false }, "MIN_SAFE_INTEGER": { value: -16777215, writable: false }, - "MAX_SAFE_INTEGER": { value: 16777215, writable: false } + "MAX_SAFE_INTEGER": { value: 16777215, writable: false }, + "POSITIVE_INFINITY": { value: Math.fround(Infinity), writable: false }, + "NEGATIVE_INFINITY": { value: Math.fround(-Infinity), writable: false } } ); @@ -92,7 +94,9 @@ Object.defineProperties( "MAX_VALUE": { value: 1.7976931348623157e+308, writable: false }, "MIN_NORMAL_VALUE": { value: 2.2250738585072014e-308 , writable: false }, "MIN_SAFE_INTEGER": { value: -9007199254740991, writable: false }, - "MAX_SAFE_INTEGER": { value: 9007199254740991, writable: false } + "MAX_SAFE_INTEGER": { value: 9007199254740991, writable: false }, + "POSITIVE_INFINITY": { value: Infinity, writable: false }, + "NEGATIVE_INFINITY": { value: -Infinity, writable: false } } ); From 92a1931f650adb814e5fa8a9e8d4811e123a4e4a Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 16:29:57 +0300 Subject: [PATCH 3/9] remove Math.fround for (+/-)inf polyfill --- std/portable/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index ee7f402ff8..78f75ccf30 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -81,8 +81,8 @@ Object.defineProperties( "MIN_NORMAL_VALUE": { value: Math.fround(1.17549435e-38), writable: false }, "MIN_SAFE_INTEGER": { value: -16777215, writable: false }, "MAX_SAFE_INTEGER": { value: 16777215, writable: false }, - "POSITIVE_INFINITY": { value: Math.fround(Infinity), writable: false }, - "NEGATIVE_INFINITY": { value: Math.fround(-Infinity), writable: false } + "POSITIVE_INFINITY": { value: Infinity, writable: false }, + "NEGATIVE_INFINITY": { value: -Infinity, writable: false } } ); From 5163663043f76dc2c3eba0388817989b06a67270 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 16:32:36 +0300 Subject: [PATCH 4/9] precompute other Math.fround values --- std/portable/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 78f75ccf30..02febd7785 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -75,10 +75,10 @@ Object.defineProperties( Object.defineProperties( globalScope["f32"] = function f32(value) { return Math.fround(value); }, { - "EPSILON": { value: Math.fround(1.1920929e-07), writable: false }, - "MIN_VALUE": { value: Math.fround(1.4012985e-45), writable: false }, - "MAX_VALUE": { value: Math.fround(3.4028235e+38), writable: false }, - "MIN_NORMAL_VALUE": { value: Math.fround(1.17549435e-38), writable: false }, + "EPSILON": { value: 1.1920928955078125e-7, writable: false }, + "MIN_VALUE": { value: 1.4012984643248170e-45, writable: false }, + "MAX_VALUE": { value: 3.4028234663852886e+38, writable: false }, + "MIN_NORMAL_VALUE": { value: 1.1754943508222875e-38, writable: false }, "MIN_SAFE_INTEGER": { value: -16777215, writable: false }, "MAX_SAFE_INTEGER": { value: 16777215, writable: false }, "POSITIVE_INFINITY": { value: Infinity, writable: false }, From eb80742752890c6f2aaeda62d2bc8fc1a3346ea2 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 16:34:31 +0300 Subject: [PATCH 5/9] aligned --- std/portable/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 02febd7785..82653bfffa 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -75,7 +75,7 @@ Object.defineProperties( Object.defineProperties( globalScope["f32"] = function f32(value) { return Math.fround(value); }, { - "EPSILON": { value: 1.1920928955078125e-7, writable: false }, + "EPSILON": { value: 1.1920928955078125e-07, writable: false }, "MIN_VALUE": { value: 1.4012984643248170e-45, writable: false }, "MAX_VALUE": { value: 3.4028234663852886e+38, writable: false }, "MIN_NORMAL_VALUE": { value: 1.1754943508222875e-38, writable: false }, @@ -89,10 +89,10 @@ Object.defineProperties( Object.defineProperties( globalScope["f64"] = function f64(value) { return +value; }, { - "EPSILON": { value: 2.2204460492503131e-16, writable: false }, + "EPSILON": { value: 2.2204460492503131e-016, writable: false }, "MIN_VALUE": { value: 5e-324, writable: false }, "MAX_VALUE": { value: 1.7976931348623157e+308, writable: false }, - "MIN_NORMAL_VALUE": { value: 2.2250738585072014e-308 , writable: false }, + "MIN_NORMAL_VALUE": { value: 2.2250738585072014e-308, writable: false }, "MIN_SAFE_INTEGER": { value: -9007199254740991, writable: false }, "MAX_SAFE_INTEGER": { value: 9007199254740991, writable: false }, "POSITIVE_INFINITY": { value: Infinity, writable: false }, From 4ad8629f77ac99cdc8123867af632866b5be10b4 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 17:19:24 +0300 Subject: [PATCH 6/9] also add f32.NaN and f64.NaN --- std/assembly/builtins.ts | 8 ++++++++ std/assembly/index.d.ts | 4 ++++ std/assembly/number.ts | 12 ++++++------ std/portable/index.d.ts | 6 ++++++ std/portable/index.js | 6 ++++-- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 24301e11e9..54ca868280 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -854,6 +854,10 @@ export namespace f32 { @lazy export const NEGATIVE_INFINITY: f32 = -Infinity; + // @ts-ignore: decorator + @inline @lazy + export const NaN: f32 = 0.0 / 0.0; + // @ts-ignore: decorator @builtin export declare function abs(value: f32): f32; @@ -941,6 +945,10 @@ export namespace f64 { @lazy export const NEGATIVE_INFINITY: f64 = -Infinity; + // @ts-ignore: decorator + @inline @lazy + export const NaN: f64 = 0.0 / 0.0; + // @ts-ignore: decorator @builtin export declare function abs(value: f64): f64; diff --git a/std/assembly/index.d.ts b/std/assembly/index.d.ts index 65cbf0f60c..b022371b24 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -515,6 +515,8 @@ declare namespace f32 { export const POSITIVE_INFINITY: f32; /** Negative infinity value. */ export const NEGATIVE_INFINITY: f32; + /** Not a number value. */ + export const NaN: f32; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f32; /** Loads a 32-bit float from memory. */ @@ -539,6 +541,8 @@ declare namespace f64 { export const POSITIVE_INFINITY: f64; /** Negative infinity value. */ export const NEGATIVE_INFINITY: f64; + /** Not a number value. */ + export const NaN: f64; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f64; /** Loads a 64-bit float from memory. */ diff --git a/std/assembly/number.ts b/std/assembly/number.ts index 916fecb0b7..c658ff18ea 100644 --- a/std/assembly/number.ts +++ b/std/assembly/number.ts @@ -268,15 +268,15 @@ export abstract class F32 { // @ts-ignore: decorator @lazy - static readonly POSITIVE_INFINITY: f32 = Infinity; + static readonly POSITIVE_INFINITY: f32 = f32.POSITIVE_INFINITY; // @ts-ignore: decorator @lazy - static readonly NEGATIVE_INFINITY: f32 = -Infinity; + static readonly NEGATIVE_INFINITY: f32 = f32.NEGATIVE_INFINITY; // @ts-ignore: decorator @lazy - static readonly NaN: f32 = NaN; + static readonly NaN: f32 = f32.NaN; static isNaN(value: f32): bool { return isNaN(value); @@ -332,15 +332,15 @@ export abstract class F64 { // @ts-ignore: decorator @lazy - static readonly POSITIVE_INFINITY: f64 = Infinity; + static readonly POSITIVE_INFINITY: f64 = f64.POSITIVE_INFINITY; // @ts-ignore: decorator @lazy - static readonly NEGATIVE_INFINITY: f64 = -Infinity; + static readonly NEGATIVE_INFINITY: f64 = f64.NEGATIVE_INFINITY; // @ts-ignore: decorator @lazy - static readonly NaN: f64 = NaN; + static readonly NaN: f64 = f64.NaN; static isNaN(value: f64): bool { return isNaN(value); diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index afda48dfde..e8a6766429 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -239,6 +239,9 @@ declare namespace f32 { export const POSITIVE_INFINITY: f32; /** Negative infinity value. */ export const NEGATIVE_INFINITY: f32; + /** Not a number value. */ + /* eslint no-shadow-restricted-names: "off" */ + export const NaN: f32; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f32; /** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */ @@ -271,6 +274,9 @@ declare namespace f64 { export const POSITIVE_INFINITY: f64; /** Negative infinity value. */ export const NEGATIVE_INFINITY: f64; + /** Not a number value. */ + /* eslint no-shadow-restricted-names: "off" */ + export const NaN: f64; /** Difference between 1 and the smallest representable value greater than 1. */ export const EPSILON: f64; /** Returns a boolean value that indicates whether a value is the reserved value NaN (not a number). */ diff --git a/std/portable/index.js b/std/portable/index.js index 82653bfffa..8a8ffa20bb 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -82,7 +82,8 @@ Object.defineProperties( "MIN_SAFE_INTEGER": { value: -16777215, writable: false }, "MAX_SAFE_INTEGER": { value: 16777215, writable: false }, "POSITIVE_INFINITY": { value: Infinity, writable: false }, - "NEGATIVE_INFINITY": { value: -Infinity, writable: false } + "NEGATIVE_INFINITY": { value: -Infinity, writable: false }, + "NaN": { value: NaN, writable: false } } ); @@ -96,7 +97,8 @@ Object.defineProperties( "MIN_SAFE_INTEGER": { value: -9007199254740991, writable: false }, "MAX_SAFE_INTEGER": { value: 9007199254740991, writable: false }, "POSITIVE_INFINITY": { value: Infinity, writable: false }, - "NEGATIVE_INFINITY": { value: -Infinity, writable: false } + "NEGATIVE_INFINITY": { value: -Infinity, writable: false }, + "NaN": { value: NaN, writable: false } } ); From 3b1d8f8c2d3e6f79ebf3c288e90168ef2f751fda Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 20 Aug 2020 18:39:56 +0300 Subject: [PATCH 7/9] Update std/assembly/builtins.ts Co-authored-by: Daniel Wirtz --- std/assembly/builtins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 54ca868280..8668a3e9c9 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -946,7 +946,7 @@ export namespace f64 { export const NEGATIVE_INFINITY: f64 = -Infinity; // @ts-ignore: decorator - @inline @lazy + @lazy export const NaN: f64 = 0.0 / 0.0; // @ts-ignore: decorator From af43171efeb7b4e6d59a048724943bef082dd109 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 20 Aug 2020 18:40:03 +0300 Subject: [PATCH 8/9] Update std/assembly/builtins.ts Co-authored-by: Daniel Wirtz --- std/assembly/builtins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 8668a3e9c9..132e693636 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -855,7 +855,7 @@ export namespace f32 { export const NEGATIVE_INFINITY: f32 = -Infinity; // @ts-ignore: decorator - @inline @lazy + @lazy export const NaN: f32 = 0.0 / 0.0; // @ts-ignore: decorator From d4eba11f1a92400bc37e32298309f788e6ebc00d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 18:50:30 +0300 Subject: [PATCH 9/9] update fixtures --- tests/compiler/number.untouched.wat | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/compiler/number.untouched.wat b/tests/compiler/number.untouched.wat index 1075677abd..19adb60dc9 100644 --- a/tests/compiler/number.untouched.wat +++ b/tests/compiler/number.untouched.wat @@ -52,10 +52,12 @@ (global $~lib/util/number/_K (mut i32) (i32.const 0)) (global $~lib/util/number/_frc_pow (mut i64) (i64.const 0)) (global $~lib/util/number/_exp_pow (mut i32) (i32.const 0)) + (global $~lib/builtins/f32.NaN f32 (f32.const nan:0x400000)) (global $~lib/number/F32.NaN f32 (f32.const nan:0x400000)) (global $~lib/builtins/f32.MIN_SAFE_INTEGER f32 (f32.const -16777215)) (global $~lib/builtins/f32.MAX_SAFE_INTEGER f32 (f32.const 16777215)) (global $~lib/builtins/f32.EPSILON f32 (f32.const 1.1920928955078125e-07)) + (global $~lib/builtins/f64.NaN f64 (f64.const nan:0x8000000000000)) (global $~lib/number/F64.NaN f64 (f64.const nan:0x8000000000000)) (global $~lib/builtins/f64.MIN_SAFE_INTEGER f64 (f64.const -9007199254740991)) (global $~lib/builtins/f64.MAX_SAFE_INTEGER f64 (f64.const 9007199254740991))