use of gov.sandia.n2a.language.Operator in project n2a by frothga.
the class Exp method determineExponent.
public void determineExponent(ExponentContext context) {
Operator op = operands[0];
op.determineExponent(context);
// If op is unsigned, we can solve for center as follows:
// o = power of op.center
// m = o^2 = magnitude of op.center
// p = power of our center (the effective output of this function)
// = floor(log2(exp(m))) = floor(ln(exp(m)) / ln(2)) = floor(m / ln(2));
// Currently, op is always signed. The best we can do is assume output is centered around 1.
int centerNew = MSB / 2;
int exponentNew = 0;
if (operands.length >= 2)
exponentNew = getExponentHint(operands[1].getString(), exponentNew);
exponentNew += MSB - centerNew;
updateExponent(context, exponentNew, centerNew);
}
use of gov.sandia.n2a.language.Operator in project n2a by frothga.
the class Exp method determineExponentNext.
public void determineExponentNext() {
Operator op = operands[0];
// exp(n) rapidly explodes, so no benefit in allowing arbitrary magnitude. Instead, use those bits for precision.
// Our goal with fixed-point is to roughly match the performance of single-precision float.
// The largest number representable in float is 2^127, so allocating 8 bits above the decimal
// should be more than enough.
op.exponentNext = 7;
op.determineExponentNext();
}
use of gov.sandia.n2a.language.Operator in project n2a by frothga.
the class XyceBackend method generateNetlist.
public void generateNetlist(MNode job, Simulator simulator, FileWriter writer) throws Exception {
Population toplevel = (Population) simulator.wrapper.valuesObject[0];
XyceRenderer renderer = new XyceRenderer(simulator);
// Header
writer.append(toplevel.equations.name + "\n");
writer.append("\n");
writer.append("* seed: " + job.get("$metadata", "seed") + "\n");
writer.append(".tran 0 " + job.get("$metadata", "duration") + "\n");
// Equations
for (Instance i : simulator) {
if (i == simulator.wrapper)
continue;
writer.append("\n");
writer.append("* " + i + "\n");
renderer.pi = i;
renderer.exceptions = null;
XyceBackendData bed = (XyceBackendData) i.equations.backendData;
if (bed.deviceSymbol != null) {
writer.append(bed.deviceSymbol.getDefinition(renderer));
}
InstanceTemporaries temp = new InstanceTemporaries(i, simulator, false, bed.internal);
for (final Variable v : i.equations.variables) {
// Compute variable v
// TODO: how to switch between multiple conditions that can be true during normal operation? IE: how to make Xyce code conditional?
// Perhaps gate each condition (through a transistor?) and sum them at a single node.
// e can be null
EquationEntry e = v.select(temp);
Symbol def = bed.equationSymbols.get(e);
if (def == null)
continue;
writer.append(def.getDefinition(renderer));
// Trace
class TraceFinder extends Visitor {
List<Operator> traces = new ArrayList<Operator>();
public boolean visit(Operator op) {
if (op instanceof Output) {
traces.add(((Output) op).operands[0]);
return false;
}
return true;
}
}
TraceFinder traceFinder = new TraceFinder();
e.expression.visit(traceFinder);
for (Operator trace : traceFinder.traces) {
// We don't know if contents is .func, expression or a node, so always wrap in braces.
writer.append(".print tran {");
if (trace instanceof AccessVariable) {
AccessVariable av = (AccessVariable) trace;
writer.append(renderer.change(av.reference));
} else // trace is an expression
{
if (// this trace wraps the entire equation
e.expression instanceof Output && ((Output) e.expression).operands[0] == trace) {
// simply print the LHS variable, similar to the AccessVariable case above
writer.append(renderer.change(v.reference));
} else {
// arbitrary expression
writer.append(renderer.change(trace));
}
}
// one .print line per variable
writer.append("}\n");
}
}
}
// Trailer
writer.append(".end\n");
}
use of gov.sandia.n2a.language.Operator in project n2a by frothga.
the class Min method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
// Check if Min appears as an operand. If so, merge its operands into ours
ArrayList<Operator> newOperands = new ArrayList<Operator>(operands.length);
boolean changed = false;
for (int i = 0; i < operands.length; i++) {
Operator o = operands[i];
if (o instanceof Min) {
Min m = (Min) o;
newOperands.addAll(Arrays.asList(m.operands));
changed = true;
} else {
newOperands.add(o);
}
}
if (changed) {
from.changed = true;
Min newMin = new Min();
newMin.operands = newOperands.toArray(new Operator[0]);
return result;
}
return this;
}
use of gov.sandia.n2a.language.Operator in project n2a by frothga.
the class OR method simplify.
public Operator simplify(Variable from) {
Operator result = super.simplify(from);
if (result != this)
return result;
if (operand0 instanceof Constant) {
Type c0 = ((Constant) operand0).value;
if (c0 instanceof Scalar) {
from.changed = true;
double value = ((Scalar) c0).value;
if (value == 0)
return operand1;
else
return new Constant(new Scalar(1));
}
} else if (operand1 instanceof Constant) {
Type c1 = ((Constant) operand1).value;
if (c1 instanceof Scalar) {
from.changed = true;
double value = ((Scalar) c1).value;
if (value == 0)
return operand0;
else
return new Constant(new Scalar(1));
}
}
return this;
}
Aggregations