Skip to content

Commit f7ce7cc

Browse files
author
Tony Kuo
committed
move decision to constructors
1 parent 5fe5e3b commit f7ce7cc

File tree

2 files changed

+28
-56
lines changed

2 files changed

+28
-56
lines changed

hnswlib/space_ip.h

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -251,40 +251,8 @@ namespace hnswlib {
251251
#endif
252252

253253
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
254-
static float
255-
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
256-
DISTFUNC<float> simdfunc_;
257-
#if defined(USE_AVX)
258-
if (AVXCapable())
259-
simdfunc_ = InnerProductSIMD4ExtAVX;
260-
else
261-
simdfunc_ = InnerProductSIMD4ExtSSE;
262-
#else
263-
simdfunc_ = InnerProductSIMD4ExtSSE;
264-
#endif
265-
return simdfunc_(pVect1v, pVect2v, qty_ptr);
266-
}
267-
268-
static float
269-
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
270-
DISTFUNC<float> simdfunc_;
271-
#if defined(USE_AVX512)
272-
if (AVX512Capable())
273-
simdfunc_ = InnerProductSIMD16ExtAVX512;
274-
else if (AVXCapable())
275-
simdfunc_ = InnerProductSIMD16ExtAVX;
276-
else
277-
simdfunc_ = InnerProductSIMD16ExtSSE;
278-
#elif defined(USE_AVX)
279-
if (AVXCapable())
280-
simdfunc_ = InnerProductSIMD16ExtAVX;
281-
else
282-
simdfunc_ = InnerProductSIMD16ExtSSE;
283-
#else
284-
simdfunc_ = InnerProductSIMD16ExtSSE;
285-
#endif
286-
return simdfunc_(pVect1v, pVect2v, qty_ptr);
287-
}
254+
DISTFUNC<float> InnerProductSIMD16Ext = InnerProductSIMD16ExtSSE;
255+
DISTFUNC<float> InnerProductSIMD4Ext = InnerProductSIMD4ExtSSE;
288256

289257
static float
290258
InnerProductSIMD16ExtResiduals(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
@@ -324,6 +292,19 @@ namespace hnswlib {
324292
InnerProductSpace(size_t dim) {
325293
fstdistfunc_ = InnerProduct;
326294
#if defined(USE_AVX) || defined(USE_SSE) || defined(USE_AVX512)
295+
#if defined(USE_AVX512)
296+
if (AVX512Capable())
297+
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX512;
298+
else if (AVXCapable())
299+
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX;
300+
#endif
301+
#if defined(USE_AVX)
302+
if (AVXCapable()) {
303+
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX;
304+
InnerProductSIMD4Ext = InnerProductSIMD4ExtAVX;
305+
}
306+
#endif
307+
327308
if (dim % 16 == 0)
328309
fstdistfunc_ = InnerProductSIMD16Ext;
329310
else if (dim % 4 == 0)

hnswlib/space_l2.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,7 @@ namespace hnswlib {
144144
#endif
145145

146146
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
147-
static float
148-
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
149-
DISTFUNC<float> simdfunc_;
150-
#if defined(USE_AVX512)
151-
if (AVX512Capable())
152-
simdfunc_ = L2SqrSIMD16ExtAVX512;
153-
else if (AVXCapable())
154-
simdfunc_ = L2SqrSIMD16ExtAVX;
155-
else
156-
simdfunc_ = L2SqrSIMD16ExtSSE;
157-
#elif defined(USE_AVX)
158-
if (AVXCapable())
159-
simdfunc_ = L2SqrSIMD16ExtAVX;
160-
else
161-
simdfunc_ = L2SqrSIMD16ExtSSE;
162-
#else
163-
simdfunc_ = L2SqrSIMD16ExtSSE;
164-
#endif
165-
return simdfunc_(pVect1v, pVect2v, qty_ptr);
166-
}
147+
DISTFUNC<float> L2SqrSIMD16Ext = L2SqrSIMD16ExtSSE;
167148

168149
static float
169150
L2SqrSIMD16ExtResiduals(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
@@ -232,7 +213,17 @@ namespace hnswlib {
232213
public:
233214
L2Space(size_t dim) {
234215
fstdistfunc_ = L2Sqr;
235-
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
216+
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
217+
#if defined(USE_AVX512)
218+
if (AVX512Capable())
219+
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX512;
220+
else if (AVXCapable())
221+
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX;
222+
#elif defined(USE_AVX)
223+
if (AVXCapable())
224+
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX;
225+
#endif
226+
236227
if (dim % 16 == 0)
237228
fstdistfunc_ = L2SqrSIMD16Ext;
238229
else if (dim % 4 == 0)
@@ -241,7 +232,7 @@ namespace hnswlib {
241232
fstdistfunc_ = L2SqrSIMD16ExtResiduals;
242233
else if (dim > 4)
243234
fstdistfunc_ = L2SqrSIMD4ExtResiduals;
244-
#endif
235+
#endif
245236
dim_ = dim;
246237
data_size_ = dim * sizeof(float);
247238
}

0 commit comments

Comments
 (0)