Trait half::vec::HalfFloatVecExt
source · pub trait HalfFloatVecExt: SealedHalfFloatVec {
// Required methods
fn reinterpret_into(self) -> Vec<u16>;
fn from_f32_slice(slice: &[f32]) -> Self;
fn from_f64_slice(slice: &[f64]) -> Self;
}
Expand description
Required Methods§
sourcefn reinterpret_into(self) -> Vec<u16>
fn reinterpret_into(self) -> Vec<u16>
Reinterprets a vector of f16
or bf16
numbers as a vector of u16
bits.
This is a zero-copy operation. The reinterpreted vector has the same memory location as
self
.
Examples
let float_buffer = vec![f16::from_f32(1.), f16::from_f32(2.), f16::from_f32(3.)];
let int_buffer = float_buffer.reinterpret_into();
assert_eq!(int_buffer, [f16::from_f32(1.).to_bits(), f16::from_f32(2.).to_bits(), f16::from_f32(3.).to_bits()]);
sourcefn from_f32_slice(slice: &[f32]) -> Self
fn from_f32_slice(slice: &[f32]) -> Self
Converts all of the elements of a [f32]
slice into a new f16
or bf16
vector.
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.
Examples
let float_values = [1., 2., 3., 4.];
let vec: Vec<f16> = Vec::from_f32_slice(&float_values);
assert_eq!(vec, vec![f16::from_f32(1.), f16::from_f32(2.), f16::from_f32(3.), f16::from_f32(4.)]);
sourcefn from_f64_slice(slice: &[f64]) -> Self
fn from_f64_slice(slice: &[f64]) -> Self
Converts all of the elements of a [f64]
slice into a new f16
or bf16
vector.
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.
Examples
let float_values = [1., 2., 3., 4.];
let vec: Vec<f16> = Vec::from_f64_slice(&float_values);
assert_eq!(vec, vec![f16::from_f64(1.), f16::from_f64(2.), f16::from_f64(3.), f16::from_f64(4.)]);