1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT license.
3 
4 #pragma once
5 
6 #if SEAL_COMPILER == SEAL_COMPILER_MSVC
7 
8 // Require Visual Studio 2017 version 15.3 or newer
9 #if (_MSC_VER < 1911)
10 #error "Microsoft Visual Studio 2017 version 15.3 or newer required"
11 #endif
12 
13 // Read in config.h
14 #include "seal/util/config.h"
15 
16 // In Visual Studio redefine std::byte (seal_byte)
17 #undef SEAL_USE_STD_BYTE
18 
19 // In Visual Studio for now we disable the use of std::shared_mutex
20 #undef SEAL_USE_SHARED_MUTEX
21 
22 // Are we compiling with C++17 or newer
23 #if (_MSVC_LANG >= 201703L)
24 
25 // Use `if constexpr'
26 #define SEAL_USE_IF_CONSTEXPR
27 
28 // Use [[maybe_unused]]
29 #define SEAL_USE_MAYBE_UNUSED
30 
31 // Use [[nodiscard]]
32 #define SEAL_USE_NODISCARD
33 
34 #else
35 
36 #ifdef SEAL_USE_IF_CONSTEXPR
37 #pragma message("Disabling `if constexpr` based on _MSVC_LANG value " SEAL_STRINGIZE( \
38     _MSVC_LANG) ": undefining SEAL_USE_IF_CONSTEXPR")
39 #undef SEAL_USE_IF_CONSTEXPR
40 #endif
41 
42 #ifdef SEAL_USE_MAYBE_UNUSED
43 #pragma message("Disabling `[[maybe_unused]]` based on _MSVC_LANG value " SEAL_STRINGIZE( \
44     _MSVC_LANG) ": undefining SEAL_USE_MAYBE_UNUSED")
45 #undef SEAL_USE_MAYBE_UNUSED
46 #endif
47 
48 #ifdef SEAL_USE_NODISCARD
49 #pragma message("Disabling `[[nodiscard]]` based on _MSVC_LANG value " SEAL_STRINGIZE( \
50     _MSVC_LANG) ": undefining SEAL_USE_NODISCARD")
51 #undef SEAL_USE_NODISCARD
52 #endif
53 #endif
54 
55 #ifdef SEAL_USE_ALIGNED_ALLOC
56 #define SEAL_MALLOC(size) static_cast<seal_byte *>(_aligned_malloc((size), 64))
57 #define SEAL_FREE(ptr) _aligned_free(ptr)
58 #endif
59 
60 // X64
61 #ifdef _M_X64
62 
63 #ifdef SEAL_USE_INTRIN
64 #include <intrin.h>
65 
66 #ifdef SEAL_USE__UMUL128
67 #pragma intrinsic(_umul128)
68 #define SEAL_MULTIPLY_UINT64_HW64(operand1, operand2, hw64) _umul128(operand1, operand2, hw64);
69 
70 #define SEAL_MULTIPLY_UINT64(operand1, operand2, result128) result128[0] = _umul128(operand1, operand2, result128 + 1);
71 #endif
72 
73 #ifdef SEAL_USE__BITSCANREVERSE64
74 #pragma intrinsic(_BitScanReverse64)
75 #define SEAL_MSB_INDEX_UINT64(result, value) _BitScanReverse64(result, value)
76 #endif
77 
78 #ifdef SEAL_USE__ADDCARRY_U64
79 #pragma intrinsic(_addcarry_u64)
80 #define SEAL_ADD_CARRY_UINT64(operand1, operand2, carry, result) _addcarry_u64(carry, operand1, operand2, result)
81 #endif
82 
83 #ifdef SEAL_USE__SUBBORROW_U64
84 #pragma intrinsic(_subborrow_u64)
85 #define SEAL_SUB_BORROW_UINT64(operand1, operand2, borrow, result) _subborrow_u64(borrow, operand1, operand2, result)
86 #endif
87 
88 #endif
89 #else
90 #undef SEAL_USE_INTRIN
91 
92 #endif //_M_X64
93 
94 // Force inline
95 #define SEAL_FORCE_INLINE __forceinline
96 
97 #endif
98