diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 34626cff90..132e693636 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,18 @@ 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 + @lazy + export const NaN: f32 = 0.0 / 0.0; + // @ts-ignore: decorator @builtin export declare function abs(value: f32): f32; @@ -925,6 +937,18 @@ 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 + @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 1a1534de31..b022371b24 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -506,11 +506,17 @@ 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. */ export const MAX_SAFE_INTEGER: f32; + /** Positive infinity value. */ + 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. */ @@ -526,11 +532,17 @@ 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. */ export const MAX_SAFE_INTEGER: f64; + /** Positive infinity value. */ + 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 bc4587db3d..e8a6766429 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -230,11 +230,18 @@ 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. */ export const MAX_SAFE_INTEGER: f32; + /** Positive infinity value. */ + 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). */ @@ -258,11 +265,18 @@ 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. */ export const MAX_SAFE_INTEGER: f64; + /** Positive infinity value. */ + 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 9271173249..8a8ffa20bb 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -75,24 +75,30 @@ 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-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 }, "MIN_SAFE_INTEGER": { value: -16777215, writable: false }, - "MAX_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 }, + "NaN": { value: NaN, writable: false } } ); 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 } + "MAX_SAFE_INTEGER": { value: 9007199254740991, writable: false }, + "POSITIVE_INFINITY": { value: Infinity, writable: false }, + "NEGATIVE_INFINITY": { value: -Infinity, writable: false }, + "NaN": { value: NaN, writable: false } } ); 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))