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 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
26 unset(${output_var} PARENT_SCOPE)
29 _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
30 if("${var}" STREQUAL "${features}")
31 set(${output_var} TRUE PARENT_SCOPE)
33 unset(${output_var} PARENT_SCOPE)
37 # ------------------------------------------------------------------------------
38 # Internal helpers and utilities.
39 # ------------------------------------------------------------------------------
41 # Computes the intersection between two lists.
42 function(_intersection output_var list1 list2)
43 foreach(element IN LISTS list1)
44 if("${list2}" MATCHES "(^|;)${element}(;|$)")
45 list(APPEND tmp "${element}")
48 set(${output_var} ${tmp} PARENT_SCOPE)
51 set(AVAILABLE_CPU_FEATURES "")
53 # If we are doing a cross build, we will just assume that all CPU features
55 set(AVAILABLE_CPU_FEATURES ${ALL_CPU_FEATURES})
57 # Try compile a C file to check if flag is supported.
58 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
59 foreach(feature IN LISTS ALL_CPU_FEATURES)
62 ${CMAKE_CURRENT_BINARY_DIR}/cpu_features
63 SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/cpu_features/check_${feature}.cpp
64 COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
67 list(APPEND AVAILABLE_CPU_FEATURES ${feature})
72 set(LIBC_CPU_FEATURES ${AVAILABLE_CPU_FEATURES} CACHE STRING "Host supported CPU features")
74 _intersection(cpu_features "${AVAILABLE_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
75 if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
76 message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
78 message(STATUS "Set CPU features: ${cpu_features}")