Skip to content

Commit

Permalink
Refactor multiarch definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Apr 4, 2024
1 parent fef58dc commit d7cf2a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
31 changes: 10 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ if(NOT isMultiConfig AND NOT CMAKE_BUILD_TYPE)
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ENABLE_ASSERT "ENABLE_ASSERT")
endif()

# primesieve binary source files #####################################

set(BIN_SRC src/app/CmdOptions.cpp
Expand Down Expand Up @@ -107,6 +111,9 @@ endif()

if(WITH_MULTIARCH)
include("${PROJECT_SOURCE_DIR}/cmake/multiarch_avx512.cmake")
if(multiarch_avx512)
set(MULTIARCH_AVX512 "MULTIARCH_AVX512")
endif()
endif()

# libprimesieve (shared library) #####################################
Expand All @@ -122,13 +129,7 @@ if(BUILD_SHARED_LIBS)
set_target_properties(libprimesieve PROPERTIES SOVERSION ${PRIMESIEVE_SOVERSION_MAJOR})
set_target_properties(libprimesieve PROPERTIES VERSION ${PRIMESIEVE_SOVERSION})
target_compile_options(libprimesieve PRIVATE ${FTREE_VECTORIZE_FLAG} ${FVECT_COST_MODEL_FLAG})

if(multiarch_avx512)
target_compile_definitions(libprimesieve PRIVATE "MULTIARCH_AVX512")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(libprimesieve PRIVATE "ENABLE_ASSERT")
endif()
target_compile_definitions(libprimesieve PRIVATE "${ENABLE_ASSERT}" "${MULTIARCH_AVX512}")

if(WIN32_MSVC_COMPATIBLE)
# On Windows the shared library will be named primesieve.dll
Expand Down Expand Up @@ -166,13 +167,7 @@ if(BUILD_STATIC_LIBS)
set_target_properties(libprimesieve-static PROPERTIES OUTPUT_NAME primesieve)
target_link_libraries(libprimesieve-static PRIVATE Threads::Threads ${LIBATOMIC})
target_compile_options(libprimesieve-static PRIVATE ${FTREE_VECTORIZE_FLAG} ${FVECT_COST_MODEL_FLAG})

if(multiarch_avx512)
target_compile_definitions(libprimesieve-static PRIVATE "MULTIARCH_AVX512")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(libprimesieve-static PRIVATE "ENABLE_ASSERT")
endif()
target_compile_definitions(libprimesieve-static PRIVATE "${ENABLE_ASSERT}" "${MULTIARCH_AVX512}")

if(WITH_MSVC_CRT_STATIC)
set_target_properties(libprimesieve-static PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded")
Expand Down Expand Up @@ -227,16 +222,10 @@ endif()
if(BUILD_PRIMESIEVE)
add_executable(primesieve ${BIN_SRC})
target_link_libraries(primesieve primesieve::primesieve Threads::Threads)
target_compile_definitions(primesieve PRIVATE "${ENABLE_ASSERT}")
target_compile_features(primesieve PRIVATE cxx_auto_type)
install(TARGETS primesieve DESTINATION ${CMAKE_INSTALL_BINDIR})

if(multiarch_avx512)
target_compile_definitions(primesieve PRIVATE "MULTIARCH_AVX512")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(primesieve PRIVATE "ENABLE_ASSERT")
endif()

if(WITH_MSVC_CRT_STATIC)
set_target_properties(primesieve PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()
Expand Down
13 changes: 9 additions & 4 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,19 @@ void cpuInfo()
else
std::cout << "Logical CPU cores: unknown" << std::endl;

// We only show AVX512 info if libprimesieve has been compiled
// with AVX512 support. If "AVX512: yes" then primesieve::iterator
// uses the AVX512 version of PrimeGenerator::fillNextPrimes().
#if defined(MULTIARCH_AVX512)
// Enable on x86 CPUs
#if defined(__x86_64__) || \
defined(__i386__) || \
defined(_M_X64) || \
defined(_M_IX86) || \
defined(__AVX512__) || \
defined(__AVX512F__)

if (cpu.hasAVX512())
std::cout << "Has AVX512: yes" << std::endl;
else
std::cout << "Has AVX512: no" << std::endl;

#endif

if (cpu.hasL1Cache())
Expand Down
4 changes: 1 addition & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ foreach(file ${files})
get_filename_component(binary_name ${file} NAME_WE)
add_executable(${binary_name} ${file})
target_link_libraries(${binary_name} primesieve::primesieve)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(${binary_name} PRIVATE "ENABLE_ASSERT")
endif()
target_compile_definitions(${binary_name} PRIVATE "${ENABLE_ASSERT}" "${MULTIARCH_AVX512}")
add_test(NAME ${binary_name} COMMAND ${binary_name})
endforeach()

0 comments on commit d7cf2a3

Please sign in to comment.