1
- use core:: { fmt, mem, ops} ;
1
+ use core:: ops:: { self , Neg } ;
2
+ use core:: { fmt, mem} ;
2
3
3
4
use super :: int_traits:: { CastFrom , Int , MinInt } ;
4
5
@@ -23,7 +24,9 @@ pub trait Float:
23
24
type Int : Int < OtherSign = Self :: SignedInt , Unsigned = Self :: Int > ;
24
25
25
26
/// A int of the same width as the float
26
- type SignedInt : Int + MinInt < OtherSign = Self :: Int , Unsigned = Self :: Int > ;
27
+ type SignedInt : Int
28
+ + MinInt < OtherSign = Self :: Int , Unsigned = Self :: Int >
29
+ + Neg < Output = Self :: SignedInt > ;
27
30
28
31
const ZERO : Self ;
29
32
const NEG_ZERO : Self ;
@@ -155,7 +158,6 @@ pub trait Float:
155
158
}
156
159
157
160
/// Access the associated `Int` type from a float (helper to avoid ambiguous associated types).
158
- #[ allow( dead_code) ]
159
161
pub type IntTy < F > = <F as Float >:: Int ;
160
162
161
163
macro_rules! float_impl {
@@ -355,3 +357,63 @@ mod tests {
355
357
assert_biteq ! ( f128:: from_parts( false , 0 , 1 ) , f128:: from_bits( 0x1 ) ) ;
356
358
}
357
359
}
360
+
361
+ /// Trait for floats twice the bit width of another integer.
362
+ #[ allow( unused) ]
363
+ pub trait DFloat : Float {
364
+ /// Float that is half the bit width of the floatthis trait is implemented for.
365
+ type H : HFloat < D = Self > ;
366
+
367
+ /// Narrow the float type.
368
+ fn narrow ( self ) -> Self :: H ;
369
+ }
370
+
371
+ /// Trait for floats half the bit width of another float.
372
+ #[ allow( unused) ]
373
+ pub trait HFloat : Float {
374
+ /// Float that is double the bit width of the float this trait is implemented for.
375
+ type D : DFloat < H = Self > ;
376
+
377
+ /// Widen the float type.
378
+ fn widen ( self ) -> Self :: D ;
379
+ }
380
+
381
+ macro_rules! impl_d_float {
382
+ ( $( $X: ident $D: ident) ,* ) => {
383
+ $(
384
+ impl DFloat for $D {
385
+ type H = $X;
386
+
387
+ fn narrow( self ) -> Self :: H {
388
+ self as $X
389
+ }
390
+ }
391
+ ) *
392
+ } ;
393
+ }
394
+
395
+ macro_rules! impl_h_float {
396
+ ( $( $H: ident $X: ident) ,* ) => {
397
+ $(
398
+ impl HFloat for $H {
399
+ type D = $X;
400
+
401
+ fn widen( self ) -> Self :: D {
402
+ self as $X
403
+ }
404
+ }
405
+ ) *
406
+ } ;
407
+ }
408
+
409
+ impl_d_float ! ( f32 f64 ) ;
410
+ #[ cfg( f16_enabled) ]
411
+ impl_d_float ! ( f16 f32 ) ;
412
+ #[ cfg( f128_enabled) ]
413
+ impl_d_float ! ( f64 f128) ;
414
+
415
+ impl_h_float ! ( f32 f64 ) ;
416
+ #[ cfg( f16_enabled) ]
417
+ impl_h_float ! ( f16 f32 ) ;
418
+ #[ cfg( f128_enabled) ]
419
+ impl_h_float ! ( f64 f128) ;
0 commit comments