Skip to content

Commit c877b50

Browse files
authored
Merge pull request #96 from turol/optimize
Optimizations
2 parents 4026e76 + 56bbcca commit c877b50

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

bddisasm/bdx86_decoder.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ NdParseMemoryOperand16(
12981298
// Store the displacement.
12991299
Operand->Info.Memory.HasDisp = !!Instrux->HasDisp;
13001300
Operand->Info.Memory.DispSize = Instrux->DispLength;
1301-
Operand->Info.Memory.Disp = ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement);
1301+
Operand->Info.Memory.Disp = Instrux->HasDisp ? ND_SIGN_EX(Instrux->DispLength, Instrux->Displacement) : 0;
13021302

13031303
return ND_STATUS_SUCCESS;
13041304
}
@@ -1416,7 +1416,7 @@ NdParseMemoryOperand3264(
14161416

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

14211421
return ND_STATUS_SUCCESS;
14221422
}
@@ -1441,14 +1441,14 @@ NdParseOperand(
14411441
ND_UINT8 opt, ops, opf, opa, opd, opb;
14421442
ND_REG_SIZE vsibRegSize;
14431443
ND_UINT8 vsibIndexSize, vsibIndexCount;
1444-
ND_OPERAND_SIZE size, bcstSize;
1444+
ND_OPERAND_SIZE size;
14451445
ND_BOOL width;
14461446

14471447
// pre-init
14481448
status = ND_STATUS_SUCCESS;
14491449
vsibRegSize = 0;
14501450
vsibIndexSize = vsibIndexCount = 0;
1451-
size = bcstSize = 0;
1451+
size = 0;
14521452

14531453
// Get actual width.
14541454
width = Instrux->Exs.w && !(Instrux->Attributes & ND_FLAG_WIG);
@@ -1877,7 +1877,7 @@ NdParseOperand(
18771877
}
18781878

18791879
// Store operand info.
1880-
operand->Size = bcstSize = size;
1880+
operand->Size = size;
18811881

18821882
//
18831883
// Fill in the operand type.
@@ -2242,7 +2242,7 @@ NdParseOperand(
22422242
break;
22432243

22442244
case ND_OPT_LSTAR:
2245-
// The operand is implicit and is the IA32_STAR.
2245+
// The operand is implicit and is the IA32_LSTAR.
22462246
operand->Type = ND_OP_REG;
22472247
operand->Info.Register.Type = ND_REG_MSR;
22482248
operand->Info.Register.Size = ND_SIZE_64BIT;
@@ -2778,6 +2778,7 @@ NdParseOperand(
27782778
// bcstSize / rawSize.
27792779
if (Instrux->HasBroadcast)
27802780
{
2781+
ND_OPERAND_SIZE bcstSize = size;
27812782
operand->Info.Memory.HasBroadcast = ND_TRUE;
27822783

27832784
if (opd & ND_OPD_B32)

benchmark.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/sh
2+
set -e
3+
4+
if [ "$#" -ne 4 ] ; then
5+
echo "Compare the speed of two different disasmtool versions"
6+
echo "Usage $0 <first disasmtool> <second disasmtool> <input file> <iterations>"
7+
exit 0
8+
fi
9+
10+
FIRST=$1
11+
SECOND=$2
12+
INPUT=$3
13+
COUNT=$4
14+
15+
if [ ! -x "$FIRST" ] ; then
16+
echo "First program $FIRST does not exist or is not executable"
17+
exit 1
18+
fi
19+
20+
if [ ! -x "$SECOND" ] ; then
21+
echo "Second program $SECOND does not exist or is not executable"
22+
exit 1
23+
fi
24+
25+
if [ ! -f "$INPUT" ] ; then
26+
echo "Input file $INPUT does not exist"
27+
exit 1
28+
fi
29+
30+
case $COUNT in
31+
''|*[!0-9]*) echo "Iteration count $COUNT is not a number" ; exit 1 ;;
32+
*) ;;
33+
esac
34+
35+
if [ "$COUNT" -lt 3 ] ; then
36+
echo "ministat requires at least 3 samples"
37+
exit 1
38+
fi
39+
40+
FIRSTRESULT="$FIRST.result"
41+
SECONDRESULT="$SECOND.result"
42+
43+
truncate -s 0 $FIRSTRESULT
44+
truncate -s 0 $SECONDRESULT
45+
46+
# Make sure all necessary files are in cache
47+
$FIRST -f $INPUT -nv > /dev/null
48+
$SECOND -f $INPUT -nv > /dev/null
49+
50+
for n in `seq 1 $COUNT` ; do
51+
echo "$n"
52+
$FIRST -f $INPUT -nv -iv | tee -a $FIRSTRESULT
53+
$SECOND -f $INPUT -nv -iv | tee -a $SECONDRESULT
54+
done
55+
56+
ministat -C 6 $FIRSTRESULT $SECONDRESULT
57+
echo 'Instructions/second, higher is better'
58+
if [ "$COUNT" -lt 30 ] ; then
59+
echo "Sample count $COUNT was less than 30, results might be unreliable"
60+
fi

inc/bdx86_core.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,14 @@ typedef ND_UINT32 ND_REG_SIZE;
353353
#define ND_SIGN_EX_16(x) (((x) & 0x00008000) ? (0xFFFFFFFFFFFF0000 | (x)) : ((x) & 0xFFFF))
354354
// Sign extend 32 bit to 64 bit.
355355
#define ND_SIGN_EX_32(x) (((x) & 0x80000000) ? (0xFFFFFFFF00000000 | (x)) : ((x) & 0xFFFFFFFF))
356-
// Wrapper for for ND_SIGN_EX_8/ND_SIGN_EX_16/ND_SIGN_EX_32. Sign extend sz bytes to 64 bits.
357-
#define ND_SIGN_EX(sz, x) ((sz) == 1 ? ND_SIGN_EX_8(x) : (sz) == 2 ? ND_SIGN_EX_16(x) : \
358-
(sz) == 4 ? ND_SIGN_EX_32(x) : (x))
356+
// Sign extend to 64 bit, with minimal branches
357+
#define ND_SIGN_EX(sz, x) (((x) & ND_SIZE_TO_MASK(sz)) | (~ND_SIZE_TO_MASK(sz) * ND_GET_SIGN(sz, x)))
358+
359359
// Trim 64 bits to sz bytes.
360360
#define ND_TRIM(sz, x) ((sz) == 1 ? (x) & 0xFF : (sz) == 2 ? (x) & 0xFFFF : \
361361
(sz) == 4 ? (x) & 0xFFFFFFFF : (x))
362362
// Returns most significant bit, given size in bytes sz.
363-
#define ND_MSB(sz, x) ((sz) == 1 ? ((x) >> 7) & 1 : (sz) == 2 ? ((x) >> 15) & 1 : \
364-
(sz) == 4 ? ((x) >> 31) & 1 : ((x) >> 63) & 1)
363+
#define ND_MSB(sz, x) (((x) >> ( (sz) * 8 - 1)) & 1)
365364
// Returns least significant bit.
366365
#define ND_LSB(sz, x) ((x) & 1)
367366
// Convert a size in bytes to a bitmask.

0 commit comments

Comments
 (0)