pr_exec.c (PR_ExecuteProgram): Implemented some of LordHavoc's

optimizations mostly by removing the temporary eval_t *a,*b and *c
variables and replacing them by OPA, OPB and OPC macros. Removed
the statement index s and used the statement pointer instead,
updating the pr_xstatement global whenever necessary. In OP_STATE,
stored OPA->_float to ed->v.frame without comparing them (I don't
understand the significance to the comparison.)  Casted st->a, b
and c to unsigned short instead of using them signed as they are
as the pr_globals index value. Updated profiling code.

git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@505 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
Ozkan Sezer 2011-12-12 09:03:34 +00:00
parent 9ba7acffc4
commit 396167e0c7
4 changed files with 112 additions and 105 deletions

View file

@ -348,15 +348,16 @@ PR_ExecuteProgram
The interpretation main loop The interpretation main loop
==================== ====================
*/ */
#define OPA ((eval_t *)&pr_globals[(unsigned short)st->a])
#define OPB ((eval_t *)&pr_globals[(unsigned short)st->b])
#define OPC ((eval_t *)&pr_globals[(unsigned short)st->c])
void PR_ExecuteProgram (func_t fnum) void PR_ExecuteProgram (func_t fnum)
{ {
eval_t *a, *b, *c;
eval_t *ptr; eval_t *ptr;
int s;
dstatement_t *st; dstatement_t *st;
dfunction_t *f, *newf; dfunction_t *f, *newf;
int runaway; int profile, startprofile;
int i;
edict_t *ed; edict_t *ed;
int exitdepth; int exitdepth;
@ -369,28 +370,23 @@ void PR_ExecuteProgram (func_t fnum)
f = &pr_functions[fnum]; f = &pr_functions[fnum];
runaway = 100000;
pr_trace = false; pr_trace = false;
// make a stack frame // make a stack frame
exitdepth = pr_depth; exitdepth = pr_depth;
s = PR_EnterFunction(f); st = &pr_statements[PR_EnterFunction(f)];
startprofile = profile = 0;
while (1) while (1)
{ {
s++; // next statement st++; /* next statement */
st = &pr_statements[s]; if (++profile > 100000)
a = (eval_t *)&pr_globals[st->a]; {
b = (eval_t *)&pr_globals[st->b]; pr_xstatement = st - pr_statements;
c = (eval_t *)&pr_globals[st->c];
if (!--runaway)
PR_RunError("runaway loop error"); PR_RunError("runaway loop error");
}
pr_xfunction->profile++;
pr_xstatement = s;
if (pr_trace) if (pr_trace)
PR_PrintStatement(st); PR_PrintStatement(st);
@ -398,123 +394,123 @@ void PR_ExecuteProgram (func_t fnum)
switch (st->op) switch (st->op)
{ {
case OP_ADD_F: case OP_ADD_F:
c->_float = a->_float + b->_float; OPC->_float = OPA->_float + OPB->_float;
break; break;
case OP_ADD_V: case OP_ADD_V:
c->vector[0] = a->vector[0] + b->vector[0]; OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
c->vector[1] = a->vector[1] + b->vector[1]; OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
c->vector[2] = a->vector[2] + b->vector[2]; OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
break; break;
case OP_SUB_F: case OP_SUB_F:
c->_float = a->_float - b->_float; OPC->_float = OPA->_float - OPB->_float;
break; break;
case OP_SUB_V: case OP_SUB_V:
c->vector[0] = a->vector[0] - b->vector[0]; OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
c->vector[1] = a->vector[1] - b->vector[1]; OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
c->vector[2] = a->vector[2] - b->vector[2]; OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
break; break;
case OP_MUL_F: case OP_MUL_F:
c->_float = a->_float * b->_float; OPC->_float = OPA->_float * OPB->_float;
break; break;
case OP_MUL_V: case OP_MUL_V:
c->_float = a->vector[0]*b->vector[0] + OPC->_float = OPA->vector[0] * OPB->vector[0] +
a->vector[1]*b->vector[1] + OPA->vector[1] * OPB->vector[1] +
a->vector[2]*b->vector[2]; OPA->vector[2] * OPB->vector[2];
break; break;
case OP_MUL_FV: case OP_MUL_FV:
c->vector[0] = a->_float * b->vector[0]; OPC->vector[0] = OPA->_float * OPB->vector[0];
c->vector[1] = a->_float * b->vector[1]; OPC->vector[1] = OPA->_float * OPB->vector[1];
c->vector[2] = a->_float * b->vector[2]; OPC->vector[2] = OPA->_float * OPB->vector[2];
break; break;
case OP_MUL_VF: case OP_MUL_VF:
c->vector[0] = b->_float * a->vector[0]; OPC->vector[0] = OPB->_float * OPA->vector[0];
c->vector[1] = b->_float * a->vector[1]; OPC->vector[1] = OPB->_float * OPA->vector[1];
c->vector[2] = b->_float * a->vector[2]; OPC->vector[2] = OPB->_float * OPA->vector[2];
break; break;
case OP_DIV_F: case OP_DIV_F:
c->_float = a->_float / b->_float; OPC->_float = OPA->_float / OPB->_float;
break; break;
case OP_BITAND: case OP_BITAND:
c->_float = (int)a->_float & (int)b->_float; OPC->_float = (int)OPA->_float & (int)OPB->_float;
break; break;
case OP_BITOR: case OP_BITOR:
c->_float = (int)a->_float | (int)b->_float; OPC->_float = (int)OPA->_float | (int)OPB->_float;
break; break;
case OP_GE: case OP_GE:
c->_float = a->_float >= b->_float; OPC->_float = OPA->_float >= OPB->_float;
break; break;
case OP_LE: case OP_LE:
c->_float = a->_float <= b->_float; OPC->_float = OPA->_float <= OPB->_float;
break; break;
case OP_GT: case OP_GT:
c->_float = a->_float > b->_float; OPC->_float = OPA->_float > OPB->_float;
break; break;
case OP_LT: case OP_LT:
c->_float = a->_float < b->_float; OPC->_float = OPA->_float < OPB->_float;
break; break;
case OP_AND: case OP_AND:
c->_float = a->_float && b->_float; OPC->_float = OPA->_float && OPB->_float;
break; break;
case OP_OR: case OP_OR:
c->_float = a->_float || b->_float; OPC->_float = OPA->_float || OPB->_float;
break; break;
case OP_NOT_F: case OP_NOT_F:
c->_float = !a->_float; OPC->_float = !OPA->_float;
break; break;
case OP_NOT_V: case OP_NOT_V:
c->_float = !a->vector[0] && !a->vector[1] && !a->vector[2]; OPC->_float = !OPA->vector[0] && !OPA->vector[1] && !OPA->vector[2];
break; break;
case OP_NOT_S: case OP_NOT_S:
c->_float = !a->string || !*PR_GetString(a->string); OPC->_float = !OPA->string || !*PR_GetString(OPA->string);
break; break;
case OP_NOT_FNC: case OP_NOT_FNC:
c->_float = !a->function; OPC->_float = !OPA->function;
break; break;
case OP_NOT_ENT: case OP_NOT_ENT:
c->_float = (PROG_TO_EDICT(a->edict) == sv.edicts); OPC->_float = (PROG_TO_EDICT(OPA->edict) == sv.edicts);
break; break;
case OP_EQ_F: case OP_EQ_F:
c->_float = a->_float == b->_float; OPC->_float = OPA->_float == OPB->_float;
break; break;
case OP_EQ_V: case OP_EQ_V:
c->_float = (a->vector[0] == b->vector[0]) && OPC->_float = (OPA->vector[0] == OPB->vector[0]) &&
(a->vector[1] == b->vector[1]) && (OPA->vector[1] == OPB->vector[1]) &&
(a->vector[2] == b->vector[2]); (OPA->vector[2] == OPB->vector[2]);
break; break;
case OP_EQ_S: case OP_EQ_S:
c->_float = !strcmp(PR_GetString(a->string), PR_GetString(b->string)); OPC->_float = !strcmp(PR_GetString(OPA->string), PR_GetString(OPB->string));
break; break;
case OP_EQ_E: case OP_EQ_E:
c->_float = a->_int == b->_int; OPC->_float = OPA->_int == OPB->_int;
break; break;
case OP_EQ_FNC: case OP_EQ_FNC:
c->_float = a->function == b->function; OPC->_float = OPA->function == OPB->function;
break; break;
case OP_NE_F: case OP_NE_F:
c->_float = a->_float != b->_float; OPC->_float = OPA->_float != OPB->_float;
break; break;
case OP_NE_V: case OP_NE_V:
c->_float = (a->vector[0] != b->vector[0]) || OPC->_float = (OPA->vector[0] != OPB->vector[0]) ||
(a->vector[1] != b->vector[1]) || (OPA->vector[1] != OPB->vector[1]) ||
(a->vector[2] != b->vector[2]); (OPA->vector[2] != OPB->vector[2]);
break; break;
case OP_NE_S: case OP_NE_S:
c->_float = strcmp(PR_GetString(a->string),PR_GetString(b->string)); OPC->_float = strcmp(PR_GetString(OPA->string), PR_GetString(OPB->string));
break; break;
case OP_NE_E: case OP_NE_E:
c->_float = a->_int != b->_int; OPC->_float = OPA->_int != OPB->_int;
break; break;
case OP_NE_FNC: case OP_NE_FNC:
c->_float = a->function != b->function; OPC->_float = OPA->function != OPB->function;
break; break;
case OP_STORE_F: case OP_STORE_F:
@ -522,12 +518,12 @@ void PR_ExecuteProgram (func_t fnum)
case OP_STORE_FLD: // integers case OP_STORE_FLD: // integers
case OP_STORE_S: case OP_STORE_S:
case OP_STORE_FNC: // pointers case OP_STORE_FNC: // pointers
b->_int = a->_int; OPB->_int = OPA->_int;
break; break;
case OP_STORE_V: case OP_STORE_V:
b->vector[0] = a->vector[0]; OPB->vector[0] = OPA->vector[0];
b->vector[1] = a->vector[1]; OPB->vector[1] = OPA->vector[1];
b->vector[2] = a->vector[2]; OPB->vector[2] = OPA->vector[2];
break; break;
case OP_STOREP_F: case OP_STOREP_F:
@ -535,24 +531,27 @@ void PR_ExecuteProgram (func_t fnum)
case OP_STOREP_FLD: // integers case OP_STOREP_FLD: // integers
case OP_STOREP_S: case OP_STOREP_S:
case OP_STOREP_FNC: // pointers case OP_STOREP_FNC: // pointers
ptr = (eval_t *)((byte *)sv.edicts + b->_int); ptr = (eval_t *)((byte *)sv.edicts + OPB->_int);
ptr->_int = a->_int; ptr->_int = OPA->_int;
break; break;
case OP_STOREP_V: case OP_STOREP_V:
ptr = (eval_t *)((byte *)sv.edicts + b->_int); ptr = (eval_t *)((byte *)sv.edicts + OPB->_int);
ptr->vector[0] = a->vector[0]; ptr->vector[0] = OPA->vector[0];
ptr->vector[1] = a->vector[1]; ptr->vector[1] = OPA->vector[1];
ptr->vector[2] = a->vector[2]; ptr->vector[2] = OPA->vector[2];
break; break;
case OP_ADDRESS: case OP_ADDRESS:
ed = PROG_TO_EDICT(a->edict); ed = PROG_TO_EDICT(OPA->edict);
#ifdef PARANOID #ifdef PARANOID
NUM_FOR_EDICT(ed); // Make sure it's in range NUM_FOR_EDICT(ed); // Make sure it's in range
#endif #endif
if (ed == (edict_t *)sv.edicts && sv.state == ss_active) if (ed == (edict_t *)sv.edicts && sv.state == ss_active)
{
pr_xstatement = st - pr_statements;
PR_RunError("assignment to world entity"); PR_RunError("assignment to world entity");
c->_int = (byte *)((int *)&ed->v + b->_int) - (byte *)sv.edicts; }
OPC->_int = (byte *)((int *)&ed->v + OPB->_int) - (byte *)sv.edicts;
break; break;
case OP_LOAD_F: case OP_LOAD_F:
@ -560,39 +559,36 @@ void PR_ExecuteProgram (func_t fnum)
case OP_LOAD_ENT: case OP_LOAD_ENT:
case OP_LOAD_S: case OP_LOAD_S:
case OP_LOAD_FNC: case OP_LOAD_FNC:
ed = PROG_TO_EDICT(a->edict); ed = PROG_TO_EDICT(OPA->edict);
#ifdef PARANOID #ifdef PARANOID
NUM_FOR_EDICT(ed); // Make sure it's in range NUM_FOR_EDICT(ed); // Make sure it's in range
#endif #endif
a = (eval_t *)((int *)&ed->v + b->_int); OPC->_int = ((eval_t *)((int *)&ed->v + OPB->_int))->_int;
c->_int = a->_int;
break; break;
case OP_LOAD_V: case OP_LOAD_V:
ed = PROG_TO_EDICT(a->edict); ed = PROG_TO_EDICT(OPA->edict);
#ifdef PARANOID #ifdef PARANOID
NUM_FOR_EDICT(ed); // Make sure it's in range NUM_FOR_EDICT(ed); // Make sure it's in range
#endif #endif
a = (eval_t *)((int *)&ed->v + b->_int); ptr = (eval_t *)((int *)&ed->v + OPB->_int);
c->vector[0] = a->vector[0]; OPC->vector[0] = ptr->vector[0];
c->vector[1] = a->vector[1]; OPC->vector[1] = ptr->vector[1];
c->vector[2] = a->vector[2]; OPC->vector[2] = ptr->vector[2];
break; break;
//==================
case OP_IFNOT: case OP_IFNOT:
if (!a->_int) if (!OPA->_int)
s += st->b - 1; // offset the s++ st += st->b - 1; /* -1 to offset the st++ */
break; break;
case OP_IF: case OP_IF:
if (a->_int) if (OPA->_int)
s += st->b - 1; // offset the s++ st += st->b - 1; /* -1 to offset the st++ */
break; break;
case OP_GOTO: case OP_GOTO:
s += st->a - 1; // offset the s++ st += st->a - 1; /* -1 to offset the st++ */
break; break;
case OP_CALL0: case OP_CALL0:
@ -604,28 +600,34 @@ void PR_ExecuteProgram (func_t fnum)
case OP_CALL6: case OP_CALL6:
case OP_CALL7: case OP_CALL7:
case OP_CALL8: case OP_CALL8:
pr_xfunction->profile += profile - startprofile;
startprofile = profile;
pr_xstatement = st - pr_statements;
pr_argc = st->op - OP_CALL0; pr_argc = st->op - OP_CALL0;
if (!a->function) if (!OPA->function)
PR_RunError("NULL function"); PR_RunError("NULL function");
newf = &pr_functions[a->function]; newf = &pr_functions[OPA->function];
if (newf->first_statement < 0) if (newf->first_statement < 0)
{ // Built-in function { // Built-in function
i = -newf->first_statement; int i = -newf->first_statement;
if (i >= pr_numbuiltins) if (i >= pr_numbuiltins)
PR_RunError("Bad builtin call number %d", i); PR_RunError("Bad builtin call number %d", i);
pr_builtins[i](); pr_builtins[i]();
break; break;
} }
// Normal function // Normal function
s = PR_EnterFunction(newf); st = &pr_statements[PR_EnterFunction(newf)];
break; break;
case OP_DONE: case OP_DONE:
case OP_RETURN: case OP_RETURN:
pr_globals[OFS_RETURN] = pr_globals[st->a]; pr_xfunction->profile += profile - startprofile;
pr_globals[OFS_RETURN + 1] = pr_globals[st->a + 1]; startprofile = profile;
pr_globals[OFS_RETURN + 2] = pr_globals[st->a + 2]; pr_xstatement = st - pr_statements;
s = PR_LeaveFunction(); pr_globals[OFS_RETURN] = pr_globals[(unsigned short)st->a];
pr_globals[OFS_RETURN + 1] = pr_globals[(unsigned short)st->a + 1];
pr_globals[OFS_RETURN + 2] = pr_globals[(unsigned short)st->a + 2];
st = &pr_statements[PR_LeaveFunction()];
if (pr_depth == exitdepth) if (pr_depth == exitdepth)
{ // Done { // Done
return; return;
@ -635,16 +637,17 @@ void PR_ExecuteProgram (func_t fnum)
case OP_STATE: case OP_STATE:
ed = PROG_TO_EDICT(pr_global_struct->self); ed = PROG_TO_EDICT(pr_global_struct->self);
ed->v.nextthink = pr_global_struct->time + 0.1; ed->v.nextthink = pr_global_struct->time + 0.1;
if (a->_float != ed->v.frame) ed->v.frame = OPA->_float;
{ ed->v.think = OPB->function;
ed->v.frame = a->_float;
}
ed->v.think = b->function;
break; break;
default: default:
pr_xstatement = st - pr_statements;
PR_RunError("Bad opcode %i", st->op); PR_RunError("Bad opcode %i", st->op);
} }
} /* end of while(1) loop */ } /* end of while(1) loop */
} }
#undef OPA
#undef OPB
#undef OPC

View file

@ -11,7 +11,7 @@
<PRE> <PRE>
</PRE> </PRE>
</P> </P>
<P>QuakeSpasm 0.85.5 (11 May 2011)</P> <P>QuakeSpasm 0.85.5 (12 December 2011)</P>
<P> <P>
<H2><A NAME="toc1">1.</A> <A HREF="README.html#s1">About </A></H2> <H2><A NAME="toc1">1.</A> <A HREF="README.html#s1">About </A></H2>
@ -164,6 +164,7 @@ Compile time options include
<LI> mlook and lookspring fixes</LI> <LI> mlook and lookspring fixes</LI>
<LI> Added support for loading external entity files, controlled by new cvar external_ents.</LI> <LI> Added support for loading external entity files, controlled by new cvar external_ents.</LI>
<LI> Made mp3 playback to allocate system memory instead of zone</LI> <LI> Made mp3 playback to allocate system memory instead of zone</LI>
<LI> Some updates to the progs interpreter code</LI>
<LI> Several code updates from uHexen2, several code cleanups.</LI> <LI> Several code updates from uHexen2, several code cleanups.</LI>
</UL> </UL>
</P> </P>

View file

@ -4,7 +4,7 @@
<toc> <toc>
<verb></verb> <verb></verb>
QuakeSpasm 0.85.5 (11 May 2011) QuakeSpasm 0.85.5 (12 December 2011)
<sect> About <p> <sect> About <p>
@ -98,6 +98,7 @@ Alternatively, have a look at <bf>Makefile.darwin</bf> for more instructions on
<item> mlook and lookspring fixes <item> mlook and lookspring fixes
<item> Added support for loading external entity files, controlled by new cvar external_ents. <item> Added support for loading external entity files, controlled by new cvar external_ents.
<item> Made mp3 playback to allocate system memory instead of zone <item> Made mp3 playback to allocate system memory instead of zone
<item> Some updates to the progs interpreter code
<item> Several code updates from uHexen2, several code cleanups. <item> Several code updates from uHexen2, several code cleanups.
</itemize> </itemize>
</p> </p>

View file

@ -29,7 +29,7 @@
______________________________________________________________________ ______________________________________________________________________
QuakeSpasm 0.85.5 (11 May 2011) QuakeSpasm 0.85.5 (12 December 2011)
1. About 1. About
@ -169,6 +169,8 @@
o Made mp3 playback to allocate system memory instead of zone o Made mp3 playback to allocate system memory instead of zone
o Some updates to the progs interpreter code
o Several code updates from uHexen2, several code cleanups. o Several code updates from uHexen2, several code cleanups.
5.2. Changes in 0.85.4 5.2. Changes in 0.85.4