@@ -42,6 +42,7 @@ function R(z: f64): f64 { // Rational approximation of (asin(x)-x)/x^3
42
42
return p / q ;
43
43
}
44
44
45
+ /** @internal */
45
46
// @ts -ignore: decorator
46
47
@inline
47
48
function expo2 ( x : f64 ) : f64 { // exp(x)/2 for x >= log(DBL_MAX)
@@ -52,6 +53,27 @@ function expo2(x: f64): f64 { // exp(x)/2 for x >= log(DBL_MAX)
52
53
return NativeMath . exp ( x - kln2 ) * scale * scale ;
53
54
}
54
55
56
+ /** @internal */
57
+ function dtoi32 ( x : f64 ) : i32 {
58
+ if ( ASC_SHRINK_LEVEL > 0 ) {
59
+ const inv32 = 1.0 / 4294967296 ;
60
+ return < i32 > < i64 > ( x - 4294967296 * floor ( x * inv32 ) ) ;
61
+ } else {
62
+ let result = 0 ;
63
+ let u = reinterpret < u64 > ( x ) ;
64
+ let e = ( u >> 52 ) & 0x7ff ;
65
+ if ( e <= 1023 + 30 ) {
66
+ result = < i32 > x ;
67
+ } else if ( e <= 1023 + 30 + 53 ) {
68
+ let v = ( u & ( ( < u64 > 1 << 52 ) - 1 ) ) | ( < u64 > 1 << 52 ) ;
69
+ v = v << e - 1023 - 52 + 32 ;
70
+ result = < i32 > ( v >> 32 ) ;
71
+ result = select < i32 > ( - result , result , u >> 63 ) ;
72
+ }
73
+ return result ;
74
+ }
75
+ }
76
+
55
77
// @ts -ignore: decorator
56
78
@lazy
57
79
var random_seeded = false ;
@@ -398,9 +420,7 @@ export namespace NativeMath {
398
420
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
399
421
* our float-point arguments before actual convertion to integers.
400
422
*/
401
- return builtin_clz (
402
- < i32 > < i64 > ( x - 4294967296 * builtin_floor ( x * ( 1.0 / 4294967296 ) ) )
403
- ) ;
423
+ return builtin_clz ( dtoi32 ( x ) ) ;
404
424
}
405
425
406
426
export function cos ( x : f64 ) : f64 { // TODO
@@ -599,11 +619,7 @@ export namespace NativeMath {
599
619
* our float-point arguments before actual convertion to integers.
600
620
*/
601
621
if ( ! isFinite ( x + y ) ) return 0 ;
602
- const inv32 = 1.0 / 4294967296 ;
603
- return (
604
- < i32 > < i64 > ( x - 4294967296 * builtin_floor ( x * inv32 ) ) *
605
- < i32 > < i64 > ( y - 4294967296 * builtin_floor ( y * inv32 ) )
606
- ) ;
622
+ return dtoi32 ( x ) * dtoi32 ( y ) ;
607
623
}
608
624
609
625
export function log ( x : f64 ) : f64 { // see: musl/src/math/log.c and SUN COPYRIGHT NOTICE above
@@ -1700,9 +1716,7 @@ export namespace NativeMathf {
1700
1716
1701
1717
export function clz32 ( x : f32 ) : f32 {
1702
1718
if ( ! isFinite ( x ) ) return 32 ;
1703
- return < f32 > builtin_clz (
1704
- < i32 > < i64 > ( x - 4294967296 * builtin_floor ( x * ( 1.0 / 4294967296 ) ) )
1705
- ) ;
1719
+ return < f32 > builtin_clz ( dtoi32 ( x ) ) ;
1706
1720
}
1707
1721
1708
1722
export function cos ( x : f32 ) : f32 { // see: musl/src/math/cosf.c
@@ -1927,11 +1941,7 @@ export namespace NativeMathf {
1927
1941
* our float-point arguments before actual convertion to integers.
1928
1942
*/
1929
1943
if ( ! isFinite ( x + y ) ) return 0 ;
1930
- const inv32 = 1.0 / 4294967296 ;
1931
- return < f32 > (
1932
- < i32 > < i64 > ( x - 4294967296 * builtin_floor ( x * inv32 ) ) *
1933
- < i32 > < i64 > ( y - 4294967296 * builtin_floor ( y * inv32 ) )
1934
- ) ;
1944
+ return < f32 > ( dtoi32 ( x ) * dtoi32 ( y ) ) ;
1935
1945
}
1936
1946
1937
1947
export function log ( x : f32 ) : f32 { // see: musl/src/math/logf.c and SUN COPYRIGHT NOTICE above
0 commit comments