Мне следует подумать, что самым быстрым способом была бы таблица поиска.
Вы можете вычислить ее во время компиляции.
#include <array>
#include <cstdint>
#include <iostream>
#include <iomanip>
constexpr std::uint64_t dup_bit(bool b)
{
unsigned result = 0;
if (b) result = 0x3;
return result;
}
constexpr std::uint64_t slow_dup_bits(std::uint64_t input)
{
std::uint64_t result = 0;
for(int i = 16 ; i-- ; )
{
result <<= 2;
result |= dup_bit(input & (1u << i));
}
return result;
}
template<std::uint64_t Start, std::uint64_t...Is>
constexpr auto make_dup_table(std::integer_sequence<std::uint64_t, Is...>)
{
return std::array<std::uint64_t, sizeof...(Is)>
{{
slow_dup_bits(Start + Is)...,
}};
}
std::uint64_t dup_bits(std::uint64_t x)
{
static const auto table = make_dup_table<0>(std::make_integer_sequence<std::uint64_t, 65536>());
auto low32 = table[x & 65535];
auto high32 = table[x >> 16];
return (high32 << 32) + low32;
}
int main()
{
for(std::uint64_t i = 0 ; i < 64 ; ++i)
std::cout << std::setw(8) << std::setfill(' ') << i
<< " -> "
<< std::hex << std::setw(64) << std::setfill('0') << dup_bits(i) << '\n';
}
Ожидаемый результат:
0 -> 0000000000000000000000000000000000000000000000000000000000000000
1 -> 0000000000000000000000000000000000000000000000000000000000000003
2 -> 000000000000000000000000000000000000000000000000000000000000000c
3 -> 000000000000000000000000000000000000000000000000000000000000000f
4 -> 0000000000000000000000000000000000000000000000000000000000000030
5 -> 0000000000000000000000000000000000000000000000000000000000000033
6 -> 000000000000000000000000000000000000000000000000000000000000003c
7 -> 000000000000000000000000000000000000000000000000000000000000003f
8 -> 00000000000000000000000000000000000000000000000000000000000000c0
9 -> 00000000000000000000000000000000000000000000000000000000000000c3
a -> 00000000000000000000000000000000000000000000000000000000000000cc
b -> 00000000000000000000000000000000000000000000000000000000000000cf
c -> 00000000000000000000000000000000000000000000000000000000000000f0
d -> 00000000000000000000000000000000000000000000000000000000000000f3
e -> 00000000000000000000000000000000000000000000000000000000000000fc
f -> 00000000000000000000000000000000000000000000000000000000000000ff
10 -> 0000000000000000000000000000000000000000000000000000000000000300
11 -> 0000000000000000000000000000000000000000000000000000000000000303
12 -> 000000000000000000000000000000000000000000000000000000000000030c
13 -> 000000000000000000000000000000000000000000000000000000000000030f
14 -> 0000000000000000000000000000000000000000000000000000000000000330
15 -> 0000000000000000000000000000000000000000000000000000000000000333
16 -> 000000000000000000000000000000000000000000000000000000000000033c
17 -> 000000000000000000000000000000000000000000000000000000000000033f
18 -> 00000000000000000000000000000000000000000000000000000000000003c0
19 -> 00000000000000000000000000000000000000000000000000000000000003c3
1a -> 00000000000000000000000000000000000000000000000000000000000003cc
1b -> 00000000000000000000000000000000000000000000000000000000000003cf
1c -> 00000000000000000000000000000000000000000000000000000000000003f0
1d -> 00000000000000000000000000000000000000000000000000000000000003f3
1e -> 00000000000000000000000000000000000000000000000000000000000003fc
1f -> 00000000000000000000000000000000000000000000000000000000000003ff
20 -> 0000000000000000000000000000000000000000000000000000000000000c00
21 -> 0000000000000000000000000000000000000000000000000000000000000c03
22 -> 0000000000000000000000000000000000000000000000000000000000000c0c
23 -> 0000000000000000000000000000000000000000000000000000000000000c0f
24 -> 0000000000000000000000000000000000000000000000000000000000000c30
25 -> 0000000000000000000000000000000000000000000000000000000000000c33
26 -> 0000000000000000000000000000000000000000000000000000000000000c3c
27 -> 0000000000000000000000000000000000000000000000000000000000000c3f
28 -> 0000000000000000000000000000000000000000000000000000000000000cc0
29 -> 0000000000000000000000000000000000000000000000000000000000000cc3
2a -> 0000000000000000000000000000000000000000000000000000000000000ccc
2b -> 0000000000000000000000000000000000000000000000000000000000000ccf
2c -> 0000000000000000000000000000000000000000000000000000000000000cf0
2d -> 0000000000000000000000000000000000000000000000000000000000000cf3
2e -> 0000000000000000000000000000000000000000000000000000000000000cfc
2f -> 0000000000000000000000000000000000000000000000000000000000000cff
30 -> 0000000000000000000000000000000000000000000000000000000000000f00
31 -> 0000000000000000000000000000000000000000000000000000000000000f03
32 -> 0000000000000000000000000000000000000000000000000000000000000f0c
33 -> 0000000000000000000000000000000000000000000000000000000000000f0f
34 -> 0000000000000000000000000000000000000000000000000000000000000f30
35 -> 0000000000000000000000000000000000000000000000000000000000000f33
36 -> 0000000000000000000000000000000000000000000000000000000000000f3c
37 -> 0000000000000000000000000000000000000000000000000000000000000f3f
38 -> 0000000000000000000000000000000000000000000000000000000000000fc0
39 -> 0000000000000000000000000000000000000000000000000000000000000fc3
3a -> 0000000000000000000000000000000000000000000000000000000000000fcc
3b -> 0000000000000000000000000000000000000000000000000000000000000fcf
3c -> 0000000000000000000000000000000000000000000000000000000000000ff0
3d -> 0000000000000000000000000000000000000000000000000000000000000ff3
3e -> 0000000000000000000000000000000000000000000000000000000000000ffc
3f -> 0000000000000000000000000000000000000000000000000000000000000fff
http://coliru.stacked -crooked.com / а / 7b92ad167bd54ae7