-
Notifications
You must be signed in to change notification settings - Fork 0
/
simd_compare.h
392 lines (335 loc) · 12.8 KB
/
simd_compare.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// MIT License
//
// Copyright (c) 2018 André Tupinambá
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef SIMD_ALGORITHMS_LESS_THAN_H
#define SIMD_ALGORITHMS_LESS_THAN_H
#include <immintrin.h>
#include <x86intrin.h>
#include <type_traits>
#include <limits>
#include <vector>
#include <string>
#include <boost/align/aligned_allocator.hpp>
namespace simd_algorithms {
struct sse_tag {};
struct avx_tag {};
// Aligned vector
// ------------------------------------------------------------------------------------------------
template< typename Val_T >
using aligned_vector = std::vector< Val_T, boost::alignment::aligned_allocator<Val_T, 64> >;
using aligned_string = std::basic_string< char,
std::char_traits<char>,
boost::alignment::aligned_allocator<char, 64> >;
// Traits
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag > struct traits { };
template< typename ValueType_T > struct traits< ValueType_T, sse_tag >
{
using simd_type = __m128i;
constexpr static size_t simd_size = sizeof(simd_type)/sizeof(ValueType_T);
// static typename std::enable_if< std::is_same< ValueType_T, int8_t >::value,
// simd_type >::type max()
// {
// return _mm_set1_epi8( std::numeric_limits<int8_t>::max() );
// }
//
// static typename std::enable_if< std::is_same< ValueType_T, int32_t >::value,
// simd_type >::type max()
// {
// return _mm_set1_epi32( std::numeric_limits<int32_t>::max() );
// }
static typename std::enable_if< std::is_integral< ValueType_T >::value,
simd_type >::type zero()
{
return _mm_setzero_si128();
}
};
template< typename ValueType_T > struct traits< ValueType_T, avx_tag >
{
using simd_type = __m256i;
constexpr static size_t simd_size = sizeof(simd_type)/sizeof(ValueType_T);
// static typename std::enable_if< std::is_same< ValueType_T, int32_t >::value,
// simd_type >::type max()
// {
// return _mm256_set1_epi32( std::numeric_limits<int32_t>::max() );
// }
static typename std::enable_if< std::is_integral< ValueType_T >::value,
simd_type >::type zero()
{
return _mm256_setzero_si256();
}
};
// Mask operations
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T >
inline uint32_t mask_to_index( uint32_t mask )
{
return (mask == 0)
? 0
: (_bit_scan_reverse( mask ) + 1) / sizeof(ValueType_T);
}
template< typename ValueType_T, typename Tag_T >
uint32_t result_to_mask( typename traits< ValueType_T, Tag_T >::simd_type )
{
return 0;
}
template<> inline uint32_t
result_to_mask<int32_t, sse_tag>( __m128i retMask )
{
return _mm_movemask_epi8( retMask );
}
template<> inline uint32_t
result_to_mask<int32_t, avx_tag>( __m256i retMask )
{
return _mm256_movemask_epi8( retMask );
}
// Select item
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
typename traits< ValueType_T, Tag_T >::simd_type
select( typename traits< ValueType_T, Tag_T >::simd_type lhs, int8_t /*index*/ )
{
return lhs;
}
template<> inline __m128i
select<int32_t, sse_tag>( __m128i lhs, int8_t index )
{
int32_t i32 = index << 2;
return _mm_shuffle_epi8( lhs, _mm_set_epi8( i32+3, i32+2, i32+1, i32,
i32+3, i32+2, i32+1, i32,
i32+3, i32+2, i32+1, i32,
i32+3, i32+2, i32+1, i32 ) );
}
template<> inline __m256i
select<int32_t, avx_tag>( __m256i lhs, int8_t index )
{
return _mm256_permutevar8x32_epi32( lhs, _mm256_set_epi32( index, index, index, index,
index, index, index, index ) );
}
// Invert bits
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
typename traits< ValueType_T, Tag_T >::simd_type
invert( typename traits< ValueType_T, Tag_T >::simd_type lhs )
{
return lhs;
}
template<> inline __m128i
invert<int32_t, sse_tag>( __m128i lhs )
{
return _mm_xor_si128( _mm_set1_epi8( 0xff ), lhs );
}
template<> inline __m256i
invert<int32_t, avx_tag>( __m256i lhs )
{
return _mm256_xor_si256( _mm256_set1_epi8( 0xff ), lhs );
}
// iif - Ternary operator
// ------------------------------------------------------------------------------------------------
template< typename Tag_T = sse_tag >
typename traits< int8_t, Tag_T >::simd_type
iif( typename traits< int8_t, Tag_T >::simd_type mask,
typename traits< int8_t, Tag_T >::simd_type /*iftrue*/,
typename traits< int8_t, Tag_T >::simd_type /*iffalse*/ )
{
return mask;
}
template<> inline __m128i
iif<sse_tag>( __m128i mask, __m128i iftrue, __m128i iffalse )
{
return _mm_blendv_epi8( iffalse, iftrue, mask );
}
template<> inline __m256i
iif<avx_tag>( __m256i mask, __m256i iftrue, __m256i iffalse )
{
return _mm256_blendv_epi8( iffalse, iftrue, mask );
}
// Bit AND
// ------------------------------------------------------------------------------------------------
template< typename Tag_T = sse_tag >
typename traits< int8_t, Tag_T >::simd_type
mask_and( typename traits< int8_t, Tag_T >::simd_type lhs,
typename traits< int8_t, Tag_T >::simd_type /*rhs*/ )
{
return lhs;
}
template<> inline __m128i
mask_and<sse_tag>( __m128i lhs, __m128i rhs )
{
return _mm_and_si128( lhs, rhs );
}
template<> inline __m256i
mask_and<avx_tag>( __m256i lhs, __m256i rhs )
{
return _mm256_and_si256( lhs, rhs );
}
// Add
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
typename traits< ValueType_T, Tag_T >::simd_type
add( typename traits< ValueType_T, Tag_T >::simd_type, ValueType_T )
{
return traits< ValueType_T, Tag_T >::zero();
}
template<> inline typename traits< char, sse_tag >::simd_type
add< char, sse_tag >( __m128i sval, char val )
{
return _mm_add_epi8( sval, _mm_set1_epi8( val ) );
}
template<> inline typename traits< char, avx_tag >::simd_type
add< char, avx_tag >( __m256i sval, char val )
{
return _mm256_add_epi8( sval, _mm256_set1_epi8( val ) );
}
// Greater than
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
typename traits< ValueType_T, Tag_T >::simd_type
greater_than( typename traits< ValueType_T, Tag_T >::simd_type, ValueType_T )
{
return traits< ValueType_T, Tag_T >::zero();
}
template<> inline typename traits< char, sse_tag >::simd_type
greater_than< char, sse_tag >( __m128i cmp, char key )
{
return _mm_cmpgt_epi8( cmp, _mm_set1_epi8( key ) );
}
template<> inline typename traits< char, avx_tag >::simd_type
greater_than< char, avx_tag >( __m256i cmp, char key )
{
return _mm256_cmpgt_epi8( cmp, _mm256_set1_epi8( key ) );
}
template< typename ValueType_T, typename Tag_T = sse_tag >
typename traits< ValueType_T, Tag_T >::simd_type
greater_than( ValueType_T, typename traits< ValueType_T, Tag_T >::simd_type )
{
return traits< ValueType_T, Tag_T >::zero();
}
template<> inline typename traits< int32_t, sse_tag >::simd_type
greater_than< int32_t, sse_tag >( int32_t key, __m128i cmp )
{
return _mm_cmpgt_epi32( _mm_set1_epi32( key ), cmp );
}
template<> inline typename traits< char, sse_tag >::simd_type
greater_than< char, sse_tag >( char key, __m128i cmp )
{
return _mm_cmpgt_epi8( _mm_set1_epi8( key ), cmp );
}
template<> inline typename traits< int32_t, avx_tag >::simd_type
greater_than< int32_t, avx_tag >( int32_t key, __m256i cmp )
{
return _mm256_cmpgt_epi32( _mm256_set1_epi32( key ), cmp );
}
template<> inline typename traits< char, avx_tag >::simd_type
greater_than< char, avx_tag >( char key, __m256i cmp )
{
return _mm256_cmpgt_epi8( _mm256_set1_epi8( key ), cmp );
}
template< typename ValueType_T, typename Tag_T = sse_tag >
typename traits< ValueType_T, Tag_T >::simd_type
greater_than( typename traits< ValueType_T, Tag_T >::simd_type,
typename traits< ValueType_T, Tag_T >::simd_type )
{
return traits< ValueType_T, Tag_T >::zero();
}
template<> inline typename traits< int32_t, sse_tag >::simd_type
greater_than< int32_t, sse_tag >( __m128i lhs, __m128i rhs )
{
return _mm_cmpgt_epi32( lhs, rhs );
}
template<> inline typename traits< int32_t, avx_tag >::simd_type
greater_than< int32_t, avx_tag >( __m256i lhs, __m256i rhs )
{
return _mm256_cmpgt_epi32( lhs, rhs );
}
// Greater than mask
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
inline uint32_t greater_than_mask( ValueType_T, typename traits< ValueType_T, Tag_T >::simd_type )
{
return 0;
}
template<> inline uint32_t
greater_than_mask< int32_t, sse_tag >( int32_t key, __m128i cmp )
{
return _mm_movemask_epi8( _mm_cmpgt_epi32( _mm_set1_epi32( key ), cmp ) );
}
template<> inline uint32_t
greater_than_mask< int32_t, avx_tag >( int32_t key, __m256i cmp )
{
return _mm256_movemask_epi8( _mm256_cmpgt_epi32( _mm256_set1_epi32( key ), cmp ) );
}
// Mask to index
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
inline uint32_t greater_than_index( ValueType_T val,
typename traits< ValueType_T, Tag_T >::simd_type simdVal )
{
return mask_to_index< ValueType_T, Tag_T >(
greater_than_mask< ValueType_T, Tag_T >( val, simdVal ) );
}
// Equal mask
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
inline uint32_t equal_mask( ValueType_T, typename traits< ValueType_T, Tag_T >::simd_type )
{
return 0;
}
template<> inline uint32_t
equal_mask< int32_t, sse_tag >( int32_t key, __m128i cmp )
{
return _mm_movemask_epi8( _mm_cmpeq_epi32( _mm_set1_epi32( key ), cmp ) );
}
template<> inline uint32_t
equal_mask< int32_t, avx_tag >( int32_t key, __m256i cmp )
{
return _mm256_movemask_epi8( _mm256_cmpeq_epi32( _mm256_set1_epi32( key ), cmp ) );
}
// Equal index
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
inline uint32_t equal_index( ValueType_T val,
typename traits< ValueType_T, Tag_T >::simd_type simdVal )
{
return mask_to_index< ValueType_T, Tag_T >(
equal_mask< ValueType_T, Tag_T >( val, simdVal ) ) - 1;
}
// Low insert
// ------------------------------------------------------------------------------------------------
template< typename ValueType_T, typename Tag_T = sse_tag >
inline typename traits< ValueType_T, Tag_T >::simd_type
high_insert( typename traits< ValueType_T, Tag_T >::simd_type, ValueType_T )
{
return traits< ValueType_T, Tag_T >::zero();
}
template<> inline __m128i
high_insert< int32_t, sse_tag >( __m128i vec, int32_t val )
{
return _mm_insert_epi32( _mm_shuffle_epi32( vec, _MM_SHUFFLE( 3, 3, 2, 1 ) ), val, 3 );
}
template<> inline __m256i
high_insert< int32_t, avx_tag >( __m256i vec, int32_t val )
{
return _mm256_insert_epi32( _mm256_permutevar8x32_epi32( vec, _mm256_set_epi32( 7, 7, 6, 5, 4, 3, 2, 1 ) ),
val, 7 );
}
} //namespace simd_algorithms
#endif // SIMD_ALGORITHMS_BINARY_SEARCH_H