Struct Vec4f

Source
pub struct Vec4f { /* private fields */ }
Expand description

Represents a packed vector of 4 single-precision floating-point values. __m128 wrapper.

Implementations§

Source§

impl Vec4f

Source

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()
);
Source

pub unsafe fn load_ptr(addr: *const [f32; 4]) -> Self

Loads vector from array pointer by addr. addr is not required to be aligned.

§Safety

addr must be a valid pointer.

§Example
let array = [42.0; 4];
let vec = unsafe { Vec4f::load_ptr(&array) };
Source

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]) };
Source

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])
);
Source

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]);
Source

pub fn load_prefix(data: &[f32]) -> Self

Loads the first four elements of data into vector.

§Panics

Panics if data contains less than four elements.

§Exmaples
assert_eq!(
    Vec4f::load_prefix(&[1.0, 2.0, 3.0, 4.0, 5.0]),
    Vec4f::new(1.0, 2.0, 3.0, 4.0)
);
Vec4f::load_prefix(&[1.0, 2.0, 3.0]);
Source

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
);
Source

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()
);
Source

pub unsafe fn store_ptr(&self, addr: *mut [f32; 4])

Stores vector into array at given address.

§Safety

addr must be a valid pointer.

Source

pub unsafe fn store_ptr_aligned(&self, addr: *mut [f32; 4])

Stores vector into aligned array at given address.

§Safety

Like store_ptr, requires addr to be valid. Unlike store_ptr, requires addr to be divisible by 16, i.e. to be a 16-bytes aligned address.

Source

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.

Source

pub fn store(&self, array: &mut [f32; 4])

Stores vector into given array.

Source

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);
Source

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);
Source

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
Source

pub fn sum(self) -> f32

Calculates the sum of all elements of vector.

§Exmaple
assert_eq!(Vec4f::new(1.0, 2.0, 3.0, 4.0).sum(), 10.0);

Trait Implementations§

Source§

impl Add<<Vec4f as SIMDVector>::Element> for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the + operator.
Source§

fn add(self, rhs: <Vec4f as SIMDVector>::Element) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Vec4f> for <Vec4f as SIMDVector>::Element

Source§

type Output = Vec4f

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vec4f) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> AddAssign<T> for Vec4f
where Self: Add<T, Output = Self>,

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl Clone for Vec4f

Source§

fn clone(&self) -> Vec4f

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Vec4f

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Vec4f

Source§

fn default() -> Self

Initializes all elements of returned vector with zero.

§Example
assert_eq!(Vec4f::default(), Vec4f::broadcast(0.0));
Source§

impl Div<<Vec4f as SIMDVector>::Element> for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the / operator.
Source§

fn div(self, rhs: <Vec4f as SIMDVector>::Element) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Vec4f> for <Vec4f as SIMDVector>::Element

Source§

type Output = Vec4f

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Vec4f) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> DivAssign<T> for Vec4f
where Self: Div<T, Output = Self>,

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl From<&[f32; 4]> for Vec4f

Source§

fn from(value: &[f32; 4]) -> Self

Does same as load.

Source§

impl From<&Vec4f> for [f32; 4]

Source§

fn from(value: &Vec4f) -> Self

Converts to this type from the input type.
Source§

impl From<[f32; 4]> for Vec4f

Source§

fn from(value: [f32; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<Vec4f> for [f32; 4]

Source§

fn from(value: Vec4f) -> Self

Converts to this type from the input type.
Source§

impl From<Vec4f> for __m128

Source§

fn from(value: Vec4f) -> Self

Unwraps given vector into raw __m128 value.

Source§

impl From<__m128> for Vec4f

Source§

fn from(value: __m128) -> Self

Wraps given value into Vec4f.

Source§

impl Mul<<Vec4f as SIMDVector>::Element> for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: <Vec4f as SIMDVector>::Element) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec4f> for <Vec4f as SIMDVector>::Element

Source§

type Output = Vec4f

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec4f) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> MulAssign<T> for Vec4f
where Self: Mul<T, Output = Self>,

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl Neg for Vec4f

Source§

fn neg(self) -> Self::Output

Flips sign bit of each element including non-finite ones.

Source§

type Output = Vec4f

The resulting type after applying the - operator.
Source§

impl PartialEq for Vec4f

Source§

fn eq(&self, other: &Self) -> bool

Checks whether all elements of vectors are equal.

Note: when NaN is an element of one of the operands the result is always false.

§Examples
let a = Vec4f::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(a, a);
assert_ne!(a, Vec4f::default());
let a = Vec4f::broadcast(f32::NAN);
assert_ne!(a, a);
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Product for Vec4f

Source§

fn product<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by multiplying the items.
Source§

impl SIMDVector for Vec4f

Source§

const ELEMENTS: usize = 4usize

Number of elements in SIMDVector.
Source§

type Underlying = __m128

Underlying intrinsic type or tuple of types implementing SIMDVector.
Source§

type Element = f32

Type of a single element of SIMDVector.
Source§

impl Sub<<Vec4f as SIMDVector>::Element> for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: <Vec4f as SIMDVector>::Element) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Vec4f> for <Vec4f as SIMDVector>::Element

Source§

type Output = Vec4f

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vec4f) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Vec4f

Source§

type Output = Vec4f

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> SubAssign<T> for Vec4f
where Self: Sub<T, Output = Self>,

Source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
Source§

impl Sum for Vec4f

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Copy for Vec4f

Auto Trait Implementations§

§

impl Freeze for Vec4f

§

impl RefUnwindSafe for Vec4f

§

impl Send for Vec4f

§

impl Sync for Vec4f

§

impl Unpin for Vec4f

§

impl UnwindSafe for Vec4f

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.