Path: blob/master/scripts/get_native_properties.sh
376 views
#!/bin/sh12#3# Returns properties of the native system.4# best architecture as supported by the CPU5# filename of the best binary uploaded as an artifact during CI6#78# Check if all the given flags are present in the CPU flags list9check_flags() {10for flag; do11printf '%s\n' "$flags" | grep -q -w "$flag" || return 112done13}1415# Set the CPU flags list16# remove underscores and points from flags, e.g. gcc uses avx512vnni, while some cpuinfo can have avx512_vnni, some systems use sse4_1 others sse4.117get_flags() {18flags=$(awk '/^flags[ \t]*:|^Features[ \t]*:/{gsub(/^flags[ \t]*:[ \t]*|^Features[ \t]*:[ \t]*|[_.]/, ""); line=$0} END{print line}' /proc/cpuinfo)19}2021# Check for gcc march "znver1" or "znver2" https://en.wikichip.org/wiki/amd/cpuid22check_znver_1_2() {23vendor_id=$(awk '/^vendor_id/{print $3; exit}' /proc/cpuinfo)24cpu_family=$(awk '/^cpu family/{print $4; exit}' /proc/cpuinfo)25[ "$vendor_id" = "AuthenticAMD" ] && [ "$cpu_family" = "23" ] && znver_1_2=true26}2728# Set the file CPU loongarch64 architecture29set_arch_loongarch64() {30if check_flags 'lasx'; then31true_arch='loongarch64-lasx'32elif check_flags 'lsx'; then33true_arch='loongarch64-lsx'34else35true_arch='loongarch64'36fi37}3839# Set the file CPU x86_64 architecture40set_arch_x86_64() {41if check_flags 'avx512f' 'avx512cd' 'avx512vl' 'avx512dq' 'avx512bw' 'avx512ifma' 'avx512vbmi' 'avx512vbmi2' 'avx512vpopcntdq' 'avx512bitalg' 'avx512vnni' 'vpclmulqdq' 'gfni' 'vaes'; then42true_arch='x86-64-avx512icl'43elif check_flags 'avx512vnni' 'avx512dq' 'avx512f' 'avx512bw' 'avx512vl'; then44true_arch='x86-64-vnni256'45elif check_flags 'avx512f' 'avx512bw'; then46true_arch='x86-64-avx512'47elif [ -z "${znver_1_2+1}" ] && check_flags 'bmi2'; then48true_arch='x86-64-bmi2'49elif check_flags 'avx2'; then50true_arch='x86-64-avx2'51elif check_flags 'sse41' && check_flags 'popcnt'; then52true_arch='x86-64-sse41-popcnt'53else54true_arch='x86-64'55fi56}5758set_arch_ppc_64() {59if grep -q -w "altivec" /proc/cpuinfo; then60power=$(grep -oP -m 1 'cpu\t+: POWER\K\d+' /proc/cpuinfo)61if [ "0$power" -gt 7 ]; then62# VSX started with POWER863true_arch='ppc-64-vsx'64else65true_arch='ppc-64-altivec'66fi67else68true_arch='ppc-64'69fi70}7172# Check the system type73uname_s=$(uname -s)74uname_m=$(uname -m)75case $uname_s in76'Darwin') # Mac OSX system77case $uname_m in78'arm64')79true_arch='apple-silicon'80file_arch='m1-apple-silicon'81;;82'x86_64')83flags=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features | tr '\n' ' ' | tr '[:upper:]' '[:lower:]' | tr -d '_.')84set_arch_x86_6485if [ "$true_arch" = 'x86-64-vnni256' ] || [ "$true_arch" = 'x86-64-avx512' ]; then86file_arch='x86-64-bmi2'87fi88;;89esac90file_os='macos'91file_ext='tar'92;;93'Linux') # Linux system94get_flags95case $uname_m in96'x86_64')97file_os='ubuntu'98check_znver_1_299set_arch_x86_64100;;101'i686')102file_os='ubuntu'103true_arch='x86-32'104;;105'ppc64'*)106file_os='ubuntu'107set_arch_ppc_64108;;109'aarch64')110file_os='android'111true_arch='armv8'112if check_flags 'asimddp'; then113true_arch="$true_arch-dotprod"114fi115;;116'armv7'*)117file_os='android'118true_arch='armv7'119if check_flags 'neon'; then120true_arch="$true_arch-neon"121fi122;;123'loongarch64'*)124file_os='linux'125set_arch_loongarch64126;;127*) # Unsupported machine type, exit with error128printf 'Unsupported machine type: %s\n' "$uname_m"129exit 1130;;131esac132file_ext='tar'133;;134'MINGW'*'ARM64'*) # Windows ARM64 system with POSIX compatibility layer135# TODO: older chips might be armv8, but we have no good way to detect, /proc/cpuinfo shows x86 info136file_os='windows'137true_arch='armv8-dotprod'138file_ext='zip'139;;140'CYGWIN'*|'MINGW'*|'MSYS'*) # Windows x86_64system with POSIX compatibility layer141get_flags142check_znver_1_2143set_arch_x86_64144file_os='windows'145file_ext='zip'146;;147*)148# Unknown system type, exit with error149printf 'Unsupported system type: %s\n' "$uname_s"150exit 1151;;152esac153154if [ -z "$file_arch" ]; then155file_arch=$true_arch156fi157158file_name="stockfish-$file_os-$file_arch.$file_ext"159160printf '%s %s\n' "$true_arch" "$file_name"161162163