Skip to content

Commit ff87857

Browse files
MaxGraeydcodeIO
authored andcommitted
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
1 parent 2874fb9 commit ff87857

File tree

3 files changed

+123
-25
lines changed

3 files changed

+123
-25
lines changed

std/assembly/builtins.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,15 @@ export namespace bool {
147147

148148
@builtin export declare function f32(value: void): f32;
149149
export namespace f32 {
150-
export const MIN_VALUE = reinterpret<f32>(0xFF7FFFFF); // -0x1.fffffep+127f
151-
export const MAX_VALUE = reinterpret<f32>(0x7F7FFFFF); // 0x1.fffffep+127f
152-
export const MIN_POSITIVE_VALUE = reinterpret<f32>(0x00800000); // 0x1p-126f
150+
export const EPSILON = reinterpret<f32>(0x34000000); // 0x1p-23f
151+
export const MIN_VALUE = reinterpret<f32>(0x00000001); // 0x0.000001p+0f
152+
export const MAX_VALUE = reinterpret<f32>(0x7F7FFFFF); // 0x1.fffffep+127f
153+
export const MIN_NORMAL_VALUE = reinterpret<f32>(0x00800000); // 0x1p-126f
153154
export const MIN_SAFE_INTEGER: f32 = -16777215;
154155
export const MAX_SAFE_INTEGER: f32 = 16777215;
155-
export const EPSILON = reinterpret<f32>(0x34000000); // 0x1p-23f
156+
export const POSITIVE_INFINITY: f32 = Infinity;
157+
export const NEGATIVE_INFINITY: f32 = -Infinity;
158+
export const NaN: f32 = NaN;
156159
@builtin export declare function abs(value: f32): f32;
157160
@builtin export declare function ceil(value: f32): f32;
158161
@builtin export declare function copysign(x: f32, y: f32): f32;
@@ -169,12 +172,15 @@ export namespace f32 {
169172

170173
@builtin export declare function f64(value: void): f64;
171174
export namespace f64 {
172-
export const MIN_VALUE = reinterpret<f64>(0xFFEFFFFFFFFFFFFF); // -0x1.fffffffffffffp+1023
173-
export const MAX_VALUE = reinterpret<f64>(0x7FEFFFFFFFFFFFFF); // 0x1.fffffffffffffp+1023
174-
export const MIN_POSITIVE_VALUE = reinterpret<f64>(0x0010000000000000); // 0x1p-1022
175+
export const EPSILON = reinterpret<f64>(0x3CB0000000000000); // 0x1p-52
176+
export const MIN_VALUE = reinterpret<f64>(0x0000000000000001); // 0x0.0000000000001p+0
177+
export const MAX_VALUE = reinterpret<f64>(0x7FEFFFFFFFFFFFFF); // 0x1.fffffffffffffp+1023
178+
export const MIN_NORMAL_VALUE = reinterpret<f64>(0x0010000000000000); // 0x1p-1022
175179
export const MIN_SAFE_INTEGER: f64 = -9007199254740991;
176180
export const MAX_SAFE_INTEGER: f64 = 9007199254740991;
177-
export const EPSILON = reinterpret<f64>(0x3CB0000000000000); // 0x1p-52
181+
export const POSITIVE_INFINITY: f64 = Infinity;
182+
export const NEGATIVE_INFINITY: f64 = -Infinity;
183+
export const NaN: f64 = NaN;
178184
@builtin export declare function abs(value: f64): f64;
179185
@builtin export declare function ceil(value: f64): f64;
180186
@builtin export declare function copysign(x: f64, y: f64): f64;

tests/compiler/builtins.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,16 +308,20 @@ assert(u64.MAX_VALUE == 0xffffffffffffffff);
308308
assert(bool.MIN_VALUE == 0); assert(bool.MIN_VALUE == false);
309309
assert(bool.MAX_VALUE == 1); assert(bool.MAX_VALUE == true);
310310

311-
assert(f32.MIN_VALUE == -3.40282347e+38);
311+
assert(f32.MIN_NORMAL_VALUE == 1.1754943508222875e-38);
312+
assert(f32.MIN_VALUE == 1.401298464324817e-45);
312313
assert(f32.MAX_VALUE == 3.40282347e+38);
313314
assert(f32.MIN_SAFE_INTEGER == -16777215);
314315
assert(f32.MAX_SAFE_INTEGER == 16777215);
315316
assert(f32.EPSILON == 1.19209290e-07);
316-
assert(f64.MIN_VALUE == -1.7976931348623157e+308);
317+
assert(isNaN<f32>(f32.NaN));
318+
assert(f64.MIN_NORMAL_VALUE == 2.2250738585072014e-308);
319+
assert(f64.MIN_VALUE == 5e-324);
317320
assert(f64.MAX_VALUE == 1.7976931348623157e+308);
318321
assert(f64.MIN_SAFE_INTEGER == -9007199254740991);
319322
assert(f64.MAX_SAFE_INTEGER == 9007199254740991);
320323
assert(f64.EPSILON == 2.2204460492503131e-16);
324+
assert(isNaN<f64>(f64.NaN));
321325

322326
// inline-assembler
323327

tests/compiler/builtins.untouched.wat

Lines changed: 103 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,20 @@
3333
(global $~lib/builtins/u64.MAX_VALUE i64 (i64.const -1))
3434
(global $~lib/builtins/bool.MIN_VALUE i32 (i32.const 0))
3535
(global $~lib/builtins/bool.MAX_VALUE i32 (i32.const 1))
36-
(global $~lib/builtins/f32.MIN_VALUE f32 (f32.const -3402823466385288598117041e14))
36+
(global $~lib/builtins/f32.MIN_NORMAL_VALUE f32 (f32.const 1.1754943508222875e-38))
37+
(global $~lib/builtins/f32.MIN_VALUE f32 (f32.const 1.401298464324817e-45))
3738
(global $~lib/builtins/f32.MAX_VALUE f32 (f32.const 3402823466385288598117041e14))
3839
(global $~lib/builtins/f32.MIN_SAFE_INTEGER f32 (f32.const -16777215))
3940
(global $~lib/builtins/f32.MAX_SAFE_INTEGER f32 (f32.const 16777215))
4041
(global $~lib/builtins/f32.EPSILON f32 (f32.const 1.1920928955078125e-07))
41-
(global $~lib/builtins/f64.MIN_VALUE f64 (f64.const -1797693134862315708145274e284))
42+
(global $~lib/builtins/f32.NaN f32 (f32.const nan:0x400000))
43+
(global $~lib/builtins/f64.MIN_NORMAL_VALUE f64 (f64.const 2.2250738585072014e-308))
44+
(global $~lib/builtins/f64.MIN_VALUE f64 (f64.const 5e-324))
4245
(global $~lib/builtins/f64.MAX_VALUE f64 (f64.const 1797693134862315708145274e284))
4346
(global $~lib/builtins/f64.MIN_SAFE_INTEGER f64 (f64.const -9007199254740991))
4447
(global $~lib/builtins/f64.MAX_SAFE_INTEGER f64 (f64.const 9007199254740991))
4548
(global $~lib/builtins/f64.EPSILON f64 (f64.const 2.220446049250313e-16))
49+
(global $~lib/builtins/f64.NaN f64 (f64.const nan:0x8000000000000))
4650
(global $HEAP_BASE i32 (i32.const 48))
4751
(table 2 2 anyfunc)
4852
(elem (i32.const 0) $null $start~anonymous|1)
@@ -2668,8 +2672,8 @@
26682672
(if
26692673
(i32.eqz
26702674
(f32.eq
2671-
(get_global $~lib/builtins/f32.MIN_VALUE)
2672-
(f32.const -3402823466385288598117041e14)
2675+
(get_global $~lib/builtins/f32.MIN_NORMAL_VALUE)
2676+
(f32.const 1.1754943508222875e-38)
26732677
)
26742678
)
26752679
(block
@@ -2682,6 +2686,23 @@
26822686
(unreachable)
26832687
)
26842688
)
2689+
(if
2690+
(i32.eqz
2691+
(f32.eq
2692+
(get_global $~lib/builtins/f32.MIN_VALUE)
2693+
(f32.const 1.401298464324817e-45)
2694+
)
2695+
)
2696+
(block
2697+
(call $~lib/env/abort
2698+
(i32.const 0)
2699+
(i32.const 8)
2700+
(i32.const 312)
2701+
(i32.const 0)
2702+
)
2703+
(unreachable)
2704+
)
2705+
)
26852706
(if
26862707
(i32.eqz
26872708
(f32.eq
@@ -2693,7 +2714,7 @@
26932714
(call $~lib/env/abort
26942715
(i32.const 0)
26952716
(i32.const 8)
2696-
(i32.const 312)
2717+
(i32.const 313)
26972718
(i32.const 0)
26982719
)
26992720
(unreachable)
@@ -2710,7 +2731,7 @@
27102731
(call $~lib/env/abort
27112732
(i32.const 0)
27122733
(i32.const 8)
2713-
(i32.const 313)
2734+
(i32.const 314)
27142735
(i32.const 0)
27152736
)
27162737
(unreachable)
@@ -2727,7 +2748,7 @@
27272748
(call $~lib/env/abort
27282749
(i32.const 0)
27292750
(i32.const 8)
2730-
(i32.const 314)
2751+
(i32.const 315)
27312752
(i32.const 0)
27322753
)
27332754
(unreachable)
@@ -2744,7 +2765,49 @@
27442765
(call $~lib/env/abort
27452766
(i32.const 0)
27462767
(i32.const 8)
2747-
(i32.const 315)
2768+
(i32.const 316)
2769+
(i32.const 0)
2770+
)
2771+
(unreachable)
2772+
)
2773+
)
2774+
(if
2775+
(i32.eqz
2776+
(i32.and
2777+
(block $~lib/builtins/isNaN<f32>|inlined.4 (result i32)
2778+
(set_local $4
2779+
(get_global $~lib/builtins/f32.NaN)
2780+
)
2781+
(f32.ne
2782+
(get_local $4)
2783+
(get_local $4)
2784+
)
2785+
)
2786+
(i32.const 1)
2787+
)
2788+
)
2789+
(block
2790+
(call $~lib/env/abort
2791+
(i32.const 0)
2792+
(i32.const 8)
2793+
(i32.const 317)
2794+
(i32.const 0)
2795+
)
2796+
(unreachable)
2797+
)
2798+
)
2799+
(if
2800+
(i32.eqz
2801+
(f64.eq
2802+
(get_global $~lib/builtins/f64.MIN_NORMAL_VALUE)
2803+
(f64.const 2.2250738585072014e-308)
2804+
)
2805+
)
2806+
(block
2807+
(call $~lib/env/abort
2808+
(i32.const 0)
2809+
(i32.const 8)
2810+
(i32.const 318)
27482811
(i32.const 0)
27492812
)
27502813
(unreachable)
@@ -2754,14 +2817,14 @@
27542817
(i32.eqz
27552818
(f64.eq
27562819
(get_global $~lib/builtins/f64.MIN_VALUE)
2757-
(f64.const -1797693134862315708145274e284)
2820+
(f64.const 5e-324)
27582821
)
27592822
)
27602823
(block
27612824
(call $~lib/env/abort
27622825
(i32.const 0)
27632826
(i32.const 8)
2764-
(i32.const 316)
2827+
(i32.const 319)
27652828
(i32.const 0)
27662829
)
27672830
(unreachable)
@@ -2778,7 +2841,7 @@
27782841
(call $~lib/env/abort
27792842
(i32.const 0)
27802843
(i32.const 8)
2781-
(i32.const 317)
2844+
(i32.const 320)
27822845
(i32.const 0)
27832846
)
27842847
(unreachable)
@@ -2795,7 +2858,7 @@
27952858
(call $~lib/env/abort
27962859
(i32.const 0)
27972860
(i32.const 8)
2798-
(i32.const 318)
2861+
(i32.const 321)
27992862
(i32.const 0)
28002863
)
28012864
(unreachable)
@@ -2812,7 +2875,7 @@
28122875
(call $~lib/env/abort
28132876
(i32.const 0)
28142877
(i32.const 8)
2815-
(i32.const 319)
2878+
(i32.const 322)
28162879
(i32.const 0)
28172880
)
28182881
(unreachable)
@@ -2829,7 +2892,32 @@
28292892
(call $~lib/env/abort
28302893
(i32.const 0)
28312894
(i32.const 8)
2832-
(i32.const 320)
2895+
(i32.const 323)
2896+
(i32.const 0)
2897+
)
2898+
(unreachable)
2899+
)
2900+
)
2901+
(if
2902+
(i32.eqz
2903+
(i32.and
2904+
(block $~lib/builtins/isNaN<f64>|inlined.4 (result i32)
2905+
(set_local $5
2906+
(get_global $~lib/builtins/f64.NaN)
2907+
)
2908+
(f64.ne
2909+
(get_local $5)
2910+
(get_local $5)
2911+
)
2912+
)
2913+
(i32.const 1)
2914+
)
2915+
)
2916+
(block
2917+
(call $~lib/env/abort
2918+
(i32.const 0)
2919+
(i32.const 8)
2920+
(i32.const 324)
28332921
(i32.const 0)
28342922
)
28352923
(unreachable)
@@ -3112,7 +3200,7 @@
31123200
)
31133201
)
31143202
(drop
3115-
(block $~lib/builtins/isNaN<f64>|inlined.4 (result i32)
3203+
(block $~lib/builtins/isNaN<f64>|inlined.5 (result i32)
31163204
(set_local $5
31173205
(f64.const 1)
31183206
)

0 commit comments

Comments
 (0)