generic_simd/
scalar.rs

1//! Extensions for scalars.
2
3use crate::vector::{width, Native, NativeWidth, Vector};
4
5/// A scalar value.
6pub trait Scalar<Token, Width>: Copy
7where
8    Token: crate::arch::Token,
9    Width: width::Width,
10{
11    type Vector: Vector<Scalar = Self, Token = Token, Width = Width>;
12
13    /// Create a vector set to zero.
14    ///
15    /// See [`zeroed`](../vector/trait.Vector.html#method.zeroed).
16    #[inline]
17    fn zeroed(token: Token) -> Self::Vector {
18        Self::Vector::zeroed(token)
19    }
20
21    /// Splat a scalar to a vector.
22    ///
23    /// See [`splat`](../vector/trait.Vector.html#tymethod.splat).
24    #[inline]
25    fn splat(self, token: Token) -> Self::Vector {
26        Self::Vector::splat(token, self)
27    }
28}
29
30macro_rules! scalar_impl {
31    {
32        $width:literal,
33        $width_type:ty,
34        $zeroed:ident,
35        $splat:ident
36    } => {
37        #[doc = "Create a vector with "]
38        #[doc = $width]
39        #[doc = " set to zero.\n\nSee [`zeroed`](../vector/trait.Vector.html#method.zeroed)."]
40        #[inline]
41        fn $zeroed(token: Token) -> <Self as Scalar<Token, $width_type>>::Vector {
42           <Self as Scalar<Token, $width_type>>::zeroed(token.into())
43        }
44
45        #[doc = "Splat a scalar to "]
46        #[doc = $width]
47        #[doc = ".\n\nSee [`splat`](../vector/trait.Vector.html#tymethod.splat)."]
48        #[inline]
49        fn $splat(self, token: Token) -> <Self as Scalar<Token, $width_type>>::Vector {
50            <Self as Scalar<Token, $width_type>>::splat(self, token.into())
51        }
52    }
53}
54
55/// A scalar value, supporting all vector widths.
56pub trait ScalarExt<Token>:
57    Native<Token>
58    + self::Scalar<Token, width::W1>
59    + self::Scalar<Token, width::W2>
60    + self::Scalar<Token, width::W4>
61    + self::Scalar<Token, width::W8>
62    + self::Scalar<Token, NativeWidth<Self, Token>>
63where
64    Token: crate::arch::Token + From<Token> + Into<Token>,
65{
66    scalar_impl! { "the native number of lanes", <Self as Native<Token>>::Width, zeroed_native, splat_native }
67    scalar_impl! { "1 lane",  width::W1, zeroed1, splat1 }
68    scalar_impl! { "2 lanes", width::W2, zeroed2, splat2 }
69    scalar_impl! { "4 lanes", width::W4, zeroed4, splat4 }
70    scalar_impl! { "8 lanes", width::W8, zeroed8, splat8 }
71}
72
73impl<Token, Scalar> ScalarExt<Token> for Scalar
74where
75    Token: crate::arch::Token,
76    Scalar: Native<Token>
77        + self::Scalar<Token, width::W1>
78        + self::Scalar<Token, width::W2>
79        + self::Scalar<Token, width::W4>
80        + self::Scalar<Token, width::W8>
81        + self::Scalar<Token, NativeWidth<Self, Token>>,
82{
83}