Skip to content

Commit b1d492d

Browse files
committed
support macos without special instructions
1 parent 2b6147a commit b1d492d

5 files changed

Lines changed: 47 additions & 15 deletions

File tree

cpp/deglib/include/config.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
#include <cstdint>
44

5+
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
6+
#define DEGLIB_X86 1
7+
#endif
8+
59
// Always include the x86 intrinsic headers so that SIMD code in the distance
610
// headers compiles regardless of the target architecture flags.
11+
#if defined(DEGLIB_X86)
712
#ifdef _MSC_VER
813
#include <intrin.h>
914
#include <stdexcept>
1015
#else
1116
#include <x86intrin.h>
1217
#include <xmmintrin.h> // for _mm_prefetch
1318
#endif
19+
#if defined(__GNUC__) || defined(__clang__)
20+
#include <cpuid.h>
21+
#endif
22+
#endif
1423

1524

1625
// ---------------------------------------------------------------------------
@@ -27,14 +36,11 @@
2736
// not inside distance calculation loops.
2837
// ---------------------------------------------------------------------------
2938

30-
#if defined(__GNUC__) || defined(__clang__)
31-
#include <cpuid.h>
32-
#endif
33-
3439
namespace deglib::cpu {
3540

3641
namespace detail {
3742

43+
#if defined(DEGLIB_X86)
3844
// Query CPUID leaf/subleaf into a 4-element int array.
3945
// Uses __cpuidex on MSVC and __cpuid_count on GCC/Clang.
4046
inline void cpuid(int leaf, int subleaf, int cpu_info[4]) {
@@ -44,16 +50,18 @@ namespace deglib::cpu {
4450
__cpuid_count(leaf, subleaf, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
4551
#endif
4652
}
53+
#endif
4754

4855
// Cached hardware feature flags, populated on first call via a function-local static.
4956
struct CpuFeatures {
50-
bool sse42;
51-
bool f16c;
52-
bool avx;
53-
bool avx2;
54-
bool avx512f;
57+
bool sse42{false};
58+
bool f16c{false};
59+
bool avx{false};
60+
bool avx2{false};
61+
bool avx512f{false};
5562

5663
CpuFeatures() {
64+
#if defined(DEGLIB_X86)
5765
int cpu_info[4] = {0};
5866

5967
// Leaf 1: feature flags in ECX and EDX
@@ -66,6 +74,7 @@ namespace deglib::cpu {
6674
cpuid(7, 0, cpu_info);
6775
avx2 = (cpu_info[1] & (1 << 5)) != 0;
6876
avx512f = (cpu_info[1] & (1 << 16)) != 0;
77+
#endif
6978
}
7079
};
7180

cpp/deglib/include/distance/fp32_ip.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace deglib::distances::fp32_ip {
4545
}
4646
};
4747

48+
#if defined(DEGLIB_X86)
4849
// -------------------------------------------------------------------
4950
// InnerProductFloat16Ext — processes 16 floats (64 bytes) per iteration.
5051
// Separate classes per SIMD width so that compare() has zero
@@ -300,10 +301,12 @@ namespace deglib::distances::fp32_ip {
300301
return 1.f - (res + res_tail);
301302
}
302303
};
304+
#endif
303305

304306
using DistanceVariant = std::variant<
305-
InnerProductFloat,
306-
InnerProductFloat16Ext_AVX512,
307+
InnerProductFloat
308+
#if defined(DEGLIB_X86)
309+
, InnerProductFloat16Ext_AVX512,
307310
InnerProductFloat16Ext_AVX2,
308311
InnerProductFloat16Ext_SSE,
309312
InnerProductFloat8Ext_AVX2,
@@ -313,9 +316,11 @@ namespace deglib::distances::fp32_ip {
313316
InnerProductFloat16ExtResiduals_AVX2,
314317
InnerProductFloat16ExtResiduals_SSE,
315318
InnerProductFloat4ExtResiduals_SSE
319+
#endif
316320
>;
317321

318322
inline DistanceVariant select_dist(const size_t dim) {
323+
#if defined(DEGLIB_X86)
319324
if (deglib::cpu::has_avx512()) {
320325
if (dim % 16 == 0)
321326
return InnerProductFloat16Ext_AVX512{};
@@ -352,6 +357,7 @@ namespace deglib::distances::fp32_ip {
352357
else if (dim > 4)
353358
return InnerProductFloat4ExtResiduals_SSE{};
354359
}
360+
#endif
355361
return InnerProductFloat{};
356362
}
357363

cpp/deglib/include/distance/fp32_l2.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace deglib::distances::fp32_l2 {
4141
}
4242
};
4343

44+
#if defined(DEGLIB_X86)
4445
// -------------------------------------------------------------------
4546
// L2Float16Ext — processes 16 floats (64 bytes) per iteration.
4647
// Separate classes per SIMD width so that compare() has zero
@@ -308,10 +309,12 @@ namespace deglib::distances::fp32_l2 {
308309
return (res + res_tail);
309310
}
310311
};
312+
#endif
311313

312314
using DistanceVariant = std::variant<
313-
L2Float,
314-
L2Float16Ext_AVX512,
315+
L2Float
316+
#if defined(DEGLIB_X86)
317+
, L2Float16Ext_AVX512,
315318
L2Float16Ext_AVX2,
316319
L2Float16Ext_SSE,
317320
L2Float8Ext_AVX2,
@@ -321,9 +324,11 @@ namespace deglib::distances::fp32_l2 {
321324
L2Float16ExtResiduals_AVX2,
322325
L2Float16ExtResiduals_SSE,
323326
L2Float4ExtResiduals_SSE
327+
#endif
324328
>;
325329

326330
inline DistanceVariant select_dist(const size_t dim) {
331+
#if defined(DEGLIB_X86)
327332
if (deglib::cpu::has_avx512()) {
328333
if (dim % 16 == 0)
329334
return L2Float16Ext_AVX512{};
@@ -360,6 +365,7 @@ namespace deglib::distances::fp32_l2 {
360365
else if (dim > 4)
361366
return L2Float4ExtResiduals_SSE{};
362367
}
368+
#endif
363369
return L2Float{};
364370
}
365371

cpp/deglib/include/distance/uint8_l2.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace deglib::distances::uint8_l2 {
2727
};
2828

2929
// -------------------------------------------------------------------
30+
#if defined(DEGLIB_X86)
31+
// -------------------------------------------------------------------
3032
// L2Uint8Ext32 — processes 32 uint8 values (32 bytes) per iteration.
3133
// Separate classes per SIMD width so that compare() has zero
3234
// runtime dispatch overhead — select_dist() chooses the class.
@@ -184,17 +186,21 @@ namespace deglib::distances::uint8_l2 {
184186
return static_cast<float>(sum_array[0] + sum_array[1] + sum_array[2] + sum_array[3]);
185187
}
186188
};
189+
#endif
187190

188191
using DistanceVariant = std::variant<
189-
L2Uint8,
190-
L2Uint8Ext32_AVX512,
192+
L2Uint8
193+
#if defined(DEGLIB_X86)
194+
, L2Uint8Ext32_AVX512,
191195
L2Uint8Ext32_AVX2,
192196
L2Uint8Ext32_SSE,
193197
L2Uint8Ext16_AVX2,
194198
L2Uint8Ext16_SSE
199+
#endif
195200
>;
196201

197202
inline DistanceVariant select_dist(const size_t dim) {
203+
#if defined(DEGLIB_X86)
198204
if (deglib::cpu::has_avx512()) {
199205
if (dim % 32 == 0)
200206
return L2Uint8Ext32_AVX512{};
@@ -213,6 +219,7 @@ namespace deglib::distances::uint8_l2 {
213219
else if (dim % 16 == 0)
214220
return L2Uint8Ext16_SSE{};
215221
}
222+
#endif
216223
return L2Uint8{};
217224
}
218225

cpp/deglib/include/memory.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ namespace deglib::memory {
1111
inline static void prefetch(const char *ptr, const size_t size = 128) {
1212
size_t pos = 0;
1313
while(pos < size) {
14+
#if defined(DEGLIB_X86)
1415
_mm_prefetch(ptr+pos, _MM_HINT_T0);
16+
#elif defined(__GNUC__) || defined(__clang__)
17+
__builtin_prefetch(ptr+pos, 0, 3);
18+
#endif
1519
pos += L1_CACHE_LINE_SIZE;
1620
}
1721
}

0 commit comments

Comments
 (0)