Ilya Zakharevich on Thu, 16 Dec 2004 07:42:01 +0100


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

Two stacks at once


Suppose we have two stacks:

  top1, bottom1, avma1;
  top2, bottom2, avma2;
  current_stack;		/* 1 or 2 */

 At the start: current_stack = 1; top = top1; bottom = bottom1; avma = avma1.

 There is a macro: SWAP_STACKS(), which resets current_stack, top,
 bottom and avma.

Given this, I think one can significantly speed up PARI: instead of
incessant moving of temporaries on the stack, they are just created on
the stack corresponding to the depth-of-calls:

 in a PARI function, before you create temporaries and call other PARI
 functions, you do SWAP_STACKS(); as a result, the temporaries you
 need and the result of the call of the PARI functions are created on
 the other stack; before creating the GEN which is going to be the
 final result, you SWAP_STACKS() again.  Also, one needs to save/reset
 OTHER_AVMA at the start/end of the subroutine too.

If you do so, no movement of GENs on the stack is needed.  Your
grandkids create the results on "yours" stack, but when these results
are perused by your children, your stack is clean again.

What do you think?
Ilya

P.S.  Maybe SWAP_STACKS() and SWAP_STACKS_BACK() is a better
      solution.  With the implementation above they should coincide,
      of course.

P.P.S.  Example code for (x+y)(x-y):

   long other_av = OTHER_AVMA;
   GEN g1, g2;

   SWAP_STACKS();
   g1 = gadd(x, y);
   g2 = gsub(x, y);
   SWAP_STACKS_BACK();
   g1 = gmul(g1, g2);
   RESET_OTHER_AVMA(other_av);
   return g1;