Skip to content

Commit af9de04

Browse files
committed
Add top-level "f16" feature + add datatype support
1 parent 377539d commit af9de04

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

hdf5/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ lzf = ["dep:lzf-sys", "dep:errno"]
2020
blosc = ["dep:blosc-sys"]
2121
static = ["hdf5-sys/static"]
2222
zlib = ["hdf5-sys/zlib"]
23+
f16 = ["hdf5-types/f16"]
2324
# The features with version numbers such as 1.10.3, 1.12.0 are metafeatures
2425
# and is only available when the HDF5 library is at least this version.
2526
# Features have_direct and have_parallel are also metafeatures and dependent

hdf5/src/hl/datatype.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ impl Datatype {
327327
Ok(string_id)
328328
}
329329

330+
#[cfg(feature = "f16")]
331+
unsafe fn f16_type() -> Result<hid_t> {
332+
use hdf5_sys::h5t::{H5Tset_ebias, H5Tset_fields};
333+
let f16_id = be_le!(H5T_IEEE_F32BE, H5T_IEEE_F32LE);
334+
h5try!(H5Tset_fields(f16_id, 15, 10, 5, 0, 10)); // cf. h5py/h5py#339
335+
h5try!(H5Tset_size(f16_id, 2));
336+
h5try!(H5Tset_ebias(f16_id, 15));
337+
Ok(f16_id)
338+
}
339+
330340
let datatype_id: Result<_> = h5lock!({
331341
match *desc {
332342
TD::Integer(size) => Ok(match size {
@@ -342,6 +352,8 @@ impl Datatype {
342352
IntSize::U8 => be_le!(H5T_STD_U64BE, H5T_STD_U64LE),
343353
}),
344354
TD::Float(size) => Ok(match size {
355+
#[cfg(feature = "f16")]
356+
FloatSize::U2 => f16_type()?,
345357
FloatSize::U4 => be_le!(H5T_IEEE_F32BE, H5T_IEEE_F32LE),
346358
FloatSize::U8 => be_le!(H5T_IEEE_I16BE, H5T_IEEE_F64LE),
347359
}),

tests/test_datatypes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub fn test_datatype_roundtrip() {
2626
check_roundtrip!(u16, TD::Unsigned(IntSize::U2));
2727
check_roundtrip!(u32, TD::Unsigned(IntSize::U4));
2828
check_roundtrip!(u64, TD::Unsigned(IntSize::U8));
29+
#[cfg(feature = "f16")]
30+
check_roundtrip!(::half::f16, TD::Float(FloatSize::U2));
2931
check_roundtrip!(f32, TD::Float(FloatSize::U4));
3032
check_roundtrip!(f64, TD::Float(FloatSize::U8));
3133
check_roundtrip!(bool, TD::Boolean);

0 commit comments

Comments
 (0)