@@ -42,6 +42,7 @@ function R(z: f64): f64 { // Rational approximation of (asin(x)-x)/x^3
4242 return p / q ;
4343}
4444
45+ /** @internal */
4546// @ts -ignore: decorator
4647@inline
4748function 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)
5253 return NativeMath . exp ( x - kln2 ) * scale * scale ;
5354}
5455
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+
5577// @ts -ignore: decorator
5678@lazy
5779var random_seeded = false ;
@@ -398,9 +420,7 @@ export namespace NativeMath {
398420 * For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
399421 * our float-point arguments before actual convertion to integers.
400422 */
401- return builtin_clz (
402- < i32 > < i64 > ( x - 4294967296 * builtin_floor ( x * ( 1.0 / 4294967296 ) ) )
403- ) ;
423+ return builtin_clz ( dtoi32 ( x ) ) ;
404424 }
405425
406426 export function cos ( x : f64 ) : f64 { // TODO
@@ -599,11 +619,7 @@ export namespace NativeMath {
599619 * our float-point arguments before actual convertion to integers.
600620 */
601621 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 ) ;
607623 }
608624
609625 export function log ( x : f64 ) : f64 { // see: musl/src/math/log.c and SUN COPYRIGHT NOTICE above
@@ -1700,9 +1716,7 @@ export namespace NativeMathf {
17001716
17011717 export function clz32 ( x : f32 ) : f32 {
17021718 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 ) ) ;
17061720 }
17071721
17081722 export function cos ( x : f32 ) : f32 { // see: musl/src/math/cosf.c
@@ -1927,11 +1941,7 @@ export namespace NativeMathf {
19271941 * our float-point arguments before actual convertion to integers.
19281942 */
19291943 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 ) ) ;
19351945 }
19361946
19371947 export function log ( x : f32 ) : f32 { // see: musl/src/math/logf.c and SUN COPYRIGHT NOTICE above
0 commit comments