-
Notifications
You must be signed in to change notification settings - Fork 1
/
shuffler.hpp
48 lines (39 loc) · 1.24 KB
/
shuffler.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
42
43
44
45
46
47
48
#ifndef SHUFFLER_HPP
# define SHUFFLER_HPP
# pragma once
#include <utility> // std::size_t, std::index_sequence
namespace shuffler
{
namespace detail
{
//////////////////////////////////////////////////////////////////////////////
template <std::size_t I, std::size_t J, typename T>
constexpr T shuffler(T const i) noexcept
{
//return T{std::uint8_t(i >> 8 * I)} << 8 * J;
return (T{0xffu} << 8 * J) & (I < J ? i << 8 * (J - I) : i >> 8 * (I - J));
}
template <std::size_t ...I, std::size_t ...J, typename T>
constexpr T shuffle(T const i, std::index_sequence<J...>) noexcept
{
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
defined(__BIG_ENDIAN__) || \
defined(__ARMEB__) || \
defined(__THUMBEB__) || \
defined(__AARCH64EB__) || \
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) || \
defined(Q_BYTE_ORDER) && Q_BYTE_ORDER == Q_BIG_ENDIAN
return (shuffler<I, sizeof(T) - 1 - J>(i) | ...);
#else
return (shuffler<I, J>(i) | ...);
#endif
}
}
//////////////////////////////////////////////////////////////////////////////
template <std::size_t ...I, typename T>
constexpr T shuffle(T const i) noexcept
{
return detail::shuffle<I...>(i, std::make_index_sequence<sizeof...(I)>());
}
}
#endif // SHUFFLER_HPP