Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations #96

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions bddisasm/bdx86_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ NdParseMemoryOperand16(
// Store the displacement.
Operand->Info.Memory.HasDisp = !!Instrux->HasDisp;
Operand->Info.Memory.DispSize = Instrux->DispLength;
Operand->Info.Memory.Disp = ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement);
Operand->Info.Memory.Disp = Instrux->HasDisp ? ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement) : 0;

return ND_STATUS_SUCCESS;
}
Expand Down Expand Up @@ -1416,7 +1416,7 @@ NdParseMemoryOperand3264(

Operand->Info.Memory.HasDisp = Instrux->HasDisp;
Operand->Info.Memory.DispSize = Instrux->DispLength;
Operand->Info.Memory.Disp = ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement);
Operand->Info.Memory.Disp = Instrux->HasDisp ? ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement) : 0;

return ND_STATUS_SUCCESS;
}
Expand All @@ -1441,14 +1441,14 @@ NdParseOperand(
ND_UINT8 opt, ops, opf, opa, opd, opb;
ND_REG_SIZE vsibRegSize;
ND_UINT8 vsibIndexSize, vsibIndexCount;
ND_OPERAND_SIZE size, bcstSize;
ND_OPERAND_SIZE size;
ND_BOOL width;

// pre-init
status = ND_STATUS_SUCCESS;
vsibRegSize = 0;
vsibIndexSize = vsibIndexCount = 0;
size = bcstSize = 0;
size = 0;

// Get actual width.
width = Instrux->Exs.w && !(Instrux->Attributes & ND_FLAG_WIG);
Expand Down Expand Up @@ -1877,7 +1877,7 @@ NdParseOperand(
}

// Store operand info.
operand->Size = bcstSize = size;
operand->Size = size;

//
// Fill in the operand type.
Expand Down Expand Up @@ -2242,7 +2242,7 @@ NdParseOperand(
break;

case ND_OPT_LSTAR:
// The operand is implicit and is the IA32_STAR.
// The operand is implicit and is the IA32_LSTAR.
operand->Type = ND_OP_REG;
operand->Info.Register.Type = ND_REG_MSR;
operand->Info.Register.Size = ND_SIZE_64BIT;
Expand Down Expand Up @@ -2778,6 +2778,7 @@ NdParseOperand(
// bcstSize / rawSize.
if (Instrux->HasBroadcast)
{
ND_OPERAND_SIZE bcstSize = size;
operand->Info.Memory.HasBroadcast = ND_TRUE;

if (opd & ND_OPD_B32)
Expand Down
60 changes: 60 additions & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh
set -e

if [ "$#" -ne 4 ] ; then
echo "Compare the speed of two different disasmtool versions"
echo "Usage $0 <first disasmtool> <second disasmtool> <input file> <iterations>"
exit 0
fi

FIRST=$1
SECOND=$2
INPUT=$3
COUNT=$4

if [ ! -x "$FIRST" ] ; then
echo "First program $FIRST does not exist or is not executable"
exit 1
fi

if [ ! -x "$SECOND" ] ; then
echo "Second program $SECOND does not exist or is not executable"
exit 1
fi

if [ ! -f "$INPUT" ] ; then
echo "Input file $INPUT does not exist"
exit 1
fi

case $COUNT in
''|*[!0-9]*) echo "Iteration count $COUNT is not a number" ; exit 1 ;;
*) ;;
esac

if [ "$COUNT" -lt 3 ] ; then
echo "ministat requires at least 3 samples"
exit 1
fi

FIRSTRESULT="$FIRST.result"
SECONDRESULT="$SECOND.result"

truncate -s 0 $FIRSTRESULT
truncate -s 0 $SECONDRESULT

# Make sure all necessary files are in cache
$FIRST -f $INPUT -nv > /dev/null
$SECOND -f $INPUT -nv > /dev/null

for n in `seq 1 $COUNT` ; do
echo "$n"
$FIRST -f $INPUT -nv -iv | tee -a $FIRSTRESULT
$SECOND -f $INPUT -nv -iv | tee -a $SECONDRESULT
done

ministat -C 6 $FIRSTRESULT $SECONDRESULT
echo 'Instructions/second, higher is better'
if [ "$COUNT" -lt 30 ] ; then
echo "Sample count $COUNT was less than 30, results might be unreliable"
fi
9 changes: 4 additions & 5 deletions inc/bdx86_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,14 @@ typedef ND_UINT32 ND_REG_SIZE;
#define ND_SIGN_EX_16(x) (((x) & 0x00008000) ? (0xFFFFFFFFFFFF0000 | (x)) : ((x) & 0xFFFF))
// Sign extend 32 bit to 64 bit.
#define ND_SIGN_EX_32(x) (((x) & 0x80000000) ? (0xFFFFFFFF00000000 | (x)) : ((x) & 0xFFFFFFFF))
// Wrapper for for ND_SIGN_EX_8/ND_SIGN_EX_16/ND_SIGN_EX_32. Sign extend sz bytes to 64 bits.
#define ND_SIGN_EX(sz, x) ((sz) == 1 ? ND_SIGN_EX_8(x) : (sz) == 2 ? ND_SIGN_EX_16(x) : \
(sz) == 4 ? ND_SIGN_EX_32(x) : (x))
// Sign extend to 64 bit, with minimal branches
#define ND_SIGN_EX(sz, x) (((x) & ND_SIZE_TO_MASK(sz)) | (~ND_SIZE_TO_MASK(sz) * ND_GET_SIGN(sz, x)))

// Trim 64 bits to sz bytes.
#define ND_TRIM(sz, x) ((sz) == 1 ? (x) & 0xFF : (sz) == 2 ? (x) & 0xFFFF : \
(sz) == 4 ? (x) & 0xFFFFFFFF : (x))
// Returns most significant bit, given size in bytes sz.
#define ND_MSB(sz, x) ((sz) == 1 ? ((x) >> 7) & 1 : (sz) == 2 ? ((x) >> 15) & 1 : \
(sz) == 4 ? ((x) >> 31) & 1 : ((x) >> 63) & 1)
#define ND_MSB(sz, x) (((x) >> ( (sz) * 8 - 1)) & 1)
// Returns least significant bit.
#define ND_LSB(sz, x) ((x) & 1)
// Convert a size in bytes to a bitmask.
Expand Down