-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManyU32.h
101 lines (97 loc) · 2.31 KB
/
ManyU32.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
/*
* By Paul Dreik 2019,2020
* https://www.pauldreik.se/
* License: Boost 1.0
* SPDX-License-Identifier: BSL-1.0
*/
#include <immintrin.h>
#include <cstdint>
#include <array>
struct ManyU32
{
using Int = std::uint32_t;
// sets all elements to the same value x
explicit ManyU32(Int x) noexcept { m_x = _mm256_set1_epi32(x); }
ManyU32(Int a, Int b, Int c, Int d, Int e, Int f, Int g, Int h)
{
// perhaps the wrong order
m_x = _mm256_set_epi32(a, b, c, d, e, f, g, h);
}
explicit ManyU32(__m256i x) noexcept
: m_x(x)
{}
ManyU32& operator^=(const ManyU32& other) noexcept
{
m_x = _mm256_xor_si256(m_x, other.m_x);
return *this;
}
ManyU32& operator&=(const ManyU32& other) noexcept
{
m_x = _mm256_and_si256(m_x, other.m_x);
return *this;
}
ManyU32& operator*=(const ManyU32& other) noexcept
{
m_x = _mm256_mullo_epi32(m_x, other.m_x);
return *this;
}
ManyU32& operator+=(const ManyU32& other) noexcept
{
m_x = _mm256_add_epi32(m_x, other.m_x);
return *this;
}
// avoid accidental use of if(obj), since
// it is unclear if it means "any" or "all"
// for a vector.
operator bool() const = delete;
template<int i>
Int get() const noexcept
{
static_assert(i >= 0 && i < 8, "outside bounds");
Int tmp[8];
static_assert(sizeof(*this) == sizeof(tmp), "");
_mm256_storeu_si256((__m256i*)&tmp[0], m_x);
return tmp[i];
}
std::array<Int, 8> toArray() const
{
std::array<Int, 8> ret;
_mm256_storeu_si256((__m256i*)&ret[0], m_x);
return ret;
}
__m256i m_x;
};
ManyU32
operator^(const ManyU32& a, const ManyU32& b) noexcept
{
return ManyU32{ _mm256_xor_si256(a.m_x, b.m_x) };
}
ManyU32 operator&(const ManyU32& a, const ManyU32& b) noexcept
{
return ManyU32{ _mm256_and_si256(a.m_x, b.m_x) };
}
ManyU32
operator|(const ManyU32& a, const ManyU32& b) noexcept
{
return ManyU32{ _mm256_or_si256(a.m_x, b.m_x) };
}
ManyU32
operator>>(const ManyU32& a, int n) noexcept
{
assert(n >= 0);
assert(n < 32);
return ManyU32{ _mm256_srli_epi32(a.m_x, n) };
}
ManyU32
operator<<(const ManyU32& a, int n) noexcept
{
assert(n >= 0);
assert(n < 32);
return ManyU32{ _mm256_slli_epi32(a.m_x, n) };
}
ManyU32
operator==(const ManyU32& a, const ManyU32& b) noexcept
{
return ManyU32{ _mm256_cmpeq_epi32(a.m_x, b.m_x) };
}