| 1 | //! This module provides wrappers for SIMD intrinsics, so they can be used on multiple platforms. |
| 2 | |
| 3 | use std::*; |
| 4 | |
| 5 | #[cfg(target_arch = "x86")] |
| 6 | use std::arch::x86::*; |
| 7 | |
| 8 | #[cfg(target_arch = "x86_64")] |
| 9 | use std::arch::x86_64::*; |
| 10 | |
| 11 | /// Jewel provides a uniform interface for SIMD operations. |
| 12 | /// |
| 13 | /// To save space, most operations are modify in place. |
| 14 | /// Jewel vectors can be easily printed for debugging purposes. |
| 15 | /// Additionally, the functions should be inlined to the caller for |
| 16 | /// maximum efficiency. |
| 17 | pub trait Jewel: fmt::Display { |
| 18 | /// Functions for allocating memory and creating a new Jewel vector. |
| 19 | unsafe fn repeating(val: u32, len: usize) -> Self; |
| 20 | unsafe fn repeating_max(len: usize) -> Self; |
| 21 | |
| 22 | /// Figure out the length of the created vector, which may |
| 23 | /// be longer than the length given by the caller. |
| 24 | fn upper_bound(&self) -> usize; |
| 25 | /// Figure out the length if it is static. |
| 26 | fn static_upper_bound() -> usize; |
| 27 | |
| 28 | /// These operations do not have to be very efficient. |
| 29 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool); |
| 30 | unsafe fn slow_extract(&self, i: usize) -> u32; |
| 31 | unsafe fn slow_insert(&mut self, i: usize, val: u32); |
| 32 | |
| 33 | /// These operations modify the Jewel struct where the function is called. |
| 34 | /// last_0 is the last element, last_1 is the second to last, etc. |
| 35 | unsafe fn insert_last_0(&mut self, val: u32); |
| 36 | unsafe fn insert_last_1(&mut self, val: u32); |
| 37 | unsafe fn insert_last_2(&mut self, val: u32); |
| 38 | unsafe fn insert_last_max(&mut self); |
| 39 | unsafe fn insert_first(&mut self, val: u32); |
| 40 | unsafe fn insert_first_max(&mut self); |
| 41 | |
| 42 | unsafe fn add_mut(&mut self, b: &Self); |
| 43 | unsafe fn adds_mut(&mut self, b: &Self); |
| 44 | unsafe fn and_mut(&mut self, b: &Self); |
| 45 | unsafe fn andnot_mut(&mut self, b: &Self); |
| 46 | unsafe fn cmpeq_mut(&mut self, b: &Self); |
| 47 | unsafe fn min_mut(&mut self, b: &Self); |
| 48 | unsafe fn max_mut(&mut self, b: &Self); |
| 49 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self); |
| 50 | unsafe fn shift_left_1_mut(&mut self); |
| 51 | unsafe fn shift_left_2_mut(&mut self); |
| 52 | unsafe fn shift_right_1_mut(&mut self); |
| 53 | |
| 54 | /// These operations overwrite a res vector to reduce memory allocations. |
| 55 | unsafe fn add(a: &Self, b: &Self, res: &mut Self); |
| 56 | unsafe fn adds(a: &Self, b: &Self, res: &mut Self); |
| 57 | unsafe fn andnot(a: &Self, b: &Self, res: &mut Self); |
| 58 | unsafe fn cmpeq(a: &Self, b: &Self, res: &mut Self); |
| 59 | unsafe fn min(a: &Self, b: &Self, res: &mut Self); |
| 60 | unsafe fn max(a: &Self, b: &Self, res: &mut Self); |
| 61 | unsafe fn shift_left_1(a: &Self, res: &mut Self); |
| 62 | unsafe fn shift_right_1(a: &Self, res: &mut Self); |
| 63 | |
| 64 | /// `triple_argmin` will allocate memory and create a new Jewel vector. |
| 65 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self; |
| 66 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, b_gap: &Self, sub_length: &Self, |
| 67 | a_gap_length: &Self, b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self); |
| 68 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self); |
| 69 | } |
| 70 | |
| 71 | // macros to help generate implementations for some of the Jewel vector functions |
| 72 | macro_rules! operation_param2 { |
| 73 | ($target:literal, $fn_name:ident, $intrinsic:ident) => { |
| 74 | #[target_feature(enable = $target)] |
| 75 | #[inline] |
| 76 | unsafe fn $fn_name(a: &Self, b: &Self, res: &mut Self) { |
| 77 | for i in 0..a.v.len() { |
| 78 | *res.v.get_unchecked_mut(i) = $intrinsic(*a.v.get_unchecked(i), *b.v.get_unchecked(i)); |
| 79 | } |
| 80 | } |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | macro_rules! operation_mut_param2 { |
| 85 | ($target:literal, $fn_name:ident, $intrinsic:ident) => { |
| 86 | #[target_feature(enable = $target)] |
| 87 | #[inline] |
| 88 | unsafe fn $fn_name(&mut self, b: &Self) { |
| 89 | for i in 0..self.v.len() { |
| 90 | *self.v.get_unchecked_mut(i) = $intrinsic(*self.v.get_unchecked(i), *b.v.get_unchecked(i)); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | /// N x 32 x 8 vector backed with 256-bit AVX vectors. |
| 97 | macro_rules! create_avx_nx32x8 { |
| 98 | ($name:ident, $num:literal) => { |
| 99 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 100 | pub struct $name { |
| 101 | v: [__m256i; $num] |
| 102 | } |
| 103 | |
| 104 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 105 | impl Jewel for $name { |
| 106 | #[target_feature(enable = "avx2")] |
| 107 | #[inline] |
| 108 | unsafe fn repeating(val: u32, _len: usize) -> Self { |
| 109 | let v = [_mm256_set1_epi8(val as i8); $num]; |
| 110 | |
| 111 | Self{ |
| 112 | v: v |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[target_feature(enable = "avx2")] |
| 117 | #[inline] |
| 118 | unsafe fn repeating_max(_len: usize) -> Self { |
| 119 | let v = [_mm256_set1_epi8(-1i8); $num]; |
| 120 | |
| 121 | Self{ |
| 122 | v: v |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #[inline] |
| 127 | fn upper_bound(&self) -> usize { |
| 128 | self.v.len() << 5 |
| 129 | } |
| 130 | |
| 131 | #[inline] |
| 132 | fn static_upper_bound() -> usize { |
| 133 | $num << 5 |
| 134 | } |
| 135 | |
| 136 | #[target_feature(enable = "avx2")] |
| 137 | #[inline] |
| 138 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool) { |
| 139 | if len == 0 { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | let mut arr = [0u8; 32]; |
| 144 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 145 | let store_idx = if reverse {31} else {0}; |
| 146 | let load_idx = if reverse {0} else {31}; |
| 147 | |
| 148 | for i in 0..len { |
| 149 | let curr_idx = if reverse {idx - i} else {idx + i}; |
| 150 | let arr_idx = curr_idx & 31; |
| 151 | |
| 152 | if arr_idx == store_idx || i == 0 { |
| 153 | // use part of the original array |
| 154 | _mm256_storeu_si256(arr_ptr, *self.v.get_unchecked(curr_idx >> 5)); |
| 155 | } |
| 156 | |
| 157 | *arr.get_unchecked_mut(arr_idx) = *ptr.offset(i as isize); |
| 158 | |
| 159 | if arr_idx == load_idx || i == len - 1 { |
| 160 | *self.v.get_unchecked_mut(curr_idx >> 5) = _mm256_loadu_si256(arr_ptr); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | #[target_feature(enable = "avx2")] |
| 166 | #[inline] |
| 167 | unsafe fn slow_extract(&self, i: usize) -> u32 { |
| 168 | let idx = i >> 5; |
| 169 | let j = i & 31; |
| 170 | let mut arr = [0u8; 32]; |
| 171 | _mm256_storeu_si256(arr.as_mut_ptr() as *mut __m256i, *self.v.get_unchecked(idx)); |
| 172 | *arr.get_unchecked(j) as u32 |
| 173 | } |
| 174 | |
| 175 | #[target_feature(enable = "avx2")] |
| 176 | #[inline] |
| 177 | unsafe fn slow_insert(&mut self, i: usize, val: u32) { |
| 178 | let idx = i >> 5; |
| 179 | let j = i & 31; |
| 180 | let mut arr = [0u8; 32]; |
| 181 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 182 | _mm256_storeu_si256(arr_ptr, *self.v.get_unchecked(idx)); |
| 183 | *arr.get_unchecked_mut(j) = val as u8; |
| 184 | *self.v.get_unchecked_mut(idx) = _mm256_loadu_si256(arr_ptr); |
| 185 | } |
| 186 | |
| 187 | #[target_feature(enable = "avx2")] |
| 188 | #[inline] |
| 189 | unsafe fn insert_last_0(&mut self, val: u32) { |
| 190 | let last = self.v.len() - 1; |
| 191 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi8(*self.v.get_unchecked(last), val as i8, 31i32); |
| 192 | } |
| 193 | |
| 194 | #[target_feature(enable = "avx2")] |
| 195 | #[inline] |
| 196 | unsafe fn insert_last_1(&mut self, val: u32) { |
| 197 | let last = self.v.len() - 1; |
| 198 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi8(*self.v.get_unchecked(last), val as i8, 30i32); |
| 199 | } |
| 200 | |
| 201 | #[target_feature(enable = "avx2")] |
| 202 | #[inline] |
| 203 | unsafe fn insert_last_2(&mut self, val: u32) { |
| 204 | let last = self.v.len() - 1; |
| 205 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi8(*self.v.get_unchecked(last), val as i8, 29i32); |
| 206 | } |
| 207 | |
| 208 | #[target_feature(enable = "avx2")] |
| 209 | #[inline] |
| 210 | unsafe fn insert_last_max(&mut self) { |
| 211 | let last = self.v.len() - 1; |
| 212 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi8(*self.v.get_unchecked(last), -1i8, 31i32); |
| 213 | } |
| 214 | |
| 215 | #[target_feature(enable = "avx2")] |
| 216 | #[inline] |
| 217 | unsafe fn insert_first(&mut self, val: u32) { |
| 218 | *self.v.get_unchecked_mut(0) = _mm256_insert_epi8(*self.v.get_unchecked(0), val as i8, 0i32); |
| 219 | } |
| 220 | |
| 221 | #[target_feature(enable = "avx2")] |
| 222 | #[inline] |
| 223 | unsafe fn insert_first_max(&mut self) { |
| 224 | *self.v.get_unchecked_mut(0) = _mm256_insert_epi8(*self.v.get_unchecked(0), -1i8, 0i32); |
| 225 | } |
| 226 | |
| 227 | operation_mut_param2!("avx2", add_mut, _mm256_add_epi8); |
| 228 | operation_mut_param2!("avx2", adds_mut, _mm256_adds_epu8); |
| 229 | operation_mut_param2!("avx2", and_mut, _mm256_and_si256); |
| 230 | operation_mut_param2!("avx2", andnot_mut, _mm256_andnot_si256); |
| 231 | operation_mut_param2!("avx2", cmpeq_mut, _mm256_cmpeq_epi8); |
| 232 | operation_mut_param2!("avx2", min_mut, _mm256_min_epu8); |
| 233 | operation_mut_param2!("avx2", max_mut, _mm256_max_epu8); |
| 234 | |
| 235 | #[target_feature(enable = "avx2")] |
| 236 | #[inline] |
| 237 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self) { |
| 238 | for i in 0..self.v.len() { |
| 239 | *self.v.get_unchecked_mut(i) = _mm256_blendv_epi8(*self.v.get_unchecked(i), *b.v.get_unchecked(i), *mask.v.get_unchecked(i)); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #[target_feature(enable = "avx2")] |
| 244 | #[inline] |
| 245 | unsafe fn shift_left_1_mut(&mut self) { |
| 246 | for i in 0..(self.v.len() - 1) { |
| 247 | let curr = *self.v.get_unchecked(i); |
| 248 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 249 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 250 | _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i + 1), 0b00100001i32), curr, 1i32); |
| 251 | } |
| 252 | |
| 253 | // last one gets to shift in zeros |
| 254 | let last = self.v.len() - 1; |
| 255 | let curr = *self.v.get_unchecked(last); |
| 256 | // permute concatenates the second half of the last vector and a vector of zeros |
| 257 | *self.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 1i32); |
| 258 | } |
| 259 | |
| 260 | #[target_feature(enable = "avx2")] |
| 261 | #[inline] |
| 262 | unsafe fn shift_left_2_mut(&mut self) { |
| 263 | for i in 0..(self.v.len() - 1) { |
| 264 | let curr = *self.v.get_unchecked(i); |
| 265 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 266 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 267 | _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i + 1), 0b00100001i32), curr, 2i32); |
| 268 | } |
| 269 | |
| 270 | // last one gets to shift in zeros |
| 271 | let last = self.v.len() - 1; |
| 272 | let curr = *self.v.get_unchecked(last); |
| 273 | // permute concatenates the second half of the last vector and a vector of zeros |
| 274 | *self.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 2i32); |
| 275 | } |
| 276 | |
| 277 | #[target_feature(enable = "avx2")] |
| 278 | #[inline] |
| 279 | unsafe fn shift_right_1_mut(&mut self) { |
| 280 | for i in (1..self.v.len()).rev() { |
| 281 | let curr = *self.v.get_unchecked(i); |
| 282 | // permute concatenates the second half of the previous vector and the first half of the current vector |
| 283 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 284 | curr, _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i - 1), 0b00000011i32), 15i32); |
| 285 | } |
| 286 | |
| 287 | // first one gets to shift in zeros |
| 288 | let curr = *self.v.get_unchecked(0); |
| 289 | // permute concatenates a vector of zeros and the first half of the first vector |
| 290 | *self.v.get_unchecked_mut(0) = _mm256_alignr_epi8(curr, _mm256_permute2x128_si256(curr, curr, 0b00001000i32), 15i32); |
| 291 | } |
| 292 | |
| 293 | operation_param2!("avx2", add, _mm256_add_epi8); |
| 294 | operation_param2!("avx2", adds, _mm256_adds_epu8); |
| 295 | operation_param2!("avx2", andnot, _mm256_andnot_si256); |
| 296 | operation_param2!("avx2", cmpeq, _mm256_cmpeq_epi8); |
| 297 | operation_param2!("avx2", min, _mm256_min_epu8); |
| 298 | operation_param2!("avx2", max, _mm256_max_epu8); |
| 299 | |
| 300 | #[target_feature(enable = "avx2")] |
| 301 | #[inline] |
| 302 | unsafe fn shift_left_1(a: &Self, res: &mut Self) { |
| 303 | for i in 0..(a.v.len() - 1) { |
| 304 | let curr = *a.v.get_unchecked(i); |
| 305 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 306 | *res.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 307 | _mm256_permute2x128_si256(curr, *a.v.get_unchecked(i + 1), 0b00100001i32), curr, 1i32); |
| 308 | } |
| 309 | |
| 310 | // last one gets to shift in zeros |
| 311 | let last = a.v.len() - 1; |
| 312 | let curr = *a.v.get_unchecked(last); |
| 313 | // permute concatenates the second half of the last vector and a vector of zeros |
| 314 | *res.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 1i32); |
| 315 | } |
| 316 | |
| 317 | #[target_feature(enable = "avx2")] |
| 318 | #[inline] |
| 319 | unsafe fn shift_right_1(a: &Self, res: &mut Self) { |
| 320 | for i in (1..a.v.len()).rev() { |
| 321 | let curr = *a.v.get_unchecked(i); |
| 322 | // permute concatenates the second half of the previous vector and the first half of the current vector |
| 323 | *res.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 324 | curr, _mm256_permute2x128_si256(curr, *a.v.get_unchecked(i - 1), 0b00000011i32), 15i32); |
| 325 | } |
| 326 | |
| 327 | // first one gets to shift in zeros |
| 328 | let curr = *a.v.get_unchecked(0); |
| 329 | // permute concatenates a vector of zeros and the first half of the first vector |
| 330 | *res.v.get_unchecked_mut(0) = _mm256_alignr_epi8(curr, _mm256_permute2x128_si256(curr, curr, 0b00001000i32), 15i32); |
| 331 | } |
| 332 | |
| 333 | #[target_feature(enable = "avx2")] |
| 334 | #[inline] |
| 335 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self { |
| 336 | // return the edit used in addition to doing a min operation |
| 337 | let mut v = [_mm256_undefined_si256(); $num]; |
| 338 | let twos = _mm256_set1_epi8(2); |
| 339 | |
| 340 | for i in 0..sub.v.len() { |
| 341 | let sub = *sub.v.get_unchecked(i); |
| 342 | let a_gap = *a_gap.v.get_unchecked(i); |
| 343 | let b_gap = *b_gap.v.get_unchecked(i); |
| 344 | |
| 345 | let res_min1 = _mm256_min_epu8(a_gap, b_gap); |
| 346 | // a gap: 2 + -1 = 1, b gap: 2 + 0 = 2 |
| 347 | let res_arg1 = _mm256_add_epi8(twos, _mm256_cmpeq_epi8(a_gap, res_min1)); |
| 348 | |
| 349 | let res_min2 = _mm256_min_epu8(sub, res_min1); |
| 350 | // sub: 0 |
| 351 | let res_arg2 = _mm256_andnot_si256(_mm256_cmpeq_epi8(sub, res_min2), res_arg1); |
| 352 | |
| 353 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 354 | *v.get_unchecked_mut(i) = res_arg2; |
| 355 | } |
| 356 | |
| 357 | Self{ |
| 358 | v: v |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | #[target_feature(enable = "avx2")] |
| 363 | #[inline] |
| 364 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, |
| 365 | b_gap: &Self, sub_length: &Self, a_gap_length: &Self, |
| 366 | b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self) { |
| 367 | // choose the length based on which edit is chosen during the min operation |
| 368 | // secondary objective of maximizing length if edit costs equal |
| 369 | for i in 0..sub.v.len() { |
| 370 | let sub = *sub.v.get_unchecked(i); |
| 371 | let a_gap = *a_gap.v.get_unchecked(i); |
| 372 | let b_gap = *b_gap.v.get_unchecked(i); |
| 373 | let sub_length = *sub_length.v.get_unchecked(i); |
| 374 | let a_gap_length = *a_gap_length.v.get_unchecked(i); |
| 375 | let b_gap_length = *b_gap_length.v.get_unchecked(i); |
| 376 | |
| 377 | let res_min1 = _mm256_min_epu8(a_gap, b_gap); |
| 378 | let a_b_gt_mask = _mm256_cmpeq_epi8(a_gap, res_min1); // a gap: -1, b gap: 0 |
| 379 | let mut res_length1 = _mm256_blendv_epi8(b_gap_length, a_gap_length, a_b_gt_mask); // lengths based on edits |
| 380 | let a_b_eq_mask = _mm256_cmpeq_epi8(a_gap, b_gap); // equal: -1 |
| 381 | let a_b_max_len = _mm256_max_epu8(a_gap_length, b_gap_length); |
| 382 | res_length1 = _mm256_blendv_epi8(res_length1, a_b_max_len, a_b_eq_mask); // maximize length if edits equal |
| 383 | |
| 384 | let res_min2 = _mm256_min_epu8(sub, res_min1); |
| 385 | let sub_gt_mask = _mm256_cmpeq_epi8(sub, res_min2); // sub: -1, prev a or b gap: 0 |
| 386 | let mut res_length2 = _mm256_blendv_epi8(res_length1, sub_length, sub_gt_mask); // length based on edits |
| 387 | let sub_eq_mask = _mm256_cmpeq_epi8(sub, res_min1); |
| 388 | let sub_max_len = _mm256_max_epu8(sub_length, res_length1); |
| 389 | res_length2 = _mm256_blendv_epi8(res_length2, sub_max_len, sub_eq_mask); // maximize length if edits equal |
| 390 | |
| 391 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 392 | *res_length.v.get_unchecked_mut(i) = res_length2; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | #[target_feature(enable = "avx2")] |
| 397 | #[inline] |
| 398 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self) { |
| 399 | // choose the length based on which gap type is chosen during the min operation |
| 400 | // secondary objective of maximizing length if edit costs equal |
| 401 | for i in 0..new_gap.v.len() { |
| 402 | let new_gap = *new_gap.v.get_unchecked(i); |
| 403 | let cont_gap = *res_cont_gap.v.get_unchecked(i); |
| 404 | let new_gap_length = *new_gap_length.v.get_unchecked(i); |
| 405 | let cont_gap_length = *res_cont_gap_length.v.get_unchecked(i); |
| 406 | |
| 407 | let res_min = _mm256_min_epu8(new_gap, cont_gap); |
| 408 | let new_cont_gt_mask = _mm256_cmpeq_epi8(new_gap, res_min); // new gap: -1, continue gap: 0 |
| 409 | let mut res_length = _mm256_blendv_epi8(cont_gap_length, new_gap_length, new_cont_gt_mask); // lengths based on edits |
| 410 | let new_cont_eq_mask = _mm256_cmpeq_epi8(new_gap, cont_gap); // equal: -1 |
| 411 | let new_cont_max_len = _mm256_max_epu8(new_gap_length, cont_gap_length); |
| 412 | res_length = _mm256_blendv_epi8(res_length, new_cont_max_len, new_cont_eq_mask); // maximize length if edits equal |
| 413 | |
| 414 | *res_cont_gap.v.get_unchecked_mut(i) = res_min; |
| 415 | *res_cont_gap_length.v.get_unchecked_mut(i) = res_length; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // this implementation will probably only be used for debugging |
| 421 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 422 | impl fmt::Display for $name { |
| 423 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 424 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 425 | #[target_feature(enable = "avx2")] |
| 426 | #[inline] |
| 427 | unsafe fn fmt_internal(s: &$name, f: &mut fmt::Formatter) -> fmt::Result { |
| 428 | write!(f, "[")?; |
| 429 | |
| 430 | let mut arr = [0u8; 32]; |
| 431 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 432 | |
| 433 | for i in 0..(s.v.len() - 1) { |
| 434 | _mm256_storeu_si256(arr_ptr, *s.v.get_unchecked(i)); |
| 435 | |
| 436 | for j in 0..32 { |
| 437 | write!(f, "{:>3}, ", *arr.get_unchecked(j))?; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // leftover elements |
| 442 | |
| 443 | _mm256_storeu_si256(arr_ptr, *s.v.get_unchecked(s.v.len() - 1)); |
| 444 | |
| 445 | let start = (s.v.len() - 1) << 5; |
| 446 | |
| 447 | for i in 0..(s.upper_bound() - start) { |
| 448 | if i == s.upper_bound() - start - 1 { |
| 449 | write!(f, "{:>3}", *arr.get_unchecked(i))?; |
| 450 | }else{ |
| 451 | write!(f, "{:>3}, ", *arr.get_unchecked(i))?; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | write!(f, "]") |
| 456 | } |
| 457 | |
| 458 | unsafe { fmt_internal(self, f) } |
| 459 | } |
| 460 | } |
| 461 | }; |
| 462 | } |
| 463 | |
| 464 | // constant array size, so the compiler should unroll the loops |
| 465 | create_avx_nx32x8!(Avx1x32x8, 1); |
| 466 | create_avx_nx32x8!(Avx2x32x8, 2); |
| 467 | create_avx_nx32x8!(Avx4x32x8, 4); |
| 468 | create_avx_nx32x8!(Avx8x32x8, 8); |
| 469 | |
| 470 | /// N x 16 x 16 vector backed with 256-bit AVX vectors. |
| 471 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 472 | pub struct AvxNx16x16 { |
| 473 | v: Vec<__m256i> |
| 474 | } |
| 475 | |
| 476 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 477 | impl Jewel for AvxNx16x16 { |
| 478 | #[target_feature(enable = "avx2")] |
| 479 | #[inline] |
| 480 | unsafe fn repeating(val: u32, len: usize) -> Self { |
| 481 | let v = vec![_mm256_set1_epi16(val as i16); (len >> 4) + if (len & 15) > 0 {1} else {0}]; |
| 482 | |
| 483 | Self{ |
| 484 | v: v |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | #[target_feature(enable = "avx2")] |
| 489 | #[inline] |
| 490 | unsafe fn repeating_max(len: usize) -> Self { |
| 491 | let v = vec![_mm256_set1_epi16(-1i16); (len >> 4) + if (len & 15) > 0 {1} else {0}]; |
| 492 | |
| 493 | Self{ |
| 494 | v: v |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | #[inline] |
| 499 | fn upper_bound(&self) -> usize { |
| 500 | self.v.len() << 4 |
| 501 | } |
| 502 | |
| 503 | #[inline] |
| 504 | fn static_upper_bound() -> usize { |
| 505 | unimplemented!() |
| 506 | } |
| 507 | |
| 508 | #[target_feature(enable = "avx2")] |
| 509 | #[inline] |
| 510 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool) { |
| 511 | if len == 0 { |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | let mut arr = [0u16; 16]; |
| 516 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 517 | let store_idx = if reverse {15} else {0}; |
| 518 | let load_idx = if reverse {0} else {15}; |
| 519 | |
| 520 | for i in 0..len { |
| 521 | let curr_idx = if reverse {idx - i} else {idx + i}; |
| 522 | let arr_idx = curr_idx & 15; |
| 523 | |
| 524 | if arr_idx == store_idx || i == 0 { |
| 525 | _mm256_storeu_si256(arr_ptr, *self.v.get_unchecked(curr_idx >> 4)); |
| 526 | } |
| 527 | |
| 528 | *arr.get_unchecked_mut(arr_idx) = *ptr.offset(i as isize) as u16; |
| 529 | |
| 530 | if arr_idx == load_idx || i == len - 1 { |
| 531 | *self.v.get_unchecked_mut(curr_idx >> 4) = _mm256_loadu_si256(arr_ptr); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | #[target_feature(enable = "avx2")] |
| 537 | #[inline] |
| 538 | unsafe fn slow_extract(&self, i: usize) -> u32 { |
| 539 | let idx = i >> 4; |
| 540 | let j = i & 15; |
| 541 | let mut arr = [0u16; 16]; |
| 542 | _mm256_storeu_si256(arr.as_mut_ptr() as *mut __m256i, *self.v.get_unchecked(idx)); |
| 543 | *arr.get_unchecked(j) as u32 |
| 544 | } |
| 545 | |
| 546 | #[target_feature(enable = "avx2")] |
| 547 | #[inline] |
| 548 | unsafe fn slow_insert(&mut self, i: usize, val: u32) { |
| 549 | let idx = i >> 4; |
| 550 | let j = i & 15; |
| 551 | let mut arr = [0u16; 16]; |
| 552 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 553 | _mm256_storeu_si256(arr_ptr, *self.v.get_unchecked(idx)); |
| 554 | *arr.get_unchecked_mut(j) = val as u16; |
| 555 | *self.v.get_unchecked_mut(idx) = _mm256_loadu_si256(arr_ptr); |
| 556 | } |
| 557 | |
| 558 | #[target_feature(enable = "avx2")] |
| 559 | #[inline] |
| 560 | unsafe fn insert_last_0(&mut self, val: u32) { |
| 561 | let last = self.v.len() - 1; |
| 562 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi16(*self.v.get_unchecked(last), val as i16, 15i32); |
| 563 | } |
| 564 | |
| 565 | #[target_feature(enable = "avx2")] |
| 566 | #[inline] |
| 567 | unsafe fn insert_last_1(&mut self, val: u32) { |
| 568 | let last = self.v.len() - 1; |
| 569 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi16(*self.v.get_unchecked(last), val as i16, 14i32); |
| 570 | } |
| 571 | |
| 572 | #[target_feature(enable = "avx2")] |
| 573 | #[inline] |
| 574 | unsafe fn insert_last_2(&mut self, val: u32) { |
| 575 | let last = self.v.len() - 1; |
| 576 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi16(*self.v.get_unchecked(last), val as i16, 13i32); |
| 577 | } |
| 578 | |
| 579 | #[target_feature(enable = "avx2")] |
| 580 | #[inline] |
| 581 | unsafe fn insert_last_max(&mut self) { |
| 582 | let last = self.v.len() - 1; |
| 583 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi16(*self.v.get_unchecked(last), -1i16, 15i32); |
| 584 | } |
| 585 | |
| 586 | #[target_feature(enable = "avx2")] |
| 587 | #[inline] |
| 588 | unsafe fn insert_first(&mut self, val: u32) { |
| 589 | *self.v.get_unchecked_mut(0) = _mm256_insert_epi16(*self.v.get_unchecked(0), val as i16, 0i32); |
| 590 | } |
| 591 | |
| 592 | #[target_feature(enable = "avx2")] |
| 593 | #[inline] |
| 594 | unsafe fn insert_first_max(&mut self) { |
| 595 | *self.v.get_unchecked_mut(0) = _mm256_insert_epi16(*self.v.get_unchecked(0), -1i16, 0i32); |
| 596 | } |
| 597 | |
| 598 | operation_mut_param2!("avx2", add_mut, _mm256_add_epi16); |
| 599 | operation_mut_param2!("avx2", adds_mut, _mm256_adds_epu16); |
| 600 | operation_mut_param2!("avx2", and_mut, _mm256_and_si256); |
| 601 | operation_mut_param2!("avx2", andnot_mut, _mm256_andnot_si256); |
| 602 | operation_mut_param2!("avx2", cmpeq_mut, _mm256_cmpeq_epi16); |
| 603 | operation_mut_param2!("avx2", min_mut, _mm256_min_epu16); |
| 604 | operation_mut_param2!("avx2", max_mut, _mm256_max_epu16); |
| 605 | |
| 606 | #[target_feature(enable = "avx2")] |
| 607 | #[inline] |
| 608 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self) { |
| 609 | for i in 0..self.v.len() { |
| 610 | *self.v.get_unchecked_mut(i) = _mm256_blendv_epi8(*self.v.get_unchecked(i), *b.v.get_unchecked(i), *mask.v.get_unchecked(i)); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | #[target_feature(enable = "avx2")] |
| 615 | #[inline] |
| 616 | unsafe fn shift_left_1_mut(&mut self) { |
| 617 | for i in 0..(self.v.len() - 1) { |
| 618 | let curr = *self.v.get_unchecked(i); |
| 619 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 620 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 621 | _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i + 1), 0b00100001i32), curr, 2i32); |
| 622 | } |
| 623 | |
| 624 | // last one gets to shift in zeros |
| 625 | let last = self.v.len() - 1; |
| 626 | let curr = *self.v.get_unchecked(last); |
| 627 | // permute concatenates the second half of the last vector and a vector of zeros |
| 628 | *self.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 2i32); |
| 629 | } |
| 630 | |
| 631 | #[target_feature(enable = "avx2")] |
| 632 | #[inline] |
| 633 | unsafe fn shift_left_2_mut(&mut self) { |
| 634 | for i in 0..(self.v.len() - 1) { |
| 635 | let curr = *self.v.get_unchecked(i); |
| 636 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 637 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 638 | _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i + 1), 0b00100001i32), curr, 4i32); |
| 639 | } |
| 640 | |
| 641 | // last one gets to shift in zeros |
| 642 | let last = self.v.len() - 1; |
| 643 | let curr = *self.v.get_unchecked(last); |
| 644 | // permute concatenates the second half of the last vector and a vector of zeros |
| 645 | *self.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 4i32); |
| 646 | } |
| 647 | |
| 648 | #[target_feature(enable = "avx2")] |
| 649 | #[inline] |
| 650 | unsafe fn shift_right_1_mut(&mut self) { |
| 651 | for i in (1..self.v.len()).rev() { |
| 652 | let curr = *self.v.get_unchecked(i); |
| 653 | // permute concatenates the second half of the previous vector and the first half of the current vector |
| 654 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 655 | curr, _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i - 1), 0b00000011i32), 14i32); |
| 656 | } |
| 657 | |
| 658 | // first one gets to shift in zeros |
| 659 | let curr = *self.v.get_unchecked(0); |
| 660 | // permute concatenates a vector of zeros and the first half of the first vector |
| 661 | *self.v.get_unchecked_mut(0) = _mm256_alignr_epi8(curr, _mm256_permute2x128_si256(curr, curr, 0b00001000i32), 14i32); |
| 662 | } |
| 663 | |
| 664 | operation_param2!("avx2", add, _mm256_add_epi16); |
| 665 | operation_param2!("avx2", adds, _mm256_adds_epu16); |
| 666 | operation_param2!("avx2", andnot, _mm256_andnot_si256); |
| 667 | operation_param2!("avx2", cmpeq, _mm256_cmpeq_epi16); |
| 668 | operation_param2!("avx2", min, _mm256_min_epu16); |
| 669 | operation_param2!("avx2", max, _mm256_max_epu16); |
| 670 | |
| 671 | #[target_feature(enable = "avx2")] |
| 672 | #[inline] |
| 673 | unsafe fn shift_left_1(a: &Self, res: &mut Self) { |
| 674 | for i in 0..(a.v.len() - 1) { |
| 675 | let curr = *a.v.get_unchecked(i); |
| 676 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 677 | *res.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 678 | _mm256_permute2x128_si256(curr, *a.v.get_unchecked(i + 1), 0b00100001i32), curr, 2i32); |
| 679 | } |
| 680 | |
| 681 | // last one gets to shift in zeros |
| 682 | let last = a.v.len() - 1; |
| 683 | let curr = *a.v.get_unchecked(last); |
| 684 | // permute concatenates the second half of the last vector and a vector of zeros |
| 685 | *res.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 2i32); |
| 686 | } |
| 687 | |
| 688 | #[target_feature(enable = "avx2")] |
| 689 | #[inline] |
| 690 | unsafe fn shift_right_1(a: &Self, res: &mut Self) { |
| 691 | for i in (1..a.v.len()).rev() { |
| 692 | let curr = *a.v.get_unchecked(i); |
| 693 | // permute concatenates the second half of the previous vector and the first half of the current vector |
| 694 | *res.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 695 | curr, _mm256_permute2x128_si256(curr, *a.v.get_unchecked(i - 1), 0b00000011i32), 14i32); |
| 696 | } |
| 697 | |
| 698 | // first one gets to shift in zeros |
| 699 | let curr = *a.v.get_unchecked(0); |
| 700 | // permute concatenates a vector of zeros and the first half of the first vector |
| 701 | *res.v.get_unchecked_mut(0) = _mm256_alignr_epi8(curr, _mm256_permute2x128_si256(curr, curr, 0b00001000i32), 14i32); |
| 702 | } |
| 703 | |
| 704 | #[target_feature(enable = "avx2")] |
| 705 | #[inline] |
| 706 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self { |
| 707 | // return the edit used in addition to doing a min operation |
| 708 | let mut v = Vec::with_capacity(sub.v.len()); |
| 709 | let twos = _mm256_set1_epi16(2); |
| 710 | |
| 711 | for i in 0..sub.v.len() { |
| 712 | let sub = *sub.v.get_unchecked(i); |
| 713 | let a_gap = *a_gap.v.get_unchecked(i); |
| 714 | let b_gap = *b_gap.v.get_unchecked(i); |
| 715 | |
| 716 | let res_min1 = _mm256_min_epu16(a_gap, b_gap); |
| 717 | // a gap: 2 + -1 = 1, b gap: 2 + 0 = 2 |
| 718 | let res_arg1 = _mm256_add_epi16(twos, _mm256_cmpeq_epi16(a_gap, res_min1)); |
| 719 | |
| 720 | let res_min2 = _mm256_min_epu16(sub, res_min1); |
| 721 | // sub: 0 |
| 722 | let res_arg2 = _mm256_andnot_si256(_mm256_cmpeq_epi16(sub, res_min2), res_arg1); |
| 723 | |
| 724 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 725 | v.push(res_arg2); |
| 726 | } |
| 727 | |
| 728 | Self{ |
| 729 | v: v |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | #[target_feature(enable = "avx2")] |
| 734 | #[inline] |
| 735 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, |
| 736 | b_gap: &Self, sub_length: &Self, a_gap_length: &Self, |
| 737 | b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self) { |
| 738 | // choose the length based on which edit is chosen during the min operation |
| 739 | // secondary objective of maximizing length if edit costs equal |
| 740 | for i in 0..sub.v.len() { |
| 741 | let sub = *sub.v.get_unchecked(i); |
| 742 | let a_gap = *a_gap.v.get_unchecked(i); |
| 743 | let b_gap = *b_gap.v.get_unchecked(i); |
| 744 | let sub_length = *sub_length.v.get_unchecked(i); |
| 745 | let a_gap_length = *a_gap_length.v.get_unchecked(i); |
| 746 | let b_gap_length = *b_gap_length.v.get_unchecked(i); |
| 747 | |
| 748 | let res_min1 = _mm256_min_epu16(a_gap, b_gap); |
| 749 | let a_b_gt_mask = _mm256_cmpeq_epi16(a_gap, res_min1); // a gap: -1, b gap: 0 |
| 750 | let mut res_length1 = _mm256_blendv_epi8(b_gap_length, a_gap_length, a_b_gt_mask); // lengths based on edits |
| 751 | let a_b_eq_mask = _mm256_cmpeq_epi16(a_gap, b_gap); // equal: -1 |
| 752 | let a_b_max_len = _mm256_max_epu16(a_gap_length, b_gap_length); |
| 753 | res_length1 = _mm256_blendv_epi8(res_length1, a_b_max_len, a_b_eq_mask); // maximize length if edits equal |
| 754 | |
| 755 | let res_min2 = _mm256_min_epu16(sub, res_min1); |
| 756 | let sub_gt_mask = _mm256_cmpeq_epi16(sub, res_min2); // sub: -1, prev a or b gap: 0 |
| 757 | let mut res_length2 = _mm256_blendv_epi8(res_length1, sub_length, sub_gt_mask); // length based on edits |
| 758 | let sub_eq_mask = _mm256_cmpeq_epi16(sub, res_min1); |
| 759 | let sub_max_len = _mm256_max_epu16(sub_length, res_length1); |
| 760 | res_length2 = _mm256_blendv_epi8(res_length2, sub_max_len, sub_eq_mask); // maximize length if edits equal |
| 761 | |
| 762 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 763 | *res_length.v.get_unchecked_mut(i) = res_length2; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | #[target_feature(enable = "avx2")] |
| 768 | #[inline] |
| 769 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self) { |
| 770 | // choose the length based on which gap type is chosen during the min operation |
| 771 | // secondary objective of maximizing length if edit costs equal |
| 772 | for i in 0..new_gap.v.len() { |
| 773 | let new_gap = *new_gap.v.get_unchecked(i); |
| 774 | let cont_gap = *res_cont_gap.v.get_unchecked(i); |
| 775 | let new_gap_length = *new_gap_length.v.get_unchecked(i); |
| 776 | let cont_gap_length = *res_cont_gap_length.v.get_unchecked(i); |
| 777 | |
| 778 | let res_min = _mm256_min_epu16(new_gap, cont_gap); |
| 779 | let new_cont_gt_mask = _mm256_cmpeq_epi16(new_gap, res_min); // new gap: -1, continue gap: 0 |
| 780 | let mut res_length = _mm256_blendv_epi8(cont_gap_length, new_gap_length, new_cont_gt_mask); // lengths based on edits |
| 781 | let new_cont_eq_mask = _mm256_cmpeq_epi16(new_gap, cont_gap); // equal: -1 |
| 782 | let new_cont_max_len = _mm256_max_epu16(new_gap_length, cont_gap_length); |
| 783 | res_length = _mm256_blendv_epi8(res_length, new_cont_max_len, new_cont_eq_mask); // maximize length if edits equal |
| 784 | |
| 785 | *res_cont_gap.v.get_unchecked_mut(i) = res_min; |
| 786 | *res_cont_gap_length.v.get_unchecked_mut(i) = res_length; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // this implementation will probably only be used for debugging |
| 792 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 793 | impl fmt::Display for AvxNx16x16 { |
| 794 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 795 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 796 | #[target_feature(enable = "avx2")] |
| 797 | #[inline] |
| 798 | unsafe fn fmt_internal(s: &AvxNx16x16, f: &mut fmt::Formatter) -> fmt::Result { |
| 799 | write!(f, "[")?; |
| 800 | |
| 801 | let mut arr = [0u16; 16]; |
| 802 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 803 | |
| 804 | for i in 0..(s.v.len() - 1) { |
| 805 | _mm256_storeu_si256(arr_ptr, *s.v.get_unchecked(i)); |
| 806 | |
| 807 | for j in 0..16 { |
| 808 | write!(f, "{:>3}, ", *arr.get_unchecked(j))?; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // leftover elements |
| 813 | |
| 814 | _mm256_storeu_si256(arr_ptr, *s.v.get_unchecked(s.v.len() - 1)); |
| 815 | |
| 816 | let start = (s.v.len() - 1) << 4; |
| 817 | |
| 818 | for i in 0..(s.upper_bound() - start) { |
| 819 | if i == s.upper_bound() - start - 1 { |
| 820 | write!(f, "{:>3}", *arr.get_unchecked(i))?; |
| 821 | }else{ |
| 822 | write!(f, "{:>3}, ", *arr.get_unchecked(i))?; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | write!(f, "]") |
| 827 | } |
| 828 | |
| 829 | unsafe { fmt_internal(self, f) } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /// N x 8 x 32 vector backed with 256-bit AVX vectors. |
| 834 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 835 | pub struct AvxNx8x32 { |
| 836 | v: Vec<__m256i> |
| 837 | } |
| 838 | |
| 839 | /// Workaround for the lack of the _mm256_adds_epu32 intrinsic. |
| 840 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 841 | #[target_feature(enable = "avx2")] |
| 842 | #[inline] |
| 843 | unsafe fn _mm256_adds_epu32(a: __m256i, b: __m256i) -> __m256i { |
| 844 | let sum = _mm256_add_epi32(a, b); |
| 845 | let min = _mm256_min_epu32(a, sum); |
| 846 | let eq = _mm256_cmpeq_epi32(a, min); |
| 847 | // if the sum is less than a, then saturate |
| 848 | // note: sum is either greater than both a and b or less than both |
| 849 | _mm256_blendv_epi8(_mm256_set1_epi32(-1i32), sum, eq) |
| 850 | } |
| 851 | |
| 852 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 853 | impl Jewel for AvxNx8x32 { |
| 854 | #[target_feature(enable = "avx2")] |
| 855 | #[inline] |
| 856 | unsafe fn repeating(val: u32, len: usize) -> Self { |
| 857 | let v = vec![_mm256_set1_epi32(val as i32); (len >> 3) + if (len & 7) > 0 {1} else {0}]; |
| 858 | |
| 859 | Self{ |
| 860 | v: v |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | #[target_feature(enable = "avx2")] |
| 865 | #[inline] |
| 866 | unsafe fn repeating_max(len: usize) -> Self { |
| 867 | let v = vec![_mm256_set1_epi32(-1i32); (len >> 3) + if (len & 7) > 0 {1} else {0}]; |
| 868 | |
| 869 | Self{ |
| 870 | v: v |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | #[inline] |
| 875 | fn upper_bound(&self) -> usize { |
| 876 | self.v.len() << 3 |
| 877 | } |
| 878 | |
| 879 | #[inline] |
| 880 | fn static_upper_bound() -> usize { |
| 881 | unimplemented!() |
| 882 | } |
| 883 | |
| 884 | #[target_feature(enable = "avx2")] |
| 885 | #[inline] |
| 886 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool) { |
| 887 | if len == 0 { |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | let mut arr = [0u32; 8]; |
| 892 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 893 | let store_idx = if reverse {7} else {0}; |
| 894 | let load_idx = if reverse {0} else {7}; |
| 895 | |
| 896 | for i in 0..len { |
| 897 | let curr_idx = if reverse {idx - i} else {idx + i}; |
| 898 | let arr_idx = curr_idx & 7; |
| 899 | |
| 900 | if arr_idx == store_idx || i == 0 { |
| 901 | _mm256_storeu_si256(arr_ptr, *self.v.get_unchecked(curr_idx >> 3)); |
| 902 | } |
| 903 | |
| 904 | *arr.get_unchecked_mut(arr_idx) = *ptr.offset(i as isize) as u32; |
| 905 | |
| 906 | if arr_idx == load_idx || i == len - 1 { |
| 907 | *self.v.get_unchecked_mut(curr_idx >> 3) = _mm256_loadu_si256(arr_ptr); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | #[target_feature(enable = "avx2")] |
| 913 | #[inline] |
| 914 | unsafe fn slow_extract(&self, i: usize) -> u32 { |
| 915 | let idx = i >> 3; |
| 916 | let j = i & 7; |
| 917 | let mut arr = [0u32; 8]; |
| 918 | _mm256_storeu_si256(arr.as_mut_ptr() as *mut __m256i, *self.v.get_unchecked(idx)); |
| 919 | *arr.get_unchecked(j) |
| 920 | } |
| 921 | |
| 922 | #[target_feature(enable = "avx2")] |
| 923 | #[inline] |
| 924 | unsafe fn slow_insert(&mut self, i: usize, val: u32) { |
| 925 | let idx = i >> 3; |
| 926 | let j = i & 7; |
| 927 | let mut arr = [0u32; 8]; |
| 928 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 929 | _mm256_storeu_si256(arr_ptr, *self.v.get_unchecked(idx)); |
| 930 | *arr.get_unchecked_mut(j) = val; |
| 931 | *self.v.get_unchecked_mut(idx) = _mm256_loadu_si256(arr_ptr); |
| 932 | } |
| 933 | |
| 934 | #[target_feature(enable = "avx2")] |
| 935 | #[inline] |
| 936 | unsafe fn insert_last_0(&mut self, val: u32) { |
| 937 | let last = self.v.len() - 1; |
| 938 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi32(*self.v.get_unchecked(last), val as i32, 7i32); |
| 939 | } |
| 940 | |
| 941 | #[target_feature(enable = "avx2")] |
| 942 | #[inline] |
| 943 | unsafe fn insert_last_1(&mut self, val: u32) { |
| 944 | let last = self.v.len() - 1; |
| 945 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi32(*self.v.get_unchecked(last), val as i32, 6i32); |
| 946 | } |
| 947 | |
| 948 | #[target_feature(enable = "avx2")] |
| 949 | #[inline] |
| 950 | unsafe fn insert_last_2(&mut self, val: u32) { |
| 951 | let last = self.v.len() - 1; |
| 952 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi32(*self.v.get_unchecked(last), val as i32, 5i32); |
| 953 | } |
| 954 | |
| 955 | #[target_feature(enable = "avx2")] |
| 956 | #[inline] |
| 957 | unsafe fn insert_last_max(&mut self) { |
| 958 | let last = self.v.len() - 1; |
| 959 | *self.v.get_unchecked_mut(last) = _mm256_insert_epi32(*self.v.get_unchecked(last), -1i32, 7i32); |
| 960 | } |
| 961 | |
| 962 | #[target_feature(enable = "avx2")] |
| 963 | #[inline] |
| 964 | unsafe fn insert_first(&mut self, val: u32) { |
| 965 | *self.v.get_unchecked_mut(0) = _mm256_insert_epi32(*self.v.get_unchecked(0), val as i32, 0i32); |
| 966 | } |
| 967 | |
| 968 | #[target_feature(enable = "avx2")] |
| 969 | #[inline] |
| 970 | unsafe fn insert_first_max(&mut self) { |
| 971 | *self.v.get_unchecked_mut(0) = _mm256_insert_epi32(*self.v.get_unchecked(0), -1i32, 0i32); |
| 972 | } |
| 973 | |
| 974 | operation_mut_param2!("avx2", add_mut, _mm256_add_epi32); |
| 975 | operation_mut_param2!("avx2", adds_mut, _mm256_adds_epu32); |
| 976 | operation_mut_param2!("avx2", and_mut, _mm256_and_si256); |
| 977 | operation_mut_param2!("avx2", andnot_mut, _mm256_andnot_si256); |
| 978 | operation_mut_param2!("avx2", cmpeq_mut, _mm256_cmpeq_epi32); |
| 979 | operation_mut_param2!("avx2", min_mut, _mm256_min_epu32); |
| 980 | operation_mut_param2!("avx2", max_mut, _mm256_max_epu32); |
| 981 | |
| 982 | #[target_feature(enable = "avx2")] |
| 983 | #[inline] |
| 984 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self) { |
| 985 | for i in 0..self.v.len() { |
| 986 | *self.v.get_unchecked_mut(i) = _mm256_blendv_epi8(*self.v.get_unchecked(i), *b.v.get_unchecked(i), *mask.v.get_unchecked(i)); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | #[target_feature(enable = "avx2")] |
| 991 | #[inline] |
| 992 | unsafe fn shift_left_1_mut(&mut self) { |
| 993 | for i in 0..(self.v.len() - 1) { |
| 994 | let curr = *self.v.get_unchecked(i); |
| 995 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 996 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 997 | _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i + 1), 0b00100001i32), curr, 4i32); |
| 998 | } |
| 999 | |
| 1000 | // last one gets to shift in zeros |
| 1001 | let last = self.v.len() - 1; |
| 1002 | let curr = *self.v.get_unchecked(last); |
| 1003 | // permute concatenates the second half of the last vector and a vector of zeros |
| 1004 | *self.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 4i32); |
| 1005 | } |
| 1006 | |
| 1007 | #[target_feature(enable = "avx2")] |
| 1008 | #[inline] |
| 1009 | unsafe fn shift_left_2_mut(&mut self) { |
| 1010 | for i in 0..(self.v.len() - 1) { |
| 1011 | let curr = *self.v.get_unchecked(i); |
| 1012 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 1013 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 1014 | _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i + 1), 0b00100001i32), curr, 8i32); |
| 1015 | } |
| 1016 | |
| 1017 | // last one gets to shift in zeros |
| 1018 | let last = self.v.len() - 1; |
| 1019 | let curr = *self.v.get_unchecked(last); |
| 1020 | // permute concatenates the second half of the last vector and a vector of zeros |
| 1021 | *self.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 8i32); |
| 1022 | } |
| 1023 | |
| 1024 | #[target_feature(enable = "avx2")] |
| 1025 | #[inline] |
| 1026 | unsafe fn shift_right_1_mut(&mut self) { |
| 1027 | for i in (1..self.v.len()).rev() { |
| 1028 | let curr = *self.v.get_unchecked(i); |
| 1029 | // permute concatenates the second half of the previous vector and the first half of the current vector |
| 1030 | *self.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 1031 | curr, _mm256_permute2x128_si256(curr, *self.v.get_unchecked(i - 1), 0b00000011i32), 12i32); |
| 1032 | } |
| 1033 | |
| 1034 | // first one gets to shift in zeros |
| 1035 | let curr = *self.v.get_unchecked(0); |
| 1036 | // permute concatenates a vector of zeros and the first half of the first vector |
| 1037 | *self.v.get_unchecked_mut(0) = _mm256_alignr_epi8(curr, _mm256_permute2x128_si256(curr, curr, 0b00001000i32), 12i32); |
| 1038 | } |
| 1039 | |
| 1040 | operation_param2!("avx2", add, _mm256_add_epi32); |
| 1041 | operation_param2!("avx2", adds, _mm256_adds_epu32); |
| 1042 | operation_param2!("avx2", andnot, _mm256_andnot_si256); |
| 1043 | operation_param2!("avx2", cmpeq, _mm256_cmpeq_epi32); |
| 1044 | operation_param2!("avx2", min, _mm256_min_epu32); |
| 1045 | operation_param2!("avx2", max, _mm256_max_epu32); |
| 1046 | |
| 1047 | #[target_feature(enable = "avx2")] |
| 1048 | #[inline] |
| 1049 | unsafe fn shift_left_1(a: &Self, res: &mut Self) { |
| 1050 | for i in 0..(a.v.len() - 1) { |
| 1051 | let curr = *a.v.get_unchecked(i); |
| 1052 | // permute concatenates the second half of the current vector and the first half of the next vector |
| 1053 | *res.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 1054 | _mm256_permute2x128_si256(curr, *a.v.get_unchecked(i + 1), 0b00100001i32), curr, 4i32); |
| 1055 | } |
| 1056 | |
| 1057 | // last one gets to shift in zeros |
| 1058 | let last = a.v.len() - 1; |
| 1059 | let curr = *a.v.get_unchecked(last); |
| 1060 | // permute concatenates the second half of the last vector and a vector of zeros |
| 1061 | *res.v.get_unchecked_mut(last) = _mm256_alignr_epi8(_mm256_permute2x128_si256(curr, curr, 0b10000001i32), curr, 4i32); |
| 1062 | } |
| 1063 | |
| 1064 | #[target_feature(enable = "avx2")] |
| 1065 | #[inline] |
| 1066 | unsafe fn shift_right_1(a: &Self, res: &mut Self) { |
| 1067 | for i in (1..a.v.len()).rev() { |
| 1068 | let curr = *a.v.get_unchecked(i); |
| 1069 | // permute concatenates the second half of the previous vector and the first half of the current vector |
| 1070 | *res.v.get_unchecked_mut(i) = _mm256_alignr_epi8( |
| 1071 | curr, _mm256_permute2x128_si256(curr, *a.v.get_unchecked(i - 1), 0b00000011i32), 12i32); |
| 1072 | } |
| 1073 | |
| 1074 | // first one gets to shift in zeros |
| 1075 | let curr = *a.v.get_unchecked(0); |
| 1076 | // permute concatenates a vector of zeros and the first half of the first vector |
| 1077 | *res.v.get_unchecked_mut(0) = _mm256_alignr_epi8(curr, _mm256_permute2x128_si256(curr, curr, 0b00001000i32), 12i32); |
| 1078 | } |
| 1079 | |
| 1080 | #[target_feature(enable = "avx2")] |
| 1081 | #[inline] |
| 1082 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self { |
| 1083 | // return the edit used in addition to doing a min operation |
| 1084 | let mut v = Vec::with_capacity(sub.v.len()); |
| 1085 | let twos = _mm256_set1_epi32(2); |
| 1086 | |
| 1087 | for i in 0..sub.v.len() { |
| 1088 | let sub = *sub.v.get_unchecked(i); |
| 1089 | let a_gap = *a_gap.v.get_unchecked(i); |
| 1090 | let b_gap = *b_gap.v.get_unchecked(i); |
| 1091 | |
| 1092 | let res_min1 = _mm256_min_epu32(a_gap, b_gap); |
| 1093 | // a gap: 2 + -1 = 1, b gap: 2 + 0 = 2 |
| 1094 | let res_arg1 = _mm256_add_epi32(twos, _mm256_cmpeq_epi32(a_gap, res_min1)); |
| 1095 | |
| 1096 | let res_min2 = _mm256_min_epu32(sub, res_min1); |
| 1097 | // sub: 0 |
| 1098 | let res_arg2 = _mm256_andnot_si256(_mm256_cmpeq_epi32(sub, res_min2), res_arg1); |
| 1099 | |
| 1100 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 1101 | v.push(res_arg2); |
| 1102 | } |
| 1103 | |
| 1104 | Self{ |
| 1105 | v: v |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | #[target_feature(enable = "avx2")] |
| 1110 | #[inline] |
| 1111 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, |
| 1112 | b_gap: &Self, sub_length: &Self, a_gap_length: &Self, |
| 1113 | b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self) { |
| 1114 | // choose the length based on which edit is chosen during the min operation |
| 1115 | // secondary objective of maximizing length if edit costs equal |
| 1116 | for i in 0..sub.v.len() { |
| 1117 | let sub = *sub.v.get_unchecked(i); |
| 1118 | let a_gap = *a_gap.v.get_unchecked(i); |
| 1119 | let b_gap = *b_gap.v.get_unchecked(i); |
| 1120 | let sub_length = *sub_length.v.get_unchecked(i); |
| 1121 | let a_gap_length = *a_gap_length.v.get_unchecked(i); |
| 1122 | let b_gap_length = *b_gap_length.v.get_unchecked(i); |
| 1123 | |
| 1124 | let res_min1 = _mm256_min_epu32(a_gap, b_gap); |
| 1125 | let a_b_gt_mask = _mm256_cmpeq_epi32(a_gap, res_min1); // a gap: -1, b gap: 0 |
| 1126 | let mut res_length1 = _mm256_blendv_epi8(b_gap_length, a_gap_length, a_b_gt_mask); // lengths based on edits |
| 1127 | let a_b_eq_mask = _mm256_cmpeq_epi32(a_gap, b_gap); // equal: -1 |
| 1128 | let a_b_max_len = _mm256_max_epu32(a_gap_length, b_gap_length); |
| 1129 | res_length1 = _mm256_blendv_epi8(res_length1, a_b_max_len, a_b_eq_mask); // maximize length if edits equal |
| 1130 | |
| 1131 | let res_min2 = _mm256_min_epu32(sub, res_min1); |
| 1132 | let sub_gt_mask = _mm256_cmpeq_epi32(sub, res_min2); // sub: -1, prev a or b gap: 0 |
| 1133 | let mut res_length2 = _mm256_blendv_epi8(res_length1, sub_length, sub_gt_mask); // length based on edits |
| 1134 | let sub_eq_mask = _mm256_cmpeq_epi32(sub, res_min1); |
| 1135 | let sub_max_len = _mm256_max_epu32(sub_length, res_length1); |
| 1136 | res_length2 = _mm256_blendv_epi8(res_length2, sub_max_len, sub_eq_mask); // maximize length if edits equal |
| 1137 | |
| 1138 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 1139 | *res_length.v.get_unchecked_mut(i) = res_length2; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | #[target_feature(enable = "avx2")] |
| 1144 | #[inline] |
| 1145 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self) { |
| 1146 | // choose the length based on which gap type is chosen during the min operation |
| 1147 | // secondary objective of maximizing length if edit costs equal |
| 1148 | for i in 0..new_gap.v.len() { |
| 1149 | let new_gap = *new_gap.v.get_unchecked(i); |
| 1150 | let cont_gap = *res_cont_gap.v.get_unchecked(i); |
| 1151 | let new_gap_length = *new_gap_length.v.get_unchecked(i); |
| 1152 | let cont_gap_length = *res_cont_gap_length.v.get_unchecked(i); |
| 1153 | |
| 1154 | let res_min = _mm256_min_epu32(new_gap, cont_gap); |
| 1155 | let new_cont_gt_mask = _mm256_cmpeq_epi32(new_gap, res_min); // new gap: -1, continue gap: 0 |
| 1156 | let mut res_length = _mm256_blendv_epi8(cont_gap_length, new_gap_length, new_cont_gt_mask); // lengths based on edits |
| 1157 | let new_cont_eq_mask = _mm256_cmpeq_epi32(new_gap, cont_gap); // equal: -1 |
| 1158 | let new_cont_max_len = _mm256_max_epu32(new_gap_length, cont_gap_length); |
| 1159 | res_length = _mm256_blendv_epi8(res_length, new_cont_max_len, new_cont_eq_mask); // maximize length if edits equal |
| 1160 | |
| 1161 | *res_cont_gap.v.get_unchecked_mut(i) = res_min; |
| 1162 | *res_cont_gap_length.v.get_unchecked_mut(i) = res_length; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | // this implementation will probably only be used for debugging |
| 1168 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1169 | impl fmt::Display for AvxNx8x32 { |
| 1170 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1171 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1172 | #[target_feature(enable = "avx2")] |
| 1173 | #[inline] |
| 1174 | unsafe fn fmt_internal(s: &AvxNx8x32, f: &mut fmt::Formatter) -> fmt::Result { |
| 1175 | write!(f, "[")?; |
| 1176 | |
| 1177 | let mut arr = [0u32; 8]; |
| 1178 | let arr_ptr = arr.as_mut_ptr() as *mut __m256i; |
| 1179 | |
| 1180 | for i in 0..(s.v.len() - 1) { |
| 1181 | _mm256_storeu_si256(arr_ptr, *s.v.get_unchecked(i)); |
| 1182 | |
| 1183 | for j in 0..8 { |
| 1184 | write!(f, "{:>3}, ", *arr.get_unchecked(j))?; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | // leftover elements |
| 1189 | |
| 1190 | _mm256_storeu_si256(arr_ptr, *s.v.get_unchecked(s.v.len() - 1)); |
| 1191 | |
| 1192 | let start = (s.v.len() - 1) << 3; |
| 1193 | |
| 1194 | for i in 0..(s.upper_bound() - start) { |
| 1195 | if i == s.upper_bound() - start - 1 { |
| 1196 | write!(f, "{:>3}", *arr.get_unchecked(i))?; |
| 1197 | }else{ |
| 1198 | write!(f, "{:>3}, ", *arr.get_unchecked(i))?; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | return write!(f, "]"); |
| 1203 | } |
| 1204 | |
| 1205 | unsafe { fmt_internal(self, f) } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /// N x 16 x 8 vector backed with 128-bit SSE vectors. |
| 1210 | macro_rules! create_sse_nx16x8 { |
| 1211 | ($name:ident, $num:literal) => { |
| 1212 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1213 | pub struct $name { |
| 1214 | v: [__m128i; $num] |
| 1215 | } |
| 1216 | |
| 1217 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1218 | impl Jewel for $name { |
| 1219 | #[target_feature(enable = "sse4.1")] |
| 1220 | #[inline] |
| 1221 | unsafe fn repeating(val: u32, _len: usize) -> Self { |
| 1222 | let v = [_mm_set1_epi8(val as i8); $num]; |
| 1223 | |
| 1224 | Self{ |
| 1225 | v: v |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | #[target_feature(enable = "sse4.1")] |
| 1230 | #[inline] |
| 1231 | unsafe fn repeating_max(_len: usize) -> Self { |
| 1232 | let v = [_mm_set1_epi8(-1i8); $num]; |
| 1233 | |
| 1234 | Self{ |
| 1235 | v: v |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | #[inline] |
| 1240 | fn upper_bound(&self) -> usize { |
| 1241 | self.v.len() << 4 |
| 1242 | } |
| 1243 | |
| 1244 | #[inline] |
| 1245 | fn static_upper_bound() -> usize { |
| 1246 | $num << 4 |
| 1247 | } |
| 1248 | |
| 1249 | #[target_feature(enable = "sse4.1")] |
| 1250 | #[inline] |
| 1251 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool) { |
| 1252 | if len == 0 { |
| 1253 | return; |
| 1254 | } |
| 1255 | |
| 1256 | let mut arr = [0u8; 16]; |
| 1257 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1258 | let store_idx = if reverse {15} else {0}; |
| 1259 | let load_idx = if reverse {0} else {15}; |
| 1260 | |
| 1261 | for i in 0..len { |
| 1262 | let curr_idx = if reverse {idx - i} else {idx + i}; |
| 1263 | let arr_idx = curr_idx & 15; |
| 1264 | |
| 1265 | if arr_idx == store_idx || i == 0 { |
| 1266 | _mm_storeu_si128(arr_ptr, *self.v.get_unchecked(curr_idx >> 4)); |
| 1267 | } |
| 1268 | |
| 1269 | *arr.get_unchecked_mut(arr_idx) = *ptr.offset(i as isize); |
| 1270 | |
| 1271 | if arr_idx == load_idx || i == len - 1 { |
| 1272 | *self.v.get_unchecked_mut(curr_idx >> 4) = _mm_loadu_si128(arr_ptr); |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | #[target_feature(enable = "sse4.1")] |
| 1278 | #[inline] |
| 1279 | unsafe fn slow_extract(&self, i: usize) -> u32 { |
| 1280 | let idx = i >> 4; |
| 1281 | let j = i & 15; |
| 1282 | let mut arr = [0u8; 16]; |
| 1283 | _mm_storeu_si128(arr.as_mut_ptr() as *mut __m128i, *self.v.get_unchecked(idx)); |
| 1284 | *arr.get_unchecked(j) as u32 |
| 1285 | } |
| 1286 | |
| 1287 | #[target_feature(enable = "sse4.1")] |
| 1288 | #[inline] |
| 1289 | unsafe fn slow_insert(&mut self, i: usize, val: u32) { |
| 1290 | let idx = i >> 4; |
| 1291 | let j = i & 15; |
| 1292 | let mut arr = [0u8; 16]; |
| 1293 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1294 | _mm_storeu_si128(arr_ptr, *self.v.get_unchecked(idx)); |
| 1295 | *arr.get_unchecked_mut(j) = val as u8; |
| 1296 | *self.v.get_unchecked_mut(idx) = _mm_loadu_si128(arr_ptr); |
| 1297 | } |
| 1298 | |
| 1299 | #[target_feature(enable = "sse4.1")] |
| 1300 | #[inline] |
| 1301 | unsafe fn insert_last_0(&mut self, val: u32) { |
| 1302 | let last = self.v.len() - 1; |
| 1303 | *self.v.get_unchecked_mut(last) = _mm_insert_epi8(*self.v.get_unchecked(last), val as i32, 15i32); |
| 1304 | } |
| 1305 | |
| 1306 | #[target_feature(enable = "sse4.1")] |
| 1307 | #[inline] |
| 1308 | unsafe fn insert_last_1(&mut self, val: u32) { |
| 1309 | let last = self.v.len() - 1; |
| 1310 | *self.v.get_unchecked_mut(last) = _mm_insert_epi8(*self.v.get_unchecked(last), val as i32, 14i32); |
| 1311 | } |
| 1312 | |
| 1313 | #[target_feature(enable = "sse4.1")] |
| 1314 | #[inline] |
| 1315 | unsafe fn insert_last_2(&mut self, val: u32) { |
| 1316 | let last = self.v.len() - 1; |
| 1317 | *self.v.get_unchecked_mut(last) = _mm_insert_epi8(*self.v.get_unchecked(last), val as i32, 13i32); |
| 1318 | } |
| 1319 | |
| 1320 | #[target_feature(enable = "sse4.1")] |
| 1321 | #[inline] |
| 1322 | unsafe fn insert_last_max(&mut self) { |
| 1323 | let last = self.v.len() - 1; |
| 1324 | *self.v.get_unchecked_mut(last) = _mm_insert_epi8(*self.v.get_unchecked(last), u8::MAX as i32, 15i32); |
| 1325 | } |
| 1326 | |
| 1327 | #[target_feature(enable = "sse4.1")] |
| 1328 | #[inline] |
| 1329 | unsafe fn insert_first(&mut self, val: u32) { |
| 1330 | *self.v.get_unchecked_mut(0) = _mm_insert_epi8(*self.v.get_unchecked(0), val as i32, 0i32); |
| 1331 | } |
| 1332 | |
| 1333 | #[target_feature(enable = "sse4.1")] |
| 1334 | #[inline] |
| 1335 | unsafe fn insert_first_max(&mut self) { |
| 1336 | *self.v.get_unchecked_mut(0) = _mm_insert_epi8(*self.v.get_unchecked(0), u8::MAX as i32, 0i32); |
| 1337 | } |
| 1338 | |
| 1339 | operation_mut_param2!("sse4.1", add_mut, _mm_add_epi8); |
| 1340 | operation_mut_param2!("sse4.1", adds_mut, _mm_adds_epu8); |
| 1341 | operation_mut_param2!("sse4.1", and_mut, _mm_and_si128); |
| 1342 | operation_mut_param2!("sse4.1", andnot_mut, _mm_andnot_si128); |
| 1343 | operation_mut_param2!("sse4.1", cmpeq_mut, _mm_cmpeq_epi8); |
| 1344 | operation_mut_param2!("sse4.1", min_mut, _mm_min_epu8); |
| 1345 | operation_mut_param2!("sse4.1", max_mut, _mm_max_epu8); |
| 1346 | |
| 1347 | #[target_feature(enable = "sse4.1")] |
| 1348 | #[inline] |
| 1349 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self) { |
| 1350 | for i in 0..self.v.len() { |
| 1351 | *self.v.get_unchecked_mut(i) = _mm_blendv_epi8(*self.v.get_unchecked(i), *b.v.get_unchecked(i), *mask.v.get_unchecked(i)); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | #[target_feature(enable = "sse4.1")] |
| 1356 | #[inline] |
| 1357 | unsafe fn shift_left_1_mut(&mut self) { |
| 1358 | for i in 0..(self.v.len() - 1) { |
| 1359 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i + 1), *self.v.get_unchecked(i), 1i32); |
| 1360 | } |
| 1361 | |
| 1362 | // last one gets to shift in zeros |
| 1363 | let last = self.v.len() - 1; |
| 1364 | *self.v.get_unchecked_mut(last) = _mm_srli_si128(*self.v.get_unchecked(last), 1i32); |
| 1365 | } |
| 1366 | |
| 1367 | #[target_feature(enable = "sse4.1")] |
| 1368 | #[inline] |
| 1369 | unsafe fn shift_left_2_mut(&mut self) { |
| 1370 | for i in 0..(self.v.len() - 1) { |
| 1371 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i + 1), *self.v.get_unchecked(i), 2i32); |
| 1372 | } |
| 1373 | |
| 1374 | // last one gets to shift in zeros |
| 1375 | let last = self.v.len() - 1; |
| 1376 | *self.v.get_unchecked_mut(last) = _mm_srli_si128(*self.v.get_unchecked(last), 2i32); |
| 1377 | } |
| 1378 | |
| 1379 | #[target_feature(enable = "sse4.1")] |
| 1380 | #[inline] |
| 1381 | unsafe fn shift_right_1_mut(&mut self) { |
| 1382 | for i in (1..self.v.len()).rev() { |
| 1383 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i), *self.v.get_unchecked(i - 1), 15i32); |
| 1384 | } |
| 1385 | |
| 1386 | // first one gets to shift in zeros |
| 1387 | *self.v.get_unchecked_mut(0) = _mm_slli_si128(*self.v.get_unchecked(0), 1i32); |
| 1388 | } |
| 1389 | |
| 1390 | operation_param2!("sse4.1", add, _mm_add_epi8); |
| 1391 | operation_param2!("sse4.1", adds, _mm_adds_epu8); |
| 1392 | operation_param2!("sse4.1", andnot, _mm_andnot_si128); |
| 1393 | operation_param2!("sse4.1", cmpeq, _mm_cmpeq_epi8); |
| 1394 | operation_param2!("sse4.1", min, _mm_min_epu8); |
| 1395 | operation_param2!("sse4.1", max, _mm_max_epu8); |
| 1396 | |
| 1397 | #[target_feature(enable = "sse4.1")] |
| 1398 | #[inline] |
| 1399 | unsafe fn shift_left_1(a: &Self, res: &mut Self) { |
| 1400 | for i in 0..(a.v.len() - 1) { |
| 1401 | *res.v.get_unchecked_mut(i) = _mm_alignr_epi8(*a.v.get_unchecked(i + 1), *a.v.get_unchecked(i), 1i32); |
| 1402 | } |
| 1403 | |
| 1404 | // last one gets to shift in zeros |
| 1405 | let last = a.v.len() - 1; |
| 1406 | *res.v.get_unchecked_mut(last) = _mm_srli_si128(*a.v.get_unchecked(last), 1i32); |
| 1407 | } |
| 1408 | |
| 1409 | #[target_feature(enable = "sse4.1")] |
| 1410 | #[inline] |
| 1411 | unsafe fn shift_right_1(a: &Self, res: &mut Self) { |
| 1412 | for i in (1..a.v.len()).rev() { |
| 1413 | *res.v.get_unchecked_mut(i) = _mm_alignr_epi8(*a.v.get_unchecked(i), *a.v.get_unchecked(i - 1), 15i32); |
| 1414 | } |
| 1415 | |
| 1416 | // first one gets to shift in zeros |
| 1417 | *res.v.get_unchecked_mut(0) = _mm_slli_si128(*a.v.get_unchecked(0), 1i32); |
| 1418 | } |
| 1419 | |
| 1420 | #[target_feature(enable = "sse4.1")] |
| 1421 | #[inline] |
| 1422 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self { |
| 1423 | // return the edit used in addition to doing a min operation |
| 1424 | let mut v = [_mm_undefined_si128(); $num]; |
| 1425 | let twos = _mm_set1_epi8(2); |
| 1426 | |
| 1427 | for i in 0..sub.v.len() { |
| 1428 | let sub = *sub.v.get_unchecked(i); |
| 1429 | let a_gap = *a_gap.v.get_unchecked(i); |
| 1430 | let b_gap = *b_gap.v.get_unchecked(i); |
| 1431 | |
| 1432 | let res_min1 = _mm_min_epu8(a_gap, b_gap); |
| 1433 | // a gap: 2 + -1 = 1, b gap: 2 + 0 = 2 |
| 1434 | let res_arg1 = _mm_add_epi8(twos, _mm_cmpeq_epi8(a_gap, res_min1)); |
| 1435 | |
| 1436 | let res_min2 = _mm_min_epu8(sub, res_min1); |
| 1437 | // sub: 0 |
| 1438 | let res_arg2 = _mm_andnot_si128(_mm_cmpeq_epi8(sub, res_min2), res_arg1); |
| 1439 | |
| 1440 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 1441 | *v.get_unchecked_mut(i) = res_arg2; |
| 1442 | } |
| 1443 | |
| 1444 | Self{ |
| 1445 | v: v |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | #[target_feature(enable = "sse4.1")] |
| 1450 | #[inline] |
| 1451 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, |
| 1452 | b_gap: &Self, sub_length: &Self, a_gap_length: &Self, |
| 1453 | b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self) { |
| 1454 | // choose the length based on which edit is chosen during the min operation |
| 1455 | // secondary objective of maximizing length if edit costs equal |
| 1456 | for i in 0..sub.v.len() { |
| 1457 | let sub = *sub.v.get_unchecked(i); |
| 1458 | let a_gap = *a_gap.v.get_unchecked(i); |
| 1459 | let b_gap = *b_gap.v.get_unchecked(i); |
| 1460 | let sub_length = *sub_length.v.get_unchecked(i); |
| 1461 | let a_gap_length = *a_gap_length.v.get_unchecked(i); |
| 1462 | let b_gap_length = *b_gap_length.v.get_unchecked(i); |
| 1463 | |
| 1464 | let res_min1 = _mm_min_epu8(a_gap, b_gap); |
| 1465 | let a_b_gt_mask = _mm_cmpeq_epi8(a_gap, res_min1); // a gap: -1, b gap: 0 |
| 1466 | let mut res_length1 = _mm_blendv_epi8(b_gap_length, a_gap_length, a_b_gt_mask); // lengths based on edits |
| 1467 | let a_b_eq_mask = _mm_cmpeq_epi8(a_gap, b_gap); // equal: -1 |
| 1468 | let a_b_max_len = _mm_max_epu8(a_gap_length, b_gap_length); |
| 1469 | res_length1 = _mm_blendv_epi8(res_length1, a_b_max_len, a_b_eq_mask); // maximize length if edits equal |
| 1470 | |
| 1471 | let res_min2 = _mm_min_epu8(sub, res_min1); |
| 1472 | let sub_gt_mask = _mm_cmpeq_epi8(sub, res_min2); // sub: -1, prev a or b gap: 0 |
| 1473 | let mut res_length2 = _mm_blendv_epi8(res_length1, sub_length, sub_gt_mask); // length based on edits |
| 1474 | let sub_eq_mask = _mm_cmpeq_epi8(sub, res_min1); |
| 1475 | let sub_max_len = _mm_max_epu8(sub_length, res_length1); |
| 1476 | res_length2 = _mm_blendv_epi8(res_length2, sub_max_len, sub_eq_mask); // maximize length if edits equal |
| 1477 | |
| 1478 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 1479 | *res_length.v.get_unchecked_mut(i) = res_length2; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | #[target_feature(enable = "sse4.1")] |
| 1484 | #[inline] |
| 1485 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self) { |
| 1486 | // choose the length based on which gap type is chosen during the min operation |
| 1487 | // secondary objective of maximizing length if edit costs equal |
| 1488 | for i in 0..new_gap.v.len() { |
| 1489 | let new_gap = *new_gap.v.get_unchecked(i); |
| 1490 | let cont_gap = *res_cont_gap.v.get_unchecked(i); |
| 1491 | let new_gap_length = *new_gap_length.v.get_unchecked(i); |
| 1492 | let cont_gap_length = *res_cont_gap_length.v.get_unchecked(i); |
| 1493 | |
| 1494 | let res_min = _mm_min_epu8(new_gap, cont_gap); |
| 1495 | let new_cont_gt_mask = _mm_cmpeq_epi8(new_gap, res_min); // new gap: -1, continue gap: 0 |
| 1496 | let mut res_length = _mm_blendv_epi8(cont_gap_length, new_gap_length, new_cont_gt_mask); // lengths based on edits |
| 1497 | let new_cont_eq_mask = _mm_cmpeq_epi8(new_gap, cont_gap); // equal: -1 |
| 1498 | let new_cont_max_len = _mm_max_epu8(new_gap_length, cont_gap_length); |
| 1499 | res_length = _mm_blendv_epi8(res_length, new_cont_max_len, new_cont_eq_mask); // maximize length if edits equal |
| 1500 | |
| 1501 | *res_cont_gap.v.get_unchecked_mut(i) = res_min; |
| 1502 | *res_cont_gap_length.v.get_unchecked_mut(i) = res_length; |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | // this implementation will probably only be used for debugging |
| 1508 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1509 | impl fmt::Display for $name { |
| 1510 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1511 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1512 | #[target_feature(enable = "sse4.1")] |
| 1513 | #[inline] |
| 1514 | unsafe fn fmt_internal(s: &$name, f: &mut fmt::Formatter) -> fmt::Result { |
| 1515 | write!(f, "[")?; |
| 1516 | |
| 1517 | let mut arr = [0u8; 16]; |
| 1518 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1519 | |
| 1520 | for i in 0..(s.v.len() - 1) { |
| 1521 | _mm_storeu_si128(arr_ptr, *s.v.get_unchecked(i)); |
| 1522 | |
| 1523 | for j in 0..16 { |
| 1524 | write!(f, "{:>3}, ", *arr.get_unchecked(j))?; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | // leftover elements |
| 1529 | |
| 1530 | _mm_storeu_si128(arr_ptr, *s.v.get_unchecked(s.v.len() - 1)); |
| 1531 | |
| 1532 | let start = (s.v.len() - 1) << 4; |
| 1533 | |
| 1534 | for i in 0..(s.upper_bound() - start) { |
| 1535 | if i == s.upper_bound() - start - 1 { |
| 1536 | write!(f, "{:>3}", *arr.get_unchecked(i))?; |
| 1537 | }else{ |
| 1538 | write!(f, "{:>3}, ", *arr.get_unchecked(i))?; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | write!(f, "]") |
| 1543 | } |
| 1544 | |
| 1545 | unsafe { fmt_internal(self, f) } |
| 1546 | } |
| 1547 | } |
| 1548 | }; |
| 1549 | } |
| 1550 | |
| 1551 | // constant array size, so the compiler should unroll the loops |
| 1552 | create_sse_nx16x8!(Sse1x16x8, 1); |
| 1553 | create_sse_nx16x8!(Sse2x16x8, 2); |
| 1554 | create_sse_nx16x8!(Sse4x16x8, 4); |
| 1555 | create_sse_nx16x8!(Sse8x16x8, 8); |
| 1556 | create_sse_nx16x8!(Sse16x16x8, 16); |
| 1557 | |
| 1558 | /// N x 8 x 16 vector backed with 128-bit SSE vectors. |
| 1559 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1560 | pub struct SseNx8x16 { |
| 1561 | v: Vec<__m128i> |
| 1562 | } |
| 1563 | |
| 1564 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1565 | impl Jewel for SseNx8x16 { |
| 1566 | #[target_feature(enable = "sse4.1")] |
| 1567 | #[inline] |
| 1568 | unsafe fn repeating(val: u32, len: usize) -> Self { |
| 1569 | let v = vec![_mm_set1_epi16(val as i16); (len >> 3) + if (len & 7) > 0 {1} else {0}]; |
| 1570 | |
| 1571 | Self{ |
| 1572 | v: v |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | #[target_feature(enable = "sse4.1")] |
| 1577 | #[inline] |
| 1578 | unsafe fn repeating_max(len: usize) -> Self { |
| 1579 | let v = vec![_mm_set1_epi16(-1i16); (len >> 3) + if (len & 7) > 0 {1} else {0}]; |
| 1580 | |
| 1581 | Self{ |
| 1582 | v: v |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | #[inline] |
| 1587 | fn upper_bound(&self) -> usize { |
| 1588 | self.v.len() << 3 |
| 1589 | } |
| 1590 | |
| 1591 | #[inline] |
| 1592 | fn static_upper_bound() -> usize { |
| 1593 | unimplemented!() |
| 1594 | } |
| 1595 | |
| 1596 | #[target_feature(enable = "sse4.1")] |
| 1597 | #[inline] |
| 1598 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool) { |
| 1599 | if len == 0 { |
| 1600 | return; |
| 1601 | } |
| 1602 | |
| 1603 | let mut arr = [0u16; 8]; |
| 1604 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1605 | let store_idx = if reverse {7} else {0}; |
| 1606 | let load_idx = if reverse {0} else {7}; |
| 1607 | |
| 1608 | for i in 0..len { |
| 1609 | let curr_idx = if reverse {idx - i} else {idx + i}; |
| 1610 | let arr_idx = curr_idx & 7; |
| 1611 | |
| 1612 | if arr_idx == store_idx || i == 0 { |
| 1613 | _mm_storeu_si128(arr_ptr, *self.v.get_unchecked(curr_idx >> 3)); |
| 1614 | } |
| 1615 | |
| 1616 | *arr.get_unchecked_mut(arr_idx) = *ptr.offset(i as isize) as u16; |
| 1617 | |
| 1618 | if arr_idx == load_idx || i == len - 1 { |
| 1619 | *self.v.get_unchecked_mut(curr_idx >> 3) = _mm_loadu_si128(arr_ptr); |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | #[target_feature(enable = "sse4.1")] |
| 1625 | #[inline] |
| 1626 | unsafe fn slow_extract(&self, i: usize) -> u32 { |
| 1627 | let idx = i >> 3; |
| 1628 | let j = i & 7; |
| 1629 | let mut arr = [0u16; 8]; |
| 1630 | _mm_storeu_si128(arr.as_mut_ptr() as *mut __m128i, *self.v.get_unchecked(idx)); |
| 1631 | *arr.get_unchecked(j) as u32 |
| 1632 | } |
| 1633 | |
| 1634 | #[target_feature(enable = "sse4.1")] |
| 1635 | #[inline] |
| 1636 | unsafe fn slow_insert(&mut self, i: usize, val: u32) { |
| 1637 | let idx = i >> 3; |
| 1638 | let j = i & 7; |
| 1639 | let mut arr = [0u16; 8]; |
| 1640 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1641 | _mm_storeu_si128(arr_ptr, *self.v.get_unchecked(idx)); |
| 1642 | *arr.get_unchecked_mut(j) = val as u16; |
| 1643 | *self.v.get_unchecked_mut(idx) = _mm_loadu_si128(arr_ptr); |
| 1644 | } |
| 1645 | |
| 1646 | #[target_feature(enable = "sse4.1")] |
| 1647 | #[inline] |
| 1648 | unsafe fn insert_last_0(&mut self, val: u32) { |
| 1649 | let last = self.v.len() - 1; |
| 1650 | *self.v.get_unchecked_mut(last) = _mm_insert_epi16(*self.v.get_unchecked(last), val as i32, 7i32); |
| 1651 | } |
| 1652 | |
| 1653 | #[target_feature(enable = "sse4.1")] |
| 1654 | #[inline] |
| 1655 | unsafe fn insert_last_1(&mut self, val: u32) { |
| 1656 | let last = self.v.len() - 1; |
| 1657 | *self.v.get_unchecked_mut(last) = _mm_insert_epi16(*self.v.get_unchecked(last), val as i32, 6i32); |
| 1658 | } |
| 1659 | |
| 1660 | #[target_feature(enable = "sse4.1")] |
| 1661 | #[inline] |
| 1662 | unsafe fn insert_last_2(&mut self, val: u32) { |
| 1663 | let last = self.v.len() - 1; |
| 1664 | *self.v.get_unchecked_mut(last) = _mm_insert_epi16(*self.v.get_unchecked(last), val as i32, 5i32); |
| 1665 | } |
| 1666 | |
| 1667 | #[target_feature(enable = "sse4.1")] |
| 1668 | #[inline] |
| 1669 | unsafe fn insert_last_max(&mut self) { |
| 1670 | let last = self.v.len() - 1; |
| 1671 | *self.v.get_unchecked_mut(last) = _mm_insert_epi16(*self.v.get_unchecked(last), u16::MAX as i32, 7i32); |
| 1672 | } |
| 1673 | |
| 1674 | #[target_feature(enable = "sse4.1")] |
| 1675 | #[inline] |
| 1676 | unsafe fn insert_first(&mut self, val: u32) { |
| 1677 | *self.v.get_unchecked_mut(0) = _mm_insert_epi16(*self.v.get_unchecked(0), val as i32, 0i32); |
| 1678 | } |
| 1679 | |
| 1680 | #[target_feature(enable = "sse4.1")] |
| 1681 | #[inline] |
| 1682 | unsafe fn insert_first_max(&mut self) { |
| 1683 | *self.v.get_unchecked_mut(0) = _mm_insert_epi16(*self.v.get_unchecked(0), u16::MAX as i32, 0i32); |
| 1684 | } |
| 1685 | |
| 1686 | operation_mut_param2!("sse4.1", add_mut, _mm_add_epi16); |
| 1687 | operation_mut_param2!("sse4.1", adds_mut, _mm_adds_epu16); |
| 1688 | operation_mut_param2!("sse4.1", and_mut, _mm_and_si128); |
| 1689 | operation_mut_param2!("sse4.1", andnot_mut, _mm_andnot_si128); |
| 1690 | operation_mut_param2!("sse4.1", cmpeq_mut, _mm_cmpeq_epi16); |
| 1691 | operation_mut_param2!("sse4.1", min_mut, _mm_min_epu16); |
| 1692 | operation_mut_param2!("sse4.1", max_mut, _mm_max_epu16); |
| 1693 | |
| 1694 | #[target_feature(enable = "sse4.1")] |
| 1695 | #[inline] |
| 1696 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self) { |
| 1697 | for i in 0..self.v.len() { |
| 1698 | *self.v.get_unchecked_mut(i) = _mm_blendv_epi8(*self.v.get_unchecked(i), *b.v.get_unchecked(i), *mask.v.get_unchecked(i)); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | #[target_feature(enable = "sse4.1")] |
| 1703 | #[inline] |
| 1704 | unsafe fn shift_left_1_mut(&mut self) { |
| 1705 | for i in 0..(self.v.len() - 1) { |
| 1706 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i + 1), *self.v.get_unchecked(i), 2i32); |
| 1707 | } |
| 1708 | |
| 1709 | // last one gets to shift in zeros |
| 1710 | let last = self.v.len() - 1; |
| 1711 | *self.v.get_unchecked_mut(last) = _mm_srli_si128(*self.v.get_unchecked(last), 2i32); |
| 1712 | } |
| 1713 | |
| 1714 | #[target_feature(enable = "sse4.1")] |
| 1715 | #[inline] |
| 1716 | unsafe fn shift_left_2_mut(&mut self) { |
| 1717 | for i in 0..(self.v.len() - 1) { |
| 1718 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i + 1), *self.v.get_unchecked(i), 4i32); |
| 1719 | } |
| 1720 | |
| 1721 | // last one gets to shift in zeros |
| 1722 | let last = self.v.len() - 1; |
| 1723 | *self.v.get_unchecked_mut(last) = _mm_srli_si128(*self.v.get_unchecked(last), 4i32); |
| 1724 | } |
| 1725 | |
| 1726 | #[target_feature(enable = "sse4.1")] |
| 1727 | #[inline] |
| 1728 | unsafe fn shift_right_1_mut(&mut self) { |
| 1729 | for i in (1..self.v.len()).rev() { |
| 1730 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i), *self.v.get_unchecked(i - 1), 14i32); |
| 1731 | } |
| 1732 | |
| 1733 | // first one gets to shift in zeros |
| 1734 | *self.v.get_unchecked_mut(0) = _mm_slli_si128(*self.v.get_unchecked(0), 2i32); |
| 1735 | } |
| 1736 | |
| 1737 | operation_param2!("sse4.1", add, _mm_add_epi16); |
| 1738 | operation_param2!("sse4.1", adds, _mm_adds_epu16); |
| 1739 | operation_param2!("sse4.1", andnot, _mm_andnot_si128); |
| 1740 | operation_param2!("sse4.1", cmpeq, _mm_cmpeq_epi16); |
| 1741 | operation_param2!("sse4.1", min, _mm_min_epu16); |
| 1742 | operation_param2!("sse4.1", max, _mm_max_epu16); |
| 1743 | |
| 1744 | #[target_feature(enable = "sse4.1")] |
| 1745 | #[inline] |
| 1746 | unsafe fn shift_left_1(a: &Self, res: &mut Self) { |
| 1747 | for i in 0..(a.v.len() - 1) { |
| 1748 | *res.v.get_unchecked_mut(i) = _mm_alignr_epi8(*a.v.get_unchecked(i + 1), *a.v.get_unchecked(i), 2i32); |
| 1749 | } |
| 1750 | |
| 1751 | // last one gets to shift in zeros |
| 1752 | let last = a.v.len() - 1; |
| 1753 | *res.v.get_unchecked_mut(last) = _mm_srli_si128(*a.v.get_unchecked(last), 2i32); |
| 1754 | } |
| 1755 | |
| 1756 | #[target_feature(enable = "sse4.1")] |
| 1757 | #[inline] |
| 1758 | unsafe fn shift_right_1(a: &Self, res: &mut Self) { |
| 1759 | for i in (1..a.v.len()).rev() { |
| 1760 | *res.v.get_unchecked_mut(i) = _mm_alignr_epi8(*a.v.get_unchecked(i), *a.v.get_unchecked(i - 1), 14i32); |
| 1761 | } |
| 1762 | |
| 1763 | // first one gets to shift in zeros |
| 1764 | *res.v.get_unchecked_mut(0) = _mm_slli_si128(*a.v.get_unchecked(0), 2i32); |
| 1765 | } |
| 1766 | |
| 1767 | #[target_feature(enable = "sse4.1")] |
| 1768 | #[inline] |
| 1769 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self { |
| 1770 | // return the edit used in addition to doing a min operation |
| 1771 | let mut v = Vec::with_capacity(sub.v.len()); |
| 1772 | let twos = _mm_set1_epi16(2); |
| 1773 | |
| 1774 | for i in 0..sub.v.len() { |
| 1775 | let sub = *sub.v.get_unchecked(i); |
| 1776 | let a_gap = *a_gap.v.get_unchecked(i); |
| 1777 | let b_gap = *b_gap.v.get_unchecked(i); |
| 1778 | |
| 1779 | let res_min1 = _mm_min_epu16(a_gap, b_gap); |
| 1780 | // a gap: 2 + -1 = 1, b gap: 2 + 0 = 2 |
| 1781 | let res_arg1 = _mm_add_epi16(twos, _mm_cmpeq_epi16(a_gap, res_min1)); |
| 1782 | |
| 1783 | let res_min2 = _mm_min_epu16(sub, res_min1); |
| 1784 | // sub: 0 |
| 1785 | let res_arg2 = _mm_andnot_si128(_mm_cmpeq_epi16(sub, res_min2), res_arg1); |
| 1786 | |
| 1787 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 1788 | v.push(res_arg2); |
| 1789 | } |
| 1790 | |
| 1791 | Self{ |
| 1792 | v: v |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | #[target_feature(enable = "sse4.1")] |
| 1797 | #[inline] |
| 1798 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, |
| 1799 | b_gap: &Self, sub_length: &Self, a_gap_length: &Self, |
| 1800 | b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self) { |
| 1801 | // choose the length based on which edit is chosen during the min operation |
| 1802 | // secondary objective of maximizing length if edit costs equal |
| 1803 | for i in 0..sub.v.len() { |
| 1804 | let sub = *sub.v.get_unchecked(i); |
| 1805 | let a_gap = *a_gap.v.get_unchecked(i); |
| 1806 | let b_gap = *b_gap.v.get_unchecked(i); |
| 1807 | let sub_length = *sub_length.v.get_unchecked(i); |
| 1808 | let a_gap_length = *a_gap_length.v.get_unchecked(i); |
| 1809 | let b_gap_length = *b_gap_length.v.get_unchecked(i); |
| 1810 | |
| 1811 | let res_min1 = _mm_min_epu16(a_gap, b_gap); |
| 1812 | let a_b_gt_mask = _mm_cmpeq_epi16(a_gap, res_min1); // a gap: -1, b gap: 0 |
| 1813 | let mut res_length1 = _mm_blendv_epi8(b_gap_length, a_gap_length, a_b_gt_mask); // lengths based on edits |
| 1814 | let a_b_eq_mask = _mm_cmpeq_epi16(a_gap, b_gap); // equal: -1 |
| 1815 | let a_b_max_len = _mm_max_epu16(a_gap_length, b_gap_length); |
| 1816 | res_length1 = _mm_blendv_epi8(res_length1, a_b_max_len, a_b_eq_mask); // maximize length if edits equal |
| 1817 | |
| 1818 | let res_min2 = _mm_min_epu16(sub, res_min1); |
| 1819 | let sub_gt_mask = _mm_cmpeq_epi16(sub, res_min2); // sub: -1, prev a or b gap: 0 |
| 1820 | let mut res_length2 = _mm_blendv_epi8(res_length1, sub_length, sub_gt_mask); // length based on edits |
| 1821 | let sub_eq_mask = _mm_cmpeq_epi16(sub, res_min1); |
| 1822 | let sub_max_len = _mm_max_epu16(sub_length, res_length1); |
| 1823 | res_length2 = _mm_blendv_epi8(res_length2, sub_max_len, sub_eq_mask); // maximize length if edits equal |
| 1824 | |
| 1825 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 1826 | *res_length.v.get_unchecked_mut(i) = res_length2; |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | #[target_feature(enable = "sse4.1")] |
| 1831 | #[inline] |
| 1832 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self) { |
| 1833 | // choose the length based on which gap type is chosen during the min operation |
| 1834 | // secondary objective of maximizing length if edit costs equal |
| 1835 | for i in 0..new_gap.v.len() { |
| 1836 | let new_gap = *new_gap.v.get_unchecked(i); |
| 1837 | let cont_gap = *res_cont_gap.v.get_unchecked(i); |
| 1838 | let new_gap_length = *new_gap_length.v.get_unchecked(i); |
| 1839 | let cont_gap_length = *res_cont_gap_length.v.get_unchecked(i); |
| 1840 | |
| 1841 | let res_min = _mm_min_epu16(new_gap, cont_gap); |
| 1842 | let new_cont_gt_mask = _mm_cmpeq_epi16(new_gap, res_min); // new gap: -1, continue gap: 0 |
| 1843 | let mut res_length = _mm_blendv_epi8(cont_gap_length, new_gap_length, new_cont_gt_mask); // lengths based on edits |
| 1844 | let new_cont_eq_mask = _mm_cmpeq_epi16(new_gap, cont_gap); // equal: -1 |
| 1845 | let new_cont_max_len = _mm_max_epu16(new_gap_length, cont_gap_length); |
| 1846 | res_length = _mm_blendv_epi8(res_length, new_cont_max_len, new_cont_eq_mask); // maximize length if edits equal |
| 1847 | |
| 1848 | *res_cont_gap.v.get_unchecked_mut(i) = res_min; |
| 1849 | *res_cont_gap_length.v.get_unchecked_mut(i) = res_length; |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | // this implementation will probably only be used for debugging |
| 1855 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1856 | impl fmt::Display for SseNx8x16 { |
| 1857 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1858 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1859 | #[target_feature(enable = "sse4.1")] |
| 1860 | #[inline] |
| 1861 | unsafe fn fmt_internal(s: &SseNx8x16, f: &mut fmt::Formatter) -> fmt::Result { |
| 1862 | write!(f, "[")?; |
| 1863 | |
| 1864 | let mut arr = [0u16; 8]; |
| 1865 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1866 | |
| 1867 | for i in 0..(s.v.len() - 1) { |
| 1868 | _mm_storeu_si128(arr_ptr, *s.v.get_unchecked(i)); |
| 1869 | |
| 1870 | for j in 0..8 { |
| 1871 | write!(f, "{:>3}, ", *arr.get_unchecked(j))?; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | // leftover elements |
| 1876 | |
| 1877 | _mm_storeu_si128(arr_ptr, *s.v.get_unchecked(s.v.len() - 1)); |
| 1878 | |
| 1879 | let start = (s.v.len() - 1) << 3; |
| 1880 | |
| 1881 | for i in 0..(s.upper_bound() - start) { |
| 1882 | if i == s.upper_bound() - start - 1 { |
| 1883 | write!(f, "{:>3}", *arr.get_unchecked(i))?; |
| 1884 | }else{ |
| 1885 | write!(f, "{:>3}, ", *arr.get_unchecked(i))?; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | write!(f, "]") |
| 1890 | } |
| 1891 | |
| 1892 | unsafe { fmt_internal(self, f) } |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | /// N x 4 x 32 vector backed with 128-bit SSE vectors. |
| 1897 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1898 | pub struct SseNx4x32 { |
| 1899 | v: Vec<__m128i> |
| 1900 | } |
| 1901 | |
| 1902 | /// Workaround for the lack of the _mm_adds_epu32 intrinsic. |
| 1903 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1904 | #[target_feature(enable = "sse4.1")] |
| 1905 | #[inline] |
| 1906 | unsafe fn _mm_adds_epu32(a: __m128i, b: __m128i) -> __m128i { |
| 1907 | let sum = _mm_add_epi32(a, b); |
| 1908 | let min = _mm_min_epu32(a, sum); |
| 1909 | let eq = _mm_cmpeq_epi32(a, min); |
| 1910 | // if the sum is less than a, then saturate |
| 1911 | // note: sum is either greater than both a and b or less than both |
| 1912 | _mm_blendv_epi8(_mm_set1_epi32(-1i32), sum, eq) |
| 1913 | } |
| 1914 | |
| 1915 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 1916 | impl Jewel for SseNx4x32 { |
| 1917 | #[target_feature(enable = "sse4.1")] |
| 1918 | #[inline] |
| 1919 | unsafe fn repeating(val: u32, len: usize) -> Self { |
| 1920 | let v = vec![_mm_set1_epi32(val as i32); (len >> 2) + if (len & 3) > 0 {1} else {0}]; |
| 1921 | |
| 1922 | Self{ |
| 1923 | v: v |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | #[target_feature(enable = "sse4.1")] |
| 1928 | #[inline] |
| 1929 | unsafe fn repeating_max(len: usize) -> Self { |
| 1930 | let v = vec![_mm_set1_epi32(-1i32); (len >> 2) + if (len & 3) > 0 {1} else {0}]; |
| 1931 | |
| 1932 | Self{ |
| 1933 | v: v |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | #[inline] |
| 1938 | fn upper_bound(&self) -> usize { |
| 1939 | self.v.len() << 2 |
| 1940 | } |
| 1941 | |
| 1942 | #[inline] |
| 1943 | fn static_upper_bound() -> usize { |
| 1944 | unimplemented!() |
| 1945 | } |
| 1946 | |
| 1947 | #[target_feature(enable = "sse4.1")] |
| 1948 | #[inline] |
| 1949 | unsafe fn slow_loadu(&mut self, idx: usize, ptr: *const u8, len: usize, reverse: bool) { |
| 1950 | if len == 0 { |
| 1951 | return; |
| 1952 | } |
| 1953 | |
| 1954 | let mut arr = [0u32; 4]; |
| 1955 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1956 | let store_idx = if reverse {3} else {0}; |
| 1957 | let load_idx = if reverse {0} else {3}; |
| 1958 | |
| 1959 | for i in 0..len { |
| 1960 | let curr_idx = if reverse {idx - i} else {idx + i}; |
| 1961 | let arr_idx = curr_idx & 3; |
| 1962 | |
| 1963 | if arr_idx == store_idx || i == 0 { |
| 1964 | _mm_storeu_si128(arr_ptr, *self.v.get_unchecked(curr_idx >> 2)); |
| 1965 | } |
| 1966 | |
| 1967 | *arr.get_unchecked_mut(arr_idx) = *ptr.offset(i as isize) as u32; |
| 1968 | |
| 1969 | if arr_idx == load_idx || i == len - 1 { |
| 1970 | *self.v.get_unchecked_mut(curr_idx >> 2) = _mm_loadu_si128(arr_ptr); |
| 1971 | } |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | #[target_feature(enable = "sse4.1")] |
| 1976 | #[inline] |
| 1977 | unsafe fn slow_extract(&self, i: usize) -> u32 { |
| 1978 | let idx = i >> 2; |
| 1979 | let j = i & 3; |
| 1980 | let mut arr = [0u32; 4]; |
| 1981 | _mm_storeu_si128(arr.as_mut_ptr() as *mut __m128i, *self.v.get_unchecked(idx)); |
| 1982 | *arr.get_unchecked(j) |
| 1983 | } |
| 1984 | |
| 1985 | #[target_feature(enable = "sse4.1")] |
| 1986 | #[inline] |
| 1987 | unsafe fn slow_insert(&mut self, i: usize, val: u32) { |
| 1988 | let idx = i >> 2; |
| 1989 | let j = i & 3; |
| 1990 | let mut arr = [0u32; 4]; |
| 1991 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 1992 | _mm_storeu_si128(arr_ptr, *self.v.get_unchecked(idx)); |
| 1993 | *arr.get_unchecked_mut(j) = val; |
| 1994 | *self.v.get_unchecked_mut(idx) = _mm_loadu_si128(arr_ptr); |
| 1995 | } |
| 1996 | |
| 1997 | #[target_feature(enable = "sse4.1")] |
| 1998 | #[inline] |
| 1999 | unsafe fn insert_last_0(&mut self, val: u32) { |
| 2000 | let last = self.v.len() - 1; |
| 2001 | *self.v.get_unchecked_mut(last) = _mm_insert_epi32(*self.v.get_unchecked(last), val as i32, 3i32); |
| 2002 | } |
| 2003 | |
| 2004 | #[target_feature(enable = "sse4.1")] |
| 2005 | #[inline] |
| 2006 | unsafe fn insert_last_1(&mut self, val: u32) { |
| 2007 | let last = self.v.len() - 1; |
| 2008 | *self.v.get_unchecked_mut(last) = _mm_insert_epi32(*self.v.get_unchecked(last), val as i32, 2i32); |
| 2009 | } |
| 2010 | |
| 2011 | #[target_feature(enable = "sse4.1")] |
| 2012 | #[inline] |
| 2013 | unsafe fn insert_last_2(&mut self, val: u32) { |
| 2014 | let last = self.v.len() - 1; |
| 2015 | *self.v.get_unchecked_mut(last) = _mm_insert_epi32(*self.v.get_unchecked(last), val as i32, 1i32); |
| 2016 | } |
| 2017 | |
| 2018 | #[target_feature(enable = "sse4.1")] |
| 2019 | #[inline] |
| 2020 | unsafe fn insert_last_max(&mut self) { |
| 2021 | let last = self.v.len() - 1; |
| 2022 | *self.v.get_unchecked_mut(last) = _mm_insert_epi32(*self.v.get_unchecked(last), -1i32, 3i32); |
| 2023 | } |
| 2024 | |
| 2025 | #[target_feature(enable = "sse4.1")] |
| 2026 | #[inline] |
| 2027 | unsafe fn insert_first(&mut self, val: u32) { |
| 2028 | *self.v.get_unchecked_mut(0) = _mm_insert_epi32(*self.v.get_unchecked(0), val as i32, 0i32); |
| 2029 | } |
| 2030 | |
| 2031 | #[target_feature(enable = "sse4.1")] |
| 2032 | #[inline] |
| 2033 | unsafe fn insert_first_max(&mut self) { |
| 2034 | *self.v.get_unchecked_mut(0) = _mm_insert_epi32(*self.v.get_unchecked(0), -1i32, 0i32); |
| 2035 | } |
| 2036 | |
| 2037 | operation_mut_param2!("sse4.1", add_mut, _mm_add_epi32); |
| 2038 | operation_mut_param2!("sse4.1", adds_mut, _mm_adds_epu32); |
| 2039 | operation_mut_param2!("sse4.1", and_mut, _mm_and_si128); |
| 2040 | operation_mut_param2!("sse4.1", andnot_mut, _mm_andnot_si128); |
| 2041 | operation_mut_param2!("sse4.1", cmpeq_mut, _mm_cmpeq_epi32); |
| 2042 | operation_mut_param2!("sse4.1", min_mut, _mm_min_epu32); |
| 2043 | operation_mut_param2!("sse4.1", max_mut, _mm_max_epu32); |
| 2044 | |
| 2045 | #[target_feature(enable = "sse4.1")] |
| 2046 | #[inline] |
| 2047 | unsafe fn blendv_mut(&mut self, b: &Self, mask: &Self) { |
| 2048 | for i in 0..self.v.len() { |
| 2049 | *self.v.get_unchecked_mut(i) = _mm_blendv_epi8(*self.v.get_unchecked(i), *b.v.get_unchecked(i), *mask.v.get_unchecked(i)); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | #[target_feature(enable = "sse4.1")] |
| 2054 | #[inline] |
| 2055 | unsafe fn shift_left_1_mut(&mut self) { |
| 2056 | for i in 0..(self.v.len() - 1) { |
| 2057 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i + 1), *self.v.get_unchecked(i), 4i32); |
| 2058 | } |
| 2059 | |
| 2060 | // last one gets to shift in zeros |
| 2061 | let last = self.v.len() - 1; |
| 2062 | *self.v.get_unchecked_mut(last) = _mm_srli_si128(*self.v.get_unchecked(last), 4i32); |
| 2063 | } |
| 2064 | |
| 2065 | #[target_feature(enable = "sse4.1")] |
| 2066 | #[inline] |
| 2067 | unsafe fn shift_left_2_mut(&mut self) { |
| 2068 | for i in 0..(self.v.len() - 1) { |
| 2069 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i + 1), *self.v.get_unchecked(i), 8i32); |
| 2070 | } |
| 2071 | |
| 2072 | // last one gets to shift in zeros |
| 2073 | let last = self.v.len() - 1; |
| 2074 | *self.v.get_unchecked_mut(last) = _mm_srli_si128(*self.v.get_unchecked(last), 8i32); |
| 2075 | } |
| 2076 | |
| 2077 | #[target_feature(enable = "sse4.1")] |
| 2078 | #[inline] |
| 2079 | unsafe fn shift_right_1_mut(&mut self) { |
| 2080 | for i in (1..self.v.len()).rev() { |
| 2081 | *self.v.get_unchecked_mut(i) = _mm_alignr_epi8(*self.v.get_unchecked(i), *self.v.get_unchecked(i - 1), 12i32); |
| 2082 | } |
| 2083 | |
| 2084 | // first one gets to shift in zeros |
| 2085 | *self.v.get_unchecked_mut(0) = _mm_slli_si128(*self.v.get_unchecked(0), 4i32); |
| 2086 | } |
| 2087 | |
| 2088 | operation_param2!("sse4.1", add, _mm_add_epi32); |
| 2089 | operation_param2!("sse4.1", adds, _mm_adds_epu32); |
| 2090 | operation_param2!("sse4.1", andnot, _mm_andnot_si128); |
| 2091 | operation_param2!("sse4.1", cmpeq, _mm_cmpeq_epi32); |
| 2092 | operation_param2!("sse4.1", min, _mm_min_epu32); |
| 2093 | operation_param2!("sse4.1", max, _mm_max_epu32); |
| 2094 | |
| 2095 | #[target_feature(enable = "sse4.1")] |
| 2096 | #[inline] |
| 2097 | unsafe fn shift_left_1(a: &Self, res: &mut Self) { |
| 2098 | for i in 0..(a.v.len() - 1) { |
| 2099 | *res.v.get_unchecked_mut(i) = _mm_alignr_epi8(*a.v.get_unchecked(i + 1), *a.v.get_unchecked(i), 4i32); |
| 2100 | } |
| 2101 | |
| 2102 | // last one gets to shift in zeros |
| 2103 | let last = a.v.len() - 1; |
| 2104 | *res.v.get_unchecked_mut(last) = _mm_srli_si128(*a.v.get_unchecked(last), 4i32); |
| 2105 | } |
| 2106 | |
| 2107 | #[target_feature(enable = "sse4.1")] |
| 2108 | #[inline] |
| 2109 | unsafe fn shift_right_1(a: &Self, res: &mut Self) { |
| 2110 | for i in (1..a.v.len()).rev() { |
| 2111 | *res.v.get_unchecked_mut(i) = _mm_alignr_epi8(*a.v.get_unchecked(i), *a.v.get_unchecked(i - 1), 12i32); |
| 2112 | } |
| 2113 | |
| 2114 | // first one gets to shift in zeros |
| 2115 | *res.v.get_unchecked_mut(0) = _mm_slli_si128(*a.v.get_unchecked(0), 4i32); |
| 2116 | } |
| 2117 | |
| 2118 | #[target_feature(enable = "sse4.1")] |
| 2119 | #[inline] |
| 2120 | unsafe fn triple_argmin(sub: &Self, a_gap: &Self, b_gap: &Self, res_min: &mut Self) -> Self { |
| 2121 | // return the edit used in addition to doing a min operation |
| 2122 | let mut v = Vec::with_capacity(sub.v.len()); |
| 2123 | let twos = _mm_set1_epi32(2); |
| 2124 | |
| 2125 | for i in 0..sub.v.len() { |
| 2126 | let sub = *sub.v.get_unchecked(i); |
| 2127 | let a_gap = *a_gap.v.get_unchecked(i); |
| 2128 | let b_gap = *b_gap.v.get_unchecked(i); |
| 2129 | |
| 2130 | let res_min1 = _mm_min_epu32(a_gap, b_gap); |
| 2131 | // a gap: 2 + -1 = 1, b gap: 2 + 0 = 2 |
| 2132 | let res_arg1 = _mm_add_epi32(twos, _mm_cmpeq_epi32(a_gap, res_min1)); |
| 2133 | |
| 2134 | let res_min2 = _mm_min_epu32(sub, res_min1); |
| 2135 | // sub: 0 |
| 2136 | let res_arg2 = _mm_andnot_si128(_mm_cmpeq_epi32(sub, res_min2), res_arg1); |
| 2137 | |
| 2138 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 2139 | v.push(res_arg2); |
| 2140 | } |
| 2141 | |
| 2142 | Self{ |
| 2143 | v: v |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | #[target_feature(enable = "sse4.1")] |
| 2148 | #[inline] |
| 2149 | unsafe fn triple_min_length(sub: &Self, a_gap: &Self, |
| 2150 | b_gap: &Self, sub_length: &Self, a_gap_length: &Self, |
| 2151 | b_gap_length: &Self, res_min: &mut Self, res_length: &mut Self) { |
| 2152 | // choose the length based on which edit is chosen during the min operation |
| 2153 | // secondary objective of maximizing length if edit costs equal |
| 2154 | for i in 0..sub.v.len() { |
| 2155 | let sub = *sub.v.get_unchecked(i); |
| 2156 | let a_gap = *a_gap.v.get_unchecked(i); |
| 2157 | let b_gap = *b_gap.v.get_unchecked(i); |
| 2158 | let sub_length = *sub_length.v.get_unchecked(i); |
| 2159 | let a_gap_length = *a_gap_length.v.get_unchecked(i); |
| 2160 | let b_gap_length = *b_gap_length.v.get_unchecked(i); |
| 2161 | |
| 2162 | let res_min1 = _mm_min_epu32(a_gap, b_gap); |
| 2163 | let a_b_gt_mask = _mm_cmpeq_epi32(a_gap, res_min1); // a gap: -1, b gap: 0 |
| 2164 | let mut res_length1 = _mm_blendv_epi8(b_gap_length, a_gap_length, a_b_gt_mask); // lengths based on edits |
| 2165 | let a_b_eq_mask = _mm_cmpeq_epi32(a_gap, b_gap); // equal: -1 |
| 2166 | let a_b_max_len = _mm_max_epu32(a_gap_length, b_gap_length); |
| 2167 | res_length1 = _mm_blendv_epi8(res_length1, a_b_max_len, a_b_eq_mask); // maximize length if edits equal |
| 2168 | |
| 2169 | let res_min2 = _mm_min_epu32(sub, res_min1); |
| 2170 | let sub_gt_mask = _mm_cmpeq_epi32(sub, res_min2); // sub: -1, prev a or b gap: 0 |
| 2171 | let mut res_length2 = _mm_blendv_epi8(res_length1, sub_length, sub_gt_mask); // length based on edits |
| 2172 | let sub_eq_mask = _mm_cmpeq_epi32(sub, res_min1); |
| 2173 | let sub_max_len = _mm_max_epu32(sub_length, res_length1); |
| 2174 | res_length2 = _mm_blendv_epi8(res_length2, sub_max_len, sub_eq_mask); // maximize length if edits equal |
| 2175 | |
| 2176 | *res_min.v.get_unchecked_mut(i) = res_min2; |
| 2177 | *res_length.v.get_unchecked_mut(i) = res_length2; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | #[target_feature(enable = "sse4.1")] |
| 2182 | #[inline] |
| 2183 | unsafe fn double_min_length(new_gap: &Self, res_cont_gap: &mut Self, new_gap_length: &Self, res_cont_gap_length: &mut Self) { |
| 2184 | // choose the length based on which gap type is chosen during the min operation |
| 2185 | // secondary objective of maximizing length if edit costs equal |
| 2186 | for i in 0..new_gap.v.len() { |
| 2187 | let new_gap = *new_gap.v.get_unchecked(i); |
| 2188 | let cont_gap = *res_cont_gap.v.get_unchecked(i); |
| 2189 | let new_gap_length = *new_gap_length.v.get_unchecked(i); |
| 2190 | let cont_gap_length = *res_cont_gap_length.v.get_unchecked(i); |
| 2191 | |
| 2192 | let res_min = _mm_min_epu32(new_gap, cont_gap); |
| 2193 | let new_cont_gt_mask = _mm_cmpeq_epi32(new_gap, res_min); // new gap: -1, continue gap: 0 |
| 2194 | let mut res_length = _mm_blendv_epi8(cont_gap_length, new_gap_length, new_cont_gt_mask); // lengths based on edits |
| 2195 | let new_cont_eq_mask = _mm_cmpeq_epi32(new_gap, cont_gap); // equal: -1 |
| 2196 | let new_cont_max_len = _mm_max_epu32(new_gap_length, cont_gap_length); |
| 2197 | res_length = _mm_blendv_epi8(res_length, new_cont_max_len, new_cont_eq_mask); // maximize length if edits equal |
| 2198 | |
| 2199 | *res_cont_gap.v.get_unchecked_mut(i) = res_min; |
| 2200 | *res_cont_gap_length.v.get_unchecked_mut(i) = res_length; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | // this implementation will probably only be used for debugging |
| 2206 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2207 | impl fmt::Display for SseNx4x32 { |
| 2208 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 2209 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2210 | #[target_feature(enable = "sse4.1")] |
| 2211 | #[inline] |
| 2212 | unsafe fn fmt_internal(s: &SseNx4x32, f: &mut fmt::Formatter) -> fmt::Result { |
| 2213 | write!(f, "[")?; |
| 2214 | |
| 2215 | let mut arr = [0u32; 4]; |
| 2216 | let arr_ptr = arr.as_mut_ptr() as *mut __m128i; |
| 2217 | |
| 2218 | for i in 0..(s.v.len() - 1) { |
| 2219 | _mm_storeu_si128(arr_ptr, *s.v.get_unchecked(i)); |
| 2220 | |
| 2221 | for j in 0..4 { |
| 2222 | write!(f, "{:>3}, ", *arr.get_unchecked(j))?; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | // leftover elements |
| 2227 | |
| 2228 | _mm_storeu_si128(arr_ptr, *s.v.get_unchecked(s.v.len() - 1)); |
| 2229 | |
| 2230 | let start = (s.v.len() - 1) << 2; |
| 2231 | |
| 2232 | for i in 0..(s.upper_bound() - start) { |
| 2233 | if i == s.upper_bound() - start - 1 { |
| 2234 | write!(f, "{:>3}", *arr.get_unchecked(i))?; |
| 2235 | }else{ |
| 2236 | write!(f, "{:>3}, ", *arr.get_unchecked(i))?; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | write!(f, "]") |
| 2241 | } |
| 2242 | |
| 2243 | unsafe { fmt_internal(self, f) } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | pub trait HammingJewel { |
| 2248 | unsafe fn loadu(ptr: *const u8, len: usize) -> Self; |
| 2249 | fn upper_bound(&self) -> usize; |
| 2250 | unsafe fn mm_count_mismatches(a_ptr: *const u8, b_ptr: *const u8, len: usize) -> u32; |
| 2251 | unsafe fn count_mismatches(a_ptr: *const u8, b_ptr: *const u8, len: usize) -> u32; |
| 2252 | unsafe fn vector_count_mismatches(a: &Self, b_ptr: *const u8, len: usize) -> u32; |
| 2253 | } |
| 2254 | |
| 2255 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2256 | pub struct Avx { |
| 2257 | v: Vec<__m256i> |
| 2258 | } |
| 2259 | |
| 2260 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2261 | impl HammingJewel for Avx { |
| 2262 | #[target_feature(enable = "avx2")] |
| 2263 | #[inline] |
| 2264 | unsafe fn loadu(ptr: *const u8, len: usize) -> Self { |
| 2265 | let word_len = len >> 5; |
| 2266 | let word_rem = len & 31; |
| 2267 | let mut v = Vec::with_capacity(word_len + if word_rem > 0 {1} else {0}); |
| 2268 | let avx2_ptr = ptr as *const __m256i; |
| 2269 | |
| 2270 | for i in 0..word_len { |
| 2271 | v.push(_mm256_loadu_si256(avx2_ptr.offset(i as isize))); |
| 2272 | } |
| 2273 | |
| 2274 | if word_rem > 0 { |
| 2275 | let mut arr = [0u8; 32]; |
| 2276 | let end_ptr = ptr.offset((word_len << 5) as isize); |
| 2277 | |
| 2278 | for i in 0..word_rem { |
| 2279 | *arr.get_unchecked_mut(i) = *end_ptr.offset(i as isize); |
| 2280 | } |
| 2281 | |
| 2282 | v.push(_mm256_loadu_si256(arr.as_ptr() as *const __m256i)); |
| 2283 | } |
| 2284 | |
| 2285 | Self{ |
| 2286 | v: v |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | #[inline] |
| 2291 | fn upper_bound(&self) -> usize { |
| 2292 | self.v.len() << 5 |
| 2293 | } |
| 2294 | |
| 2295 | #[target_feature(enable = "avx2")] |
| 2296 | #[inline] |
| 2297 | unsafe fn mm_count_mismatches(a_ptr: *const u8, b_ptr: *const u8, len: usize) -> u32 { |
| 2298 | let mut res = 0u32; |
| 2299 | let div_len = (len >> 5) as isize; |
| 2300 | let avx2_a_ptr = a_ptr as *const __m256i; |
| 2301 | let avx2_b_ptr = b_ptr as *const __m256i; |
| 2302 | |
| 2303 | for i in 0..div_len { |
| 2304 | let a = _mm256_loadu_si256(avx2_a_ptr.offset(i)); |
| 2305 | let b = _mm256_loadu_si256(avx2_b_ptr.offset(i)); |
| 2306 | let eq = _mm256_cmpeq_epi8(a, b); |
| 2307 | // basic movemask count equal bytes |
| 2308 | res += _mm256_movemask_epi8(eq).count_ones(); |
| 2309 | } |
| 2310 | |
| 2311 | for i in (div_len << 5)..len as isize { |
| 2312 | res += (*a_ptr.offset(i) == *b_ptr.offset(i)) as u32; |
| 2313 | } |
| 2314 | |
| 2315 | len as u32 - res |
| 2316 | } |
| 2317 | |
| 2318 | #[target_feature(enable = "avx2")] |
| 2319 | #[inline] |
| 2320 | unsafe fn count_mismatches(a_ptr: *const u8, b_ptr: *const u8, len: usize) -> u32 { |
| 2321 | let refresh_len = (len / (255 * 32)) as isize; |
| 2322 | let zeros = _mm256_setzero_si256(); |
| 2323 | let mut sad = zeros; |
| 2324 | let avx2_a_ptr = a_ptr as *const __m256i; |
| 2325 | let avx2_b_ptr = b_ptr as *const __m256i; |
| 2326 | |
| 2327 | for i in 0..refresh_len { |
| 2328 | let mut curr = zeros; |
| 2329 | |
| 2330 | for j in (i * 255)..((i + 1) * 255) { |
| 2331 | let a = _mm256_loadu_si256(avx2_a_ptr.offset(j)); |
| 2332 | let b = _mm256_loadu_si256(avx2_b_ptr.offset(j)); |
| 2333 | let eq = _mm256_cmpeq_epi8(a, b); |
| 2334 | curr = _mm256_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2335 | // counting matches instead of mismatches for speed |
| 2336 | } |
| 2337 | |
| 2338 | // subtract 0 and sum up 8 bytes at once horizontally into four 64 bit ints |
| 2339 | // accumulate those 64 bit ints |
| 2340 | sad = _mm256_add_epi64(sad, _mm256_sad_epu8(curr, zeros)); |
| 2341 | } |
| 2342 | |
| 2343 | let word_len = (len >> 5) as isize; |
| 2344 | let mut curr = zeros; |
| 2345 | |
| 2346 | // leftover blocks of 32 bytes |
| 2347 | for i in (refresh_len * 255)..word_len { |
| 2348 | let a = _mm256_loadu_si256(avx2_a_ptr.offset(i)); |
| 2349 | let b = _mm256_loadu_si256(avx2_b_ptr.offset(i)); |
| 2350 | let eq = _mm256_cmpeq_epi8(a, b); |
| 2351 | curr = _mm256_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2352 | } |
| 2353 | |
| 2354 | sad = _mm256_add_epi64(sad, _mm256_sad_epu8(curr, zeros)); |
| 2355 | let mut sad_arr = [0u32; 8]; |
| 2356 | _mm256_storeu_si256(sad_arr.as_mut_ptr() as *mut __m256i, sad); |
| 2357 | let mut res = *sad_arr.get_unchecked(0) + *sad_arr.get_unchecked(2) |
| 2358 | + *sad_arr.get_unchecked(4) + *sad_arr.get_unchecked(6); |
| 2359 | |
| 2360 | for i in (word_len << 5)..len as isize { |
| 2361 | res += (*a_ptr.offset(i) == *b_ptr.offset(i)) as u32; |
| 2362 | } |
| 2363 | |
| 2364 | len as u32 - res |
| 2365 | } |
| 2366 | |
| 2367 | #[target_feature(enable = "avx2")] |
| 2368 | #[inline] |
| 2369 | unsafe fn vector_count_mismatches(a: &Self, b_ptr: *const u8, len: usize) -> u32 { |
| 2370 | let refresh_len = (a.v.len() / 255) as isize; |
| 2371 | let zeros = _mm256_setzero_si256(); |
| 2372 | let mut sad = zeros; |
| 2373 | let avx2_b_ptr = b_ptr as *const __m256i; |
| 2374 | |
| 2375 | for i in 0..refresh_len { |
| 2376 | let mut curr = zeros; |
| 2377 | |
| 2378 | for j in (i * 255)..((i + 1) * 255) { |
| 2379 | let a = *a.v.get_unchecked(j as usize); |
| 2380 | let b = _mm256_loadu_si256(avx2_b_ptr.offset(j)); |
| 2381 | let eq = _mm256_cmpeq_epi8(a, b); |
| 2382 | curr = _mm256_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2383 | // counting matches instead of mismatches for speed |
| 2384 | } |
| 2385 | |
| 2386 | // subtract 0 and sum up 8 bytes at once horizontally into four 64 bit ints |
| 2387 | // accumulate those 64 bit ints |
| 2388 | sad = _mm256_add_epi64(sad, _mm256_sad_epu8(curr, zeros)); |
| 2389 | } |
| 2390 | |
| 2391 | let mut curr = zeros; |
| 2392 | |
| 2393 | // leftover blocks of 32 bytes |
| 2394 | for i in (refresh_len * 255)..a.v.len() as isize { |
| 2395 | let a = *a.v.get_unchecked(i as usize); |
| 2396 | let b = _mm256_loadu_si256(avx2_b_ptr.offset(i)); |
| 2397 | let eq = _mm256_cmpeq_epi8(a, b); |
| 2398 | curr = _mm256_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2399 | } |
| 2400 | |
| 2401 | sad = _mm256_add_epi64(sad, _mm256_sad_epu8(curr, zeros)); |
| 2402 | let mut sad_arr = [0u32; 8]; |
| 2403 | _mm256_storeu_si256(sad_arr.as_mut_ptr() as *mut __m256i, sad); |
| 2404 | let res = *sad_arr.get_unchecked(0) + *sad_arr.get_unchecked(2) |
| 2405 | + *sad_arr.get_unchecked(4) + *sad_arr.get_unchecked(6); |
| 2406 | |
| 2407 | len as u32 - res |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2412 | pub struct Sse { |
| 2413 | v: Vec<__m128i> |
| 2414 | } |
| 2415 | |
| 2416 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 2417 | impl HammingJewel for Sse { |
| 2418 | #[target_feature(enable = "sse4.1")] |
| 2419 | #[inline] |
| 2420 | unsafe fn loadu(ptr: *const u8, len: usize) -> Self { |
| 2421 | let word_len = len >> 4; |
| 2422 | let word_rem = len & 15; |
| 2423 | let mut v = Vec::with_capacity(word_len + if word_rem > 0 {1} else {0}); |
| 2424 | let sse_ptr = ptr as *const __m128i; |
| 2425 | |
| 2426 | for i in 0..word_len { |
| 2427 | v.push(_mm_loadu_si128(sse_ptr.offset(i as isize))); |
| 2428 | } |
| 2429 | |
| 2430 | if word_rem > 0 { |
| 2431 | let mut arr = [0u8; 16]; |
| 2432 | let end_ptr = ptr.offset((word_len << 4) as isize); |
| 2433 | |
| 2434 | for i in 0..word_rem { |
| 2435 | *arr.get_unchecked_mut(i) = *end_ptr.offset(i as isize); |
| 2436 | } |
| 2437 | |
| 2438 | v.push(_mm_loadu_si128(arr.as_ptr() as *const __m128i)); |
| 2439 | } |
| 2440 | |
| 2441 | Self{ |
| 2442 | v: v |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | #[inline] |
| 2447 | fn upper_bound(&self) -> usize { |
| 2448 | self.v.len() << 4 |
| 2449 | } |
| 2450 | |
| 2451 | #[target_feature(enable = "sse4.1")] |
| 2452 | #[inline] |
| 2453 | unsafe fn mm_count_mismatches(a_ptr: *const u8, b_ptr: *const u8, len: usize) -> u32 { |
| 2454 | let mut res = 0u32; |
| 2455 | let div_len = (len >> 4) as isize; |
| 2456 | let sse_a_ptr = a_ptr as *const __m128i; |
| 2457 | let sse_b_ptr = b_ptr as *const __m128i; |
| 2458 | |
| 2459 | for i in 0..div_len { |
| 2460 | let a = _mm_loadu_si128(sse_a_ptr.offset(i)); |
| 2461 | let b = _mm_loadu_si128(sse_b_ptr.offset(i)); |
| 2462 | let eq = _mm_cmpeq_epi8(a, b); |
| 2463 | // basic movemask count equal bytes |
| 2464 | res += _mm_movemask_epi8(eq).count_ones(); |
| 2465 | } |
| 2466 | |
| 2467 | for i in (div_len << 4)..len as isize { |
| 2468 | res += (*a_ptr.offset(i) == *b_ptr.offset(i)) as u32; |
| 2469 | } |
| 2470 | |
| 2471 | len as u32 - res |
| 2472 | } |
| 2473 | |
| 2474 | #[target_feature(enable = "sse4.1")] |
| 2475 | #[inline] |
| 2476 | unsafe fn count_mismatches(a_ptr: *const u8, b_ptr: *const u8, len: usize) -> u32 { |
| 2477 | let refresh_len = (len / (255 * 16)) as isize; |
| 2478 | let zeros = _mm_setzero_si128(); |
| 2479 | let mut sad = zeros; |
| 2480 | let sse_a_ptr = a_ptr as *const __m128i; |
| 2481 | let sse_b_ptr = b_ptr as *const __m128i; |
| 2482 | |
| 2483 | for i in 0..refresh_len { |
| 2484 | let mut curr = zeros; |
| 2485 | |
| 2486 | for j in (i * 255)..((i + 1) * 255) { |
| 2487 | let a = _mm_loadu_si128(sse_a_ptr.offset(j)); |
| 2488 | let b = _mm_loadu_si128(sse_b_ptr.offset(j)); |
| 2489 | let eq = _mm_cmpeq_epi8(a, b); |
| 2490 | curr = _mm_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2491 | // counting matches instead of mismatches for speed |
| 2492 | } |
| 2493 | |
| 2494 | // subtract 0 and sum up 8 bytes at once horizontally into two 64 bit ints |
| 2495 | // accumulate those 64 bit ints |
| 2496 | sad = _mm_add_epi64(sad, _mm_sad_epu8(curr, zeros)); |
| 2497 | } |
| 2498 | |
| 2499 | let word_len = (len >> 4) as isize; |
| 2500 | let mut curr = zeros; |
| 2501 | |
| 2502 | // leftover blocks of 16 bytes |
| 2503 | for i in (refresh_len * 255)..word_len { |
| 2504 | let a = _mm_loadu_si128(sse_a_ptr.offset(i)); |
| 2505 | let b = _mm_loadu_si128(sse_b_ptr.offset(i)); |
| 2506 | let eq = _mm_cmpeq_epi8(a, b); |
| 2507 | curr = _mm_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2508 | } |
| 2509 | |
| 2510 | sad = _mm_add_epi64(sad, _mm_sad_epu8(curr, zeros)); |
| 2511 | let mut sad_arr = [0u32; 4]; |
| 2512 | _mm_storeu_si128(sad_arr.as_mut_ptr() as *mut __m128i, sad); |
| 2513 | let mut res = *sad_arr.get_unchecked(0) + *sad_arr.get_unchecked(2); |
| 2514 | |
| 2515 | for i in (word_len << 4)..len as isize { |
| 2516 | res += (*a_ptr.offset(i) == *b_ptr.offset(i)) as u32; |
| 2517 | } |
| 2518 | |
| 2519 | len as u32 - res |
| 2520 | } |
| 2521 | |
| 2522 | #[target_feature(enable = "sse4.1")] |
| 2523 | #[inline] |
| 2524 | unsafe fn vector_count_mismatches(a: &Self, b_ptr: *const u8, len: usize) -> u32 { |
| 2525 | let refresh_len = (a.v.len() / 255) as isize; |
| 2526 | let zeros = _mm_setzero_si128(); |
| 2527 | let mut sad = zeros; |
| 2528 | let sse_b_ptr = b_ptr as *const __m128i; |
| 2529 | |
| 2530 | for i in 0..refresh_len { |
| 2531 | let mut curr = zeros; |
| 2532 | |
| 2533 | for j in (i * 255)..((i + 1) * 255) { |
| 2534 | let a = *a.v.get_unchecked(j as usize); |
| 2535 | let b = _mm_loadu_si128(sse_b_ptr.offset(j)); |
| 2536 | let eq = _mm_cmpeq_epi8(a, b); |
| 2537 | curr = _mm_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2538 | // counting matches instead of mismatches for speed |
| 2539 | } |
| 2540 | |
| 2541 | // subtract 0 and sum up 8 bytes at once horizontally into two 64 bit ints |
| 2542 | // accumulate those 64 bit ints |
| 2543 | sad = _mm_add_epi64(sad, _mm_sad_epu8(curr, zeros)); |
| 2544 | } |
| 2545 | |
| 2546 | let mut curr = zeros; |
| 2547 | |
| 2548 | // leftover blocks of 16 bytes |
| 2549 | for i in (refresh_len * 255)..a.v.len() as isize { |
| 2550 | let a = *a.v.get_unchecked(i as usize); |
| 2551 | let b = _mm_loadu_si128(sse_b_ptr.offset(i)); |
| 2552 | let eq = _mm_cmpeq_epi8(a, b); |
| 2553 | curr = _mm_sub_epi8(curr, eq); // subtract -1 = add 1 when matching |
| 2554 | } |
| 2555 | |
| 2556 | sad = _mm_add_epi64(sad, _mm_sad_epu8(curr, zeros)); |
| 2557 | let mut sad_arr = [0u32; 4]; |
| 2558 | _mm_storeu_si128(sad_arr.as_mut_ptr() as *mut __m128i, sad); |
| 2559 | let res = *sad_arr.get_unchecked(0) + *sad_arr.get_unchecked(2); |
| 2560 | |
| 2561 | len as u32 - res |
| 2562 | } |
| 2563 | } |