Back to index

wims  3.65+svn20090927
Public Member Functions | Package Functions | Static Package Attributes | Private Member Functions | Private Attributes | Static Private Attributes
org.javia.arity.CompiledFunction Class Reference
Inheritance diagram for org.javia.arity.CompiledFunction:
Inheritance graph
[legend]
Collaboration diagram for org.javia.arity.CompiledFunction:
Collaboration graph
[legend]

List of all members.

Public Member Functions

int arity ()
 Gives the arity of this function.
String toString ()
double eval () throws ArityException
 Evaluates an arity-0 function (a function with no arguments).
double eval (double x) throws ArityException
double eval (double x, double y) throws ArityException
double eval (double args[]) throws ArityException
 Evaluates the function given the argument values.
int arity ()
 Gives the arity of this function.
String toString ()
double eval () throws ArityException
 Evaluates an arity-0 function (a function with no arguments).
double eval (double x) throws ArityException
double eval (double x, double y) throws ArityException
double eval (double args[]) throws ArityException
 Evaluates the function given the argument values.

Package Functions

 CompiledFunction (int arity, byte[] code, double[] consts, Function funcs[])
 CompiledFunction ()
void init (int arity, byte[] code, double[] consts, Function funcs[])
void exec (double s[], int p)
int execWithoutCheck (double s[], int p)
 CompiledFunction (int arity, byte[] code, double[] consts, Function funcs[])
 CompiledFunction ()
void init (int arity, byte[] code, double[] consts, Function funcs[])
void exec (double s[], int p)
int execWithoutCheck (double s[], int p)

Static Package Attributes

static final int MAX_STACK_SIZE = 128
static final double NO_ARGS [] = new double[0]

Private Member Functions

void checkArity (int nArgs) throws ArityException
double[] getStack ()
void checkArity (int nArgs) throws ArityException
double[] getStack ()

Private Attributes

double consts []
Function funcs []
byte code []
int arity

Static Private Attributes

static Random random = new Random()
static final ThreadLocal stacks = new ThreadLocal()

Detailed Description

Definition at line 21 of file CompiledFunction.java.


Constructor & Destructor Documentation

org.javia.arity.CompiledFunction.CompiledFunction ( int  arity,
byte[]  code,
double[]  consts,
Function  funcs[] 
) [inline, package]

Definition at line 33 of file CompiledFunction.java.

Here is the call graph for this function:

Definition at line 40 of file CompiledFunction.java.

                       {
        init(0, null, null, null);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

org.javia.arity.CompiledFunction.CompiledFunction ( int  arity,
byte[]  code,
double[]  consts,
Function  funcs[] 
) [inline, package]

Definition at line 33 of file CompiledFunction.java.

Here is the call graph for this function:

Definition at line 40 of file CompiledFunction.java.

                       {
        init(0, null, null, null);
    }

Here is the call graph for this function:


Member Function Documentation

Gives the arity of this function.

Returns:
the arity (the number of arguments). Arity >= 0.

Reimplemented from org.javia.arity.Function.

Definition at line 51 of file CompiledFunction.java.

                       {
        return arity;
    }

Here is the caller graph for this function:

Gives the arity of this function.

Returns:
the arity (the number of arguments). Arity >= 0.

Reimplemented from org.javia.arity.Function.

Definition at line 51 of file CompiledFunction.java.

                       {
        return arity;
    }

Here is the call graph for this function:

void org.javia.arity.CompiledFunction.checkArity ( int  nArgs) throws ArityException [inline, private]

Definition at line 81 of file CompiledFunction.java.

                                                             {
        if (arity() != nArgs) {
            throw new ArityException("Expected " + arity() + " arguments, got " + nArgs);
        }        
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void org.javia.arity.CompiledFunction.checkArity ( int  nArgs) throws ArityException [inline, private]

Definition at line 81 of file CompiledFunction.java.

                                                             {
        if (arity() != nArgs) {
            throw new ArityException("Expected " + arity() + " arguments, got " + nArgs);
        }        
    }

Here is the call graph for this function:

Evaluates an arity-0 function (a function with no arguments).

Returns:
the value of the function

Reimplemented from org.javia.arity.Function.

Definition at line 96 of file CompiledFunction.java.

                                               {
        return eval(NO_ARGS);
    }

Here is the call graph for this function:

Evaluates an arity-0 function (a function with no arguments).

Returns:
the value of the function

Reimplemented from org.javia.arity.Function.

Definition at line 96 of file CompiledFunction.java.

                                               {
        return eval(NO_ARGS);
    }

Here is the caller graph for this function:

double org.javia.arity.CompiledFunction.eval ( double  x) throws ArityException [inline]

Reimplemented from org.javia.arity.Function.

Definition at line 100 of file CompiledFunction.java.

                                                       {
        checkArity(1);
        double stack[] = getStack();
        stack[0] = x;
        exec(stack, 0);
        return stack[0];
    }

Here is the call graph for this function:

double org.javia.arity.CompiledFunction.eval ( double  x) throws ArityException [inline]

Reimplemented from org.javia.arity.Function.

Definition at line 100 of file CompiledFunction.java.

                                                       {
        checkArity(1);
        double stack[] = getStack();
        stack[0] = x;
        exec(stack, 0);
        return stack[0];
    }

Here is the call graph for this function:

double org.javia.arity.CompiledFunction.eval ( double  x,
double  y 
) throws ArityException [inline]

Reimplemented from org.javia.arity.Function.

Definition at line 108 of file CompiledFunction.java.

                                                                 {
        checkArity(2);
        double stack[] = getStack();
        stack[0] = x;
        stack[1] = y;
        exec(stack, 1);
        return stack[0];
    }

Here is the call graph for this function:

double org.javia.arity.CompiledFunction.eval ( double  x,
double  y 
) throws ArityException [inline]

Reimplemented from org.javia.arity.Function.

Definition at line 108 of file CompiledFunction.java.

                                                                 {
        checkArity(2);
        double stack[] = getStack();
        stack[0] = x;
        stack[1] = y;
        exec(stack, 1);
        return stack[0];
    }

Here is the call graph for this function:

double org.javia.arity.CompiledFunction.eval ( double  args[]) throws ArityException [inline]

Evaluates the function given the argument values.

Parameters:
argsarray containing the arguments.
Returns:
the value of the function
Exceptions:
ArityExceptionif args.length != arity.

Reimplemented from org.javia.arity.Function.

Definition at line 117 of file CompiledFunction.java.

                                                            {
        checkArity(args.length);
        double stack[] = getStack();
        if (args.length > 0) {
            System.arraycopy(args, 0, stack, 0, args.length);
        }
        exec(stack, args.length - 1);
        return stack[0];
    }

Here is the call graph for this function:

double org.javia.arity.CompiledFunction.eval ( double  args[]) throws ArityException [inline]

Evaluates the function given the argument values.

Parameters:
argsarray containing the arguments.
Returns:
the value of the function
Exceptions:
ArityExceptionif args.length != arity.

Reimplemented from org.javia.arity.Function.

Definition at line 117 of file CompiledFunction.java.

                                                            {
        checkArity(args.length);
        double stack[] = getStack();
        if (args.length > 0) {
            System.arraycopy(args, 0, stack, 0, args.length);
        }
        exec(stack, args.length - 1);
        return stack[0];
    }

Here is the call graph for this function:

void org.javia.arity.CompiledFunction.exec ( double  s[],
int  p 
) [inline, package]

Definition at line 127 of file CompiledFunction.java.

                                 {
        int finalP = execWithoutCheck(s, p);
        int expected = p - arity + 1;
        if (finalP != expected) {
            throw new Error("stack pointer after exec: expected " + 
                            expected + ", got " + finalP);
        }
    }

Here is the call graph for this function:

void org.javia.arity.CompiledFunction.exec ( double  s[],
int  p 
) [inline, package]

Definition at line 127 of file CompiledFunction.java.

                                 {
        int finalP = execWithoutCheck(s, p);
        int expected = p - arity + 1;
        if (finalP != expected) {
            throw new Error("stack pointer after exec: expected " + 
                            expected + ", got " + finalP);
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

int org.javia.arity.CompiledFunction.execWithoutCheck ( double  s[],
int  p 
) [inline, package]

Definition at line 136 of file CompiledFunction.java.

                                            {
        byte[] code = this.code;
        int initialSP = p;
        int constp = 0;
        int funp   = 0;
        final double angleFactor = 1; // 1/Calc.cfg.trigFactor;
        
        // arguments, read from stack on exec entry
        // we don't use an array in order to avoid the dynamic allocation (new)
        // @see Compiler.MAX_ARITY
        double a0, a1, a2, a3, a4;
        a0 = a1 = a2 = a3 = a4 = Double.NaN;
        switch (arity) {
        case 5: a4 = s[p--];
        case 4: a3 = s[p--];
        case 3: a2 = s[p--];
        case 2: a1 = s[p--];
        case 1: a0 = s[p--];
        }

        //int expectedExitSP = p+1;
        int codeLen = code.length;
        for (int pc = 0; pc < codeLen; ++pc) {
            switch (code[pc]) {
            case VM.CONST: s[++p] = consts[constp++]; break;
            case VM.CALL: { 
                Function f = funcs[funp++];
                if (f instanceof CompiledFunction) { 
                    p = ((CompiledFunction) f).execWithoutCheck(s, p);
                } else {
                    int arity = f.arity();
                    p -= arity;
                    try {
                        double result;
                        switch (arity) {
                        case 0:
                            result = f.eval();
                            break;
                        case 1:
                            result = f.eval(s[p+1]);
                            break;
                        case 2:
                            result = f.eval(s[p+1], s[p+2]);
                            break;
                        default:
                            double args[] = new double[arity];
                            System.arraycopy(s, p+1, args, 0, arity);
                            result = f.eval(args);
                        }
                        s[++p] = result;                                      
                    } catch (ArityException e) {
                        throw new Error(""+e); //never
                    }
                }
                break;
            }
                
            case VM.RND: s[++p] = random.nextDouble(); break;
                    
            case VM.ADD: s[--p] += s[p+1]; break;
            case VM.SUB: s[--p] -= s[p+1]; break;
            case VM.MUL: s[--p] *= s[p+1]; break;
            case VM.DIV: s[--p] /= s[p+1]; break;
            case VM.MOD: s[--p] %= s[p+1]; break;
            case VM.POWER: s[--p] = MoreMath.pow(s[p], s[p+1]); break;
                
            case VM.UMIN: s[p] = -s[p]; break;
            case VM.FACT: s[p] = MoreMath.factorial(s[p]); break;                   
                
            case VM.SIN:  s[p] = Math.sin(s[p] * angleFactor); break;
            case VM.COS:  s[p] = Math.cos(s[p] * angleFactor); break;
            case VM.TAN:  s[p] = Math.tan(s[p] * angleFactor); break;
            case VM.ASIN: s[p] = MoreMath.asin(s[p]) / angleFactor; break;
            case VM.ACOS: s[p] = MoreMath.acos(s[p]) / angleFactor; break;
            case VM.ATAN: s[p] = MoreMath.atan(s[p]) / angleFactor; break;
                    
            case VM.EXP: s[p] = MoreMath.exp(s[p]); break;
            case VM.LOG: s[p] = MoreMath.log(s[p]); break;
                
            case VM.SQRT: s[p] = Math.sqrt(s[p]); break;
            case VM.CBRT: s[p] = MoreMath.cbrt(s[p]); break;
                
            case VM.SINH: s[p] = MoreMath.sinh(s[p]); break;
            case VM.COSH: s[p] = MoreMath.cosh(s[p]); break;
            case VM.TANH: s[p] = MoreMath.tanh(s[p]); break;
            case VM.ASINH: s[p] = MoreMath.asinh(s[p]); break;
            case VM.ACOSH: s[p] = MoreMath.acosh(s[p]); break;
            case VM.ATANH: s[p] = MoreMath.atanh(s[p]); break;
                    
            case VM.ABS:   s[p] = Math.abs(s[p]); break;
            case VM.FLOOR: s[p] = Math.floor(s[p]); break;
            case VM.CEIL:  s[p] = Math.ceil(s[p]); break;
            case VM.SIGN:  s[p] = s[p] > 0 ? 1 : s[p] < 0 ? -1 : 0; break;
                
            case VM.MIN:  s[--p] = Math.min(s[p], s[p+1]); break;
            case VM.MAX:  s[--p] = Math.min(s[p], s[p+1]); break;
            case VM.GCD:  s[--p] = MoreMath.gcd(s[p], s[p+1]); break;
            case VM.COMB: s[--p] = MoreMath.comb(s[p], s[p+1]); break;
            case VM.PERM: s[--p] = MoreMath.perm(s[p], s[p+1]); break;
                    
            case VM.LOAD0: s[++p] = a0; break;
            case VM.LOAD1: s[++p] = a1; break;
            case VM.LOAD2: s[++p] = a2; break;
            case VM.LOAD3: s[++p] = a3; break;
            case VM.LOAD4: s[++p] = a4; break;
            default:
                throw new Error("Unknown opcode " + code[pc]);
            }
        }
        return p;
        /*
        if (p != expectedExitSP) {
            throw new Error("stack pointer after exec: expected " + expectedExitSP +
                            " , got " + p);
        }
        */
    }

Here is the call graph for this function:

Here is the caller graph for this function:

int org.javia.arity.CompiledFunction.execWithoutCheck ( double  s[],
int  p 
) [inline, package]

Definition at line 136 of file CompiledFunction.java.

                                            {
        byte[] code = this.code;
        int initialSP = p;
        int constp = 0;
        int funp   = 0;
        final double angleFactor = 1; // 1/Calc.cfg.trigFactor;
        
        // arguments, read from stack on exec entry
        // we don't use an array in order to avoid the dynamic allocation (new)
        // @see Compiler.MAX_ARITY
        double a0, a1, a2, a3, a4;
        a0 = a1 = a2 = a3 = a4 = Double.NaN;
        switch (arity) {
        case 5: a4 = s[p--];
        case 4: a3 = s[p--];
        case 3: a2 = s[p--];
        case 2: a1 = s[p--];
        case 1: a0 = s[p--];
        }

        //int expectedExitSP = p+1;
        int codeLen = code.length;
        for (int pc = 0; pc < codeLen; ++pc) {
            switch (code[pc]) {
            case VM.CONST: s[++p] = consts[constp++]; break;
            case VM.CALL: { 
                Function f = funcs[funp++];
                if (f instanceof CompiledFunction) { 
                    p = ((CompiledFunction) f).execWithoutCheck(s, p);
                } else {
                    int arity = f.arity();
                    p -= arity;
                    try {
                        double result;
                        switch (arity) {
                        case 0:
                            result = f.eval();
                            break;
                        case 1:
                            result = f.eval(s[p+1]);
                            break;
                        case 2:
                            result = f.eval(s[p+1], s[p+2]);
                            break;
                        default:
                            double args[] = new double[arity];
                            System.arraycopy(s, p+1, args, 0, arity);
                            result = f.eval(args);
                        }
                        s[++p] = result;                                      
                    } catch (ArityException e) {
                        throw new Error(""+e); //never
                    }
                }
                break;
            }
                
            case VM.RND: s[++p] = random.nextDouble(); break;
                    
            case VM.ADD: s[--p] += s[p+1]; break;
            case VM.SUB: s[--p] -= s[p+1]; break;
            case VM.MUL: s[--p] *= s[p+1]; break;
            case VM.DIV: s[--p] /= s[p+1]; break;
            case VM.MOD: s[--p] %= s[p+1]; break;
            case VM.POWER: s[--p] = MoreMath.pow(s[p], s[p+1]); break;
                
            case VM.UMIN: s[p] = -s[p]; break;
            case VM.FACT: s[p] = MoreMath.factorial(s[p]); break;                   
                
            case VM.SIN:  s[p] = Math.sin(s[p] * angleFactor); break;
            case VM.COS:  s[p] = Math.cos(s[p] * angleFactor); break;
            case VM.TAN:  s[p] = Math.tan(s[p] * angleFactor); break;
            case VM.ASIN: s[p] = MoreMath.asin(s[p]) / angleFactor; break;
            case VM.ACOS: s[p] = MoreMath.acos(s[p]) / angleFactor; break;
            case VM.ATAN: s[p] = MoreMath.atan(s[p]) / angleFactor; break;
                    
            case VM.EXP: s[p] = MoreMath.exp(s[p]); break;
            case VM.LOG: s[p] = MoreMath.log(s[p]); break;
                
            case VM.SQRT: s[p] = Math.sqrt(s[p]); break;
            case VM.CBRT: s[p] = MoreMath.cbrt(s[p]); break;
                
            case VM.SINH: s[p] = MoreMath.sinh(s[p]); break;
            case VM.COSH: s[p] = MoreMath.cosh(s[p]); break;
            case VM.TANH: s[p] = MoreMath.tanh(s[p]); break;
            case VM.ASINH: s[p] = MoreMath.asinh(s[p]); break;
            case VM.ACOSH: s[p] = MoreMath.acosh(s[p]); break;
            case VM.ATANH: s[p] = MoreMath.atanh(s[p]); break;
                    
            case VM.ABS:   s[p] = Math.abs(s[p]); break;
            case VM.FLOOR: s[p] = Math.floor(s[p]); break;
            case VM.CEIL:  s[p] = Math.ceil(s[p]); break;
            case VM.SIGN:  s[p] = s[p] > 0 ? 1 : s[p] < 0 ? -1 : 0; break;
                
            case VM.MIN:  s[--p] = Math.min(s[p], s[p+1]); break;
            case VM.MAX:  s[--p] = Math.min(s[p], s[p+1]); break;
            case VM.GCD:  s[--p] = MoreMath.gcd(s[p], s[p+1]); break;
            case VM.COMB: s[--p] = MoreMath.comb(s[p], s[p+1]); break;
            case VM.PERM: s[--p] = MoreMath.perm(s[p], s[p+1]); break;
                    
            case VM.LOAD0: s[++p] = a0; break;
            case VM.LOAD1: s[++p] = a1; break;
            case VM.LOAD2: s[++p] = a2; break;
            case VM.LOAD3: s[++p] = a3; break;
            case VM.LOAD4: s[++p] = a4; break;
            default:
                throw new Error("Unknown opcode " + code[pc]);
            }
        }
        return p;
        /*
        if (p != expectedExitSP) {
            throw new Error("stack pointer after exec: expected " + expectedExitSP +
                            " , got " + p);
        }
        */
    }

Here is the call graph for this function:

double [] org.javia.arity.CompiledFunction.getStack ( ) [inline, private]

Definition at line 87 of file CompiledFunction.java.

                                {
        double stack[];
        if ((stack = (double[]) stacks.get()) == null) {
            stacks.set(stack = new double[MAX_STACK_SIZE]);
        }
        return stack;
    }
double [] org.javia.arity.CompiledFunction.getStack ( ) [inline, private]

Definition at line 87 of file CompiledFunction.java.

                                {
        double stack[];
        if ((stack = (double[]) stacks.get()) == null) {
            stacks.set(stack = new double[MAX_STACK_SIZE]);
        }
        return stack;
    }

Here is the caller graph for this function:

void org.javia.arity.CompiledFunction.init ( int  arity,
byte[]  code,
double[]  consts,
Function  funcs[] 
) [inline, package]

Definition at line 44 of file CompiledFunction.java.

                                                                         {
        this.arity  = arity;
        this.code   = code;
        this.consts = consts;
        this.funcs  = funcs;
    }

Here is the call graph for this function:

void org.javia.arity.CompiledFunction.init ( int  arity,
byte[]  code,
double[]  consts,
Function  funcs[] 
) [inline, package]

Definition at line 44 of file CompiledFunction.java.

                                                                         {
        this.arity  = arity;
        this.code   = code;
        this.consts = consts;
        this.funcs  = funcs;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 55 of file CompiledFunction.java.

                             {
        StringBuffer buf = new StringBuffer();
        int cpos = 0, fpos = 0;
        if (arity != 0) {
            buf.append("arity ").append(arity).append("; ");
        }
        for (int i = 0; i < code.length; ++i) {
            byte op = code[i];
            buf.append(VM.opcodeName[op]);
            if (op == VM.CONST) {
                buf.append(' ').append(consts[cpos++]);
            } else if (op == VM.CALL) {
                ++fpos;
                //buf.append(" {").append(funcs[fpos++].toString()).append('}');
            }
            buf.append("; ");
        }
        if (cpos != consts.length) {
            buf.append("\nuses only ").append(cpos).append(" consts out of ").append(consts.length);
        }
        if (fpos != funcs.length) {
            buf.append("\nuses only ").append(fpos).append(" funcs out of ").append(funcs.length);
        }
        return buf.toString();
    }

Here is the call graph for this function:

Definition at line 55 of file CompiledFunction.java.

                             {
        StringBuffer buf = new StringBuffer();
        int cpos = 0, fpos = 0;
        if (arity != 0) {
            buf.append("arity ").append(arity).append("; ");
        }
        for (int i = 0; i < code.length; ++i) {
            byte op = code[i];
            buf.append(VM.opcodeName[op]);
            if (op == VM.CONST) {
                buf.append(' ').append(consts[cpos++]);
            } else if (op == VM.CALL) {
                ++fpos;
                //buf.append(" {").append(funcs[fpos++].toString()).append('}');
            }
            buf.append("; ");
        }
        if (cpos != consts.length) {
            buf.append("\nuses only ").append(cpos).append(" consts out of ").append(consts.length);
        }
        if (fpos != funcs.length) {
            buf.append("\nuses only ").append(fpos).append(" funcs out of ").append(funcs.length);
        }
        return buf.toString();
    }

Here is the call graph for this function:


Member Data Documentation

Definition at line 29 of file CompiledFunction.java.

Definition at line 28 of file CompiledFunction.java.

Definition at line 26 of file CompiledFunction.java.

Definition at line 27 of file CompiledFunction.java.

static final int org.javia.arity.CompiledFunction.MAX_STACK_SIZE = 128 [static, package]

Definition at line 23 of file CompiledFunction.java.

static final double org.javia.arity.CompiledFunction.NO_ARGS = new double[0] [static, package]

Definition at line 95 of file CompiledFunction.java.

static Random org.javia.arity.CompiledFunction.random = new Random() [static, private]

Definition at line 24 of file CompiledFunction.java.

static final ThreadLocal org.javia.arity.CompiledFunction.stacks = new ThreadLocal() [static, private]

Definition at line 31 of file CompiledFunction.java.


The documentation for this class was generated from the following files: