From ff87857f408b71a5f65b7013c865789a6e18685d Mon Sep 17 00:00:00 2001 From: Max Graey Date: Mon, 1 Oct 2018 23:54:00 +0300 Subject: [PATCH] Change semantics of f32/f64.MIN_VALUE to match JS's Number.MIN_VALUE * Also renames MIN_POSITIVE_VALUE to MIN_NORMAL_VALUE * Also adds POSITIVE_INFINITY/NEGATIVE_INFINITY and NaN --- std/assembly/builtins.ts | 22 +++-- tests/compiler/builtins.ts | 8 +- tests/compiler/builtins.untouched.wat | 118 ++++++++++++++++++++++---- 3 files changed, 123 insertions(+), 25 deletions(-) diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 94e8b19c..f38b5e7d 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -147,12 +147,15 @@ export namespace bool { @builtin export declare function f32(value: void): f32; export namespace f32 { - export const MIN_VALUE = reinterpret(0xFF7FFFFF); // -0x1.fffffep+127f - export const MAX_VALUE = reinterpret(0x7F7FFFFF); // 0x1.fffffep+127f - export const MIN_POSITIVE_VALUE = reinterpret(0x00800000); // 0x1p-126f + export const EPSILON = reinterpret(0x34000000); // 0x1p-23f + export const MIN_VALUE = reinterpret(0x00000001); // 0x0.000001p+0f + export const MAX_VALUE = reinterpret(0x7F7FFFFF); // 0x1.fffffep+127f + export const MIN_NORMAL_VALUE = reinterpret(0x00800000); // 0x1p-126f export const MIN_SAFE_INTEGER: f32 = -16777215; export const MAX_SAFE_INTEGER: f32 = 16777215; - export const EPSILON = reinterpret(0x34000000); // 0x1p-23f + export const POSITIVE_INFINITY: f32 = Infinity; + export const NEGATIVE_INFINITY: f32 = -Infinity; + export const NaN: f32 = NaN; @builtin export declare function abs(value: f32): f32; @builtin export declare function ceil(value: f32): f32; @builtin export declare function copysign(x: f32, y: f32): f32; @@ -169,12 +172,15 @@ export namespace f32 { @builtin export declare function f64(value: void): f64; export namespace f64 { - export const MIN_VALUE = reinterpret(0xFFEFFFFFFFFFFFFF); // -0x1.fffffffffffffp+1023 - export const MAX_VALUE = reinterpret(0x7FEFFFFFFFFFFFFF); // 0x1.fffffffffffffp+1023 - export const MIN_POSITIVE_VALUE = reinterpret(0x0010000000000000); // 0x1p-1022 + export const EPSILON = reinterpret(0x3CB0000000000000); // 0x1p-52 + export const MIN_VALUE = reinterpret(0x0000000000000001); // 0x0.0000000000001p+0 + export const MAX_VALUE = reinterpret(0x7FEFFFFFFFFFFFFF); // 0x1.fffffffffffffp+1023 + export const MIN_NORMAL_VALUE = reinterpret(0x0010000000000000); // 0x1p-1022 export const MIN_SAFE_INTEGER: f64 = -9007199254740991; export const MAX_SAFE_INTEGER: f64 = 9007199254740991; - export const EPSILON = reinterpret(0x3CB0000000000000); // 0x1p-52 + export const POSITIVE_INFINITY: f64 = Infinity; + export const NEGATIVE_INFINITY: f64 = -Infinity; + export const NaN: f64 = NaN; @builtin export declare function abs(value: f64): f64; @builtin export declare function ceil(value: f64): f64; @builtin export declare function copysign(x: f64, y: f64): f64; diff --git a/tests/compiler/builtins.ts b/tests/compiler/builtins.ts index 5612d062..486df391 100644 --- a/tests/compiler/builtins.ts +++ b/tests/compiler/builtins.ts @@ -308,16 +308,20 @@ assert(u64.MAX_VALUE == 0xffffffffffffffff); assert(bool.MIN_VALUE == 0); assert(bool.MIN_VALUE == false); assert(bool.MAX_VALUE == 1); assert(bool.MAX_VALUE == true); -assert(f32.MIN_VALUE == -3.40282347e+38); +assert(f32.MIN_NORMAL_VALUE == 1.1754943508222875e-38); +assert(f32.MIN_VALUE == 1.401298464324817e-45); assert(f32.MAX_VALUE == 3.40282347e+38); assert(f32.MIN_SAFE_INTEGER == -16777215); assert(f32.MAX_SAFE_INTEGER == 16777215); assert(f32.EPSILON == 1.19209290e-07); -assert(f64.MIN_VALUE == -1.7976931348623157e+308); +assert(isNaN(f32.NaN)); +assert(f64.MIN_NORMAL_VALUE == 2.2250738585072014e-308); +assert(f64.MIN_VALUE == 5e-324); assert(f64.MAX_VALUE == 1.7976931348623157e+308); assert(f64.MIN_SAFE_INTEGER == -9007199254740991); assert(f64.MAX_SAFE_INTEGER == 9007199254740991); assert(f64.EPSILON == 2.2204460492503131e-16); +assert(isNaN(f64.NaN)); // inline-assembler diff --git a/tests/compiler/builtins.untouched.wat b/tests/compiler/builtins.untouched.wat index 63d98c2b..6d372a89 100644 --- a/tests/compiler/builtins.untouched.wat +++ b/tests/compiler/builtins.untouched.wat @@ -33,16 +33,20 @@ (global $~lib/builtins/u64.MAX_VALUE i64 (i64.const -1)) (global $~lib/builtins/bool.MIN_VALUE i32 (i32.const 0)) (global $~lib/builtins/bool.MAX_VALUE i32 (i32.const 1)) - (global $~lib/builtins/f32.MIN_VALUE f32 (f32.const -3402823466385288598117041e14)) + (global $~lib/builtins/f32.MIN_NORMAL_VALUE f32 (f32.const 1.1754943508222875e-38)) + (global $~lib/builtins/f32.MIN_VALUE f32 (f32.const 1.401298464324817e-45)) (global $~lib/builtins/f32.MAX_VALUE f32 (f32.const 3402823466385288598117041e14)) (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.MIN_VALUE f64 (f64.const -1797693134862315708145274e284)) + (global $~lib/builtins/f32.NaN f32 (f32.const nan:0x400000)) + (global $~lib/builtins/f64.MIN_NORMAL_VALUE f64 (f64.const 2.2250738585072014e-308)) + (global $~lib/builtins/f64.MIN_VALUE f64 (f64.const 5e-324)) (global $~lib/builtins/f64.MAX_VALUE f64 (f64.const 1797693134862315708145274e284)) (global $~lib/builtins/f64.MIN_SAFE_INTEGER f64 (f64.const -9007199254740991)) (global $~lib/builtins/f64.MAX_SAFE_INTEGER f64 (f64.const 9007199254740991)) (global $~lib/builtins/f64.EPSILON f64 (f64.const 2.220446049250313e-16)) + (global $~lib/builtins/f64.NaN f64 (f64.const nan:0x8000000000000)) (global $HEAP_BASE i32 (i32.const 48)) (table 2 2 anyfunc) (elem (i32.const 0) $null $start~anonymous|1) @@ -2668,8 +2672,8 @@ (if (i32.eqz (f32.eq - (get_global $~lib/builtins/f32.MIN_VALUE) - (f32.const -3402823466385288598117041e14) + (get_global $~lib/builtins/f32.MIN_NORMAL_VALUE) + (f32.const 1.1754943508222875e-38) ) ) (block @@ -2682,6 +2686,23 @@ (unreachable) ) ) + (if + (i32.eqz + (f32.eq + (get_global $~lib/builtins/f32.MIN_VALUE) + (f32.const 1.401298464324817e-45) + ) + ) + (block + (call $~lib/env/abort + (i32.const 0) + (i32.const 8) + (i32.const 312) + (i32.const 0) + ) + (unreachable) + ) + ) (if (i32.eqz (f32.eq @@ -2693,7 +2714,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 312) + (i32.const 313) (i32.const 0) ) (unreachable) @@ -2710,7 +2731,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 313) + (i32.const 314) (i32.const 0) ) (unreachable) @@ -2727,7 +2748,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 314) + (i32.const 315) (i32.const 0) ) (unreachable) @@ -2744,7 +2765,49 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 315) + (i32.const 316) + (i32.const 0) + ) + (unreachable) + ) + ) + (if + (i32.eqz + (i32.and + (block $~lib/builtins/isNaN|inlined.4 (result i32) + (set_local $4 + (get_global $~lib/builtins/f32.NaN) + ) + (f32.ne + (get_local $4) + (get_local $4) + ) + ) + (i32.const 1) + ) + ) + (block + (call $~lib/env/abort + (i32.const 0) + (i32.const 8) + (i32.const 317) + (i32.const 0) + ) + (unreachable) + ) + ) + (if + (i32.eqz + (f64.eq + (get_global $~lib/builtins/f64.MIN_NORMAL_VALUE) + (f64.const 2.2250738585072014e-308) + ) + ) + (block + (call $~lib/env/abort + (i32.const 0) + (i32.const 8) + (i32.const 318) (i32.const 0) ) (unreachable) @@ -2754,14 +2817,14 @@ (i32.eqz (f64.eq (get_global $~lib/builtins/f64.MIN_VALUE) - (f64.const -1797693134862315708145274e284) + (f64.const 5e-324) ) ) (block (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 316) + (i32.const 319) (i32.const 0) ) (unreachable) @@ -2778,7 +2841,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 317) + (i32.const 320) (i32.const 0) ) (unreachable) @@ -2795,7 +2858,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 318) + (i32.const 321) (i32.const 0) ) (unreachable) @@ -2812,7 +2875,7 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 319) + (i32.const 322) (i32.const 0) ) (unreachable) @@ -2829,7 +2892,32 @@ (call $~lib/env/abort (i32.const 0) (i32.const 8) - (i32.const 320) + (i32.const 323) + (i32.const 0) + ) + (unreachable) + ) + ) + (if + (i32.eqz + (i32.and + (block $~lib/builtins/isNaN|inlined.4 (result i32) + (set_local $5 + (get_global $~lib/builtins/f64.NaN) + ) + (f64.ne + (get_local $5) + (get_local $5) + ) + ) + (i32.const 1) + ) + ) + (block + (call $~lib/env/abort + (i32.const 0) + (i32.const 8) + (i32.const 324) (i32.const 0) ) (unreachable) @@ -3112,7 +3200,7 @@ ) ) (drop - (block $~lib/builtins/isNaN|inlined.4 (result i32) + (block $~lib/builtins/isNaN|inlined.5 (result i32) (set_local $5 (f64.const 1) )