@@ -16,17 +16,14 @@ pub trait Float: Sized + Copy {
16
16
/// Returns the bitwidth of the significand
17
17
fn significand_bits ( ) -> u32 ;
18
18
19
- /// Returns `self` transmuted to `Self::Int `
20
- fn repr ( self ) -> Self :: Int ;
19
+ /// Returns a mask for the sign bit of `self `
20
+ fn sign_mask ( ) -> Self :: Int ;
21
21
22
- #[ cfg( test) ]
23
- /// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
24
- /// represented in multiple different ways. This methods returns `true` if two NaNs are
25
- /// compared.
26
- fn eq_repr ( self , rhs : Self ) -> bool ;
22
+ /// Returns a mask for the exponent portion of `self`
23
+ fn exponent_mask ( ) -> Self :: Int ;
27
24
28
- /// Returns a `Self::Int` transmuted back to `Self `
29
- fn from_repr ( a : Self :: Int ) -> Self ;
25
+ /// Returns a mask for the significand portion of `self `
26
+ fn significand_mask ( ) -> Self :: Int ;
30
27
31
28
/// Returns the sign bit of `self`
32
29
fn sign ( self ) -> bool ;
@@ -37,6 +34,21 @@ pub trait Float: Sized + Copy {
37
34
/// Returns the significand portion of `self`
38
35
fn significand ( self ) -> Self :: Int ;
39
36
37
+ /// Returns `self` transmuted to `Self::Int`
38
+ fn repr ( self ) -> Self :: Int ;
39
+
40
+ #[ cfg( test) ]
41
+ /// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
42
+ /// represented in multiple different ways. This method returns `true` if two NaNs are
43
+ /// compared.
44
+ fn eq_repr ( self , rhs : Self ) -> bool ;
45
+
46
+ /// Returns a `Self::Int` transmuted back to `Self`
47
+ fn from_repr ( a : Self :: Int ) -> Self ;
48
+
49
+ /// Constructs a `Self` from its parts
50
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self ;
51
+
40
52
/// Returns (normalized exponent, normalized significand)
41
53
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) ;
42
54
}
@@ -52,6 +64,15 @@ impl Float for f32 {
52
64
fn significand_bits ( ) -> u32 {
53
65
23
54
66
}
67
+ fn sign_mask ( ) -> Self :: Int {
68
+ 1 << ( Self :: bits ( ) - 1 )
69
+ }
70
+ fn exponent_mask ( ) -> Self :: Int {
71
+ ( ( 1 << Self :: exponent_bits ( ) ) - 1 ) << Self :: significand_bits ( )
72
+ }
73
+ fn significand_mask ( ) -> Self :: Int {
74
+ ( 1 << Self :: significand_bits ( ) ) - 1
75
+ }
55
76
fn repr ( self ) -> Self :: Int {
56
77
unsafe { mem:: transmute ( self ) }
57
78
}
@@ -66,15 +87,20 @@ impl Float for f32 {
66
87
fn from_repr ( a : Self :: Int ) -> Self {
67
88
unsafe { mem:: transmute ( a) }
68
89
}
90
+
91
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self {
92
+ Self :: from_repr ( ( ( sign as Self :: Int ) << ( Self :: bits ( ) - 1 ) ) |
93
+ exponent & Self :: exponent_mask ( ) |
94
+ significand & Self :: significand_mask ( ) )
95
+ }
69
96
fn sign ( self ) -> bool {
70
- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
97
+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
71
98
}
72
99
fn exponent ( self ) -> Self :: Int {
73
- self . repr ( ) >> Self :: significand_bits ( )
74
- & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
100
+ self . repr ( ) >> Self :: significand_bits ( ) & Self :: exponent_mask ( )
75
101
}
76
102
fn significand ( self ) -> Self :: Int {
77
- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
103
+ self . repr ( ) & Self :: significand_mask ( )
78
104
}
79
105
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
80
106
let shift = significand. leading_zeros ( )
@@ -93,6 +119,15 @@ impl Float for f64 {
93
119
fn significand_bits ( ) -> u32 {
94
120
52
95
121
}
122
+ fn sign_mask ( ) -> Self :: Int {
123
+ 1 << ( Self :: bits ( ) - 1 )
124
+ }
125
+ fn exponent_mask ( ) -> Self :: Int {
126
+ ( ( 1 << Self :: exponent_bits ( ) ) - 1 ) << Self :: significand_bits ( )
127
+ }
128
+ fn significand_mask ( ) -> Self :: Int {
129
+ ( 1 << Self :: significand_bits ( ) ) - 1
130
+ }
96
131
fn repr ( self ) -> Self :: Int {
97
132
unsafe { mem:: transmute ( self ) }
98
133
}
@@ -107,15 +142,19 @@ impl Float for f64 {
107
142
fn from_repr ( a : Self :: Int ) -> Self {
108
143
unsafe { mem:: transmute ( a) }
109
144
}
145
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self {
146
+ Self :: from_repr ( ( ( sign as Self :: Int ) << ( Self :: bits ( ) - 1 ) ) |
147
+ exponent & Self :: exponent_mask ( ) |
148
+ significand & Self :: significand_mask ( ) )
149
+ }
110
150
fn sign ( self ) -> bool {
111
- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
151
+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
112
152
}
113
153
fn exponent ( self ) -> Self :: Int {
114
- self . repr ( ) >> Self :: significand_bits ( )
115
- & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
154
+ self . repr ( ) >> Self :: significand_bits ( ) & Self :: exponent_mask ( )
116
155
}
117
156
fn significand ( self ) -> Self :: Int {
118
- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
157
+ self . repr ( ) & Self :: significand_mask ( )
119
158
}
120
159
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
121
160
let shift = significand. leading_zeros ( )
0 commit comments