@@ -10,6 +10,9 @@ pub trait Float: Sized + Copy {
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
@@ -25,6 +28,15 @@ pub trait Float: Sized + Copy {
25
28
/// Returns a `Self::Int` transmuted back to `Self`
26
29
fn from_repr ( a : Self :: Int ) -> Self ;
27
30
31
+ /// Returns the sign bit of `self`
32
+ fn sign ( self ) -> bool ;
33
+
34
+ /// Returns the exponent portion of `self`, shifted to the right
35
+ fn exponent ( self ) -> Self :: Int ;
36
+
37
+ /// Returns the significand portion of `self`
38
+ fn significand ( self ) -> Self :: Int ;
39
+
28
40
/// Returns (normalized exponent, normalized significand)
29
41
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) ;
30
42
}
@@ -34,6 +46,9 @@ impl Float for f32 {
34
46
fn bits ( ) -> u32 {
35
47
32
36
48
}
49
+ fn exponent_bits ( ) -> u32 {
50
+ 8
51
+ }
37
52
fn significand_bits ( ) -> u32 {
38
53
23
39
54
}
@@ -51,6 +66,16 @@ impl Float for f32 {
51
66
fn from_repr ( a : Self :: Int ) -> Self {
52
67
unsafe { mem:: transmute ( a) }
53
68
}
69
+ fn sign ( self ) -> bool {
70
+ ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
71
+ }
72
+ fn exponent ( self ) -> Self :: Int {
73
+ self . repr ( ) >> Self :: significand_bits ( )
74
+ & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
75
+ }
76
+ fn significand ( self ) -> Self :: Int {
77
+ self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
78
+ }
54
79
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
55
80
let shift = significand. leading_zeros ( )
56
81
. wrapping_sub ( ( 1u32 << Self :: significand_bits ( ) ) . leading_zeros ( ) ) ;
@@ -62,6 +87,9 @@ impl Float for f64 {
62
87
fn bits ( ) -> u32 {
63
88
64
64
89
}
90
+ fn exponent_bits ( ) -> u32 {
91
+ 11
92
+ }
65
93
fn significand_bits ( ) -> u32 {
66
94
52
67
95
}
@@ -79,6 +107,16 @@ impl Float for f64 {
79
107
fn from_repr ( a : Self :: Int ) -> Self {
80
108
unsafe { mem:: transmute ( a) }
81
109
}
110
+ fn sign ( self ) -> bool {
111
+ ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
112
+ }
113
+ fn exponent ( self ) -> Self :: Int {
114
+ self . repr ( ) >> Self :: significand_bits ( )
115
+ & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
116
+ }
117
+ fn significand ( self ) -> Self :: Int {
118
+ self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
119
+ }
82
120
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
83
121
let shift = significand. leading_zeros ( )
84
122
. wrapping_sub ( ( 1u64 << Self :: significand_bits ( ) ) . leading_zeros ( ) ) ;
0 commit comments