Path: blob/21.2-virgl/src/freedreno/computerator/examples/test-opcodes.sh
4570 views
#!/bin/bash1#2# Test various instructions to check whether half<->full widening/narrowing3# works. The basic premise is to perform the same instruction with and4# without the widening/narrowing folded in and check if the results match.5#6# Note this doesn't currently diferentiate between signed/unsigned/bool,7# and just assumes int is signed (since unsigned is basically(ish) like8# signed but without sign extension)9#10# TODO probably good pick numeric src values that are better at triggering11# edge cases, while still not loosing precision in a full->half->full12# seqeuence.. but some instructions like absneg don't even appear to be13# subtlely wrong when you try to fold in a precision conversion.14#15# add '-v' arg to see the result values1617set -e1819#20# Templates for float->float instructions:21#22f2f_instrs=(23'add.f $dst, $src1, $src2'24'min.f $dst, $src1, $src2'25'min.f $dst, $src2, $src1'26'max.f $dst, $src1, $src2'27'max.f $dst, $src2, $src1'28'mul.f $dst, $src1, $src2'29'sign.f $dst, $src1'30'absneg.f $dst, \(neg\)$src1'31'absneg.f $dst, \(abs\)$src1'32'floor.f $dst, $src1'33'ceil.f $dst, $src1'34'rndne.f $dst, $src1'35'rndaz.f $dst, $src1'36'trunc.f $dst, $src1'37)3839#40# Templates for float->int instructions:41#42f2i_instrs=(43'cmps.f.gt $dst, $src1, $src2'44'cmps.f.lt $dst, $src1, $src2'45'cmpv.f.gt $dst, $src1, $src2'46'cmpv.f.lt $dst, $src1, $src2'47)4849#50# Templates for int->int instructions:51#52i2i_instrs=(53'add.u $dst, $src1, $src2'54'add.s $dst, $src1, $src2'55'sub.u $dst, $src1, $src2'56'sub.s $dst, $src1, $src2'57'cmps.f.gt $dst, $src1, $src2'58'cmps.f.lt $dst, $src1, $src2'59'min.u $dst, $src1, $src2'60'min.u $dst, $src2, $src1'61'min.s $dst, $src1, $src2'62'min.s $dst, $src2, $src1'63'max.u $dst, $src1, $src2'64'max.u $dst, $src2, $src1'65'max.s $dst, $src1, $src2'66'max.s $dst, $src2, $src1'67'absneg.s $dst, \(neg\)$src1'68'absneg.s $dst, \(abs\)$src1'69'and.b $dst, $src2, $src3'70'or.b $dst, $src1, $src2'71'not.b $dst, $src1'72'xor.b $dst, $src1, $src2'73'cmpv.u.gt $dst, $src1, $src2'74'cmpv.u.lt $dst, $src1, $src2'75'cmpv.s.gt $dst, $src1, $src2'76'cmpv.s.lt $dst, $src1, $src2'77'mul.u24 $dst, $src1, $src2'78'mul.s24 $dst, $src1, $src2'79'mull.u $dst, $src1, $src2'80'bfrev.b $dst, $src1'81'clz.s $dst, $src2'82'clz.b $dst, $src2'83'shl.b $dst, $src1, $src2'84'shr.b $dst, $src3, $src1'85'ashr.b $dst, $src3, $src1'86'mgen.b $dst, $src1, $src2'87'getbit.b $dst, $src3, $src2'88'setrm $dst, $src1'89'cbits.b $dst, $src3'90'shb $dst, $src1, $src2'91'msad $dst, $src1, $src2'92)9394#95# Helper to expand instruction template:96#97expand() {98instr=$199dst=$2100src1=$3101src2=$4102src3=$5103eval echo $instr104}105106expand_test() {107instr=$1108109echo '; control, half->half:'110expand $instr "hr1.x" "hr0.x" "hr0.y" "hr0.z"111echo '; test, full->half:'112expand $instr "hr1.y" "r1.x" "r1.y" "r1.z"113114echo '; control, full->full:'115expand $instr "r2.x" "r1.x" "r1.y" "r1.z"116echo '; test, half->full:'117expand $instr "r2.y" "hr0.x" "hr0.y" "hr0.z"118119echo "(rpt5)nop"120}121122#123# Helpers to construct test program assembly:124#125header_asm() {126cat <<EOF127@localsize 1, 1, 1128@buf 4 ; g[0]129EOF130}131132footer_asm() {133cat <<EOF134; dest offsets:135mov.u32u32 r3.x, 0136mov.u32u32 r3.y, 1137mov.u32u32 r3.z, 2138mov.u32u32 r3.w, 3139(rpt5)nop140141; and store results:142stib.untyped.1d.u32.1 r2.x, r3.x, 0 ; control: full->full143stib.untyped.1d.u32.1 r2.y, r3.y, 0 ; test: half->full144stib.untyped.1d.u32.1 r2.z, r3.z, 0 ; control: half->half145stib.untyped.1d.u32.1 r2.w, r3.w, 0 ; test: full->half146(sy)nop147end148EOF149}150151setup_asm_float() {152cat <<EOF153; hr0->hr1 (r0) avail for half, hr0 for src, hr1 for dst154; r1->r2 avail for full, r1 for src, r2 for dst155cov.f32f16 hr0.x, (1.0)156cov.f32f16 hr0.y, (2.0)157cov.f32f16 hr0.z, (3.0)158mov.f32f32 r1.x, (1.0)159mov.f32f32 r1.y, (2.0)160mov.f32f32 r1.z, (3.0)161(rpt5)nop162EOF163}164165setup_asm_int() {166cat <<EOF167; hr0->hr1 (r0) avail for half, hr0 for src, hr1 for dst168; r1->r2 avail for full, r1 for src, r2 for dst169cov.s32s16 hr0.x, 1170cov.s32s16 hr0.y, -2171cov.s32s16 hr0.z, 3172mov.s32s32 r1.x, 1173mov.s32s32 r1.y, -2174mov.s32s32 r1.z, 3175(rpt5)nop176EOF177}178179#180# Generate assembly code to test float->float opcode181#182f2f_asm() {183instr=$1184185header_asm186setup_asm_float187expand_test $instr188189cat <<EOF190; convert half results back to full:191cov.f16f32 r2.z, hr1.x192cov.f16f32 r2.w, hr1.y193EOF194195footer_asm196}197198#199# Generate assembly code to test float->int opcode200#201f2i_asm() {202instr=$1203204header_asm205setup_asm_float206expand_test $instr207208cat <<EOF209; convert half results back to full:210cov.s16s32 r2.z, hr1.x211cov.s16s32 r2.w, hr1.y212EOF213214footer_asm215}216217#218# Generate assembly code to test int->int opcode219#220i2i_asm() {221instr=$1222223header_asm224setup_asm_int225expand_test $instr226227cat <<EOF228; convert half results back to full:229cov.s16s32 r2.z, hr1.x230cov.s16s32 r2.w, hr1.y231EOF232233footer_asm234}235236237#238# Helper to parse computerator output and print results:239#240check_results() {241str=`cat - | grep " " | head -1 | xargs`242243if [ "$verbose" = "true" ]; then244echo $str245fi246247# Split components of result buffer:248cf=$(echo $str | cut -f1 -d' ')249tf=$(echo $str | cut -f2 -d' ')250ch=$(echo $str | cut -f3 -d' ')251th=$(echo $str | cut -f4 -d' ')252253# Sanity test, make sure the control results match:254if [ $cf != $ch ]; then255echo " FAIL: control results do not match! Half vs full op is not equivalent!"256echo " full=$cf half=$ch"257fi258259# Compare test (with conversion folded) to control:260if [ $cf != $tf ]; then261echo " FAIL: half -> full widening result does not match control!"262echo " control=$cf result=$tf"263fi264if [ $ch != $th ]; then265echo " FAIL: full -> half narrowing result does not match control!"266echo " control=$ch result=$th"267fi268269# HACK without a delay different invocations270# of computerator seem to somehow clobber each271# other.. which isn't great..272sleep 0.1273}274275#276# Run the tests!277#278279if [ "$1" = "-v" ]; then280verbose="true"281fi282283IFS=""284for instr in ${f2f_instrs[@]}; do285echo "TEST: $instr"286f2f_asm $instr | ./computerator -g 1,1,1 | check_results287done288for instr in ${f2i_instrs[@]}; do289echo "TEST: $instr"290f2i_asm $instr | ./computerator -g 1,1,1 | check_results291done292for instr in ${i2i_instrs[@]}; do293echo "TEST: $instr"294i2i_asm $instr | ./computerator -g 1,1,1 | check_results295done296297298299