Skip to content

Instantly share code, notes, and snippets.

@zhangyoufu
Created December 12, 2025 02:05
Show Gist options
  • Select an option

  • Save zhangyoufu/8fd6f360c7a9c5def77c5c74a20d4a65 to your computer and use it in GitHub Desktop.

Select an option

Save zhangyoufu/8fd6f360c7a9c5def77c5c74a20d4a65 to your computer and use it in GitHub Desktop.
detect x86-64 ABI version on Linux, using /proc/cpuinfo
#!/bin/bash
# Note on hybrid CPUs (P-cores/E-cores):
# Linux /proc/cpuinfo reports only features common to ALL cores.
# This script will correctly detect the lowest ISA level supported
# across all core types, which matches glibc's behavior.
get_x86_64_abi_version() {
local FLAGS
local DETECTED_LEVEL
local LEVEL
local FEATURES
local FEATURE
local MISSING_FEATURE=0
FLAGS=$(sed -n -e '/flags :/{s///;p;q}' /proc/cpuinfo) || return
DETECTED_LEVEL=0
while IFS=: read -r LEVEL FEATURES; do
for FEATURE in $FEATURES; do
case "$FLAGS" in
*" $FEATURE "*)
continue
;;
*)
if [ $MISSING_FEATURE -eq 0 ]; then
echo "missing features for x86-64-v$LEVEL:" >&2
MISSING_FEATURE=1
fi
echo "- $FEATURE" >&2
;;
esac
done
if [ $MISSING_FEATURE -eq 1 ]; then
break
fi
DETECTED_LEVEL=$LEVEL
done <<EOF
1:lm cmov cx8 fpu fxsr mmx syscall sse sse2
2:cx16 lahf_lm popcnt pni sse4_1 sse4_2 ssse3
3:avx avx2 bmi1 bmi2 f16c fma abm movbe xsave
4:avx512f avx512bw avx512cd avx512dq avx512vl
EOF
# level definitions: "level_number:features"
# each level includes features beyond the previous level
# note: 'lm' = Long Mode (64-bit), 'pni' = SSE3, 'abm' = LZCNT
echo "$DETECTED_LEVEL"
}
ABI_VERSION=$(get_x86_64_abi_version)
if [ -n "$ABI_VERSION" ]; then
echo "result: x86-64-v$ABI_VERSION"
exit 0
else
echo 'failed to detect x86-64 ABI version' >&2
exit 1
fi
@zhangyoufu
Copy link
Author

% ./detect.sh
missing features for x86-64-v4:
- avx512f
- avx512bw
- avx512cd
- avx512dq
- avx512vl
result: x86-64-v3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment