forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_avx2.cc
More file actions
222 lines (199 loc) · 9.39 KB
/
Copy pathutil_avx2.cc
File metadata and controls
222 lines (199 loc) · 9.39 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <immintrin.h>
#include "arrow/compute/exec/util.h"
#include "arrow/util/bit_util.h"
namespace arrow {
namespace util {
#if defined(ARROW_HAVE_AVX2)
void bit_util::bits_to_indexes_avx2(int bit_to_search, const int num_bits,
const uint8_t* bits, int* num_indexes,
uint16_t* indexes, uint16_t base_index) {
if (bit_to_search == 0) {
bits_to_indexes_imp_avx2<0>(num_bits, bits, num_indexes, indexes, base_index);
} else {
ARROW_DCHECK(bit_to_search == 1);
bits_to_indexes_imp_avx2<1>(num_bits, bits, num_indexes, indexes, base_index);
}
}
template <int bit_to_search>
void bit_util::bits_to_indexes_imp_avx2(const int num_bits, const uint8_t* bits,
int* num_indexes, uint16_t* indexes,
uint16_t base_index) {
// 64 bits at a time
constexpr int unroll = 64;
// The caller takes care of processing the remaining bits at the end outside of the
// multiples of 64
ARROW_DCHECK(num_bits % unroll == 0);
constexpr uint64_t kEachByteIs1 = 0X0101010101010101ULL;
constexpr uint64_t kEachByteIs8 = 0x0808080808080808ULL;
constexpr uint64_t kByteSequence0To7 = 0x0706050403020100ULL;
uint8_t byte_indexes[64];
const uint64_t incr = kEachByteIs8;
const uint64_t mask = kByteSequence0To7;
*num_indexes = 0;
for (int i = 0; i < num_bits / unroll; ++i) {
uint64_t word = reinterpret_cast<const uint64_t*>(bits)[i];
if (bit_to_search == 0) {
word = ~word;
}
uint64_t base = 0;
int num_indexes_loop = 0;
while (word) {
uint64_t byte_indexes_next =
_pext_u64(mask, _pdep_u64(word, kEachByteIs1) * 0xff) + base;
*reinterpret_cast<uint64_t*>(byte_indexes + num_indexes_loop) = byte_indexes_next;
base += incr;
num_indexes_loop += static_cast<int>(arrow::bit_util::PopCount(word & 0xff));
word >>= 8;
}
// Unpack indexes to 16-bits and either add the base of i * 64 or shuffle input
// indexes
for (int j = 0; j < (num_indexes_loop + 15) / 16; ++j) {
__m256i output = _mm256_cvtepi8_epi16(
_mm_loadu_si128(reinterpret_cast<const __m128i*>(byte_indexes) + j));
output = _mm256_add_epi16(output, _mm256_set1_epi16(i * 64 + base_index));
_mm256_storeu_si256(((__m256i*)(indexes + *num_indexes)) + j, output);
}
*num_indexes += num_indexes_loop;
}
}
void bit_util::bits_filter_indexes_avx2(int bit_to_search, const int num_bits,
const uint8_t* bits,
const uint16_t* input_indexes, int* num_indexes,
uint16_t* indexes) {
if (bit_to_search == 0) {
bits_filter_indexes_imp_avx2<0>(num_bits, bits, input_indexes, num_indexes, indexes);
} else {
bits_filter_indexes_imp_avx2<1>(num_bits, bits, input_indexes, num_indexes, indexes);
}
}
template <int bit_to_search>
void bit_util::bits_filter_indexes_imp_avx2(const int num_bits, const uint8_t* bits,
const uint16_t* input_indexes,
int* out_num_indexes, uint16_t* indexes) {
// 64 bits at a time
constexpr int unroll = 64;
// The caller takes care of processing the remaining bits at the end outside of the
// multiples of 64
ARROW_DCHECK(num_bits % unroll == 0);
constexpr uint64_t kRepeatedBitPattern0001 = 0x1111111111111111ULL;
constexpr uint64_t k4BitSequence0To15 = 0xfedcba9876543210ULL;
constexpr uint64_t kByteSequence_0_0_1_1_2_2_3_3 = 0x0303020201010000ULL;
constexpr uint64_t kByteSequence_4_4_5_5_6_6_7_7 = 0x0707060605050404ULL;
constexpr uint64_t kByteSequence_0_2_4_6_8_10_12_14 = 0x0e0c0a0806040200ULL;
constexpr uint64_t kByteSequence_1_3_5_7_9_11_13_15 = 0x0f0d0b0907050301ULL;
constexpr uint64_t kByteSequence_0_8_1_9_2_10_3_11 = 0x0b030a0209010800ULL;
constexpr uint64_t kByteSequence_4_12_5_13_6_14_7_15 = 0x0f070e060d050c04ULL;
const uint64_t mask = k4BitSequence0To15;
int num_indexes = 0;
for (int i = 0; i < num_bits / unroll; ++i) {
uint64_t word = reinterpret_cast<const uint64_t*>(bits)[i];
if (bit_to_search == 0) {
word = ~word;
}
int loop_id = 0;
while (word) {
uint64_t indexes_4bit =
_pext_u64(mask, _pdep_u64(word, kRepeatedBitPattern0001) * 0xf);
// Unpack 4 bit indexes to 8 bits
__m256i indexes_8bit = _mm256_set1_epi64x(indexes_4bit);
indexes_8bit = _mm256_shuffle_epi8(
indexes_8bit,
_mm256_setr_epi64x(kByteSequence_0_0_1_1_2_2_3_3, kByteSequence_4_4_5_5_6_6_7_7,
kByteSequence_0_0_1_1_2_2_3_3,
kByteSequence_4_4_5_5_6_6_7_7));
indexes_8bit = _mm256_blendv_epi8(
_mm256_and_si256(indexes_8bit, _mm256_set1_epi8(0x0f)),
_mm256_and_si256(_mm256_srli_epi32(indexes_8bit, 4), _mm256_set1_epi8(0x0f)),
_mm256_set1_epi16(static_cast<uint16_t>(0xff00)));
__m256i input =
_mm256_loadu_si256(((const __m256i*)input_indexes) + 4 * i + loop_id);
// Shuffle bytes to get low bytes in the first 128-bit lane and high bytes in the
// second
input = _mm256_shuffle_epi8(
input, _mm256_setr_epi64x(
kByteSequence_0_2_4_6_8_10_12_14, kByteSequence_1_3_5_7_9_11_13_15,
kByteSequence_0_2_4_6_8_10_12_14, kByteSequence_1_3_5_7_9_11_13_15));
input = _mm256_permute4x64_epi64(input, 0xd8); // 0b11011000
// Apply permutation
__m256i output = _mm256_shuffle_epi8(input, indexes_8bit);
// Move low and high bytes across 128-bit lanes to assemble back 16-bit indexes.
// (This is the reverse of the byte permutation we did on the input)
output = _mm256_permute4x64_epi64(output,
0xd8); // The reverse of swapping 2nd and 3rd
// 64-bit element is the same permutation
output = _mm256_shuffle_epi8(output,
_mm256_setr_epi64x(kByteSequence_0_8_1_9_2_10_3_11,
kByteSequence_4_12_5_13_6_14_7_15,
kByteSequence_0_8_1_9_2_10_3_11,
kByteSequence_4_12_5_13_6_14_7_15));
_mm256_storeu_si256((__m256i*)(indexes + num_indexes), output);
num_indexes += static_cast<int>(arrow::bit_util::PopCount(word & 0xffff));
word >>= 16;
++loop_id;
}
}
*out_num_indexes = num_indexes;
}
void bit_util::bits_to_bytes_avx2(const int num_bits, const uint8_t* bits,
uint8_t* bytes) {
constexpr int unroll = 32;
constexpr uint64_t kEachByteIs1 = 0x0101010101010101ULL;
constexpr uint64_t kEachByteIs2 = 0x0202020202020202ULL;
constexpr uint64_t kEachByteIs3 = 0x0303030303030303ULL;
constexpr uint64_t kByteSequencePowersOf2 = 0x8040201008040201ULL;
// Processing 32 bits at a time
for (int i = 0; i < num_bits / unroll; ++i) {
__m256i unpacked = _mm256_set1_epi32(reinterpret_cast<const uint32_t*>(bits)[i]);
unpacked = _mm256_shuffle_epi8(
unpacked, _mm256_setr_epi64x(0ULL, kEachByteIs1, kEachByteIs2, kEachByteIs3));
__m256i bits_in_bytes = _mm256_set1_epi64x(kByteSequencePowersOf2);
unpacked =
_mm256_cmpeq_epi8(bits_in_bytes, _mm256_and_si256(unpacked, bits_in_bytes));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(bytes) + i, unpacked);
}
}
void bit_util::bytes_to_bits_avx2(const int num_bits, const uint8_t* bytes,
uint8_t* bits) {
constexpr int unroll = 32;
// Processing 32 bits at a time
for (int i = 0; i < num_bits / unroll; ++i) {
reinterpret_cast<uint32_t*>(bits)[i] = _mm256_movemask_epi8(
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(bytes) + i));
}
}
bool bit_util::are_all_bytes_zero_avx2(const uint8_t* bytes, uint32_t num_bytes) {
__m256i result_or = _mm256_setzero_si256();
uint32_t i;
for (i = 0; i < num_bytes / 32; ++i) {
__m256i x = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(bytes) + i);
result_or = _mm256_or_si256(result_or, x);
}
result_or = _mm256_cmpeq_epi8(result_or, _mm256_set1_epi8(0));
result_or =
_mm256_andnot_si256(result_or, _mm256_set1_epi8(static_cast<uint8_t>(0xff)));
uint32_t result_or32 = _mm256_movemask_epi8(result_or);
if (num_bytes % 32 > 0) {
uint64_t tail[4] = {0, 0, 0, 0};
result_or32 |= memcmp(bytes + i * 32, tail, num_bytes % 32);
}
return result_or32 == 0;
}
#endif // ARROW_HAVE_AVX2
} // namespace util
} // namespace arrow