File tree 14 files changed +112
-0
lines changed
14 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,13 @@ matrix:
29
29
- env : TARGET=cargo-fmt
30
30
rust : beta
31
31
32
+ - env : TARGET=wasm32-unknown-unknown
33
+ rust : nightly
34
+ install : rustup target add $TARGET
35
+ script :
36
+ - cargo build --target $TARGET
37
+ - cargo build --no-default-features --target $TARGET
38
+
32
39
before_install : set -e
33
40
34
41
install :
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ version = "0.1.2"
12
12
[features ]
13
13
# only used to run our test suite
14
14
checked = []
15
+ default = [' stable' ]
16
+ stable = []
15
17
16
18
[workspace ]
17
19
members = [
Original file line number Diff line number Diff line change 11
11
12
12
#![ deny( warnings) ]
13
13
#![ no_std]
14
+ #![ cfg_attr(
15
+ all( target_arch = "wasm32" , not( feature = "stable" ) ) ,
16
+ feature( core_intrinsics)
17
+ ) ]
14
18
15
19
mod math;
16
20
Original file line number Diff line number Diff line change @@ -4,6 +4,14 @@ const TOINT: f64 = 1. / f64::EPSILON;
4
4
5
5
#[ inline]
6
6
pub fn ceil ( x : f64 ) -> f64 {
7
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
8
+ // `f64.ceil` native instruction, so we can leverage this for both code size
9
+ // and speed.
10
+ llvm_intrinsically_optimized ! {
11
+ #[ cfg( target_arch = "wasm32" ) ] {
12
+ return unsafe { :: core:: intrinsics:: ceilf64( x) }
13
+ }
14
+ }
7
15
let u: u64 = x. to_bits ( ) ;
8
16
let e: i64 = ( u >> 52 & 0x7ff ) as i64 ;
9
17
let y: f64 ;
Original file line number Diff line number Diff line change @@ -2,6 +2,14 @@ use core::f32;
2
2
3
3
#[ inline]
4
4
pub fn ceilf ( x : f32 ) -> f32 {
5
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
6
+ // `f32.ceil` native instruction, so we can leverage this for both code size
7
+ // and speed.
8
+ llvm_intrinsically_optimized ! {
9
+ #[ cfg( target_arch = "wasm32" ) ] {
10
+ return unsafe { :: core:: intrinsics:: ceilf32( x) }
11
+ }
12
+ }
5
13
let mut ui = x. to_bits ( ) ;
6
14
let e = ( ( ( ui >> 23 ) & 0xff ) - 0x7f ) as i32 ;
7
15
Original file line number Diff line number Diff line change @@ -2,5 +2,13 @@ use core::u64;
2
2
3
3
#[ inline]
4
4
pub fn fabs ( x : f64 ) -> f64 {
5
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
6
+ // `f64.abs` native instruction, so we can leverage this for both code size
7
+ // and speed.
8
+ llvm_intrinsically_optimized ! {
9
+ #[ cfg( target_arch = "wasm32" ) ] {
10
+ return unsafe { :: core:: intrinsics:: fabsf64( x) }
11
+ }
12
+ }
5
13
f64:: from_bits ( x. to_bits ( ) & ( u64:: MAX / 2 ) )
6
14
}
Original file line number Diff line number Diff line change 1
1
#[ inline]
2
2
pub fn fabsf ( x : f32 ) -> f32 {
3
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
4
+ // `f32.abs` native instruction, so we can leverage this for both code size
5
+ // and speed.
6
+ llvm_intrinsically_optimized ! {
7
+ #[ cfg( target_arch = "wasm32" ) ] {
8
+ return unsafe { :: core:: intrinsics:: fabsf32( x) }
9
+ }
10
+ }
3
11
f32:: from_bits ( x. to_bits ( ) & 0x7fffffff )
4
12
}
Original file line number Diff line number Diff line change @@ -4,6 +4,14 @@ const TOINT: f64 = 1. / f64::EPSILON;
4
4
5
5
#[ inline]
6
6
pub fn floor ( x : f64 ) -> f64 {
7
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
8
+ // `f64.floor` native instruction, so we can leverage this for both code size
9
+ // and speed.
10
+ llvm_intrinsically_optimized ! {
11
+ #[ cfg( target_arch = "wasm32" ) ] {
12
+ return unsafe { :: core:: intrinsics:: floorf64( x) }
13
+ }
14
+ }
7
15
let ui = x. to_bits ( ) ;
8
16
let e = ( ( ui >> 52 ) & 0x7ff ) as i32 ;
9
17
Original file line number Diff line number Diff line change @@ -2,6 +2,14 @@ use core::f32;
2
2
3
3
#[ inline]
4
4
pub fn floorf ( x : f32 ) -> f32 {
5
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
6
+ // `f32.floor` native instruction, so we can leverage this for both code size
7
+ // and speed.
8
+ llvm_intrinsically_optimized ! {
9
+ #[ cfg( target_arch = "wasm32" ) ] {
10
+ return unsafe { :: core:: intrinsics:: floorf32( x) }
11
+ }
12
+ }
5
13
let mut ui = x. to_bits ( ) ;
6
14
let e = ( ( ( ui >> 23 ) & 0xff ) - 0x7f ) as i32 ;
7
15
Original file line number Diff line number Diff line change @@ -58,6 +58,17 @@ macro_rules! i {
58
58
} ;
59
59
}
60
60
61
+ macro_rules! llvm_intrinsically_optimized {
62
+ ( #[ cfg( $( $clause: tt) * ) ] $e: expr) => {
63
+ #[ cfg( all( not( feature = "stable" ) , $( $clause) * ) ) ]
64
+ {
65
+ if true { // thwart the dead code lint
66
+ $e
67
+ }
68
+ }
69
+ } ;
70
+ }
71
+
61
72
// Public modules
62
73
mod acos;
63
74
mod acosf;
Original file line number Diff line number Diff line change @@ -82,6 +82,18 @@ const TINY: f64 = 1.0e-300;
82
82
83
83
#[ inline]
84
84
pub fn sqrt ( x : f64 ) -> f64 {
85
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
86
+ // `f64.sqrt` native instruction, so we can leverage this for both code size
87
+ // and speed.
88
+ llvm_intrinsically_optimized ! {
89
+ #[ cfg( target_arch = "wasm32" ) ] {
90
+ return if x < 0.0 {
91
+ f64 :: NAN
92
+ } else {
93
+ unsafe { :: core:: intrinsics:: sqrtf64( x) }
94
+ }
95
+ }
96
+ }
85
97
let mut z: f64 ;
86
98
let sign: u32 = 0x80000000 ;
87
99
let mut ix0: i32 ;
Original file line number Diff line number Diff line change @@ -17,6 +17,18 @@ const TINY: f32 = 1.0e-30;
17
17
18
18
#[ inline]
19
19
pub fn sqrtf ( x : f32 ) -> f32 {
20
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
21
+ // `f32.sqrt` native instruction, so we can leverage this for both code size
22
+ // and speed.
23
+ llvm_intrinsically_optimized ! {
24
+ #[ cfg( target_arch = "wasm32" ) ] {
25
+ return if x < 0.0 {
26
+ :: core:: f32 :: NAN
27
+ } else {
28
+ unsafe { :: core:: intrinsics:: sqrtf32( x) }
29
+ }
30
+ }
31
+ }
20
32
let mut z: f32 ;
21
33
let sign: i32 = 0x80000000u32 as i32 ;
22
34
let mut ix: i32 ;
Original file line number Diff line number Diff line change @@ -2,6 +2,14 @@ use core::f64;
2
2
3
3
#[ inline]
4
4
pub fn trunc ( x : f64 ) -> f64 {
5
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
6
+ // `f64.trunc` native instruction, so we can leverage this for both code size
7
+ // and speed.
8
+ llvm_intrinsically_optimized ! {
9
+ #[ cfg( target_arch = "wasm32" ) ] {
10
+ return unsafe { :: core:: intrinsics:: truncf64( x) }
11
+ }
12
+ }
5
13
let x1p120 = f64:: from_bits ( 0x4770000000000000 ) ; // 0x1p120f === 2 ^ 120
6
14
7
15
let mut i: u64 = x. to_bits ( ) ;
Original file line number Diff line number Diff line change @@ -2,6 +2,14 @@ use core::f32;
2
2
3
3
#[ inline]
4
4
pub fn truncf ( x : f32 ) -> f32 {
5
+ // On wasm32 we know that LLVM's intrinsic will compile to an optimized
6
+ // `f32.trunc` native instruction, so we can leverage this for both code size
7
+ // and speed.
8
+ llvm_intrinsically_optimized ! {
9
+ #[ cfg( target_arch = "wasm32" ) ] {
10
+ return unsafe { :: core:: intrinsics:: truncf32( x) }
11
+ }
12
+ }
5
13
let x1p120 = f32:: from_bits ( 0x7b800000 ) ; // 0x1p120f === 2 ^ 120
6
14
7
15
let mut i: u32 = x. to_bits ( ) ;
You can’t perform that action at this time.
0 commit comments