Skip to content

Commit

Permalink
Various improvements for #85
Browse files Browse the repository at this point in the history
  • Loading branch information
yoanlcq committed Apr 24, 2022
1 parent e966957 commit 3febca2
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
version = "0.15.7" # remember to update html_root_url in src/lib.rs
version = "0.15.8" # remember to update html_root_url in src/lib.rs
authors = ["Yoan Lecoq <[email protected]>", "Joshua Barretto <[email protected]>", "Sunjay Varma <[email protected]>", "timokoesters <[email protected]>", "Imbris <[email protected]>"]
description = "Generic 2D-3D math swiss army knife for game engines, with SIMD support and focus on convenience."
documentation = "https://docs.rs/vek"
Expand Down
10 changes: 10 additions & 0 deletions examples/simd_intrinsics_compile_errors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
9 changes: 9 additions & 0 deletions examples/simd_intrinsics_compile_errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "simd_intrinsics_compile_errors"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
vek = { path = "../../", features = ["repr_simd", "platform_intrinsics"] }
12 changes: 12 additions & 0 deletions examples/simd_intrinsics_compile_errors/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use vek::vec::repr_simd::Vec4;

fn main() {
assert!(Vec4::broadcast(true).reduce_and());
assert!(Vec4::broadcast(true).reduce_or());
assert!(Vec4::broadcast(1i32).reduce_and());
assert!(Vec4::broadcast(1i32).reduce_or());
assert!(Vec4::broadcast(0f32).partial_cmplt(&Vec4::broadcast(1f32)) != Vec4::broadcast(false));
assert!(Vec4::broadcast(0f32).partial_cmpgt(&Vec4::broadcast(1f32)) == Vec4::broadcast(false));
assert!(Vec4::broadcast(0f32).partial_cmplt_simd(Vec4::broadcast(1f32)) != Vec4::zero());
assert!(Vec4::broadcast(0f32).partial_cmpgt_simd(Vec4::broadcast(1f32)) == Vec4::zero());
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#![no_std]
#![doc(
test(attr(deny(warnings))),
html_root_url = "https://docs.rs/vek/0.15.7",
html_root_url = "https://docs.rs/vek/0.15.8",
//html_logo_url = "https://yoanlcq.github.io/vek/logo.png",
//html_favicon_url = "https://yoanlcq.github.io/vek/favicon.ico",
)]
Expand Down Expand Up @@ -93,3 +93,5 @@ pub mod bezier;
pub use crate::bezier::*;
pub mod geom;
pub use crate::geom::*;
pub mod simd_traits;
pub use crate::simd_traits::*;
57 changes: 57 additions & 0 deletions src/simd_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! SIMD traits; they are useful when the features "repr_simd" and "platform_intrinsics" are enabled.
use std::num::Wrapping;
use num_traits;

/// This trait should be implemented by scalar types, vectors of which are supported by SIMD intrinsics.
/// For instance, i16 and f32 can implement this trait, as well as `#[repr(transparent)]` wrappers of these, but not hand-built numeric types.
pub trait SimdElement {
/// The corresponding mask type for this element type: this is usually the unsigned integer type with the same size.
type SimdMaskType: SimdMask;
}

/// Implemented by unsigned integer types that can represent the result of SIMD comparison elements: semantically, these are booleans, but in their representation, zero is false and any other value is true.
/// Typically, SIMD comparison of two vectors will yield a vector of mask elements, on which you can call `reduce_and()` or `reduce_or()`.
pub trait SimdMask: num_traits::sign::Unsigned + num_traits::bounds::Bounded + num_traits::cast::FromPrimitive {
/// Used for fallback code.
#[inline]
fn from_bool(b: bool) -> Self {
Self::from_u8(b as _).unwrap()
}
}

macro_rules! impl_simd_mask {
($T:ty) => {
impl SimdMask for $T {}
}
}

macro_rules! impl_simd_element {
($T:ty, $M:ty) => {
impl SimdElement for $T {
type SimdMaskType = $M;
}
}
}

impl_simd_mask!{u8}
impl_simd_mask!{u16}
impl_simd_mask!{u32}
impl_simd_mask!{u64}

impl<T: SimdMask> SimdMask for Wrapping<T> where Wrapping<T>: num_traits::sign::Unsigned {}

impl_simd_element!{i8, u8}
impl_simd_element!{i16, u16}
impl_simd_element!{i32, u32}
impl_simd_element!{i64, u64}
impl_simd_element!{u8, u8}
impl_simd_element!{u16, u16}
impl_simd_element!{u32, u32}
impl_simd_element!{u64, u64}
impl_simd_element!{f32, u32}
impl_simd_element!{f64, u64}

impl<T: SimdElement> SimdElement for Wrapping<T> {
type SimdMaskType = T::SimdMaskType; // Don't propagate the Wrapping<>, masks are only intended for reduction to booleans; it doesn't make sense to perform arithmetic on them.
}
Loading

0 comments on commit 3febca2

Please sign in to comment.