1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #ifdef _MSC_VER
11 
12 /*******************************************************
13  * Windows specific macros
14  *******************************************************/
15 
16 #ifdef FAISS_MAIN_LIB
17 #define FAISS_API __declspec(dllexport)
18 #else // _FAISS_MAIN_LIB
19 #define FAISS_API __declspec(dllimport)
20 #endif // FAISS_MAIN_LIB
21 
22 #define __PRETTY_FUNCTION__ __FUNCSIG__
23 
24 #define posix_memalign(p, a, s) \
25     (((*(p)) = _aligned_malloc((s), (a))), *(p) ? 0 : errno)
26 #define posix_memalign_free _aligned_free
27 
28 // aligned should be in front of the declaration
29 #define ALIGNED(x) __declspec(align(x))
30 
31 // redefine the GCC intrinsics with Windows equivalents
32 
33 #include <intrin.h>
34 
__builtin_ctzll(uint64_t x)35 inline int __builtin_ctzll(uint64_t x) {
36     unsigned long ret;
37     _BitScanForward64(&ret, x);
38     return (int)ret;
39 }
40 
41 // cudatoolkit provides __builtin_ctz for NVCC >= 11.0
42 #if !defined(__CUDACC__) || __CUDACC_VER_MAJOR__ < 11
__builtin_ctz(unsigned long x)43 inline int __builtin_ctz(unsigned long x) {
44     unsigned long ret;
45     _BitScanForward(&ret, x);
46     return (int)ret;
47 }
48 #endif
49 
__builtin_clzll(uint64_t x)50 inline int __builtin_clzll(uint64_t x) {
51     return (int)__lzcnt64(x);
52 }
53 
54 #define __builtin_popcount __popcnt
55 #define __builtin_popcountl __popcnt64
56 
57 // MSVC does not define __SSEx__, and _M_IX86_FP is only defined on 32-bit
58 // processors cf.
59 // https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
60 #ifdef __AVX__
61 #define __SSE__ 1
62 #define __SSE2__ 1
63 #define __SSE3__ 1
64 #define __SSE4_1__ 1
65 #define __SSE4_2__ 1
66 #endif
67 
68 // MSVC sets FMA and F16C automatically when using AVX2
69 // Ref. FMA (under /arch:AVX2):
70 // https://docs.microsoft.com/en-us/cpp/build/reference/arch-x64 Ref. F16C (2nd
71 // paragraph): https://walbourn.github.io/directxmath-avx2/
72 #ifdef __AVX2__
73 #define __FMA__ 1
74 #define __F16C__ 1
75 #endif
76 
77 #else
78 /*******************************************************
79  * Linux and OSX
80  *******************************************************/
81 
82 #define FAISS_API
83 #define posix_memalign_free free
84 
85 // aligned should be *in front* of the declaration, for compatibility with
86 // windows
87 #define ALIGNED(x) __attribute__((aligned(x)))
88 
89 #endif // _MSC_VER
90