forked from WojciechMula/sse-popcount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sse_operators.cpp
86 lines (53 loc) · 1.62 KB
/
sse_operators.cpp
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
struct sse_vector final {
union {
__m128i v;
uint8_t u8[16];
uint16_t u16[8];
uint32_t u32[4];
uint64_t u64[2];
};
sse_vector() = delete;
sse_vector(sse_vector&) = delete;
explicit sse_vector(const __m128i& vec): v(vec) {}
};
__m128i operator&(sse_vector a, sse_vector b) {
return _mm_and_si128(a.v, b.v);
}
__m128i operator|(sse_vector a, sse_vector b) {
return _mm_or_si128(a.v, b.v);
}
__m128i operator^(sse_vector a, sse_vector b) {
return _mm_xor_si128(a.v, b.v);
}
struct shift16 final {
const unsigned bits;
shift16() = delete;
explicit shift16(unsigned bits) : bits(bits) {};
};
__m128i operator>>(const __m128i a, const shift16 amount) {
return _mm_srli_epi16(a, amount.bits);
}
uint64_t lower_qword(const __m128i v) {
return _mm_cvtsi128_si64(v);
}
uint64_t higher_qword(const __m128i v) {
return lower_qword(_mm_srli_si128(v, 8));
}
uint64_t simd_sum_epu64(const __m128i v) {
return lower_qword(v) + higher_qword(v);
}
#if defined(HAVE_AVX2_INSTRUCTIONS)
uint64_t simd_sum_epu64(const __m256i v) {
return static_cast<uint64_t>(_mm256_extract_epi64(v, 0))
+ static_cast<uint64_t>(_mm256_extract_epi64(v, 1))
+ static_cast<uint64_t>(_mm256_extract_epi64(v, 2))
+ static_cast<uint64_t>(_mm256_extract_epi64(v, 3));
}
#endif
#if defined(HAVE_AVX512BW_INSTRUCTIONS)
uint64_t simd_sum_epu64(const __m512i v) {
const __m256i lo = _mm512_extracti64x4_epi64(v, 0);
const __m256i hi = _mm512_extracti64x4_epi64(v, 1);
return simd_sum_epu64(lo) + simd_sum_epu64(hi);
}
#endif