From c6a8429258ffbb42120d8a6c75ceb852a98de7b8 Mon Sep 17 00:00:00 2001 From: rlar Date: Sat, 20 Sep 2014 14:09:11 +0200 Subject: [PATCH] src/maths/cmaths, implement `nint()' (.control language) --- src/frontend/parse.c | 1 + src/include/ngspice/fteext.h | 1 + src/maths/cmaths/cmath2.c | 29 +++++++++++++++++++++++++++++ src/maths/cmaths/cmath2.h | 1 + 4 files changed, 32 insertions(+) diff --git a/src/frontend/parse.c b/src/frontend/parse.c index 23ad4bd50..b2180ae4e 100644 --- a/src/frontend/parse.c +++ b/src/frontend/parse.c @@ -170,6 +170,7 @@ struct func ft_funcs[] = { { "exponential", cx_exponential }, { "sgauss", cx_sgauss }, { "pos", cx_pos }, + { "nint", cx_nint }, { "floor", cx_floor }, { "ceil", cx_ceil }, { "mean", cx_mean }, diff --git a/src/include/ngspice/fteext.h b/src/include/ngspice/fteext.h index 0c859c56f..f936b5ace 100644 --- a/src/include/ngspice/fteext.h +++ b/src/include/ngspice/fteext.h @@ -74,6 +74,7 @@ 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 *); +extern void *cx_nint(void *, short int , int , int *, short int *); extern void *cx_sortorder(void *, short int , int , int *, short int *); /* cmath2.c */ diff --git a/src/maths/cmaths/cmath2.c b/src/maths/cmaths/cmath2.c index 7672763bb..73a25fb70 100644 --- a/src/maths/cmaths/cmath2.c +++ b/src/maths/cmaths/cmath2.c @@ -838,3 +838,32 @@ cx_ceil(void *data, short int type, int length, int *newlength, short int *newty return ((void *) d); } } + +void * +cx_nint(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]) = nearbyint(realpart(cc[i])); + imagpart(c[i]) = nearbyint(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] = nearbyint(dd[i]); + return ((void *) d); + } +} diff --git a/src/maths/cmaths/cmath2.h b/src/maths/cmaths/cmath2.h index 0ecc2f21d..4b71e2b12 100644 --- a/src/maths/cmaths/cmath2.h +++ b/src/maths/cmaths/cmath2.h @@ -29,6 +29,7 @@ void * cx_d(void *data, short int type, int length, int *newlength, short int *n 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); +void * cx_nint(void *data, short int type, int length, int *newlength, short int *newtype); #endif