1 # ------------------------------------------------------------------------------
2 # Cpu features definition and flags
3 # ------------------------------------------------------------------------------
5 # Initialize ALL_CPU_FEATURES as empty list.
6 set(ALL_CPU_FEATURES "")
8 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
9 set(ALL_CPU_FEATURES SSE2 SSE4_2 AVX AVX2 AVX512F AVX512BW FMA)
10 set(LIBC_COMPILE_OPTIONS_NATIVE -march=native)
11 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
12 set(LIBC_COMPILE_OPTIONS_NATIVE -mcpu=native)
15 # Making sure ALL_CPU_FEATURES is sorted.
16 list(SORT ALL_CPU_FEATURES)
18 # Function to check whether the target CPU supports the provided set of features.
22 # <list of cpu features>
24 function(cpu_supports output_var features)
25 _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
26 if("${var}" STREQUAL "${features}")
27 set(${output_var} TRUE PARENT_SCOPE)
29 unset(${output_var} PARENT_SCOPE)
33 # ------------------------------------------------------------------------------
34 # Internal helpers and utilities.
35 # ------------------------------------------------------------------------------
37 # Computes the intersection between two lists.
38 function(_intersection output_var list1 list2)
39 foreach(element IN LISTS list1)
40 if("${list2}" MATCHES "(^|;)${element}(;|$)")
41 list(APPEND tmp "${element}")
44 set(${output_var} ${tmp} PARENT_SCOPE)
47 set(AVAILABLE_CPU_FEATURES "")
49 # If we are doing a cross build, we will just assume that all CPU features
51 set(AVAILABLE_CPU_FEATURES ${ALL_CPU_FEATURES})
53 # Try compile a C file to check if flag is supported.
54 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
55 foreach(feature IN LISTS ALL_CPU_FEATURES)
58 ${CMAKE_CURRENT_BINARY_DIR}/cpu_features
59 SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/cpu_features/check_${feature}.cpp
60 COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
63 list(APPEND AVAILABLE_CPU_FEATURES ${feature})
68 set(LIBC_CPU_FEATURES ${AVAILABLE_CPU_FEATURES} CACHE STRING "Host supported CPU features")
70 _intersection(cpu_features "${AVAILABLE_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
71 if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
72 message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
74 message(STATUS "Set CPU features: ${cpu_features}")