pub trait HalfFloatSliceExt: SealedHalfFloatSlice {
// Required methods
fn reinterpret_cast(&self) -> &[u16];
fn reinterpret_cast_mut(&mut self) -> &mut [u16];
fn convert_from_f32_slice(&mut self, src: &[f32]);
fn convert_from_f64_slice(&mut self, src: &[f64]);
fn convert_to_f32_slice(&self, dst: &mut [f32]);
fn convert_to_f64_slice(&self, dst: &mut [f64]);
}
target_arch="spirv"
only.Expand description
Extensions to [f16]
and [bf16]
slices to support conversion and reinterpret operations.
This trait is sealed and cannot be implemented outside of this crate.
Required Methods§
Sourcefn reinterpret_cast(&self) -> &[u16]
fn reinterpret_cast(&self) -> &[u16]
Reinterprets a slice of [f16
] or bf16
numbers as a slice of u16
bits.
This is a zero-copy operation. The reinterpreted slice has the same lifetime and memory
location as self
.
§Examples
let float_buffer = [f16::from_f32(1.), f16::from_f32(2.), f16::from_f32(3.)];
let int_buffer = float_buffer.reinterpret_cast();
assert_eq!(int_buffer, [float_buffer[0].to_bits(), float_buffer[1].to_bits(), float_buffer[2].to_bits()]);
Sourcefn reinterpret_cast_mut(&mut self) -> &mut [u16]
fn reinterpret_cast_mut(&mut self) -> &mut [u16]
Reinterprets a mutable slice of [f16
] or bf16
numbers as a mutable slice of u16
.
bits
This is a zero-copy operation. The transmuted slice has the same lifetime as the original,
which prevents mutating self
as long as the returned &mut [u16]
is borrowed.
§Examples
let mut float_buffer = [f16::from_f32(1.), f16::from_f32(2.), f16::from_f32(3.)];
{
let int_buffer = float_buffer.reinterpret_cast_mut();
assert_eq!(int_buffer, [f16::from_f32(1.).to_bits(), f16::from_f32(2.).to_bits(), f16::from_f32(3.).to_bits()]);
// Mutating the u16 slice will mutating the original
int_buffer[0] = 0;
}
// Note that we need to drop int_buffer before using float_buffer again or we will get a borrow error.
assert_eq!(float_buffer, [f16::from_f32(0.), f16::from_f32(2.), f16::from_f32(3.)]);
Sourcefn convert_from_f32_slice(&mut self, src: &[f32])
fn convert_from_f32_slice(&mut self, src: &[f32])
Converts all of the elements of a [f32]
slice into [f16
] or bf16
values in self
.
The length of src
must be the same as self
.
The conversion operation is vectorized over the slice, meaning the conversion may be more efficient than converting individual elements on some hardware that supports SIMD conversions. See crate documentation for more information on hardware conversion support.
§Panics
This function will panic if the two slices have different lengths.
§Examples
// Initialize an empty buffer
let mut buffer = [0u16; 4];
let buffer = buffer.reinterpret_cast_mut::<f16>();
let float_values = [1., 2., 3., 4.];
// Now convert
buffer.convert_from_f32_slice(&float_values);
assert_eq!(buffer, [f16::from_f32(1.), f16::from_f32(2.), f16::from_f32(3.), f16::from_f32(4.)]);
Sourcefn convert_from_f64_slice(&mut self, src: &[f64])
fn convert_from_f64_slice(&mut self, src: &[f64])
Converts all of the elements of a [f64]
slice into [f16
] or bf16
values in self
.
The length of src
must be the same as self
.
The conversion operation is vectorized over the slice, meaning the conversion may be more efficient than converting individual elements on some hardware that supports SIMD conversions. See crate documentation for more information on hardware conversion support.
§Panics
This function will panic if the two slices have different lengths.
§Examples
// Initialize an empty buffer
let mut buffer = [0u16; 4];
let buffer = buffer.reinterpret_cast_mut::<f16>();
let float_values = [1., 2., 3., 4.];
// Now convert
buffer.convert_from_f64_slice(&float_values);
assert_eq!(buffer, [f16::from_f64(1.), f16::from_f64(2.), f16::from_f64(3.), f16::from_f64(4.)]);
Sourcefn convert_to_f32_slice(&self, dst: &mut [f32])
fn convert_to_f32_slice(&self, dst: &mut [f32])
Converts all of the [f16
] or bf16
elements of self
into f32
values in dst
.
The length of src
must be the same as self
.
The conversion operation is vectorized over the slice, meaning the conversion may be more efficient than converting individual elements on some hardware that supports SIMD conversions. See crate documentation for more information on hardware conversion support.
§Panics
This function will panic if the two slices have different lengths.
§Examples
// Initialize an empty buffer
let mut buffer = [0f32; 4];
let half_values = [f16::from_f32(1.), f16::from_f32(2.), f16::from_f32(3.), f16::from_f32(4.)];
// Now convert
half_values.convert_to_f32_slice(&mut buffer);
assert_eq!(buffer, [1., 2., 3., 4.]);
Sourcefn convert_to_f64_slice(&self, dst: &mut [f64])
fn convert_to_f64_slice(&self, dst: &mut [f64])
Converts all of the [f16
] or bf16
elements of self
into f64
values in dst
.
The length of src
must be the same as self
.
The conversion operation is vectorized over the slice, meaning the conversion may be more efficient than converting individual elements on some hardware that supports SIMD conversions. See crate documentation for more information on hardware conversion support.
§Panics
This function will panic if the two slices have different lengths.
§Examples
// Initialize an empty buffer
let mut buffer = [0f64; 4];
let half_values = [f16::from_f64(1.), f16::from_f64(2.), f16::from_f64(3.), f16::from_f64(4.)];
// Now convert
half_values.convert_to_f64_slice(&mut buffer);
assert_eq!(buffer, [1., 2., 3., 4.]);