diff --git a/src/expr.cpp b/src/expr.cpp index 8d6600d..add6c8b 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -219,6 +219,35 @@ Expr *Expr::From(hParam p) { } Expr *Expr::From(double v) { + // Statically allocate common constants. + // Note: this is only valid because AllocExpr() uses AllocTemporary(), + // and Expr* is never explicitly freed. + + if(v == 0.0) { + static Expr zero(0.0); + return &zero; + } + + if(v == 1.0) { + static Expr one(1.0); + return &one; + } + + if(v == -1.0) { + static Expr mone(-1.0); + return &mone; + } + + if(v == 0.5) { + static Expr half(0.5); + return ½ + } + + if(v == -0.5) { + static Expr mhalf(-0.5); + return &mhalf; + } + Expr *r = AllocExpr(); r->op = CONSTANT; r->x.v = v; diff --git a/src/expr.h b/src/expr.h index 30f7b26..d53b0b2 100644 --- a/src/expr.h +++ b/src/expr.h @@ -63,6 +63,9 @@ public: char c; } x; + Expr() { } + Expr(double v) : op(CONSTANT) { x.v = v; } + static inline Expr *AllocExpr(void) { return (Expr *)AllocTemporary(sizeof(Expr)); }