@@ -16,17 +16,14 @@ pub trait Float: Sized + Copy {
1616 /// Returns the bitwidth of the significand
1717 fn significand_bits ( ) -> u32 ;
1818
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 ;
2121
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 ;
2724
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 ;
3027
3128 /// Returns the sign bit of `self`
3229 fn sign ( self ) -> bool ;
@@ -37,6 +34,21 @@ pub trait Float: Sized + Copy {
3734 /// Returns the significand portion of `self`
3835 fn significand ( self ) -> Self :: Int ;
3936
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+
4052 /// Returns (normalized exponent, normalized significand)
4153 fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) ;
4254}
@@ -52,6 +64,15 @@ impl Float for f32 {
5264 fn significand_bits ( ) -> u32 {
5365 23
5466 }
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+ }
5576 fn repr ( self ) -> Self :: Int {
5677 unsafe { mem:: transmute ( self ) }
5778 }
@@ -66,15 +87,20 @@ impl Float for f32 {
6687 fn from_repr ( a : Self :: Int ) -> Self {
6788 unsafe { mem:: transmute ( a) }
6889 }
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+ }
6996 fn sign ( self ) -> bool {
70- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
97+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
7198 }
7299 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 ( )
75101 }
76102 fn significand ( self ) -> Self :: Int {
77- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
103+ self . repr ( ) & Self :: significand_mask ( )
78104 }
79105 fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
80106 let shift = significand. leading_zeros ( )
@@ -93,6 +119,15 @@ impl Float for f64 {
93119 fn significand_bits ( ) -> u32 {
94120 52
95121 }
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+ }
96131 fn repr ( self ) -> Self :: Int {
97132 unsafe { mem:: transmute ( self ) }
98133 }
@@ -107,15 +142,19 @@ impl Float for f64 {
107142 fn from_repr ( a : Self :: Int ) -> Self {
108143 unsafe { mem:: transmute ( a) }
109144 }
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+ }
110150 fn sign ( self ) -> bool {
111- ( self . repr ( ) & 1 << Self :: bits ( ) ) != 0
151+ ( self . repr ( ) & Self :: sign_mask ( ) ) != 0
112152 }
113153 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 ( )
116155 }
117156 fn significand ( self ) -> Self :: Int {
118- self . repr ( ) & ( ( 1 << Self :: significand_bits ( ) ) - 1 )
157+ self . repr ( ) & Self :: significand_mask ( )
119158 }
120159 fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) {
121160 let shift = significand. leading_zeros ( )
0 commit comments