Skip to content

Commit

Permalink
Add from array for boolean vectors. (#482)
Browse files Browse the repository at this point in the history
Fixes #482
  • Loading branch information
bitshifter authored Mar 3, 2024
1 parent aef0e00 commit b6d1756
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 14 deletions.
20 changes: 19 additions & 1 deletion codegen/templates/vec_mask.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl {{ self_t }} {
{% endif %}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Expand All @@ -141,6 +141,17 @@ impl {{ self_t }} {
)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; {{ dim }}]) -> Self {
Self::new(
{% for c in components %}
a[{{ loop.index0 }}],
{%- endfor %}
)
}

/// Returns a bitmask with the lowest {{ dim }} bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -499,6 +510,13 @@ impl fmt::Display for {{ self_t }} {
}
}

impl From<[bool; {{ dim }}]> for {{ self_t }} {
#[inline]
fn from(a: [bool; {{ dim }}]) -> Self {
Self::from_array(a)
}
}

impl From<{{ self_t }}> for [bool; {{ dim }}] {
#[inline]
fn from(mask: {{ self_t }}) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/bvec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ impl BVec2 {
Self { x, y }
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 2]) -> Self {
Self::new(a[0], a[1])
}

/// Returns a bitmask with the lowest 2 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -185,6 +192,13 @@ impl fmt::Display for BVec2 {
}
}

impl From<[bool; 2]> for BVec2 {
#[inline]
fn from(a: [bool; 2]) -> Self {
Self::from_array(a)
}
}

impl From<BVec2> for [bool; 2] {
#[inline]
fn from(mask: BVec2) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/bvec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ impl BVec3 {
Self { x, y, z }
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 3]) -> Self {
Self::new(a[0], a[1], a[2])
}

/// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -203,6 +210,13 @@ impl fmt::Display for BVec3 {
}
}

impl From<[bool; 3]> for BVec3 {
#[inline]
fn from(a: [bool; 3]) -> Self {
Self::from_array(a)
}
}

impl From<BVec3> for [bool; 3] {
#[inline]
fn from(mask: BVec3) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/bvec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ impl BVec4 {
Self { x, y, z, w }
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 4]) -> Self {
Self::new(a[0], a[1], a[2], a[3])
}

/// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -212,6 +219,13 @@ impl fmt::Display for BVec4 {
}
}

impl From<[bool; 4]> for BVec4 {
#[inline]
fn from(a: [bool; 4]) -> Self {
Self::from_array(a)
}
}

impl From<BVec4> for [bool; 4] {
#[inline]
fn from(mask: BVec4) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/coresimd/bvec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ impl BVec3A {
}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 3]) -> Self {
Self::new(a[0], a[1], a[2])
}

/// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -213,6 +220,13 @@ impl fmt::Display for BVec3A {
}
}

impl From<[bool; 3]> for BVec3A {
#[inline]
fn from(a: [bool; 3]) -> Self {
Self::from_array(a)
}
}

impl From<BVec3A> for [bool; 3] {
#[inline]
fn from(mask: BVec3A) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/coresimd/bvec4a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ impl BVec4A {
}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 4]) -> Self {
Self::new(a[0], a[1], a[2], a[3])
}

/// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -225,6 +232,13 @@ impl fmt::Display for BVec4A {
}
}

impl From<[bool; 4]> for BVec4A {
#[inline]
fn from(a: [bool; 4]) -> Self {
Self::from_array(a)
}
}

impl From<BVec4A> for [bool; 4] {
#[inline]
fn from(mask: BVec4A) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/scalar/bvec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ impl BVec3A {
}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 3]) -> Self {
Self::new(a[0], a[1], a[2])
}

/// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -207,6 +214,13 @@ impl fmt::Display for BVec3A {
}
}

impl From<[bool; 3]> for BVec3A {
#[inline]
fn from(a: [bool; 3]) -> Self {
Self::from_array(a)
}
}

impl From<BVec3A> for [bool; 3] {
#[inline]
fn from(mask: BVec3A) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/scalar/bvec4a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ impl BVec4A {
}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 4]) -> Self {
Self::new(a[0], a[1], a[2], a[3])
}

/// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -217,6 +224,13 @@ impl fmt::Display for BVec4A {
}
}

impl From<[bool; 4]> for BVec4A {
#[inline]
fn from(a: [bool; 4]) -> Self {
Self::from_array(a)
}
}

impl From<BVec4A> for [bool; 4] {
#[inline]
fn from(mask: BVec4A) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/sse2/bvec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ impl BVec3A {
}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 3]) -> Self {
Self::new(a[0], a[1], a[2])
}

/// Returns a bitmask with the lowest 3 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -224,6 +231,13 @@ impl fmt::Display for BVec3A {
}
}

impl From<[bool; 3]> for BVec3A {
#[inline]
fn from(a: [bool; 3]) -> Self {
Self::from_array(a)
}
}

impl From<BVec3A> for [bool; 3] {
#[inline]
fn from(mask: BVec3A) -> Self {
Expand Down
16 changes: 15 additions & 1 deletion src/bool/sse2/bvec4a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ impl BVec4A {
}
}

/// Creates a vector with all elements set to `v`.
/// Creates a vector mask with all elements set to `v`.
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v, v)
}

/// Creates a new vector mask from a bool array.
#[inline]
#[must_use]
pub const fn from_array(a: [bool; 4]) -> Self {
Self::new(a[0], a[1], a[2], a[3])
}

/// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
///
/// A true element results in a `1` bit and a false element in a `0` bit. Element `x` goes
Expand Down Expand Up @@ -237,6 +244,13 @@ impl fmt::Display for BVec4A {
}
}

impl From<[bool; 4]> for BVec4A {
#[inline]
fn from(a: [bool; 4]) -> Self {
Self::from_array(a)
}
}

impl From<BVec4A> for [bool; 4] {
#[inline]
fn from(mask: BVec4A) -> Self {
Expand Down
Loading

0 comments on commit b6d1756

Please sign in to comment.