-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiteralSIMDShuffleMask.hpp
41 lines (38 loc) · 2.31 KB
/
LiteralSIMDShuffleMask.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Flexible structure that can be specialized to assign a character to a certain element in a vector.
template < char C > struct CharToShuffleIndex;
// Specialization for characters that point to the first element in the vector.
template < > struct CharToShuffleIndex< '0' > { static constexpr char value = 0; };
template < > struct CharToShuffleIndex< 'X' > { static constexpr char value = 0; };
template < > struct CharToShuffleIndex< 'x' > { static constexpr char value = 0; };
// Specialization for characters that point to the second element in the vector.
template < > struct CharToShuffleIndex< '1' > { static constexpr char value = 1; };
template < > struct CharToShuffleIndex< 'Y' > { static constexpr char value = 1; };
template < > struct CharToShuffleIndex< 'y' > { static constexpr char value = 1; };
// Specialization for characters that point to the third element in the vector.
template < > struct CharToShuffleIndex< '2' > { static constexpr char value = 2; };
template < > struct CharToShuffleIndex< 'Z' > { static constexpr char value = 2; };
template < > struct CharToShuffleIndex< 'z' > { static constexpr char value = 2; };
// Specialization for characters that point to the fourth element in the vector.
template < > struct CharToShuffleIndex< '3' > { static constexpr char value = 3; };
template < > struct CharToShuffleIndex< 'W' > { static constexpr char value = 3; };
template < > struct CharToShuffleIndex< 'w' > { static constexpr char value = 3; };
// Terminator of the character to shuffle mask recursion.
template < char C > constexpr unsigned PackShuffleCharMask(unsigned m)
{
return m | CharToShuffleIndex< C >::value;
}
// Packs a series of characters into a SIMD shuffle mask.
template < char C, char Cn, char... Cs > constexpr unsigned PackShuffleCharMask(unsigned m)
{
return PackShuffleCharMask< Cn, Cs... >((m | CharToShuffleIndex< C >::value) << 2);
}
// Takes a series of numbers and treats them as a series of characters to generate a shuffle mask.
template < char... Cs > constexpr int operator"" _Shuffle()
{
return static_cast< int >(PackShuffleCharMask< Cs... >(0));
}
// Takes a literal string and treats it as a series of characters to generate a shuffle mask.
template < typename CharT, CharT... Cs > constexpr int operator"" _Shuffle()
{
return static_cast< int >(PackShuffleCharMask< Cs... >(0));
}