Skip to content

Commit 64123dc

Browse files
committed
Docs: Hamming plans for integers
1 parent d9ca85d commit 64123dc

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

include/simsimd/binary.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* @date July 1, 2023
66
*
77
* Contains:
8-
* - Hamming distance
9-
* - Jaccard distance (Tanimoto coefficient)
8+
* - Bit-level Hamming distance
9+
* - Bit-level Jaccard distance (Tanimoto coefficient)
10+
* - TODO: Hamming distance for integer vectors - `u16`, `u32`
1011
*
1112
* For hardware architectures:
1213
* - Arm: NEON, SVE
@@ -55,23 +56,23 @@ extern "C" {
5556

5657
// clang-format off
5758

58-
/* Serial backends for bitsets. */
59+
/* Serial backends for bitsets and integers. */
5960
SIMSIMD_PUBLIC void simsimd_hamming_b8_serial(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
6061
SIMSIMD_PUBLIC void simsimd_jaccard_b8_serial(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
6162

62-
/* Arm NEON backend for bitsets. */
63+
/* Arm NEON backend for bitsets and integers. */
6364
SIMSIMD_PUBLIC void simsimd_hamming_b8_neon(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
6465
SIMSIMD_PUBLIC void simsimd_jaccard_b8_neon(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
6566

66-
/* Arm SVE backend for bitsets. */
67+
/* Arm SVE backend for bitsets and integers. */
6768
SIMSIMD_PUBLIC void simsimd_hamming_b8_sve(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
6869
SIMSIMD_PUBLIC void simsimd_jaccard_b8_sve(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
6970

70-
/* x86 AVX2 backend for bitsets for Intel Haswell CPUs and newer, needs only POPCNT extensions. */
71+
/* x86 AVX2 backend for bitsets and integers for Intel Haswell CPUs and newer, needs only POPCNT extensions. */
7172
SIMSIMD_PUBLIC void simsimd_hamming_b8_haswell(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
7273
SIMSIMD_PUBLIC void simsimd_jaccard_b8_haswell(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
7374

74-
/* x86 AVX512 backend for bitsets for Intel Ice Lake CPUs and newer, using VPOPCNTDQ extensions. */
75+
/* x86 AVX512 backend for bitsets and integers for Intel Ice Lake CPUs and newer, using VPOPCNTDQ extensions. */
7576
SIMSIMD_PUBLIC void simsimd_hamming_b8_ice(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
7677
SIMSIMD_PUBLIC void simsimd_jaccard_b8_ice(simsimd_b8_t const* a, simsimd_b8_t const* b, simsimd_size_t n_words, simsimd_distance_t* distance);
7778
// clang-format on
@@ -91,14 +92,14 @@ SIMSIMD_PUBLIC unsigned char simsimd_popcount_b8(simsimd_b8_t x) {
9192

9293
SIMSIMD_PUBLIC void simsimd_hamming_b8_serial(simsimd_b8_t const *a, simsimd_b8_t const *b, simsimd_size_t n_words,
9394
simsimd_distance_t *result) {
94-
simsimd_i32_t differences = 0;
95+
simsimd_u32_t differences = 0;
9596
for (simsimd_size_t i = 0; i != n_words; ++i) differences += simsimd_popcount_b8(a[i] ^ b[i]);
9697
*result = differences;
9798
}
9899

99100
SIMSIMD_PUBLIC void simsimd_jaccard_b8_serial(simsimd_b8_t const *a, simsimd_b8_t const *b, simsimd_size_t n_words,
100101
simsimd_distance_t *result) {
101-
simsimd_i32_t intersection = 0, union_ = 0;
102+
simsimd_u32_t intersection = 0, union_ = 0;
102103
for (simsimd_size_t i = 0; i != n_words; ++i)
103104
intersection += simsimd_popcount_b8(a[i] & b[i]), union_ += simsimd_popcount_b8(a[i] | b[i]);
104105
*result = (union_ != 0) ? 1 - (simsimd_f64_t)intersection / (simsimd_f64_t)union_ : 1;

include/simsimd/dot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ SIMSIMD_PUBLIC void simsimd_dot_i8_haswell(simsimd_i8_t const *a_scalars, simsim
11631163
__m256i b_i16_low_vec = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(b_i8_vec, 0));
11641164
__m256i b_i16_high_vec = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(b_i8_vec, 1));
11651165

1166-
// Multiply and accumulate at int16 level, accumulate at `int32` level
1166+
// Multiply and accumulate at `int16` level, accumulate at `int32` level
11671167
ab_i32_low_vec = _mm256_add_epi32(ab_i32_low_vec, _mm256_madd_epi16(a_i16_low_vec, b_i16_low_vec));
11681168
ab_i32_high_vec = _mm256_add_epi32(ab_i32_high_vec, _mm256_madd_epi16(a_i16_high_vec, b_i16_high_vec));
11691169
}
@@ -1197,7 +1197,7 @@ SIMSIMD_PUBLIC void simsimd_dot_u8_haswell(simsimd_u8_t const *a_scalars, simsim
11971197
__m256i b_i16_low_vec = _mm256_unpacklo_epi8(b_u8_vec, zeros_vec);
11981198
__m256i b_i16_high_vec = _mm256_unpackhi_epi8(b_u8_vec, zeros_vec);
11991199

1200-
// Multiply and accumulate at int16 level, accumulate at int32 level
1200+
// Multiply and accumulate at `int16` level, accumulate at `int32` level
12011201
ab_i32_low_vec = _mm256_add_epi32(ab_i32_low_vec, _mm256_madd_epi16(a_i16_low_vec, b_i16_low_vec));
12021202
ab_i32_high_vec = _mm256_add_epi32(ab_i32_high_vec, _mm256_madd_epi16(a_i16_high_vec, b_i16_high_vec));
12031203
}

0 commit comments

Comments
 (0)