@@ -178,6 +178,11 @@ declare -rA ARCH_MAP=(
178178 [mips64]=" mips64el" # uname -m on little-endian MIPS64
179179 [mipsel]=" mipsel"
180180 [mips]=" mipsel" # uname -m on little-endian MIPS32
181+ # 32-bit x86 - will be refined by detect_x86_binary()
182+ [i686]=" x86_32"
183+ [i586]=" x86_32"
184+ [i486]=" x86_32"
185+ [i386]=" x86_32"
181186)
182187
183188# -------------------------------------------------------------------------------
@@ -334,6 +339,59 @@ get_system_endianness() {
334339 fi
335340}
336341
342+ # -------------------------------------------------------------------------------
343+ # x86 CPU Feature Detection
344+ # -------------------------------------------------------------------------------
345+ detect_x86_binary () {
346+ # Check for SSE2 support in /proc/cpuinfo flags
347+ # SSE2 is the deciding factor between i686 (pentium4) and i586 binaries
348+ # i686 binary: Pentium 4 baseline with SSE2, MMX, cmov
349+ # i586 binary: Original Pentium baseline, no SSE, no cmov
350+ if grep -qE " ^flags[[:space:]]*:.*\bsse2\b" /proc/cpuinfo 2> /dev/null; then
351+ echo " i686"
352+ else
353+ echo " i586"
354+ fi
355+ }
356+
357+ # -------------------------------------------------------------------------------
358+ # x86_64 CPU Feature Detection (AVX-512 for v4)
359+ # -------------------------------------------------------------------------------
360+ detect_amd64_binary () {
361+ # Check for x86-64-v4 support (AVX-512 foundation extensions)
362+ # x86-64-v4 requires: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
363+ # amd64_v4 binary: Zen4/Ice Lake+ optimised with AVX-512
364+ # amd64 binary: Baseline x86_64 (maximum compatibility)
365+ local flags
366+ flags=$( grep -m1 " ^flags" /proc/cpuinfo 2> /dev/null || echo " " )
367+ if [[ ${flags} == * avx512f* ]] \
368+ && [[ ${flags} == * avx512bw* ]] \
369+ && [[ ${flags} == * avx512cd* ]] \
370+ && [[ ${flags} == * avx512dq* ]] \
371+ && [[ ${flags} == * avx512vl* ]]; then
372+ echo " amd64_v4"
373+ else
374+ echo " amd64"
375+ fi
376+ }
377+
378+ # -------------------------------------------------------------------------------
379+ # ARM64 CPU Feature Detection (SVE2 for v9)
380+ # -------------------------------------------------------------------------------
381+ detect_arm64_binary () {
382+ # Check for ARMv9 support (SVE2 is the key indicator)
383+ # ARMv9 requires: SVE2 (Scalable Vector Extension 2)
384+ # arm64v9 binary: Neoverse N2/Graviton 3+ optimised with SVE2
385+ # arm64 binary: Baseline ARMv8-A (maximum compatibility)
386+ local features
387+ features=$( grep -m1 " ^Features" /proc/cpuinfo 2> /dev/null || echo " " )
388+ if [[ ${features} == * sve2* ]]; then
389+ echo " arm64v9"
390+ else
391+ echo " arm64"
392+ fi
393+ }
394+
337395# Dry-run aware execution
338396execute () {
339397 if [[ ${DRY_RUN} == true ]]; then
@@ -826,6 +884,32 @@ detect_architecture() {
826884 local machine
827885 machine=" $( uname -m) " || die " Failed to detect architecture" " ${EXIT_GENERAL_ERROR} "
828886
887+ # Handle x86 specially - check CPU features to select optimal binary
888+ case " ${machine} " in
889+ x86_64)
890+ ARCH=$( detect_amd64_binary)
891+ local feature_msg
892+ feature_msg=$( [[ ${ARCH} == " amd64_v4" ]] && echo " AVX-512 detected" || echo " baseline" )
893+ log_success " Architecture: ${machine} -> ${ARCH} (${feature_msg} )"
894+ return 0
895+ ;;
896+ aarch64)
897+ ARCH=$( detect_arm64_binary)
898+ local feature_msg
899+ feature_msg=$( [[ ${ARCH} == " arm64v9" ]] && echo " SVE2 detected" || echo " baseline" )
900+ log_success " Architecture: ${machine} -> ${ARCH} (${feature_msg} )"
901+ return 0
902+ ;;
903+ i? 86)
904+ ARCH=$( detect_x86_binary)
905+ local feature_msg
906+ feature_msg=$( [[ ${ARCH} == " i686" ]] && echo " SSE2 detected" || echo " legacy/no SSE2" )
907+ log_success " Architecture: ${machine} -> ${ARCH} (${feature_msg} )"
908+ return 0
909+ ;;
910+ * ) ;;
911+ esac
912+
829913 # Check for MIPS big-endian systems (uname reports same for BE and LE)
830914 if [[ ${machine} == mips64 || ${machine} == mips ]]; then
831915 local endianness
@@ -838,6 +922,14 @@ detect_architecture() {
838922
839923 if [[ -v ARCH_MAP[${machine} ] ]]; then
840924 ARCH=" ${ARCH_MAP[${machine}]} "
925+ # Handle x86_32 placeholder (shouldn't reach here due to case above, but safety)
926+ if [[ ${ARCH} == " x86_32" ]]; then
927+ ARCH=$( detect_x86_binary)
928+ local feature_msg
929+ feature_msg=$( [[ ${ARCH} == " i686" ]] && echo " SSE2 detected" || echo " legacy/no SSE2" )
930+ log_success " Architecture: ${machine} -> ${ARCH} (${feature_msg} )"
931+ return 0
932+ fi
841933 log_success " Architecture: ${machine} -> ${ARCH} "
842934 else
843935 die " Unsupported architecture: ${machine} . Supported: ${! ARCH_MAP[*]} " " ${EXIT_UNSUPPORTED_ARCH} "
0 commit comments