Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/stockfish
Path: blob/master/scripts/get_native_properties.sh
393 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-vnni512'
46
elif check_flags 'avx512f' 'avx512bw'; then
47
true_arch='x86-64-avx512'
48
elif check_flags 'avxvnni'; then
49
true_arch='x86-64-avxvnni'
50
elif [ -z "${znver_1_2+1}" ] && check_flags 'bmi2'; then
51
true_arch='x86-64-bmi2'
52
elif check_flags 'avx2'; then
53
true_arch='x86-64-avx2'
54
elif check_flags 'sse41' && check_flags 'popcnt'; then
55
true_arch='x86-64-sse41-popcnt'
56
else
57
true_arch='x86-64'
58
fi
59
}
60
61
set_arch_ppc_64() {
62
if grep -q -w "altivec" /proc/cpuinfo; then
63
power=$(grep -oP -m 1 'cpu\t+: POWER\K\d+' /proc/cpuinfo)
64
if [ "0$power" -gt 7 ]; then
65
# VSX started with POWER8
66
true_arch='ppc-64-vsx'
67
else
68
true_arch='ppc-64-altivec'
69
fi
70
else
71
true_arch='ppc-64'
72
fi
73
}
74
75
# Check the system type
76
uname_s=$(uname -s)
77
uname_m=$(uname -m)
78
case $uname_s in
79
'Darwin') # Mac OSX system
80
case $uname_m in
81
'arm64')
82
true_arch='apple-silicon'
83
file_arch='m1-apple-silicon'
84
;;
85
'x86_64')
86
flags=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features | tr '\n' ' ' | tr '[:upper:]' '[:lower:]' | tr -d '_.')
87
set_arch_x86_64
88
if [ "$true_arch" = 'x86-64-avx512' ]; then
89
file_arch='x86-64-bmi2'
90
fi
91
;;
92
esac
93
file_os='macos'
94
file_ext='tar'
95
;;
96
'Linux') # Linux system
97
get_flags
98
case $uname_m in
99
'x86_64')
100
file_os='ubuntu'
101
check_znver_1_2
102
set_arch_x86_64
103
;;
104
'i686')
105
file_os='ubuntu'
106
true_arch='x86-32'
107
;;
108
'ppc64'*)
109
file_os='ubuntu'
110
set_arch_ppc_64
111
;;
112
'aarch64')
113
file_os='android'
114
true_arch='armv8'
115
if check_flags 'asimddp'; then
116
true_arch="$true_arch-dotprod"
117
fi
118
;;
119
'armv7'*)
120
file_os='android'
121
true_arch='armv7'
122
if check_flags 'neon'; then
123
true_arch="$true_arch-neon"
124
fi
125
;;
126
'loongarch64'*)
127
file_os='linux'
128
set_arch_loongarch64
129
;;
130
*) # Unsupported machine type, exit with error
131
printf 'Unsupported machine type: %s\n' "$uname_m"
132
exit 1
133
;;
134
esac
135
file_ext='tar'
136
;;
137
'MINGW'*'ARM64'*) # Windows ARM64 system with POSIX compatibility layer
138
# TODO: older chips might be armv8, but we have no good way to detect, /proc/cpuinfo shows x86 info
139
file_os='windows'
140
true_arch='armv8-dotprod'
141
file_ext='zip'
142
;;
143
'CYGWIN'*|'MINGW'*|'MSYS'*) # Windows x86_64system with POSIX compatibility layer
144
get_flags
145
check_znver_1_2
146
set_arch_x86_64
147
file_os='windows'
148
file_ext='zip'
149
;;
150
*)
151
# Unknown system type, exit with error
152
printf 'Unsupported system type: %s\n' "$uname_s"
153
exit 1
154
;;
155
esac
156
157
if [ -z "$file_arch" ]; then
158
file_arch=$true_arch
159
fi
160
161
file_name="stockfish-$file_os-$file_arch.$file_ext"
162
163
printf '%s %s\n' "$true_arch" "$file_name"
164
165