--- ray/src/common/calfunc.c 1992/11/22 12:11:48 2.6 +++ ray/src/common/calfunc.c 2019/06/10 16:52:27 2.22 @@ -1,32 +1,28 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: calfunc.c,v 2.22 2019/06/10 16:52:27 greg Exp $"; #endif - /* * calfunc.c - routines for calcomp using functions. * - * The define BIGLIB pulls in a large number of the - * available math routines. - * - * If VARIABLE is not defined, only library functions + * If VARIABLE is not set, only library functions * can be accessed. * - * 4/2/86 + * 2/19/03 Eliminated conditional compiles in favor of esupport extern. */ -#include +#include "copyright.h" +#include +#include #include - #include +#include "rterror.h" #include "calcomp.h" /* bits in argument flag (better be right!) */ #define AFLAGSIZ (8*sizeof(unsigned long)) -#define ALISTSIZ 6 /* maximum saved argument list */ +#define ALISTSIZ 8 /* maximum saved argument list */ typedef struct activation { char *name; /* function name */ @@ -38,20 +34,21 @@ typedef struct activation { static ACTIVATION *curact = NULL; -static double libfunc(); +static double libfunc(char *fname, VARDEF *vp); +#ifndef MAXLIB #define MAXLIB 64 /* maximum number of library functions */ - -static double l_if(), l_select(), l_rand(); -static double l_floor(), l_ceil(); -#ifdef BIGLIB -static double l_sqrt(); -static double l_sin(), l_cos(), l_tan(); -static double l_asin(), l_acos(), l_atan(), l_atan2(); -static double l_exp(), l_log(), l_log10(); #endif -#ifdef BIGLIB +static double l_if(char *), l_select(char *); +static double l_min(char *), l_max(char *); +static double l_rand(char *); +static double l_floor(char *), l_ceil(char *); +static double l_sqrt(char *); +static double l_sin(char *), l_cos(char *), l_tan(char *); +static double l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *); +static double l_exp(char *), l_log(char *), l_log10(char *); + /* functions must be listed alphabetically */ static LIBR library[MAXLIB] = { { "acos", 1, ':', l_acos }, @@ -65,6 +62,8 @@ static LIBR library[MAXLIB] = { { "if", 3, ':', l_if }, { "log", 1, ':', l_log }, { "log10", 1, ':', l_log10 }, + { "max", 1, ':', l_max }, + { "min", 1, ':', l_min }, { "rand", 1, ':', l_rand }, { "select", 1, ':', l_select }, { "sin", 1, ':', l_sin }, @@ -72,69 +71,50 @@ static LIBR library[MAXLIB] = { { "tan", 1, ':', l_tan }, }; -static int libsize = 16; +static int libsize = 18; -#else - /* functions must be listed alphabetically */ -static LIBR library[MAXLIB] = { - { "ceil", 1, ':', l_ceil }, - { "floor", 1, ':', l_floor }, - { "if", 3, ':', l_if }, - { "rand", 1, ':', l_rand }, - { "select", 1, ':', l_select }, -}; - -static int libsize = 5; - -#endif - -extern char *savestr(), *emalloc(); - -extern VARDEF *argf(); - -#ifdef VARIABLE #define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan)) -#else -#define resolve(ep) ((ep)->v.ln) -#define varlookup(name) NULL -#endif int -fundefined(fname) /* return # of arguments for function */ -char *fname; +fundefined( /* return # of req'd arguments for function */ + char *fname +) { LIBR *lp; - register VARDEF *vp; + VARDEF *vp; - if ((vp = varlookup(fname)) == NULL || vp->def == NULL - || vp->def->v.kid->type != FUNC) - if ((lp = liblookup(fname)) == NULL) - return(0); - else - return(lp->nargs); - else + if ((vp = varlookup(fname)) != NULL && vp->def != NULL + && vp->def->v.kid->type == FUNC) return(nekids(vp->def->v.kid) - 1); + lp = vp != NULL ? vp->lib : liblookup(fname); + if (lp == NULL) + return(0); + return(lp->nargs); } double -funvalue(fname, n, a) /* return a function value to the user */ -char *fname; -int n; -double *a; +funvalue( /* return a function value to the user */ + char *fname, + int n, + double *a +) { ACTIVATION act; - register VARDEF *vp; + VARDEF *vp; double rval; /* push environment */ act.name = fname; act.prev = curact; act.ap = a; - if (n >= AFLAGSIZ) - act.an = ~0; - else + if (n < AFLAGSIZ) act.an = (1L< AFLAGSIZ) + wputs("Excess arguments in funvalue()\n"); + } act.fun = NULL; curact = &act; @@ -149,36 +129,41 @@ double *a; } -funset(fname, nargs, assign, fptr) /* set a library function */ -char *fname; -int nargs; -int assign; -double (*fptr)(); +void +funset( /* set a library function */ + char *fname, + int nargs, + int assign, + double (*fptr)(char *) +) { int oldlibsize = libsize; - register LIBR *lp; - + char *cp; + LIBR *lp; + /* check for context */ + for (cp = fname; *cp; cp++) + ; + if (cp == fname) + return; + while (cp[-1] == CNTXMARK) { + *--cp = '\0'; + if (cp == fname) return; + } if ((lp = liblookup(fname)) == NULL) { /* insert */ if (libsize >= MAXLIB) { eputs("Too many library functons!\n"); quit(1); } for (lp = &library[libsize]; lp > library; lp--) - if (strcmp(lp[-1].fname, fname) > 0) { - lp[0].fname = lp[-1].fname; - lp[0].nargs = lp[-1].nargs; - lp[0].atyp = lp[-1].atyp; - lp[0].f = lp[-1].f; - } else + if (strcmp(lp[-1].fname, fname) > 0) + lp[0] = lp[-1]; + else break; libsize++; } if (fptr == NULL) { /* delete */ while (lp < &library[libsize-1]) { - lp[0].fname = lp[1].fname; - lp[0].nargs = lp[1].nargs; - lp[0].atyp = lp[1].atyp; - lp[0].f = lp[1].f; + lp[0] = lp[1]; lp++; } libsize--; @@ -194,9 +179,9 @@ double (*fptr)(); int -nargum() /* return number of available arguments */ +nargum(void) /* return number of available arguments */ { - register int n; + int n; if (curact == NULL) return(0); @@ -210,30 +195,31 @@ nargum() /* return number of available arguments */ double -argument(n) /* return nth argument for active function */ -register int n; +argument(int n) /* return nth argument for active function */ { - register ACTIVATION *actp = curact; - register EPNODE *ep; + ACTIVATION *actp = curact; + EPNODE *ep = NULL; double aval; - if (actp == NULL || --n < 0) { + if (!n) /* asking for # arguments? */ + return((double)nargum()); + + if (!actp | (--n < 0)) { eputs("Bad call to argument!\n"); quit(1); } - /* already computed? */ - if (n < AFLAGSIZ && 1L<an) + if ((n < AFLAGSIZ) & actp->an >> n) /* already computed? */ return(actp->ap[n]); - if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) { + if (!actp->fun || !(ep = ekid(actp->fun, n+1))) { eputs(actp->name); eputs(": too few arguments\n"); quit(1); } - curact = actp->prev; /* pop environment */ + curact = actp->prev; /* previous context */ aval = evalue(ep); /* compute argument */ - curact = actp; /* push back environment */ - if (n < ALISTSIZ) { /* save value */ + curact = actp; /* put back calling context */ + if (n < ALISTSIZ) { /* save value if room */ actp->ap[n] = aval; actp->an |= 1L<prev) { @@ -277,26 +261,24 @@ badarg: eputs(actp->name); eputs(": argument not a function\n"); quit(1); + return NULL; /* pro forma return */ } char * -argfun(n) /* return function name for nth argument */ -int n; +argfun(int n) /* return function name for nth argument */ { return(argf(n)->name); } -#endif double -efunc(ep) /* evaluate a function */ -register EPNODE *ep; +efunc(EPNODE *ep) /* evaluate a function */ { ACTIVATION act; double alist[ALISTSIZ]; double rval; - register VARDEF *dp; + VARDEF *dp; /* push environment */ dp = resolve(ep->v.kid); act.name = dp->name; @@ -317,11 +299,10 @@ register EPNODE *ep; LIBR * -liblookup(fname) /* look up a library function */ -char *fname; +liblookup(char *fname) /* look up a library function */ { int upper, lower; - register int cm, i; + int cm, i; lower = 0; upper = cm = libsize; @@ -340,67 +321,18 @@ char *fname; } -#ifndef VARIABLE -static VARDEF *varlist = NULL; /* our list of dummy variables */ - - -VARDEF * -varinsert(vname) /* dummy variable insert */ -char *vname; -{ - register VARDEF *vp; - - vp = (VARDEF *)emalloc(sizeof(VARDEF)); - vp->name = savestr(vname); - vp->nlinks = 1; - vp->def = NULL; - vp->lib = liblookup(vname); - vp->next = varlist; - varlist = vp; - return(vp); -} - - -varfree(vp) /* free dummy variable */ -register VARDEF *vp; -{ - register VARDEF *vp2; - - if (vp == varlist) - varlist = vp->next; - else { - for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next) - ; - vp2->next = vp->next; - } - freestr(vp->name); - efree((char *)vp); -} - - -libupdate(nm) /* update library */ -char *nm; -{ - register VARDEF *vp; - - for (vp = varlist; vp != NULL; vp = vp->next) - vp->lib = liblookup(vp->name); -} -#endif - - - /* * The following routines are for internal use: */ static double -libfunc(fname, vp) /* execute library function */ -char *fname; -VARDEF *vp; +libfunc( /* execute library function */ + char *fname, + VARDEF *vp +) { - register LIBR *lp; + LIBR *lp; double d; int lasterrno; @@ -416,14 +348,15 @@ VARDEF *vp; lasterrno = errno; errno = 0; d = (*lp->f)(lp->fname); -#ifdef IEEE - if (errno == 0) +#ifdef isnan + if (errno == 0) { if (isnan(d)) errno = EDOM; else if (isinf(d)) errno = ERANGE; + } #endif - if (errno) { + if (errno == EDOM || errno == ERANGE) { wputs(fname); if (errno == EDOM) wputs(": domain error\n"); @@ -444,7 +377,7 @@ VARDEF *vp; static double -l_if() /* if(cond, then, else) conditional expression */ +l_if(char *nm) /* if(cond, then, else) conditional expression */ /* cond evaluates true if greater than zero */ { if (argument(1) > 0.0) @@ -455,11 +388,11 @@ l_if() /* if(cond, then, else) conditional expressio static double -l_select() /* return argument #(A1+1) */ +l_select(char *nm) /* return argument #(A1+1) */ { - register int n; + int n; - n = argument(1) + .5; + n = (int)(argument(1) + .5); if (n == 0) return(nargum()-1); if (n < 1 || n > nargum()-1) { @@ -471,8 +404,40 @@ l_select() /* return argument #(A1+1) */ static double -l_rand() /* random function between 0 and 1 */ +l_max(char *nm) /* general maximum function */ { + int n = nargum(); + int i = 1; + int vmax = argument(1); + + while (i++ < n) { + double v = argument(i); + if (vmax < v) + vmax = v; + } + return(vmax); +} + + +static double +l_min(char *nm) /* general minimum function */ +{ + int n = nargum(); + int i = 1; + int vmin = argument(1); + + while (i++ < n) { + double v = argument(i); + if (vmin > v) + vmin = v; + } + return(vmin); +} + + +static double +l_rand(char *nm) /* random function between 0 and 1 */ +{ double x; x = argument(1); @@ -484,93 +449,91 @@ l_rand() /* random function between 0 and 1 */ static double -l_floor() /* return largest integer not greater than arg1 */ +l_floor(char *nm) /* return largest integer not greater than arg1 */ { return(floor(argument(1))); } static double -l_ceil() /* return smallest integer not less than arg1 */ +l_ceil(char *nm) /* return smallest integer not less than arg1 */ { return(ceil(argument(1))); } -#ifdef BIGLIB static double -l_sqrt() +l_sqrt(char *nm) { return(sqrt(argument(1))); } static double -l_sin() +l_sin(char *nm) { return(sin(argument(1))); } static double -l_cos() +l_cos(char *nm) { return(cos(argument(1))); } static double -l_tan() +l_tan(char *nm) { return(tan(argument(1))); } static double -l_asin() +l_asin(char *nm) { return(asin(argument(1))); } static double -l_acos() +l_acos(char *nm) { return(acos(argument(1))); } static double -l_atan() +l_atan(char *nm) { return(atan(argument(1))); } static double -l_atan2() +l_atan2(char *nm) { return(atan2(argument(1), argument(2))); } static double -l_exp() +l_exp(char *nm) { return(exp(argument(1))); } static double -l_log() +l_log(char *nm) { return(log(argument(1))); } static double -l_log10() +l_log10(char *nm) { return(log10(argument(1))); } -#endif