Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/scripts/get_native_properties.sh
376 views
1
#!/bin/sh
2
3
#
4
# Returns properties of the native system.
5
# best architecture as supported by the CPU
6
# filename of the best binary uploaded as an artifact during CI
7
#
8
9
# Check if all the given flags are present in the CPU flags list
10
check_flags() {
11
for flag; do
12
printf '%s\n' "$flags" | grep -q -w "$flag" || return 1
13
done
14
}
15
16
# Set the CPU flags list
17
# 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.1
18
get_flags() {
19
flags=$(awk '/^flags[ \t]*:|^Features[ \t]*:/{gsub(/^flags[ \t]*:[ \t]*|^Features[ \t]*:[ \t]*|[_.]/, ""); line=$0} END{print line}' /proc/cpuinfo)
20
}
21
22
# Check for gcc march "znver1" or "znver2" https://en.wikichip.org/wiki/amd/cpuid
23
check_znver_1_2() {
24
vendor_id=$(awk '/^vendor_id/{print $3; exit}' /proc/cpuinfo)
25
cpu_family=$(awk '/^cpu family/{print $4; exit}' /proc/cpuinfo)
26
[ "$vendor_id" = "AuthenticAMD" ] && [ "$cpu_family" = "23" ] && znver_1_2=true
27
}
28
29
# Set the file CPU loongarch64 architecture
30
set_arch_loongarch64() {
31
if check_flags 'lasx'; then
32
true_arch='loongarch64-lasx'
33
elif check_flags 'lsx'; then
34
true_arch='loongarch64-lsx'
35
else
36
true_arch='loongarch64'
37
fi
38
}
39
40
# Set the file CPU x86_64 architecture
41
set_arch_x86_64() {
42
if check_flags 'avx512f' 'avx512cd' 'avx512vl' 'avx512dq' 'avx512bw' 'avx512ifma' 'avx512vbmi' 'avx512vbmi2' 'avx512vpopcntdq' 'avx512bitalg' 'avx512vnni' 'vpclmulqdq' 'gfni' 'vaes'; then
43
true_arch='x86-64-avx512icl'
44
elif check_flags 'avx512vnni' 'avx512dq' 'avx512f' 'avx512bw' 'avx512vl'; then
45
true_arch='x86-64-vnni256'
46
elif check_flags 'avx512f' 'avx512bw'; then
47
true_arch='x86-64-avx512'
48
elif [ -z "${znver_1_2+1}" ] && check_flags 'bmi2'; then
49
true_arch='x86-64-bmi2'
50
elif check_flags 'avx2'; then
51
true_arch='x86-64-avx2'
52
elif check_flags 'sse41' && check_flags 'popcnt'; then
53
true_arch='x86-64-sse41-popcnt'
54
else
55
true_arch='x86-64'
56
fi
57
}
58
59
set_arch_ppc_64() {
60
if grep -q -w "altivec" /proc/cpuinfo; then
61
power=$(grep -oP -m 1 'cpu\t+: POWER\K\d+' /proc/cpuinfo)
62
if [ "0$power" -gt 7 ]; then
63
# VSX started with POWER8
64
true_arch='ppc-64-vsx'
65
else
66
true_arch='ppc-64-altivec'
67
fi
68
else
69
true_arch='ppc-64'
70
fi
71
}
72
73
# Check the system type
74
uname_s=$(uname -s)
75
uname_m=$(uname -m)
76
case $uname_s in
77
'Darwin') # Mac OSX system
78
case $uname_m in
79
'arm64')
80
true_arch='apple-silicon'
81
file_arch='m1-apple-silicon'
82
;;
83
'x86_64')
84
flags=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features | tr '\n' ' ' | tr '[:upper:]' '[:lower:]' | tr -d '_.')
85
set_arch_x86_64
86
if [ "$true_arch" = 'x86-64-vnni256' ] || [ "$true_arch" = 'x86-64-avx512' ]; then
87
file_arch='x86-64-bmi2'
88
fi
89
;;
90
esac
91
file_os='macos'
92
file_ext='tar'
93
;;
94
'Linux') # Linux system
95
get_flags
96
case $uname_m in
97
'x86_64')
98
file_os='ubuntu'
99
check_znver_1_2
100
set_arch_x86_64
101
;;
102
'i686')
103
file_os='ubuntu'
104
true_arch='x86-32'
105
;;
106
'ppc64'*)
107
file_os='ubuntu'
108
set_arch_ppc_64
109
;;
110
'aarch64')
111
file_os='android'
112
true_arch='armv8'
113
if check_flags 'asimddp'; then
114
true_arch="$true_arch-dotprod"
115
fi
116
;;
117
'armv7'*)
118
file_os='android'
119
true_arch='armv7'
120
if check_flags 'neon'; then
121
true_arch="$true_arch-neon"
122
fi
123
;;
124
'loongarch64'*)
125
file_os='linux'
126
set_arch_loongarch64
127
;;
128
*) # Unsupported machine type, exit with error
129
printf 'Unsupported machine type: %s\n' "$uname_m"
130
exit 1
131
;;
132
esac
133
file_ext='tar'
134
;;
135
'MINGW'*'ARM64'*) # Windows ARM64 system with POSIX compatibility layer
136
# TODO: older chips might be armv8, but we have no good way to detect, /proc/cpuinfo shows x86 info
137
file_os='windows'
138
true_arch='armv8-dotprod'
139
file_ext='zip'
140
;;
141
'CYGWIN'*|'MINGW'*|'MSYS'*) # Windows x86_64system with POSIX compatibility layer
142
get_flags
143
check_znver_1_2
144
set_arch_x86_64
145
file_os='windows'
146
file_ext='zip'
147
;;
148
*)
149
# Unknown system type, exit with error
150
printf 'Unsupported system type: %s\n' "$uname_s"
151
exit 1
152
;;
153
esac
154
155
if [ -z "$file_arch" ]; then
156
file_arch=$true_arch
157
fi
158
159
file_name="stockfish-$file_os-$file_arch.$file_ext"
160
161
printf '%s %s\n' "$true_arch" "$file_name"
162
163