#!/usr/bin/env bash
# The fallback path must run on any x86-64: outside the AVX2, AVX-512 and POPCNT objects (the files named *_avx2.c, *_avx512.c, *_popcnt.c: functions
# with the popcnt target that the cpu dispatch picks) there is no POPCNT instruction, and nothing links libgcc's __popcountdi2 (a distribution may build it with
# POPCNT). Run after a build.
#
#   scripts/isa_check.sh
set -eu
cd "$(dirname "$0")/.."

[[ $(uname -m) == x86_64 ]] || { echo "isa_check: skipped, the POPCNT baseline is an x86-64 rule (NEON is part of the AArch64 baseline)"; exit 0; }

bad=0 checked=0 skipped=0
for o in src/*/*.o src/*/*/*.o; do
	case $o in *_avx2.o | *_avx512.o | *_popcnt.o) skipped=$((skipped + 1)); continue ;; esac
	checked=$((checked + 1))
	insns=$(objdump -d "$o" | grep -cw popcnt || true)
	libcalls=$(nm "$o" 2>/dev/null | grep -c ' U __popcount' || true)
	if ((insns + libcalls)); then
		echo "baseline object uses POPCNT: $o"
		bad=1
	fi
done
if [[ $(nm pg_sift.so | grep -ci '__popcount.i2' || true) != 0 ]]; then
	echo "pg_sift.so links a libgcc popcount helper"
	bad=1
fi
((checked)) || { echo "isa_check: no objects in src/, build first"; exit 1; }
if ((bad)); then echo "isa_check: FAIL"; else echo "isa_check: ok, $checked baseline objects without POPCNT or libgcc popcount calls ($skipped AVX2/AVX-512/POPCNT objects skipped), pg_sift.so links no libgcc popcount helper"; fi
exit $bad
