Skip to content

Commit 4afa162

Browse files
author
Peter Steinbach
authored
Merge pull request #12 from psteinb/cmake-detect-feature
Cmake detect feature
2 parents be79a83 + aca0b90 commit 4afa162

File tree

8 files changed

+148
-49
lines changed

8 files changed

+148
-49
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ endif()
3535

3636
check_cxx_compiler_flag(-Wl,-Bsymbolic HAS_BSYMBOLIC_COMPILERFLAG)
3737
check_cxx_compiler_flag("-Xclang -march=native" HAS_XCLANG_COMPILERFLAG)
38+
check_cxx_compiler_flag("-march=native" HAS_MARCH_COMPILERFLAG)
39+
check_cxx_compiler_flag("-xHost" HAS_XHOST_COMPILERFLAG)
3840
check_cxx_compiler_flag(-Wall HAS_WALL_COMPILERFLAG)
3941
check_cxx_compiler_flag(-ggdb HAS_GGDB_COMPILERFLAG)
4042

include/detail/ct/preprocessor_impl.hpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
#ifndef COMPASS_CT_PREPROCESSOR_IMPL_H_
22
#define COMPASS_CT_PREPROCESSOR_IMPL_H_
33

4-
#ifndef WIN32
5-
#if defined __SSE2__ && defined __SSE2_MATH__
6-
#define COMPASS_HAS_SSE2
7-
#endif
8-
#else
9-
#if _M_IX86_FP >= 2
10-
#define COMPASS_HAS_SSE2
11-
#endif
12-
#endif
4+
135

146
#ifndef WIN32
15-
#if defined __SSE3__ && defined __SSSE3__
16-
#define COMPASS_HAS_SSE3
17-
#endif
187

19-
#if defined __SSE4_2__ && defined __SSE4_1__
20-
#define COMPASS_HAS_SSE4
21-
#endif
8+
#if defined(__SSE2__) || defined(__SSE2_MATH__) // && __SSE2__ != 0 && __SSE2_MATH__ != 0
9+
#define COMPASS_CT_HAS_SSE2 1
10+
#endif
11+
12+
#if defined(__SSE3__) && defined(__SSSE3__)
13+
#define COMPASS_CT_HAS_SSE3 1
14+
#endif
15+
16+
#if defined(__SSE4_2__) && defined(__SSE4_1__)
17+
#define COMPASS_CT_HAS_SSE4 1
18+
#endif
19+
2220
#else
2321
//TODO: try to warn users on Windows that we are enabling SSE3 + SSE4 upon assumption here
24-
#define COMPASS_HAS_SSE3
25-
#define COMPASS_HAS_SSE4
22+
#if _M_IX86_FP >= 2
23+
#define COMPASS_CT_HAS_SSE2 1
24+
#define COMPASS_CT_HAS_SSE3 1
25+
#define COMPASS_CT_HAS_SSE4 1
26+
#endif
2627
#endif
2728

29+
2830
#include "detail/tags.hpp"
2931

3032
namespace compass {

include/detail/definitions.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ namespace compass {
2424

2525
};
2626

27-
//#include "ct/preprocessor_impl.hpp"
28-
2927
#endif /* DEFINITIONS_H */

include/detail/dispatch.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "detail/definitions.hpp"
99

1010
#ifdef COMPASS_CT_ARCH_X86
11+
#include "ct/preprocessor_impl.hpp"
1112
#include "rt/x86_impl.hpp"
1213
#include "rt/x86_sizes.hpp"
1314
#endif

single_include/compass.hpp

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ namespace compass {
4747

4848
};
4949

50-
51-
5250
#endif
5351
namespace compass {
5452

@@ -252,6 +250,106 @@ namespace compass {
252250

253251
#endif
254252
#ifdef COMPASS_CT_ARCH_X86
253+
#ifndef COMPASS_CT_PREPROCESSOR_IMPL_H_
254+
#define COMPASS_CT_PREPROCESSOR_IMPL_H_
255+
256+
257+
258+
#ifndef WIN32
259+
260+
#if defined(__SSE2__) || defined(__SSE2_MATH__)
261+
#define COMPASS_CT_HAS_SSE2 1
262+
#endif
263+
264+
#if defined(__SSE3__) && defined(__SSSE3__)
265+
#define COMPASS_CT_HAS_SSE3 1
266+
#endif
267+
268+
#if defined(__SSE4_2__) && defined(__SSE4_1__)
269+
#define COMPASS_CT_HAS_SSE4 1
270+
#endif
271+
272+
#else
273+
274+
#if _M_IX86_FP >= 2
275+
#define COMPASS_CT_HAS_SSE2 1
276+
#define COMPASS_CT_HAS_SSE3 1
277+
#define COMPASS_CT_HAS_SSE4 1
278+
#endif
279+
#endif
280+
#ifndef COMPASS_TAGS_H_
281+
#define COMPASS_TAGS_H_
282+
283+
284+
namespace compass {
285+
286+
287+
namespace feature {
288+
289+
290+
291+
292+
struct sse {};
293+
struct sse2 {};
294+
struct sse3 {};
295+
struct sse4 {};
296+
297+
298+
struct avx {};
299+
struct avx2 {};
300+
301+
302+
};
303+
304+
305+
};
306+
#endif
307+
namespace compass {
308+
309+
namespace compiletime {
310+
311+
template<typename feature_t>
312+
struct has{
313+
static const bool enabled = false;
314+
};
315+
316+
template<>
317+
struct has<feature::sse2>{
318+
static const bool enabled=
319+
#ifdef COMPASS_CT_HAS_SSE2
320+
true;
321+
#else
322+
false;
323+
#endif
324+
325+
};
326+
327+
template<>
328+
struct has<feature::sse3>{
329+
static const bool enabled=
330+
#ifdef COMPASS_CT_HAS_SSE3
331+
true;
332+
#else
333+
false;
334+
#endif
335+
336+
};
337+
338+
template<>
339+
struct has<feature::sse4>{
340+
static const bool enabled=
341+
#ifdef COMPASS_CT_HAS_SSE4
342+
true;
343+
#else
344+
false;
345+
#endif
346+
347+
};
348+
349+
};
350+
};
351+
352+
#endif
255353
#ifndef COMPASS_RT_X86_IMPL_H_
256354
#define COMPASS_RT_X86_IMPL_H_
257355
#ifndef COMPASS_RT_X86_CPUID_H
@@ -515,33 +613,6 @@ namespace compass {
515613
#endif
516614
#endif
517615

518-
#endif
519-
#ifndef COMPASS_TAGS_H_
520-
#define COMPASS_TAGS_H_
521-
522-
523-
namespace compass {
524-
525-
526-
namespace feature {
527-
528-
529-
530-
531-
struct sse {};
532-
struct sse2 {};
533-
struct sse3 {};
534-
struct sse4 {};
535-
536-
537-
struct avx {};
538-
struct avx2 {};
539-
540-
541-
};
542-
543-
544-
};
545616
#endif
546617
#ifndef COMPASS_BIT_VIEW_H
547618
#define COMPASS_BIT_VIEW_H

tests/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,20 @@ configure_file(${PROJECT_SOURCE_DIR}/tests/build_machine.hpp.in ${CMAKE_CURRENT_
8686

8787
add_executable(test_build_machine test_build_machine.cpp $<TARGET_OBJECTS:catcho>)
8888
target_include_directories(test_build_machine PRIVATE ${CATCH2_HEADER_PATH} ${COMPASS_INCLUDE_BUILD_DIR} ${CMAKE_CURRENT_BINARY_DIR})
89+
if(HAS_MARCH_COMPILERFLAG)
90+
target_compile_options(test_build_machine PRIVATE "-march=native")
91+
endif()
92+
93+
if(HAS_XHOST_COMPILERFLAG)
94+
target_compile_options(test_build_machine PRIVATE "-xHost")
95+
endif()
96+
97+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
98+
if(SSE2_FOUND)
99+
target_compile_options(test_build_machine PRIVATE "/arch:SSE2")
100+
elseif(AVX_FOUND)
101+
target_compile_options(test_build_machine PRIVATE "/arch:AVX")
102+
elseif(AVX2_FOUND)
103+
target_compile_options(test_build_machine PRIVATE "/arch:AVX2")
104+
endif()
105+
endif()

tests/test_build_machine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ TEST_CASE_METHOD( host_reference, "machine_specific" ){
6666

6767
REQUIRE(value==expected_has_sse);
6868

69+
6970
}
7071

7172
SECTION( "has_sse2_right" ){
7273

7374
auto value = compass::runtime::has(compass::feature::sse2());
7475

7576
REQUIRE(value==expected_has_sse2);
77+
if(expected_has_sse2){
78+
REQUIRE(compass::compiletime::has<compass::feature::sse2>::enabled==expected_has_sse2);
79+
}
7680

7781
}
7882

@@ -81,6 +85,8 @@ TEST_CASE_METHOD( host_reference, "machine_specific" ){
8185
auto value = compass::runtime::has(compass::feature::sse3());
8286

8387
REQUIRE(value==expected_has_sse3);
88+
if(expected_has_sse3)
89+
REQUIRE(compass::compiletime::has<compass::feature::sse3>::enabled==expected_has_sse3);
8490

8591
}
8692

@@ -89,6 +95,8 @@ TEST_CASE_METHOD( host_reference, "machine_specific" ){
8995
auto value = compass::runtime::has(compass::feature::sse4());
9096

9197
REQUIRE(value==expected_has_sse4);
98+
if(expected_has_sse4)
99+
REQUIRE(compass::compiletime::has<compass::feature::sse4>::enabled==expected_has_sse4);
92100

93101
}
94102

tests/test_compass_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ TEST_CASE( "compass_fundamentals" ){
3838
REQUIRE(value>0u);
3939

4040
}
41-
}
4241

4342

43+
}

0 commit comments

Comments
 (0)