From 75767fcc3da6068864c85e35e054a409454dc890 Mon Sep 17 00:00:00 2001 From: h_vogt Date: Mon, 26 Dec 2011 11:34:21 +0000 Subject: [PATCH] floor(), ceil() added --- ChangeLog | 6 ++++ src/frontend/numparam/xpressn.c | 10 ++++-- src/frontend/parse.c | 2 ++ src/include/ngspice/fteext.h | 2 ++ src/include/ngspice/inpptree.h | 2 ++ src/maths/cmaths/cmath2.c | 58 +++++++++++++++++++++++++++++++++ src/maths/cmaths/cmath2.h | 4 ++- src/spicelib/parser/inp.h | 3 +- src/spicelib/parser/inpptree.c | 11 +++++++ src/spicelib/parser/ptfuncs.c | 13 ++++++++ 10 files changed, 107 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4264d8ae9..67874cc50 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2011-12-26 Holger Vogt + * xpressn.c + * ptfuncs.c, inpptree.c, inpptree.h, spicelib/parser/inp.h + * cmath2.c, cmath2.h, fteext.h, parse.c + functions floor and ceil added to numparam, ASRC and scripting language + 2011-12-25 Holger Vogt * src/frontend/inpcom.c : remove 'params:' from X or .SUBCKT lines of input deck (in fcn inp_fix_for_numparam()) diff --git a/src/frontend/numparam/xpressn.c b/src/frontend/numparam/xpressn.c index 80d4a4804..c4625e6d6 100644 --- a/src/frontend/numparam/xpressn.c +++ b/src/frontend/numparam/xpressn.c @@ -88,7 +88,7 @@ initkeys (void) " include for to downto is var"); scopy_up (&fmathS, "sqr sqrt sin cos exp ln arctan abs pow pwr max min int log sinh cosh" - " tanh ternary_fcn v agauss sgn gauss unif aunif limit"); + " tanh ternary_fcn v agauss sgn gauss unif aunif limit ceil floor"); } static double @@ -149,11 +149,17 @@ mathfunction (int f, double z, double x) case 17: y=sinh(x)/cosh(x); break; - case 21: + case 21: /* sgn */ if (x>0) y=1.; else if (x == 0) y=0.; else y = -1.; break; + case 26: + y=ceil(x); + break; + case 27: + y=floor(x); + break; default: y = x; break; diff --git a/src/frontend/parse.c b/src/frontend/parse.c index 03f15c1e0..48bc6561d 100644 --- a/src/frontend/parse.c +++ b/src/frontend/parse.c @@ -170,6 +170,8 @@ struct func ft_funcs[] = { { "exponential", cx_exponential } , { "sgauss", cx_sgauss } , { "pos", cx_pos } , + { "floor", cx_floor } , + { "ceil", cx_ceil } , { "mean", cx_mean } , { "avg", cx_avg } , /* A.Roldan 03/06/05 incremental average new function */ { "group_delay", (cx_function_t*) cx_group_delay } , /* A.Roldan 10/06/05 group delay new function */ diff --git a/src/include/ngspice/fteext.h b/src/include/ngspice/fteext.h index 1d84dce2d..a39a5ccf1 100644 --- a/src/include/ngspice/fteext.h +++ b/src/include/ngspice/fteext.h @@ -71,6 +71,8 @@ extern void *cx_cosh(void *, short int , int , int *, short int *); extern void *cx_tan(void *, short int , int , int *, short int *); extern void *cx_tanh(void *, short int , int , int *, short int *); extern void *cx_atan(void *, short int , int , int *, short int *); +extern void *cx_floor(void *, short int , int , int *, short int *); +extern void *cx_ceil(void *, short int , int , int *, short int *); /* cmath2.c */ diff --git a/src/include/ngspice/inpptree.h b/src/include/ngspice/inpptree.h index 812916569..f4a26880f 100644 --- a/src/include/ngspice/inpptree.h +++ b/src/include/ngspice/inpptree.h @@ -122,6 +122,8 @@ typedef struct INPparseNode { #define PTF_POW 30 #define PTF_MIN 31 #define PTF_MAX 32 +#define PTF_CEIL 33 +#define PTF_FLOOR 34 /* The following things are used by the parser -- these are the token types the * lexer returns. diff --git a/src/maths/cmaths/cmath2.c b/src/maths/cmaths/cmath2.c index 6e13a8c09..2b4c175b8 100644 --- a/src/maths/cmaths/cmath2.c +++ b/src/maths/cmaths/cmath2.c @@ -786,3 +786,61 @@ cx_d(void *data, short int type, int length, int *newlength, short int *newtype) return ((void *) c); } } + +void * +cx_floor(void *data, short int type, int length, int *newlength, short int *newtype) +{ + *newlength = length; + if (type == VF_COMPLEX) { + ngcomplex_t *c; + ngcomplex_t *cc = (ngcomplex_t *) data; + int i; + + c = alloc_c(length); + *newtype = VF_COMPLEX; + for (i = 0; i < length; i++) { + realpart(&c[i]) = floor(realpart(&cc[i])); + imagpart(&c[i]) = floor(imagpart(&cc[i])); + } + return ((void *) c); + } else { + double *d; + double *dd = (double *) data; + int i; + + d = alloc_d(length); + *newtype = VF_REAL; + for (i = 0; i < length; i++) + d[i] = floor(dd[i]); + return ((void *) d); + } +} + +void * +cx_ceil(void *data, short int type, int length, int *newlength, short int *newtype) +{ + *newlength = length; + if (type == VF_COMPLEX) { + ngcomplex_t *c; + ngcomplex_t *cc = (ngcomplex_t *) data; + int i; + + c = alloc_c(length); + *newtype = VF_COMPLEX; + for (i = 0; i < length; i++) { + realpart(&c[i]) = ceil(realpart(&cc[i])); + imagpart(&c[i]) = ceil(imagpart(&cc[i])); + } + return ((void *) c); + } else { + double *d; + double *dd = (double *) data; + int i; + + d = alloc_d(length); + *newtype = VF_REAL; + for (i = 0; i < length; i++) + d[i] = ceil(dd[i]); + return ((void *) d); + } +} diff --git a/src/maths/cmaths/cmath2.h b/src/maths/cmaths/cmath2.h index f34b69f3e..2c9e7efe1 100644 --- a/src/maths/cmaths/cmath2.h +++ b/src/maths/cmaths/cmath2.h @@ -26,7 +26,9 @@ void * cx_mod(void *data1, void *data2, short int datatype1, short int datatype2 void * cx_max(void *data, short int type, int length, int *newlength, short int *newtype); void * cx_min(void *data, short int type, int length, int *newlength, short int *newtype); void * cx_d(void *data, short int type, int length, int *newlength, short int *newtype); -void *cx_avg(void *data, short int type, int length, int *newlength, short int *newtype); +void * cx_avg(void *data, short int type, int length, int *newlength, short int *newtype); +void * cx_floor(void *data, short int type, int length, int *newlength, short int *newtype); +void * cx_ceil(void *data, short int type, int length, int *newlength, short int *newtype); #endif diff --git a/src/spicelib/parser/inp.h b/src/spicelib/parser/inp.h index e171062f0..9c679d9a7 100644 --- a/src/spicelib/parser/inp.h +++ b/src/spicelib/parser/inp.h @@ -73,6 +73,7 @@ double PTgt0(double arg); double PTlt0(double arg); double PTge0(double arg); double PTle0(double arg); - +double PTceil(double arg); +double PTfloor(double arg); #endif diff --git a/src/spicelib/parser/inpptree.c b/src/spicelib/parser/inpptree.c index 0f8f33793..24b001223 100644 --- a/src/spicelib/parser/inpptree.c +++ b/src/spicelib/parser/inpptree.c @@ -83,6 +83,8 @@ static struct func { { "tanh", PTF_TANH, (void(*)(void)) PTtanh } , { "u", PTF_USTEP, (void(*)(void)) PTustep } , { "uramp", PTF_URAMP, (void(*)(void)) PTuramp } , + { "ceil", PTF_CEIL, (void(*)(void)) PTceil } , + { "floor", PTF_FLOOR, (void(*)(void)) PTfloor } , { "-", PTF_UMINUS, (void(*)(void)) PTuminus }, /* MW. cif function added */ { "u2", PTF_USTEP2, (void(*)(void)) PTustep2}, @@ -402,6 +404,15 @@ static INPparseNode *PTdifferentiate(INPparseNode * p, int varnum) arg1 = mkf(PTF_USTEP, p->left); break; + case PTF_FLOOR: /* floor(u) */ + arg1 = mkf(PTF_FLOOR, p->left); + break; + + case PTF_CEIL: /* ceil(u) */ + arg1 = mkf(PTF_CEIL, p->left); + break; + + /* MW. PTF_CIF for new cif function */ case PTF_USTEP2: /* ustep2=uramp(x)-uramp(x-1) ustep2'=ustep(x)-ustep(x-1) */ arg1 = mkb(PT_MINUS, diff --git a/src/spicelib/parser/ptfuncs.c b/src/spicelib/parser/ptfuncs.c index e92d24931..b07b38dc1 100644 --- a/src/spicelib/parser/ptfuncs.c +++ b/src/spicelib/parser/ptfuncs.c @@ -340,3 +340,16 @@ PTpwl_derivative(double arg, void *data) return y; } + +double +PTceil(double arg1) +{ + return (ceil(arg1)); +} + +double +PTfloor(double arg1) +{ + return (floor(arg1)); +} +