Path: blob/master/scripts/get_native_properties.sh
393 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-vnni512'45elif check_flags 'avx512f' 'avx512bw'; then46true_arch='x86-64-avx512'47elif check_flags 'avxvnni'; then48true_arch='x86-64-avxvnni'49elif [ -z "${znver_1_2+1}" ] && check_flags 'bmi2'; then50true_arch='x86-64-bmi2'51elif check_flags 'avx2'; then52true_arch='x86-64-avx2'53elif check_flags 'sse41' && check_flags 'popcnt'; then54true_arch='x86-64-sse41-popcnt'55else56true_arch='x86-64'57fi58}5960set_arch_ppc_64() {61if grep -q -w "altivec" /proc/cpuinfo; then62power=$(grep -oP -m 1 'cpu\t+: POWER\K\d+' /proc/cpuinfo)63if [ "0$power" -gt 7 ]; then64# VSX started with POWER865true_arch='ppc-64-vsx'66else67true_arch='ppc-64-altivec'68fi69else70true_arch='ppc-64'71fi72}7374# Check the system type75uname_s=$(uname -s)76uname_m=$(uname -m)77case $uname_s in78'Darwin') # Mac OSX system79case $uname_m in80'arm64')81true_arch='apple-silicon'82file_arch='m1-apple-silicon'83;;84'x86_64')85flags=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features | tr '\n' ' ' | tr '[:upper:]' '[:lower:]' | tr -d '_.')86set_arch_x86_6487if [ "$true_arch" = 'x86-64-avx512' ]; then88file_arch='x86-64-bmi2'89fi90;;91esac92file_os='macos'93file_ext='tar'94;;95'Linux') # Linux system96get_flags97case $uname_m in98'x86_64')99file_os='ubuntu'100check_znver_1_2101set_arch_x86_64102;;103'i686')104file_os='ubuntu'105true_arch='x86-32'106;;107'ppc64'*)108file_os='ubuntu'109set_arch_ppc_64110;;111'aarch64')112file_os='android'113true_arch='armv8'114if check_flags 'asimddp'; then115true_arch="$true_arch-dotprod"116fi117;;118'armv7'*)119file_os='android'120true_arch='armv7'121if check_flags 'neon'; then122true_arch="$true_arch-neon"123fi124;;125'loongarch64'*)126file_os='linux'127set_arch_loongarch64128;;129*) # Unsupported machine type, exit with error130printf 'Unsupported machine type: %s\n' "$uname_m"131exit 1132;;133esac134file_ext='tar'135;;136'MINGW'*'ARM64'*) # Windows ARM64 system with POSIX compatibility layer137# TODO: older chips might be armv8, but we have no good way to detect, /proc/cpuinfo shows x86 info138file_os='windows'139true_arch='armv8-dotprod'140file_ext='zip'141;;142'CYGWIN'*|'MINGW'*|'MSYS'*) # Windows x86_64system with POSIX compatibility layer143get_flags144check_znver_1_2145set_arch_x86_64146file_os='windows'147file_ext='zip'148;;149*)150# Unknown system type, exit with error151printf 'Unsupported system type: %s\n' "$uname_s"152exit 1153;;154esac155156if [ -z "$file_arch" ]; then157file_arch=$true_arch158fi159160file_name="stockfish-$file_os-$file_arch.$file_ext"161162printf '%s %s\n' "$true_arch" "$file_name"163164165