@@ -6,7 +6,7 @@ pub mod add;
6
6
pub trait Float : Sized {
7
7
/// A uint of the same with as the float
8
8
type Int ;
9
-
9
+
10
10
/// Returns the bitwidth of the float type
11
11
fn bits ( ) -> u32 ;
12
12
@@ -16,17 +16,14 @@ pub trait Float: Sized {
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 {
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 methods 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,29 +64,35 @@ 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
}
58
- #[ cfg( test) ]
59
- fn eq_repr ( self , rhs : Self ) -> bool {
60
- if self . is_nan ( ) && rhs. is_nan ( ) {
61
- true
62
- } else {
63
- self . repr ( ) == rhs. repr ( )
64
- }
65
- }
66
79
fn from_repr ( a : Self :: Int ) -> Self {
67
80
unsafe { mem:: transmute ( a) }
68
81
}
82
+
83
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self {
84
+ Self :: from_repr ( ( ( sign as Self :: Int ) << ( Self :: bits ( ) - 1 ) ) |
85
+ exponent & Self :: exponent_mask ( ) |
86
+ significand & Self :: significand_mask ( ) )
87
+ }
69
88
fn sign ( self ) -> bool {
70
- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
89
+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
71
90
}
72
91
fn exponent ( self ) -> Self :: Int {
73
- self . repr ( ) >> Self :: significand_bits ( )
74
- & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
92
+ self . repr ( ) >> Self :: significand_bits ( ) & Self :: exponent_mask ( )
75
93
}
76
94
fn significand ( self ) -> Self :: Int {
77
- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
95
+ self . repr ( ) & Self :: significand_mask ( )
78
96
}
79
97
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
80
98
let shift = significand. leading_zeros ( )
@@ -93,6 +111,15 @@ impl Float for f64 {
93
111
fn significand_bits ( ) -> u32 {
94
112
52
95
113
}
114
+ fn sign_mask ( ) -> Self :: Int {
115
+ 1 << ( Self :: bits ( ) - 1 )
116
+ }
117
+ fn exponent_mask ( ) -> Self :: Int {
118
+ ( ( 1 << Self :: exponent_bits ( ) ) - 1 ) << Self :: significand_bits ( )
119
+ }
120
+ fn significand_mask ( ) -> Self :: Int {
121
+ ( 1 << Self :: significand_bits ( ) ) - 1
122
+ }
96
123
fn repr ( self ) -> Self :: Int {
97
124
unsafe { mem:: transmute ( self ) }
98
125
}
@@ -107,15 +134,19 @@ impl Float for f64 {
107
134
fn from_repr ( a : Self :: Int ) -> Self {
108
135
unsafe { mem:: transmute ( a) }
109
136
}
137
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self {
138
+ Self :: from_repr ( ( ( sign as Self :: Int ) << ( Self :: bits ( ) - 1 ) ) |
139
+ exponent & Self :: exponent_mask ( ) |
140
+ significand & Self :: significand_mask ( ) )
141
+ }
110
142
fn sign ( self ) -> bool {
111
- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
143
+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
112
144
}
113
145
fn exponent ( self ) -> Self :: Int {
114
- self . repr ( ) >> Self :: significand_bits ( )
115
- & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
146
+ self . repr ( ) >> Self :: significand_bits ( ) & Self :: exponent_mask ( )
116
147
}
117
148
fn significand ( self ) -> Self :: Int {
118
- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
149
+ self . repr ( ) & Self :: significand_mask ( )
119
150
}
120
151
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
121
152
let shift = significand. leading_zeros ( )
0 commit comments