Bill Allombert on Sun, 26 Nov 2006 01:26:12 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: New GP evaluator release 2.4.1.compile-5


On Sat, Nov 25, 2006 at 09:07:51PM +0100, Bill Allombert wrote:
> The part that I might want to change is the bytecode format.
> I lack experience about the various trade-off involved and whether
> it is worth the trouble to store opcodes on a single byte instead 
> of four.

Please find a patch that implement that.

With this patch, closures look that way
["opcodes",Vecsmall([operand]),[data]]

It seems a little faster and allow the 1.6MB script cubic-map.gp 
(by F. Villegas, http://www.ma.utexas.edu/users/villegas/cnt/cubic-map.gp)
to be read with 8MB of stack instead of 12MB.

Cheers,
Bill.
Index: parigp3/src/language/compile.c
===================================================================
--- parigp3.orig/src/language/compile.c	2006-11-25 16:59:48.000000000 +0100
+++ parigp3/src/language/compile.c	2006-11-25 23:36:11.000000000 +0100
@@ -26,8 +26,9 @@
  **                                                                       **
  ***************************************************************************/
 
-static THREAD gp2c_stack s_opcode, s_data;
-static THREAD long *opcode;
+static THREAD gp2c_stack s_opcode, s_operand, s_data;
+static THREAD char *opcode;
+static THREAD long *operand;
 static THREAD GEN *data;
 static THREAD long offset=-1;
 
@@ -35,6 +36,7 @@
 pari_init_compiler(void)
 {
   stack_init(&s_opcode,sizeof(*opcode),(void **)&opcode);
+  stack_init(&s_operand,sizeof(*operand),(void **)&operand);
   stack_init(&s_data,sizeof(*data),(void **)&data);
 }
 
@@ -52,16 +54,24 @@
 {
   long lop =s_opcode.n+1-sop;
   long ldat=s_data.n+1-sdat;
-  GEN cl=cgetg(3,t_VEC);
+  GEN cl=cgetg(4,t_VEC);
+  char *s;
   long i;
-  gel(cl,1) = cgetg(lop,  t_VECSMALL);
-  gel(cl,2) = cgetg(ldat, t_VEC);
+  gel(cl,1) = cgetg(nchar2nlong(lop)+1, t_STR);
+  gel(cl,2) = cgetg(lop,  t_VECSMALL);
+  gel(cl,3) = cgetg(ldat, t_VEC);
+  s=GSTR(gel(cl,1))-1;
   for(i=1;i<lop;i++)
-    mael(cl, 1, i) = opcode[i+sop-1];
+  {
+    s[i] = opcode[i+sop-1];
+    mael(cl, 2, i) = operand[i+sop-1];
+  }
+  s[i]=0;
   s_opcode.n=sop;
+  s_operand.n=sop;
   for(i=1;i<ldat;i++)
   {
-    gmael(cl, 2, i) = gcopy(data[i+sdat-1]);
+    gmael(cl, 3, i) = gcopy(data[i+sdat-1]);
     gunclone(data[i+sdat-1]);
   }
   s_data.n=sdat;
@@ -73,9 +83,9 @@
 op_push(op_code o, long x)
 {
   long n=stack_new(&s_opcode);
-  long m=stack_new(&s_opcode);
+  long m=stack_new(&s_operand);
   opcode[n]=o;
-  opcode[m]=x;
+  operand[m]=x;
 }
 
 static long
Index: parigp3/src/language/eval.c
===================================================================
--- parigp3.orig/src/language/eval.c	2006-11-25 16:59:48.000000000 +0100
+++ parigp3/src/language/eval.c	2006-11-26 00:05:53.000000000 +0100
@@ -431,13 +431,14 @@
 {
   gp_pointer ptrs[16];
   long rp=0;
-  GEN code=gel(C,1);
-  GEN data=gel(C,2);
+  char *code=GSTR(gel(C,1))-1;
+  GEN oper=gel(C,2);
+  GEN data=gel(C,3);
   long pc, j;
-  for(pc=1;pc<lg(code);pc+=2)
+  for(pc=1;pc<lg(oper);pc++)
   {
     op_code opcode=(op_code) code[pc];
-    long operand=code[pc+1];
+    long operand=oper[pc];
     entree *ep;
     if (sp>1000) pari_err(talker,"evaluator stack exhausted");
     switch(opcode)
@@ -944,15 +945,24 @@
 void
 closure_disassemble(GEN C)
 {
-  GEN code;
+  char * code;
+  GEN oper;
   long i;
-  if (typ(C)!=t_VEC || lg(C)!=3 || typ(C[1])!=t_VECSMALL) 
+  if (typ(C)==t_STR)
+  {
+    entree *ep=fetch_entry(GSTR(C),strlen(GSTR(C)));
+    if (ep->valence!=EpUSER)
+      pari_err(typeer,"disassemble");
+    C=(GEN)ep->value;
+  }
+  if (typ(C)!=t_VEC || lg(C)!=4 || typ(C[1])!=t_STR || typ(C[2])!=t_VECSMALL) 
     pari_err(typeer,"disassemble");
-  code=gel(C,1);
-  for(i=1;i<lg(code);i+=2)
+  code=GSTR(gel(C,1))-1;
+  oper=gel(C,2);
+  for(i=1;i<lg(oper);i++)
   {
     op_code opcode=(op_code) code[i];
-    long operand=code[i+1];
+    long operand=oper[i];
     entree *ep;
     pariprintf("%05ld\t",i);
     switch(opcode)
Index: parigp3/src/language/opcode.h
===================================================================
--- parigp3.orig/src/language/opcode.h	2006-11-25 16:59:48.000000000 +0100
+++ parigp3/src/language/opcode.h	2006-11-25 23:35:29.000000000 +0100
@@ -16,7 +16,7 @@
 
 typedef enum {Gvoid, Gsmall, Gvec, Gvar, Ggen} Gtype;
 
-typedef enum {OCpushlong,OCpushgen,OCpushreal,OCpushstoi,
+typedef enum {OCpushlong='A',OCpushgen,OCpushreal,OCpushstoi,
               OCpushvalue,OCpushvar,
               OCpop,
               OCstoi,OCitos,OCtostr,OCvarn,OCpolx,