Skip to content

Commit a4f8cc3

Browse files
authored
The LS1 PR: Introduce system for checking compiler flags: avx2 & fma (#34)
* Introduce system for checking compiler flags + i7-2600 was otherwise not able to built + super backward change + does not apply/test on macos
1 parent b2aa364 commit a4f8cc3

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

CMakeLists.txt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,36 @@ add_library(FANS::FANS ALIAS FANS_FANS)
113113
if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm|aarch64")
114114
target_compile_options(FANS_FANS PUBLIC -march=armv8-a+simd+fp+crypto)
115115
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
116-
target_compile_options(FANS_FANS PUBLIC -mavx2 -mfma)
117-
endif ()
116+
# Detecting if there is avx2 and fma support on linux
117+
# Deteced together, because these flags were introduced by Intel Haskell 4th. Gen
118+
# Distinguishing between mac/linux because lscpu is used, which is not supported on mac.
119+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
120+
# macOS: Assume AVX2 and FMA are supported as per requirement
121+
message(STATUS "Assuming AVX2 and FMA support on macOS; using -mavx2 and -mfma.")
122+
target_compile_options(FANS_FANS PUBLIC -mavx2 -mfma)
123+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
124+
# Linux: Check for AVX2 support using lscpu
125+
execute_process(COMMAND lscpu OUTPUT_VARIABLE LSCPU_OUTPUT ERROR_QUIET)
126+
127+
# Default to AVX; enable AVX2 if supported by hardware
128+
set(FANS_USE_AVX2 FALSE)
129+
if (LSCPU_OUTPUT MATCHES "avx2")
130+
set(FANS_USE_AVX2 TRUE)
131+
endif()
132+
133+
# Apply compiler options based on hardware detection
134+
if (FANS_USE_AVX2)
135+
message(STATUS "AVX2 and FMA supported by hardware; using -mavx2 and -mfma.")
136+
target_compile_options(FANS_FANS PUBLIC -mavx2 -mfma)
137+
else()
138+
message(WARNING "AVX2 and FMA not supported by hardware; falling back to AVX.")
139+
target_compile_options(FANS_FANS PUBLIC -mavx)
140+
endif()
141+
else()
142+
message(WARNING "Unsupported system for AVX2 detection; defaulting to -mavx.")
143+
target_compile_options(FANS_FANS PUBLIC -mavx)
144+
endif()
145+
endif()
118146

119147
add_executable(FANS_main)
120148
add_custom_command(

0 commit comments

Comments
 (0)