pub struct Vec4f { /* private fields */ }
Expand description
Represents a packed vector of 4 single-precision floating-point values. __m128
wrapper.
Implementations§
Source§impl Vec4f
impl Vec4f
Sourcepub fn new(v0: f32, v1: f32, v2: f32, v3: f32) -> Self
pub fn new(v0: f32, v1: f32, v2: f32, v3: f32) -> Self
Initializes elements of returned vector with given values.
§Example
assert_eq!(
Vec4f::new(1.0, 2.0, 3.0, 4.0),
[1.0, 2.0, 3.0, 4.0].into()
);
Sourcepub unsafe fn load_ptr_aligned(addr: *const [f32; 4]) -> Self
pub unsafe fn load_ptr_aligned(addr: *const [f32; 4]) -> Self
Loads vector from aligned array pointed by addr
.
§Safety
Like load
, requires addr
to be valid.
Unlike load
, requires addr
to be divisible by 16
, i.e. to be a 16
-bytes aligned address.
§Examples
#[repr(align(16))]
struct AlignedArray([f32; 4]);
let array = AlignedArray([42.0; 4]);
let vec = unsafe { Vec4f::load_ptr_aligned(&array.0) };
assert_eq!(vec, Vec4f::broadcast(42.0));
In the following example zeros
is aligned 2-bytes aligned. Therefore
zeros.as_ptr().byte_add(1)
is an odd address and hence not divisible by 16
.
let zeros = unsafe { std::mem::zeroed::<[u16; 10]>() };
unsafe { Vec4f::load_ptr_aligned(zeros.as_ptr().byte_add(1) as *const [f32; 4]) };
Sourcepub fn load(data: &[f32; 4]) -> Self
pub fn load(data: &[f32; 4]) -> Self
Loads values of returned vector from given data.
§Exmaple
assert_eq!(
Vec4f::new(1.0, 2.0, 3.0, 4.0),
Vec4f::load(&[1.0, 2.0, 3.0, 4.0])
);
Sourcepub fn load_checked(data: &[f32]) -> Self
pub fn load_checked(data: &[f32]) -> Self
Checks that data contains exactly four elements and loads them into vector.
§Panics
Panics if data.len()
isn’t 4
.
§Examples
assert_eq!(
Vec4f::load_checked(&[1.0, 2.0, 3.0, 4.0]),
Vec4f::new(1.0, 2.0, 3.0, 4.0)
);
Vec4f::load_checked(&[1.0, 2.0, 3.0]);
Vec4f::load_checked(&[1.0, 2.0, 3.0, 4.0, 5.0]);
Sourcepub fn load_prefix(data: &[f32]) -> Self
pub fn load_prefix(data: &[f32]) -> Self
Sourcepub fn load_partial(data: &[f32]) -> Self
pub fn load_partial(data: &[f32]) -> Self
Loads first 4 elements of data
if available otherwise initializes first elements of
returned vector with values of data
and rest elements with zeros.
§Example
let values = [1.0, 2.0, 3.0, 4.0, 5.0];
assert_eq!(
Vec4f::load_partial(&values),
Vec4f::from(&values[..4].try_into().unwrap())
);
assert_eq!(
Vec4f::load_partial(&values[..2]),
Vec4f::new(1.0, 2.0, 0.0, 0.0) // note zeros here
);
Sourcepub fn broadcast(value: f32) -> Self
pub fn broadcast(value: f32) -> Self
Returns vector with all its elements initialized with a given value
, i.e. broadcasts
value
to all elements of returned vector.
§Example
assert_eq!(
Vec4f::broadcast(42.0),
[42.0; 4].into()
);
Sourcepub unsafe fn store_ptr_aligned(&self, addr: *mut [f32; 4])
pub unsafe fn store_ptr_aligned(&self, addr: *mut [f32; 4])
Sourcepub unsafe fn store_ptr_non_temporal(&self, addr: *mut [f32; 4])
pub unsafe fn store_ptr_non_temporal(&self, addr: *mut [f32; 4])
Stores vector into aligned array at given address in uncached memory (non-temporal store).
This may be more efficient than store_ptr_aligned
if it is unlikely that stored data will
stay in cache until it is read again, for instance, when storing large blocks of memory.
§Safety
Has same requirements as store_ptr_aligned
: addr
must be valid and
divisible by 16
, i.e. to be a 16-bytes aligned address.
Sourcepub fn store_checked(&self, slice: &mut [f32])
pub fn store_checked(&self, slice: &mut [f32])
Checkes that slice
contains exactly four elements and store elements of vector there.
§Panics
Panics if slice.len()
isn’t 4
.
§Examples
let mut data = [-1.0; 4];
Vec4f::default().store_checked(&mut data);
assert_eq!(data, [0.0; 4]);
let mut data = [-1.0; 3];
Vec4f::default().store_checked(&mut data);
let mut data = [-1.0; 5];
Vec4f::default().store_checked(&mut data);
Sourcepub fn store_prefix(&self, slice: &mut [f32])
pub fn store_prefix(&self, slice: &mut [f32])
Stores elements of vector into the first four elements of slice
.
§Panics
Panics if slice
contains less then four elements.
§Exmaples
let mut data = [-1.0; 5];
Vec4f::broadcast(2.0).store_prefix(&mut data);
assert_eq!(data, [2.0, 2.0, 2.0, 2.0, -1.0]);
let mut data = [-1.0; 3];
Vec4f::default().store_prefix(&mut data);
Sourcepub fn store_partial(&self, slice: &mut [f32])
pub fn store_partial(&self, slice: &mut [f32])
Stores min(4, slice.len())
elements of vector into prefix of slice
.
§Exmaples
let mut data = [0.0; 3];
Vec4f::broadcast(1.0).store_partial(&mut data);
assert_eq!(data, [1.0; 3]);
let mut data = [0.0; 5];
Vec4f::broadcast(1.0).store_partial(&mut data);
assert_eq!(data, [1.0, 1.0, 1.0, 1.0, 0.0]); // note last zero
Trait Implementations§
Source§impl<T> AddAssign<T> for Vec4fwhere
Self: Add<T, Output = Self>,
impl<T> AddAssign<T> for Vec4fwhere
Self: Add<T, Output = Self>,
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read moreSource§impl<T> DivAssign<T> for Vec4fwhere
Self: Div<T, Output = Self>,
impl<T> DivAssign<T> for Vec4fwhere
Self: Div<T, Output = Self>,
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read moreSource§impl<T> MulAssign<T> for Vec4fwhere
Self: Mul<T, Output = Self>,
impl<T> MulAssign<T> for Vec4fwhere
Self: Mul<T, Output = Self>,
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read moreSource§impl SIMDVector for Vec4f
impl SIMDVector for Vec4f
Source§const ELEMENTS: usize = 4usize
const ELEMENTS: usize = 4usize
SIMDVector
.Source§type Underlying = __m128
type Underlying = __m128
SIMDVector
.Source§type Element = f32
type Element = f32
SIMDVector
.Source§impl<T> SubAssign<T> for Vec4fwhere
Self: Sub<T, Output = Self>,
impl<T> SubAssign<T> for Vec4fwhere
Self: Sub<T, Output = Self>,
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-=
operation. Read more