@@ -10,6 +10,9 @@ pub trait Float: Sized {
10
10
/// Returns the bitwidth of the float type
11
11
fn bits ( ) -> u32 ;
12
12
13
+ /// Returns the bitwidth of the exponent
14
+ fn exponent_bits ( ) -> u32 ;
15
+
13
16
/// Returns the bitwidth of the significand
14
17
fn significand_bits ( ) -> u32 ;
15
18
@@ -19,6 +22,15 @@ pub trait Float: Sized {
19
22
/// Returns a `Self::Int` transmuted back to `Self`
20
23
fn from_repr ( a : Self :: Int ) -> Self ;
21
24
25
+ /// Returns the sign bit of `self`
26
+ fn sign ( self ) -> bool ;
27
+
28
+ /// Returns the exponent portion of `self`, shifted to the right
29
+ fn exponent ( self ) -> Self :: Int ;
30
+
31
+ /// Returns the significand portion of `self`
32
+ fn significand ( self ) -> Self :: Int ;
33
+
22
34
/// Returns (normalized exponent, normalized significand)
23
35
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) ;
24
36
}
@@ -28,6 +40,9 @@ impl Float for f32 {
28
40
fn bits ( ) -> u32 {
29
41
32
30
42
}
43
+ fn exponent_bits ( ) -> u32 {
44
+ 8
45
+ }
31
46
fn significand_bits ( ) -> u32 {
32
47
23
33
48
}
@@ -37,6 +52,16 @@ impl Float for f32 {
37
52
fn from_repr ( a : Self :: Int ) -> Self {
38
53
unsafe { mem:: transmute ( a) }
39
54
}
55
+ fn sign ( self ) -> bool {
56
+ ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
57
+ }
58
+ fn exponent ( self ) -> Self :: Int {
59
+ self . repr ( ) >> Self :: significand_bits ( )
60
+ & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
61
+ }
62
+ fn significand ( self ) -> Self :: Int {
63
+ self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
64
+ }
40
65
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
41
66
let shift = significand. leading_zeros ( )
42
67
. wrapping_sub ( ( 1u32 << Self :: significand_bits ( ) ) . leading_zeros ( ) ) ;
@@ -48,6 +73,9 @@ impl Float for f64 {
48
73
fn bits ( ) -> u32 {
49
74
64
50
75
}
76
+ fn exponent_bits ( ) -> u32 {
77
+ 11
78
+ }
51
79
fn significand_bits ( ) -> u32 {
52
80
52
53
81
}
@@ -57,6 +85,16 @@ impl Float for f64 {
57
85
fn from_repr ( a : Self :: Int ) -> Self {
58
86
unsafe { mem:: transmute ( a) }
59
87
}
88
+ fn sign ( self ) -> bool {
89
+ ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
90
+ }
91
+ fn exponent ( self ) -> Self :: Int {
92
+ self . repr ( ) >> Self :: significand_bits ( )
93
+ & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
94
+ }
95
+ fn significand ( self ) -> Self :: Int {
96
+ self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
97
+ }
60
98
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
61
99
let shift = significand. leading_zeros ( )
62
100
. wrapping_sub ( ( 1u64 << Self :: significand_bits ( ) ) . leading_zeros ( ) ) ;
0 commit comments