@@ -113,8 +113,36 @@ add_library(FANS::FANS ALIAS FANS_FANS)
113113if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm|aarch64" )
114114 target_compile_options (FANS_FANS PUBLIC -march=armv8-a+simd+fp+crypto)
115115elseif (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
119147add_executable (FANS_main)
120148add_custom_command (
0 commit comments