Repository: qdrant/qdrant
File: lib/segment/src/spaces/metric_uint/sse2/cosine.rs (Rust)

Code before:
use std::arch::x86_64::*;

use crate::spaces::simple_sse::hsum128_ps_sse;

#[target_feature(enable = "sse")]
#[target_feature(enable = "sse2")]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn sse_cosine_similarity_bytes(v1: &[u8], v2: &[u8]) -> f32 {
    debug_assert!(v1.len() == v2.len());
    debug_assert!(is_x86_feature_detected!("sse"));
    debug_assert!(is_x86_feature_detected!("sse2"));

    let mut ptr1: *const u8 = v1.as_ptr();
    let mut ptr2: *const u8 = v2.as_ptr();

    unsafe {
        // sum accumulator for 4x32 bit integers
        let mut dot_acc = _mm_setzero_si128();
        let mut norm1_acc = _mm_setzero_si128();
        let mut norm2_acc = _mm_setzero_si128();
        // mask to take only lower 8 bits from 16 bits
        let mask_epu16_epu8 = _mm_set1_epi16(0xFF);
        let len = v1.len();
        for _ in 0..len / 16 {
            // load 16 bytes
            let p1 = _mm_loadu_si128(ptr1.cast::<__m128i>());
            let p2 = _mm_loadu_si128(ptr2.cast::<__m128i>());
            ptr1 = ptr1.add(16);
            ptr2 = ptr2.add(16);

            // convert 16x8 bit integers into 8x16 bit integers using bitwise AND
            // conversion is done by taking only lower 8 bits from 16 bits
            // p1 = [byte0, byte1, byte2, byte3, ..]
            // p1_low = [0, byte1, 0, byte3, ..]
            // p1_high = [0, byte0, 0, byte2, ..]
            let p1_low = _mm_and_si128(p1, mask_epu16_epu8);

Review comment (bug):
```
error: this assertion has a constant value
  --> lib/segment/src/spaces/metric_uint/sse2/manhattan.rs:11:5
   |
11 |     debug_assert!(is_x86_feature_detected!("sse2"));
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider moving this into a const block: `const { assert!(..) }`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#assertions_on_constants
```

we do not have those debug assertions in other metrics implementations so I just removed those

Code after:
use std::arch::x86_64::*;

use crate::spaces::simple_sse::hsum128_ps_sse;

#[target_feature(enable = "sse")]
#[target_feature(enable = "sse2")]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn sse_cosine_similarity_bytes(v1: &[u8], v2: &[u8]) -> f32 {
    debug_assert!(v1.len() == v2.len());

    let mut ptr1: *const u8 = v1.as_ptr();
    let mut ptr2: *const u8 = v2.as_ptr();

    unsafe {
        // sum accumulator for 4x32 bit integers
        let mut dot_acc = _mm_setzero_si128();
        let mut norm1_acc = _mm_setzero_si128();
        let mut norm2_acc = _mm_setzero_si128();
        // mask to take only lower 8 bits from 16 bits
        let mask_epu16_epu8 = _mm_set1_epi16(0xFF);
        let len = v1.len();
        for _ in 0..len / 16 {
            // load 16 bytes
            let p1 = _mm_loadu_si128(ptr1.cast::<__m128i>());
            let p2 = _mm_loadu_si128(ptr2.cast::<__m128i>());
            ptr1 = ptr1.add(16);
            ptr2 = ptr2.add(16);

            // convert 16x8 bit integers into 8x16 bit integers using bitwise AND
            // conversion is done by taking only lower 8 bits from 16 bits
            // p1 = [byte0, byte1, byte2, byte3, ..]
            // p1_low = [0, byte1, 0, byte3, ..]
            // p1_high = [0, byte0, 0, byte2, ..]
            let p1_low = _mm_and_si128(p1, mask_epu16_epu8);
            let p1_high = _mm_and_si128(_mm_bsrli_si128(p1, 1), mask_epu16_epu8);
            let p2_low = _mm_and_si128(p2, mask_epu16_epu8);