Skip to content

Commit 21b54fe

Browse files
authored
Merge pull request #243 from alicevision/dev/l2sqrI4x
L2SqrI: add fallback if the dimension is not a multiple of 4
2 parents ab012ae + 6f2c3fb commit 21b54fe

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

hnswlib/space_l2.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ namespace hnswlib {
204204
};
205205

206206
static int
207-
L2SqrI(const void *__restrict pVect1, const void *__restrict pVect2, const void *__restrict qty_ptr) {
207+
L2SqrI4x(const void *__restrict pVect1, const void *__restrict pVect2, const void *__restrict qty_ptr) {
208208

209209
size_t qty = *((size_t *) qty_ptr);
210210
int res = 0;
@@ -226,12 +226,23 @@ namespace hnswlib {
226226
res += ((*a) - (*b)) * ((*a) - (*b));
227227
a++;
228228
b++;
229+
}
230+
return (res);
231+
}
229232

233+
static int L2SqrI(const void* __restrict pVect1, const void* __restrict pVect2, const void* __restrict qty_ptr) {
234+
size_t qty = *((size_t*)qty_ptr);
235+
int res = 0;
236+
unsigned char* a = (unsigned char*)pVect1;
237+
unsigned char* b = (unsigned char*)pVect2;
230238

239+
for(size_t i = 0; i < qty; i++)
240+
{
241+
res += ((*a) - (*b)) * ((*a) - (*b));
242+
a++;
243+
b++;
231244
}
232-
233245
return (res);
234-
235246
}
236247

237248
class L2SpaceI : public SpaceInterface<int> {
@@ -241,7 +252,12 @@ namespace hnswlib {
241252
size_t dim_;
242253
public:
243254
L2SpaceI(size_t dim) {
244-
fstdistfunc_ = L2SqrI;
255+
if(dim % 4 == 0) {
256+
fstdistfunc_ = L2SqrI4x;
257+
}
258+
else {
259+
fstdistfunc_ = L2SqrI;
260+
}
245261
dim_ = dim;
246262
data_size_ = dim * sizeof(unsigned char);
247263
}

0 commit comments

Comments
 (0)