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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
pub type Simd = __m256i;
pub type HalfSimd = __m128i;
pub type TraceType = i32;
pub const L: usize = 16;
pub const L_BYTES: usize = L * 2;
pub const HALFSIMD_MUL: usize = 1;
pub const ZERO: i16 = 1 << 14;
pub const MIN: i16 = 0;
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn store_trace(ptr: *mut TraceType, trace: TraceType) { _mm_stream_si32(ptr, trace); }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_adds_i16(a: Simd, b: Simd) -> Simd { _mm256_adds_epi16(a, b) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_subs_i16(a: Simd, b: Simd) -> Simd { _mm256_subs_epi16(a, b) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_max_i16(a: Simd, b: Simd) -> Simd { _mm256_max_epi16(a, b) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_cmpeq_i16(a: Simd, b: Simd) -> Simd { _mm256_cmpeq_epi16(a, b) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_cmpgt_i16(a: Simd, b: Simd) -> Simd { _mm256_cmpgt_epi16(a, b) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_blend_i8(a: Simd, b: Simd, mask: Simd) -> Simd { _mm256_blendv_epi8(a, b, mask) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_load(ptr: *const Simd) -> Simd { _mm256_load_si256(ptr) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_store(ptr: *mut Simd, a: Simd) { _mm256_store_si256(ptr, a) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_set1_i16(v: i16) -> Simd { _mm256_set1_epi16(v) }
#[macro_export]
#[doc(hidden)]
macro_rules! simd_extract_i16 {
($a:expr, $num:expr) => {
{
debug_assert!($num < L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
_mm256_extract_epi16($a, $num as i32) as i16
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! simd_insert_i16 {
($a:expr, $v:expr, $num:expr) => {
{
debug_assert!($num < L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
_mm256_insert_epi16($a, $v, $num as i32)
}
};
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_movemask_i8(a: Simd) -> u32 { _mm256_movemask_epi8(a) as u32 }
#[macro_export]
#[doc(hidden)]
macro_rules! simd_sl_i16 {
($a:expr, $b:expr, $num:expr) => {
{
debug_assert!(2 * $num <= L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
if $num == L / 2 {
_mm256_permute2x128_si256($a, $b, 0x03)
} else {
_mm256_alignr_epi8($a, _mm256_permute2x128_si256($a, $b, 0x03), (L - (2 * $num)) as i32)
}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! simd_sr_i16 {
($a:expr, $b:expr, $num:expr) => {
{
debug_assert!(2 * $num <= L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
if $num == L / 2 {
_mm256_permute2x128_si256($a, $b, 0x03)
} else {
_mm256_alignr_epi8(_mm256_permute2x128_si256($a, $b, 0x03), $b, (2 * $num) as i32)
}
}
};
}
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn simd_sl_i128(a: Simd, b: Simd) -> Simd {
_mm256_permute2x128_si256(a, b, 0x03)
}
macro_rules! simd_sllz_i16 {
($a:expr, $num:expr) => {
{
debug_assert!(2 * $num < L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
_mm256_slli_si256($a, ($num * 2) as i32)
}
};
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_broadcasthi_i16(v: Simd) -> Simd {
let v = _mm256_shufflehi_epi16(v, 0b11111111);
_mm256_permute4x64_epi64(v, 0b11111111)
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_slow_extract_i16(v: Simd, i: usize) -> i16 {
debug_assert!(i < L);
#[repr(align(32))]
struct A([i16; L]);
let mut a = A([0i16; L]);
simd_store(a.0.as_mut_ptr() as *mut Simd, v);
*a.0.as_ptr().add(i)
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_hmax_i16(v: Simd) -> i16 {
let mut v2 = _mm256_max_epi16(v, _mm256_srli_si256(v, 2));
v2 = _mm256_max_epi16(v2, _mm256_srli_si256(v2, 4));
v2 = _mm256_max_epi16(v2, _mm256_srli_si256(v2, 8));
v2 = _mm256_max_epi16(v2, simd_sl_i128(v2, v2));
simd_extract_i16!(v2, 0)
}
#[macro_export]
#[doc(hidden)]
macro_rules! simd_prefix_hadd_i16 {
($a:expr, $num:expr) => {
{
debug_assert!(2 * $num <= L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
let mut v = _mm256_subs_epi16($a, _mm256_set1_epi16(ZERO));
if $num > 4 {
v = _mm256_adds_epi16(v, _mm256_srli_si256(v, 8));
}
if $num > 2 {
v = _mm256_adds_epi16(v, _mm256_srli_si256(v, 4));
}
if $num > 1 {
v = _mm256_adds_epi16(v, _mm256_srli_si256(v, 2));
}
simd_extract_i16!(v, 0)
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! simd_prefix_hmax_i16 {
($a:expr, $num:expr) => {
{
debug_assert!(2 * $num <= L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
let mut v = $a;
if $num > 4 {
v = _mm256_max_epi16(v, _mm256_srli_si256(v, 8));
}
if $num > 2 {
v = _mm256_max_epi16(v, _mm256_srli_si256(v, 4));
}
if $num > 1 {
v = _mm256_max_epi16(v, _mm256_srli_si256(v, 2));
}
simd_extract_i16!(v, 0)
}
};
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn simd_hargmax_i16(v: Simd, max: i16) -> usize {
let v2 = _mm256_cmpeq_epi16(v, _mm256_set1_epi16(max));
(simd_movemask_i8(v2).trailing_zeros() as usize) / 2
}
#[target_feature(enable = "avx2")]
#[inline]
#[allow(non_snake_case)]
#[allow(dead_code)]
pub unsafe fn simd_naive_prefix_scan_i16(R_max: Simd, (gap_cost, _gap_cost12345678): PrefixScanConsts) -> Simd {
let mut curr = R_max;
for _i in 0..(L - 1) {
let prev = curr;
curr = simd_sl_i16!(curr, _mm256_setzero_si256(), 1);
curr = _mm256_adds_epi16(curr, gap_cost);
curr = _mm256_max_epi16(curr, prev);
}
curr
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn get_gap_extend_all(gap: i16) -> Simd {
_mm256_set_epi16(
gap * 16, gap * 15, gap * 14, gap * 13,
gap * 12, gap * 11, gap * 10, gap * 9,
gap * 8, gap * 7, gap * 6, gap * 5,
gap * 4, gap * 3, gap * 2, gap * 1
)
}
pub type PrefixScanConsts = (Simd, Simd);
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn get_prefix_scan_consts(gap: i16) -> PrefixScanConsts {
let gap_cost = _mm256_set1_epi16(gap);
let gap_cost12345678 = _mm256_set_epi16(
gap * 8, gap * 7, gap * 6, gap * 5,
gap * 4, gap * 3, gap * 2, gap * 1,
gap * 8, gap * 7, gap * 6, gap * 5,
gap * 4, gap * 3, gap * 2, gap * 1
);
(gap_cost, gap_cost12345678)
}
#[target_feature(enable = "avx2")]
#[inline]
#[allow(non_snake_case)]
pub unsafe fn simd_prefix_scan_i16(R_max: Simd, (gap_cost, gap_cost12345678): PrefixScanConsts) -> Simd {
let mut shift1 = simd_sllz_i16!(R_max, 1);
shift1 = _mm256_adds_epi16(shift1, gap_cost);
shift1 = _mm256_max_epi16(R_max, shift1);
let mut shift2 = simd_sllz_i16!(shift1, 2);
shift2 = _mm256_adds_epi16(shift2, _mm256_slli_epi16(gap_cost, 1));
shift2 = _mm256_max_epi16(shift1, shift2);
let mut shift4 = simd_sllz_i16!(shift2, 4);
shift4 = _mm256_adds_epi16(shift4, _mm256_slli_epi16(gap_cost, 2));
shift4 = _mm256_max_epi16(shift2, shift4);
let mut correct1 = _mm256_shufflehi_epi16(shift4, 0b11111111);
correct1 = _mm256_permute4x64_epi64(correct1, 0b01010000);
correct1 = _mm256_adds_epi16(correct1, gap_cost12345678);
_mm256_max_epi16(shift4, correct1)
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_lookup2_i16(lut1: HalfSimd, lut2: HalfSimd, v: HalfSimd) -> Simd {
let a = _mm_shuffle_epi8(lut1, v);
let b = _mm_shuffle_epi8(lut2, v);
let mask = _mm_slli_epi16(v, 3);
let c = _mm_blendv_epi8(a, b, mask);
_mm256_cvtepi8_epi16(c)
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_lookup1_i16(lut: HalfSimd, v: HalfSimd) -> Simd {
_mm256_cvtepi8_epi16(_mm_shuffle_epi8(lut, v))
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_lookup_bytes_i16(match_scores: HalfSimd, mismatch_scores: HalfSimd, a: HalfSimd, b: HalfSimd) -> Simd {
let mask = _mm_cmpeq_epi8(a, b);
let c = _mm_blendv_epi8(mismatch_scores, match_scores, mask);
_mm256_cvtepi8_epi16(c)
}
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_load(ptr: *const HalfSimd) -> HalfSimd { _mm_load_si128(ptr) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_loadu(ptr: *const HalfSimd) -> HalfSimd { _mm_loadu_si128(ptr) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_store(ptr: *mut HalfSimd, a: HalfSimd) { _mm_store_si128(ptr, a) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_sub_i8(a: HalfSimd, b: HalfSimd) -> HalfSimd { _mm_sub_epi8(a, b) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_set1_i8(v: i8) -> HalfSimd { _mm_set1_epi8(v) }
#[target_feature(enable = "avx2")]
#[inline]
pub unsafe fn halfsimd_get_idx(i: usize) -> usize { i }
#[macro_export]
#[doc(hidden)]
macro_rules! halfsimd_sr_i8 {
($a:expr, $b:expr, $num:expr) => {
{
debug_assert!($num <= L);
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
_mm_alignr_epi8($a, $b, $num as i32)
}
};
}
#[target_feature(enable = "avx2")]
#[allow(dead_code)]
pub unsafe fn simd_dbg_i16(v: Simd) {
#[repr(align(32))]
struct A([i16; L]);
let mut a = A([0i16; L]);
simd_store(a.0.as_mut_ptr() as *mut Simd, v);
for i in (0..a.0.len()).rev() {
print!("{:6} ", a.0[i]);
}
println!();
}
#[target_feature(enable = "avx2")]
#[allow(dead_code)]
pub unsafe fn halfsimd_dbg_i8(v: HalfSimd) {
#[repr(align(16))]
struct A([i8; L]);
let mut a = A([0i8; L]);
halfsimd_store(a.0.as_mut_ptr() as *mut HalfSimd, v);
for i in (0..a.0.len()).rev() {
print!("{:3} ", a.0[i]);
}
println!();
}
#[target_feature(enable = "avx2")]
#[allow(dead_code)]
pub unsafe fn simd_assert_vec_eq(a: Simd, b: [i16; L]) {
#[repr(align(32))]
struct A([i16; L]);
let mut arr = A([0i16; L]);
simd_store(arr.0.as_mut_ptr() as *mut Simd, a);
assert_eq!(arr.0, b);
}
#[target_feature(enable = "avx2")]
#[allow(dead_code)]
pub unsafe fn halfsimd_assert_vec_eq(a: HalfSimd, b: [i8; L]) {
#[repr(align(32))]
struct A([i8; L]);
let mut arr = A([0i8; L]);
halfsimd_store(arr.0.as_mut_ptr() as *mut HalfSimd, a);
assert_eq!(arr.0, b);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prefix_scan() {
#[target_feature(enable = "avx2")]
unsafe fn inner() {
#[repr(align(32))]
struct A([i16; L]);
let vec = A([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 12, 13, 14, 11]);
let consts = get_prefix_scan_consts(0);
let res = simd_prefix_scan_i16(simd_load(vec.0.as_ptr() as *const Simd), consts);
simd_assert_vec_eq(res, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 15, 15, 15, 15]);
let vec = A([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 12, 13, 14, 11]);
let consts = get_prefix_scan_consts(-1);
let res = simd_prefix_scan_i16(simd_load(vec.0.as_ptr() as *const Simd), consts);
simd_assert_vec_eq(res, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 14, 13, 14, 13]);
}
unsafe { inner(); }
}
}