@@ -16,11 +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
- /// Returns a `Self::Int` transmuted back to `Self`
23
- fn from_repr ( a : Self :: Int ) -> Self ;
22
+ /// Returns a mask for the exponent portion of `self`
23
+ fn exponent_mask ( ) -> Self :: Int ;
24
+
25
+ /// Returns a mask for the significand portion of `self`
26
+ fn significand_mask ( ) -> Self :: Int ;
24
27
25
28
/// Returns the sign bit of `self`
26
29
fn sign ( self ) -> bool ;
@@ -31,6 +34,15 @@ pub trait Float: Sized {
31
34
/// Returns the significand portion of `self`
32
35
fn significand ( self ) -> Self :: Int ;
33
36
37
+ /// Returns `self` transmuted to `Self::Int`
38
+ fn repr ( self ) -> Self :: Int ;
39
+
40
+ /// Returns a `Self::Int` transmuted back to `Self`
41
+ fn from_repr ( a : Self :: Int ) -> Self ;
42
+
43
+ /// Constructs a `Self` from its parts
44
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self ;
45
+
34
46
/// Returns (normalized exponent, normalized significand)
35
47
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) ;
36
48
}
@@ -46,21 +58,34 @@ impl Float for f32 {
46
58
fn significand_bits ( ) -> u32 {
47
59
23
48
60
}
61
+ fn sign_mask ( ) -> Self :: Int {
62
+ 1 << ( Self :: bits ( ) - 1 )
63
+ }
64
+ fn exponent_mask ( ) -> Self :: Int {
65
+ ( ( 1 << Self :: exponent_bits ( ) ) - 1 ) << Self :: significand_bits ( )
66
+ }
67
+ fn significand_mask ( ) -> Self :: Int {
68
+ ( 1 << Self :: significand_bits ( ) ) - 1
69
+ }
49
70
fn repr ( self ) -> Self :: Int {
50
71
unsafe { mem:: transmute ( self ) }
51
72
}
52
73
fn from_repr ( a : Self :: Int ) -> Self {
53
74
unsafe { mem:: transmute ( a) }
54
75
}
76
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self {
77
+ Self :: from_repr ( ( ( sign as Self :: Int ) << ( Self :: bits ( ) - 1 ) ) |
78
+ exponent & Self :: exponent_mask ( ) |
79
+ significand & Self :: significand_mask ( ) )
80
+ }
55
81
fn sign ( self ) -> bool {
56
- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
82
+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
57
83
}
58
84
fn exponent ( self ) -> Self :: Int {
59
- self . repr ( ) >> Self :: significand_bits ( )
60
- & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
85
+ self . repr ( ) >> Self :: significand_bits ( ) & Self :: exponent_mask ( )
61
86
}
62
87
fn significand ( self ) -> Self :: Int {
63
- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
88
+ self . repr ( ) & Self :: significand_mask ( )
64
89
}
65
90
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
66
91
let shift = significand. leading_zeros ( )
@@ -79,21 +104,34 @@ impl Float for f64 {
79
104
fn significand_bits ( ) -> u32 {
80
105
52
81
106
}
107
+ fn sign_mask ( ) -> Self :: Int {
108
+ 1 << ( Self :: bits ( ) - 1 )
109
+ }
110
+ fn exponent_mask ( ) -> Self :: Int {
111
+ ( ( 1 << Self :: exponent_bits ( ) ) - 1 ) << Self :: significand_bits ( )
112
+ }
113
+ fn significand_mask ( ) -> Self :: Int {
114
+ ( 1 << Self :: significand_bits ( ) ) - 1
115
+ }
82
116
fn repr ( self ) -> Self :: Int {
83
117
unsafe { mem:: transmute ( self ) }
84
118
}
85
119
fn from_repr ( a : Self :: Int ) -> Self {
86
120
unsafe { mem:: transmute ( a) }
87
121
}
122
+ fn from_parts ( sign : bool , exponent : Self :: Int , significand : Self :: Int ) -> Self {
123
+ Self :: from_repr ( ( ( sign as Self :: Int ) << ( Self :: bits ( ) - 1 ) ) |
124
+ exponent & Self :: exponent_mask ( ) |
125
+ significand & Self :: significand_mask ( ) )
126
+ }
88
127
fn sign ( self ) -> bool {
89
- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
128
+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
90
129
}
91
130
fn exponent ( self ) -> Self :: Int {
92
- self . repr ( ) >> Self :: significand_bits ( )
93
- & ( ( 1 << Self :: exponent_bits ( ) ) - 1 )
131
+ self . repr ( ) >> Self :: significand_bits ( ) & Self :: exponent_mask ( )
94
132
}
95
133
fn significand ( self ) -> Self :: Int {
96
- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
134
+ self . repr ( ) & Self :: significand_mask ( )
97
135
}
98
136
fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
99
137
let shift = significand. leading_zeros ( )
0 commit comments