@@ -35,6 +35,7 @@ pub(crate) mod _struct {
35
35
} ;
36
36
use crate :: slots:: PyIter ;
37
37
use crate :: VirtualMachine ;
38
+ use half:: f16;
38
39
39
40
#[ derive( Debug , Copy , Clone , PartialEq ) ]
40
41
enum Endianness {
@@ -72,7 +73,7 @@ pub(crate) mod _struct {
72
73
LongLong = b'q' ,
73
74
ULongLong = b'Q' ,
74
75
Bool = b'?' ,
75
- // TODO: Half = 'e',
76
+ Half = b 'e',
76
77
Float = b'f' ,
77
78
Double = b'd' ,
78
79
VoidP = b'P' ,
@@ -149,6 +150,7 @@ pub(crate) mod _struct {
149
150
LongLong => nonnative_info!( i64 , $end) ,
150
151
ULongLong => nonnative_info!( u64 , $end) ,
151
152
Bool => nonnative_info!( bool , $end) ,
153
+ Half => nonnative_info!( f16, $end) ,
152
154
Float => nonnative_info!( f32 , $end) ,
153
155
Double => nonnative_info!( f64 , $end) ,
154
156
_ => unreachable!( ) , // size_t or void*
@@ -182,6 +184,7 @@ pub(crate) mod _struct {
182
184
LongLong => native_info ! ( raw:: c_longlong) ,
183
185
ULongLong => native_info ! ( raw:: c_ulonglong) ,
184
186
Bool => native_info ! ( bool ) ,
187
+ Half => native_info ! ( f16) ,
185
188
Float => native_info ! ( raw:: c_float) ,
186
189
Double => native_info ! ( raw:: c_double) ,
187
190
VoidP => native_info ! ( * mut raw:: c_void) ,
@@ -623,6 +626,29 @@ pub(crate) mod _struct {
623
626
make_pack_float ! ( f32 ) ;
624
627
make_pack_float ! ( f64 ) ;
625
628
629
+ impl Packable for f16 {
630
+ fn pack < E : ByteOrder > (
631
+ vm : & VirtualMachine ,
632
+ arg : PyObjectRef ,
633
+ data : & mut [ u8 ] ,
634
+ ) -> PyResult < ( ) > {
635
+ let f_64 = float:: try_float ( & arg, vm) ?;
636
+ let f_16 = f16:: from_f64 ( f_64) ;
637
+ if f_16. is_infinite ( ) != f_64. is_infinite ( ) {
638
+ return Err (
639
+ vm. new_overflow_error ( "float too large to pack with e format" . to_owned ( ) )
640
+ ) ;
641
+ }
642
+ f_16. to_bits ( ) . pack_int :: < E > ( data) ;
643
+ Ok ( ( ) )
644
+ }
645
+
646
+ fn unpack < E : ByteOrder > ( vm : & VirtualMachine , rdr : & [ u8 ] ) -> PyObjectRef {
647
+ let i = PackInt :: unpack_int :: < E > ( rdr) ;
648
+ f16:: from_bits ( i) . to_f64 ( ) . into_pyobject ( vm)
649
+ }
650
+ }
651
+
626
652
impl Packable for * mut raw:: c_void {
627
653
fn pack < E : ByteOrder > (
628
654
vm : & VirtualMachine ,
0 commit comments