Skip to content

Commit 650f47f

Browse files
committed
Improve compile time detection MSVC vs GCC
1 parent cd5b3ad commit 650f47f

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ target_include_directories(BasicSIMD INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
3333
target_sources(BasicSIMD INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/SIMD.h)
3434

3535
if(MSVC)
36-
target_compile_options(BasicSIMD INTERFACE
37-
/arch:AVX2
38-
)
36+
target_compile_options(BasicSIMD INTERFACE /arch:AVX2)
3937
else()
4038
target_compile_options(BasicSIMD INTERFACE -march=native -fno-strict-aliasing)
4139
endif()

SIMD.h

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,68 @@ _SIMD_INL_ void SIMD_Type_t<double, XXX, double>::DivideInplaceRaw(double* to, c
11651165
_mm##XXX##_store_pd((double*)to, _mm##XXX##_div_pd(_mm##XXX##_load_pd((double*)to), _mm##XXX##_load_pd((double*)from)));\
11661166
}
11671167

1168-
#if defined(__SSE2__)
1168+
//Get GCC/MSVC Compile Time SIMD Macros
1169+
1170+
#if defined(__MSVC__)
1171+
#if defined(_M_IX86_FP)
1172+
#if _M_IX86_FP == 1
1173+
#define SSE_AVAILABLE 1
1174+
#elif _M_IX86_FP == 2
1175+
#define SSE2_AVAILABLE 1
1176+
#define SSE4_1_AVAILABLE 1
1177+
#endif
1178+
#endif
1179+
#elif defined(__GNUC__)
1180+
#if defined(__SSE__)
1181+
#define SSE_AVAILABLE 1
1182+
#endif
1183+
#if defined(__SSE2__)
1184+
#define SSE2_AVAILABLE 1
1185+
#endif
1186+
#if defined(__SSE4_1__)
1187+
#define SSE4_1_AVAILABLE 1
1188+
#endif
1189+
#endif
1190+
1191+
#if defined(__AVX__)
1192+
#define AVX_AVAILABLE 1
1193+
#define SSE_AVAILABLE 1
1194+
#define SSE2_AVAILABLE 1
1195+
#endif
1196+
#if defined(__AVX2__)
1197+
#define AVX2_AVAILABLE 1
1198+
#define SSE4_1_AVAILABLE 1
1199+
#endif
1200+
#if defined(__AVX512F__)
1201+
#define AVX512F_AVAILABLE 1
1202+
#endif
1203+
1204+
//print pragma messages for debug
1205+
#if defined(SSE_AVAILABLE)
1206+
#pragma message("SSE Available")
1207+
#endif
1208+
#if defined(SSE2_AVAILABLE)
1209+
#pragma message("SSE2 Available")
1210+
#endif
1211+
#if defined(SSE4_1_AVAILABLE)
1212+
#pragma message("SSE4.1 Available")
1213+
#endif
1214+
#if defined(AVX_AVAILABLE)
1215+
#pragma message("AVX Available")
1216+
#endif
1217+
#if defined(AVX2_AVAILABLE)
1218+
#pragma message("AVX2 Available")
1219+
#endif
1220+
#if defined(AVX512F_AVAILABLE)
1221+
#pragma message("AVX512F Available")
1222+
#endif
1223+
1224+
//print if none is available
1225+
#if !defined(SSE_AVAILABLE) && !defined(SSE2_AVAILABLE) && !defined(SSE4_1_AVAILABLE) && !defined(AVX_AVAILABLE) && !defined(AVX2_AVAILABLE) && !defined(AVX512F_AVAILABLE)
1226+
#pragma message("No SIMD Available")
1227+
#endif
1228+
1229+
#if defined(SSE2_AVAILABLE)
11691230
#define SIMD_USE_TYPE_INT_128
11701231

11711232
CREATE_INT128_OPERATOR_PLUS(8);
@@ -1180,12 +1241,12 @@ _SIMD_INL_ void SIMD_Type_t<double, XXX, double>::DivideInplaceRaw(double* to, c
11801241
CREATE_INT128_OPERATOR_MULTIPLY(16);
11811242
#endif
11821243

1183-
#if defined(__SSE4_1__)
1244+
#if defined(SSE4_1_AVAILABLE)
11841245
#define SIMD_USE_TYPE_INT_256 1
11851246
CREATE_INT128_OPERATOR_MULTIPLY(32);
11861247
#endif
11871248

1188-
#if defined(__AVX2__)
1249+
#if defined(AVX2_AVAILABLE)
11891250
#define SIMD_USE_TYPE_INT_256 1
11901251

11911252
CREATE_INT256_OPERATOR_PLUS(8);
@@ -1211,7 +1272,7 @@ _SIMD_INL_ void SIMD_Type_t<double, XXX, double>::DivideInplaceRaw(double* to, c
12111272
#endif
12121273

12131274

1214-
#if defined(__AVX__)
1275+
#if defined(AVX_AVAILABLE)
12151276
#define SIMD_USE_TYPE_FLOAT_256 1
12161277
#define SIMD_USE_TYPE_DOUBLE_256 1
12171278
CREATE_FLOAT_OPERATOR_PLUS(256);
@@ -1233,7 +1294,7 @@ _SIMD_INL_ void SIMD_Type_t<double, XXX, double>::DivideInplaceRaw(double* to, c
12331294
DECLARE_SIMD_USE_TYPE_FLOATING(double, 256);
12341295
#endif
12351296

1236-
#if defined(__AVX512F__)
1297+
#if defined(AVX512F_AVAILABLE)
12371298
//TODO: This part couldn't be tested yet due to some hardware incapabilities... :(
12381299
CREATE_INT128_OPERATOR_MULTIPLY(64);
12391300
CREATE_INT256_OPERATOR_MULTIPLY(64);
@@ -1271,13 +1332,13 @@ _SIMD_INL_ void SIMD_Type_t<double, XXX, double>::DivideInplaceRaw(double* to, c
12711332
//SIMD::int_XXX checks are not ideal...
12721333
template<typename T>
12731334
using IsSIMDType = typename std::enable_if<
1274-
#if defined(__SSE2__) || defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
1335+
#if defined(SSE2_AVAILABLE) || defined(AVX_AVAILABLE) || defined(AVX2_AVAILABLE) || defined(AVX512F_AVAILABLE)
12751336
(std::is_same<int, typename T::Type>::value && (T::BitWidth == 128) && IsElementAnyOfInts<typename T::ElementType>::value) || ( std::is_same<int, typename T::Type>::value && (T::BitWidth == 256) && IsElementAnyOfInts<typename T::ElementType>::value) ||
12761337
#endif
1277-
#if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
1338+
#if defined(AVX_AVAILABLE) || defined(AVX2_AVAILABLE) || defined(AVX512F_AVAILABLE)
12781339
std::is_same<T, SIMD::float_256>::value || std::is_same<T, SIMD::double_256>::value ||
12791340
#endif
1280-
#if defined(__AVX512F__)
1341+
#if defined(AVX512F_AVAILABLE)
12811342
( std::is_same<int, typename T::Type>::value && (T::BitWidth == 512) && IsElementAnyOfInts<typename T::ElementType>::value) || std::is_same<T, SIMD::float_512>::value || std::is_same<T, SIMD::double_512>::value ||
12821343
#endif
12831344
std::is_same<int,float>::value, //dummy

0 commit comments

Comments
 (0)