Skip to content

Commit 377539d

Browse files
committed
Add support for float16 (half::f16) in hdf5-types
1 parent 94e882c commit 377539d

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

hdf5-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ edition.workspace = true
1616
[features]
1717
h5-alloc = []
1818
complex = ["num-complex"]
19+
f16 = ["half"]
1920

2021
[dependencies]
2122
ascii = "1.1"
2223
cfg-if = { workspace = true }
2324
hdf5-sys = { workspace = true }
2425
libc = { workspace = true }
2526
num-complex = { version = "0.4", optional = true, default-features = false }
27+
half = { version = "2.2", optional = true, default-features = false }
2628

2729
[dev-dependencies]
2830
quickcheck = { version = "1.0", default-features = false }

hdf5-types/src/dyn_value.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ impl From<DynInteger> for DynValue<'_> {
117117
#[derive(Copy, Clone, PartialEq)]
118118
pub enum DynScalar {
119119
Integer(DynInteger),
120+
#[cfg(feature = "f16")]
121+
Float16(::half::f16),
120122
Float32(f32),
121123
Float64(f64),
122124
Boolean(bool),
@@ -126,6 +128,8 @@ unsafe impl DynClone for DynScalar {
126128
fn dyn_clone(&mut self, out: &mut [u8]) {
127129
match self {
128130
Self::Integer(x) => x.dyn_clone(out),
131+
#[cfg(feature = "f16")]
132+
Self::Float16(x) => write_raw(out, *x),
129133
Self::Float32(x) => write_raw(out, *x),
130134
Self::Float64(x) => write_raw(out, *x),
131135
Self::Boolean(x) => write_raw(out, *x),
@@ -137,6 +141,8 @@ impl Debug for DynScalar {
137141
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138142
match self {
139143
Self::Integer(x) => Debug::fmt(&x, f),
144+
#[cfg(feature = "f16")]
145+
Self::Float16(x) => Debug::fmt(&x, f),
140146
Self::Float32(x) => Debug::fmt(&x, f),
141147
Self::Float64(x) => Debug::fmt(&x, f),
142148
Self::Boolean(x) => Debug::fmt(&x, f),
@@ -637,6 +643,8 @@ impl<'a> DynValue<'a> {
637643

638644
match tp {
639645
Integer(size) | Unsigned(size) => DynInteger::read(buf, true, *size).into(),
646+
#[cfg(feature = "f16")]
647+
Float(FloatSize::U2) => DynScalar::Float16(read_raw(buf)).into(),
640648
Float(FloatSize::U4) => DynScalar::Float32(read_raw(buf)).into(),
641649
Float(FloatSize::U8) => DynScalar::Float64(read_raw(buf)).into(),
642650
Boolean => DynScalar::Boolean(read_raw(buf)).into(),

hdf5-types/src/h5type.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,24 @@ impl IntSize {
4040

4141
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
4242
pub enum FloatSize {
43+
#[cfg(feature = "f16")]
44+
U2 = 2,
4345
U4 = 4,
4446
U8 = 8,
4547
}
4648

4749
impl FloatSize {
4850
pub const fn from_int(size: usize) -> Option<Self> {
49-
if size == 4 {
50-
Some(Self::U4)
51-
} else if size == 8 {
52-
Some(Self::U8)
53-
} else {
54-
None
51+
#[cfg(feature = "f16")]
52+
{
53+
if size == 2 {
54+
return Some(Self::U2);
55+
}
56+
}
57+
match size {
58+
4 => Some(Self::U4),
59+
8 => Some(Self::U8),
60+
_ => None,
5561
}
5662
}
5763
}
@@ -167,6 +173,8 @@ impl Display for TypeDescriptor {
167173
TypeDescriptor::Unsigned(IntSize::U2) => write!(f, "uint16"),
168174
TypeDescriptor::Unsigned(IntSize::U4) => write!(f, "uint32"),
169175
TypeDescriptor::Unsigned(IntSize::U8) => write!(f, "uint64"),
176+
#[cfg(feature = "f16")]
177+
TypeDescriptor::Float(FloatSize::U2) => write!(f, "float16"),
170178
TypeDescriptor::Float(FloatSize::U4) => write!(f, "float32"),
171179
TypeDescriptor::Float(FloatSize::U8) => write!(f, "float64"),
172180
TypeDescriptor::Boolean => write!(f, "bool"),
@@ -251,6 +259,8 @@ impl_h5type!(u8, Unsigned, IntSize::U1);
251259
impl_h5type!(u16, Unsigned, IntSize::U2);
252260
impl_h5type!(u32, Unsigned, IntSize::U4);
253261
impl_h5type!(u64, Unsigned, IntSize::U8);
262+
#[cfg(feature = "f16")]
263+
impl_h5type!(::half::f16, Float, FloatSize::U2);
254264
impl_h5type!(f32, Float, FloatSize::U4);
255265
impl_h5type!(f64, Float, FloatSize::U8);
256266

0 commit comments

Comments
 (0)