Skip to content

Commit

Permalink
Merge pull request #12 from psteinb/cmake-detect-feature
Browse files Browse the repository at this point in the history
Cmake detect feature
  • Loading branch information
Peter Steinbach authored Apr 30, 2018
2 parents be79a83 + aca0b90 commit 4afa162
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 49 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ endif()

check_cxx_compiler_flag(-Wl,-Bsymbolic HAS_BSYMBOLIC_COMPILERFLAG)
check_cxx_compiler_flag("-Xclang -march=native" HAS_XCLANG_COMPILERFLAG)
check_cxx_compiler_flag("-march=native" HAS_MARCH_COMPILERFLAG)
check_cxx_compiler_flag("-xHost" HAS_XHOST_COMPILERFLAG)
check_cxx_compiler_flag(-Wall HAS_WALL_COMPILERFLAG)
check_cxx_compiler_flag(-ggdb HAS_GGDB_COMPILERFLAG)

Expand Down
36 changes: 19 additions & 17 deletions include/detail/ct/preprocessor_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
#ifndef COMPASS_CT_PREPROCESSOR_IMPL_H_
#define COMPASS_CT_PREPROCESSOR_IMPL_H_

#ifndef WIN32
#if defined __SSE2__ && defined __SSE2_MATH__
#define COMPASS_HAS_SSE2
#endif
#else
#if _M_IX86_FP >= 2
#define COMPASS_HAS_SSE2
#endif
#endif


#ifndef WIN32
#if defined __SSE3__ && defined __SSSE3__
#define COMPASS_HAS_SSE3
#endif

#if defined __SSE4_2__ && defined __SSE4_1__
#define COMPASS_HAS_SSE4
#endif
#if defined(__SSE2__) || defined(__SSE2_MATH__) // && __SSE2__ != 0 && __SSE2_MATH__ != 0
#define COMPASS_CT_HAS_SSE2 1
#endif

#if defined(__SSE3__) && defined(__SSSE3__)
#define COMPASS_CT_HAS_SSE3 1
#endif

#if defined(__SSE4_2__) && defined(__SSE4_1__)
#define COMPASS_CT_HAS_SSE4 1
#endif

#else
//TODO: try to warn users on Windows that we are enabling SSE3 + SSE4 upon assumption here
#define COMPASS_HAS_SSE3
#define COMPASS_HAS_SSE4
#if _M_IX86_FP >= 2
#define COMPASS_CT_HAS_SSE2 1
#define COMPASS_CT_HAS_SSE3 1
#define COMPASS_CT_HAS_SSE4 1
#endif
#endif


#include "detail/tags.hpp"

namespace compass {
Expand Down
2 changes: 0 additions & 2 deletions include/detail/definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ namespace compass {

};

//#include "ct/preprocessor_impl.hpp"

#endif /* DEFINITIONS_H */
1 change: 1 addition & 0 deletions include/detail/dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "detail/definitions.hpp"

#ifdef COMPASS_CT_ARCH_X86
#include "ct/preprocessor_impl.hpp"
#include "rt/x86_impl.hpp"
#include "rt/x86_sizes.hpp"
#endif
Expand Down
129 changes: 100 additions & 29 deletions single_include/compass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ namespace compass {

};



#endif
namespace compass {

Expand Down Expand Up @@ -252,6 +250,106 @@ namespace compass {

#endif
#ifdef COMPASS_CT_ARCH_X86
#ifndef COMPASS_CT_PREPROCESSOR_IMPL_H_
#define COMPASS_CT_PREPROCESSOR_IMPL_H_



#ifndef WIN32

#if defined(__SSE2__) || defined(__SSE2_MATH__)
#define COMPASS_CT_HAS_SSE2 1
#endif

#if defined(__SSE3__) && defined(__SSSE3__)
#define COMPASS_CT_HAS_SSE3 1
#endif

#if defined(__SSE4_2__) && defined(__SSE4_1__)
#define COMPASS_CT_HAS_SSE4 1
#endif

#else

#if _M_IX86_FP >= 2
#define COMPASS_CT_HAS_SSE2 1
#define COMPASS_CT_HAS_SSE3 1
#define COMPASS_CT_HAS_SSE4 1
#endif
#endif
#ifndef COMPASS_TAGS_H_
#define COMPASS_TAGS_H_


namespace compass {


namespace feature {




struct sse {};
struct sse2 {};
struct sse3 {};
struct sse4 {};


struct avx {};
struct avx2 {};


};


};
#endif
namespace compass {

namespace compiletime {

template<typename feature_t>
struct has{
static const bool enabled = false;
};

template<>
struct has<feature::sse2>{
static const bool enabled=
#ifdef COMPASS_CT_HAS_SSE2
true;
#else
false;
#endif

};

template<>
struct has<feature::sse3>{
static const bool enabled=
#ifdef COMPASS_CT_HAS_SSE3
true;
#else
false;
#endif

};

template<>
struct has<feature::sse4>{
static const bool enabled=
#ifdef COMPASS_CT_HAS_SSE4
true;
#else
false;
#endif

};

};
};

#endif
#ifndef COMPASS_RT_X86_IMPL_H_
#define COMPASS_RT_X86_IMPL_H_
#ifndef COMPASS_RT_X86_CPUID_H
Expand Down Expand Up @@ -515,33 +613,6 @@ namespace compass {
#endif
#endif

#endif
#ifndef COMPASS_TAGS_H_
#define COMPASS_TAGS_H_


namespace compass {


namespace feature {




struct sse {};
struct sse2 {};
struct sse3 {};
struct sse4 {};


struct avx {};
struct avx2 {};


};


};
#endif
#ifndef COMPASS_BIT_VIEW_H
#define COMPASS_BIT_VIEW_H
Expand Down
17 changes: 17 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ configure_file(${PROJECT_SOURCE_DIR}/tests/build_machine.hpp.in ${CMAKE_CURRENT_

add_executable(test_build_machine test_build_machine.cpp $<TARGET_OBJECTS:catcho>)
target_include_directories(test_build_machine PRIVATE ${CATCH2_HEADER_PATH} ${COMPASS_INCLUDE_BUILD_DIR} ${CMAKE_CURRENT_BINARY_DIR})
if(HAS_MARCH_COMPILERFLAG)
target_compile_options(test_build_machine PRIVATE "-march=native")
endif()

if(HAS_XHOST_COMPILERFLAG)
target_compile_options(test_build_machine PRIVATE "-xHost")
endif()

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
if(SSE2_FOUND)
target_compile_options(test_build_machine PRIVATE "/arch:SSE2")
elseif(AVX_FOUND)
target_compile_options(test_build_machine PRIVATE "/arch:AVX")
elseif(AVX2_FOUND)
target_compile_options(test_build_machine PRIVATE "/arch:AVX2")
endif()
endif()
8 changes: 8 additions & 0 deletions tests/test_build_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ TEST_CASE_METHOD( host_reference, "machine_specific" ){

REQUIRE(value==expected_has_sse);


}

SECTION( "has_sse2_right" ){

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

REQUIRE(value==expected_has_sse2);
if(expected_has_sse2){
REQUIRE(compass::compiletime::has<compass::feature::sse2>::enabled==expected_has_sse2);
}

}

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

REQUIRE(value==expected_has_sse3);
if(expected_has_sse3)
REQUIRE(compass::compiletime::has<compass::feature::sse3>::enabled==expected_has_sse3);

}

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

REQUIRE(value==expected_has_sse4);
if(expected_has_sse4)
REQUIRE(compass::compiletime::has<compass::feature::sse4>::enabled==expected_has_sse4);

}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_compass_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ TEST_CASE( "compass_fundamentals" ){
REQUIRE(value>0u);

}
}


}

0 comments on commit 4afa162

Please sign in to comment.