#!/bin/sh
# LP: #2169719, OpenBLAS #5917: on arm64, sdot added the caller's v0 to the
# result.  Build the reproducer against the installed library and run it on
# the core dispatch picks, then on each kernel the fix changed: NEOVERSEN1
# (Apple silicon, Neoverse N1, AmpereOne), THUNDERX2T99 and THUNDERX3T110.

set -e

if [ -z "$AUTOPKGTEST_TMP" ]
then
	echo "Temporary directory \$AUTOPKGTEST_TMP is not set. Aborting" >&2
	exit 1
fi

# Tell us which flavor is being used
update-alternatives --get-selections | grep '^libopenblas.so'

gcc -O2 -Wall debian/tests/sdot-v0.c -o "$AUTOPKGTEST_TMP"/sdot-v0 -lopenblas
"$AUTOPKGTEST_TMP"/sdot-v0

# Force each kernel the fix changed, when the CPU has the extensions it was
# built for (-march): THUNDERX2T99 armv8.1-a, NEOVERSEN1 armv8.2-a,
# THUNDERX3T110 armv8.3-a.
has() {
	for f in "$@"
	do
		grep -m1 '^Features' /proc/cpuinfo | grep -qw "$f" || return 1
	done
}
force() {
	core=$1; shift
	if has "$@"
	then
		echo "OPENBLAS_CORETYPE=$core"
		OPENBLAS_CORETYPE=$core "$AUTOPKGTEST_TMP"/sdot-v0
	else
		echo "OPENBLAS_CORETYPE=$core: skipped, CPU lacks one of: $*"
	fi
}
force THUNDERX2T99 atomics asimdrdm
force NEOVERSEN1 atomics asimdrdm dcpop
force THUNDERX3T110 atomics asimdrdm dcpop jscvt fcma lrcpc
