Skip to content

Commit

Permalink
The LS1 PR: Introduce system for checking compiler flags: avx2 & fma (#…
Browse files Browse the repository at this point in the history
…34)

* Introduce system for checking compiler flags
+ i7-2600 was otherwise not able to built
+ super backward change
+ does not apply/test on macos
  • Loading branch information
claudiushaag authored Dec 3, 2024
1 parent b2aa364 commit a4f8cc3
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,36 @@ add_library(FANS::FANS ALIAS FANS_FANS)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm|aarch64")
target_compile_options(FANS_FANS PUBLIC -march=armv8-a+simd+fp+crypto)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
target_compile_options(FANS_FANS PUBLIC -mavx2 -mfma)
endif ()
# Detecting if there is avx2 and fma support on linux
# Deteced together, because these flags were introduced by Intel Haskell 4th. Gen
# Distinguishing between mac/linux because lscpu is used, which is not supported on mac.
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
# macOS: Assume AVX2 and FMA are supported as per requirement
message(STATUS "Assuming AVX2 and FMA support on macOS; using -mavx2 and -mfma.")
target_compile_options(FANS_FANS PUBLIC -mavx2 -mfma)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Linux: Check for AVX2 support using lscpu
execute_process(COMMAND lscpu OUTPUT_VARIABLE LSCPU_OUTPUT ERROR_QUIET)

# Default to AVX; enable AVX2 if supported by hardware
set(FANS_USE_AVX2 FALSE)
if (LSCPU_OUTPUT MATCHES "avx2")
set(FANS_USE_AVX2 TRUE)
endif()

# Apply compiler options based on hardware detection
if (FANS_USE_AVX2)
message(STATUS "AVX2 and FMA supported by hardware; using -mavx2 and -mfma.")
target_compile_options(FANS_FANS PUBLIC -mavx2 -mfma)
else()
message(WARNING "AVX2 and FMA not supported by hardware; falling back to AVX.")
target_compile_options(FANS_FANS PUBLIC -mavx)
endif()
else()
message(WARNING "Unsupported system for AVX2 detection; defaulting to -mavx.")
target_compile_options(FANS_FANS PUBLIC -mavx)
endif()
endif()

add_executable(FANS_main)
add_custom_command(
Expand Down

0 comments on commit a4f8cc3

Please sign in to comment.