ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/calfunc.c
(Generate patch)

Comparing ray/src/common/calfunc.c (file contents):
Revision 2.13 by greg, Mon Aug 4 22:37:53 2003 UTC vs.
Revision 2.31 by greg, Mon Feb 26 20:55:00 2024 UTC

# Line 15 | Line 15 | static const char      RCSid[] = "$Id$";
15   #include  <stdio.h>
16   #include  <string.h>
17   #include  <errno.h>
18 + #include  <stdlib.h>
19   #include  <math.h>
20  
21   #include  "rterror.h"
# Line 22 | Line 23 | static const char      RCSid[] = "$Id$";
23  
24                                  /* bits in argument flag (better be right!) */
25   #define  AFLAGSIZ       (8*sizeof(unsigned long))
26 < #define  ALISTSIZ       6       /* maximum saved argument list */
26 > #define  ALISTSIZ       10      /* maximum saved argument list */
27  
28   typedef struct activation {
29      char  *name;                /* function name */
# Line 40 | Line 41 | static double  libfunc(char *fname, VARDEF *vp);
41   #define  MAXLIB         64      /* maximum number of library functions */
42   #endif
43  
44 < static double  l_if(char *), l_select(char *), l_rand(char *);
44 > static double  l_if(char *), l_select(char *);
45 > static double  l_min(char *), l_max(char *);
46 > static double  l_rand(char *);
47   static double  l_floor(char *), l_ceil(char *);
48   static double  l_sqrt(char *);
49   static double  l_sin(char *), l_cos(char *), l_tan(char *);
# Line 60 | Line 63 | static LIBR  library[MAXLIB] = {
63      { "if", 3, ':', l_if },
64      { "log", 1, ':', l_log },
65      { "log10", 1, ':', l_log10 },
66 +    { "max", 1, ':', l_max },
67 +    { "min", 1, ':', l_min },
68      { "rand", 1, ':', l_rand },
69      { "select", 1, ':', l_select },
70      { "sin", 1, ':', l_sin },
# Line 67 | Line 72 | static LIBR  library[MAXLIB] = {
72      { "tan", 1, ':', l_tan },
73   };
74  
75 < static int  libsize = 16;
75 > static int  libsize = 18;
76  
77   #define  resolve(ep)    ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
78  
79  
80   int
81 < fundefined(fname)               /* return # of arguments for function */
82 < char  *fname;
81 > fundefined(                     /* return # of req'd arguments for function */
82 >        char  *fname
83 > )
84   {
85 <    register LIBR  *lp;
86 <    register VARDEF  *vp;
85 >    LIBR  *lp;
86 >    VARDEF  *vp;
87  
88      if ((vp = varlookup(fname)) != NULL && vp->def != NULL
89                  && vp->def->v.kid->type == FUNC)
# Line 90 | Line 96 | char  *fname;
96  
97  
98   double
99 < funvalue(fname, n, a)           /* return a function value to the user */
100 < char  *fname;
101 < int  n;
102 < double  *a;
99 > funvalue(                       /* return a function value to the user */
100 >        char  *fname,
101 >        int  n,
102 >        double  *a
103 > )
104   {
105      ACTIVATION  act;
106 <    register VARDEF  *vp;
106 >    VARDEF  *vp;
107      double  rval;
108                                          /* push environment */
109      act.name = fname;
110      act.prev = curact;
111      act.ap = a;
112 <    if (n >= AFLAGSIZ)
106 <        act.an = ~0;
107 <    else
112 >    if (n < AFLAGSIZ)
113          act.an = (1L<<n)-1;
114 +    else {
115 +        act.an = ~0;
116 +        if (n > AFLAGSIZ)
117 +            wputs("Excess arguments in funvalue()\n");
118 +    }
119      act.fun = NULL;
120      curact = &act;
121  
# Line 121 | Line 131 | double  *a;
131  
132  
133   void
134 < funset(fname, nargs, assign, fptr)      /* set a library function */
135 < char  *fname;
136 < int  nargs;
137 < int  assign;
138 < double  (*fptr)(char *);
134 > funset(                         /* set a library function */
135 >        char  *fname,
136 >        int  nargs,
137 >        int  assign,
138 >        double  (*fptr)(char *)
139 > )
140   {
141      int  oldlibsize = libsize;
142      char *cp;
143 <    register LIBR  *lp;
143 >    LIBR  *lp;
144                                                  /* check for context */
145      for (cp = fname; *cp; cp++)
146          ;
147      if (cp == fname)
148          return;
149 <    if (cp[-1] == CNTXMARK)
149 >    while (cp[-1] == CNTXMARK) {
150          *--cp = '\0';
151 +        if (cp == fname) return;
152 +    }
153      if ((lp = liblookup(fname)) == NULL) {      /* insert */
154 +        if (fptr == NULL)
155 +                return;                         /* nothing! */
156          if (libsize >= MAXLIB) {
157              eputs("Too many library functons!\n");
158              quit(1);
159          }
160          for (lp = &library[libsize]; lp > library; lp--)
161 <            if (strcmp(lp[-1].fname, fname) > 0) {
162 <                lp[0].fname = lp[-1].fname;
163 <                lp[0].nargs = lp[-1].nargs;
149 <                lp[0].atyp = lp[-1].atyp;
150 <                lp[0].f = lp[-1].f;
151 <            } else
161 >            if (strcmp(lp[-1].fname, fname) > 0)
162 >                lp[0] = lp[-1];
163 >            else
164                  break;
165          libsize++;
166      }
167      if (fptr == NULL) {                         /* delete */
168          while (lp < &library[libsize-1]) {
169 <            lp[0].fname = lp[1].fname;
158 <            lp[0].nargs = lp[1].nargs;
159 <            lp[0].atyp = lp[1].atyp;
160 <            lp[0].f = lp[1].f;
169 >            lp[0] = lp[1];
170              lp++;
171          }
172          libsize--;
# Line 173 | Line 182 | double  (*fptr)(char *);
182  
183  
184   int
185 < nargum()                        /* return number of available arguments */
185 > nargum(void)                    /* return number of available arguments */
186   {
187 <    register int  n;
187 >    int  n;
188  
189      if (curact == NULL)
190          return(0);
# Line 189 | Line 198 | nargum()                       /* return number of available arguments */
198  
199  
200   double
201 < argument(n)                     /* return nth argument for active function */
193 < register int  n;
201 > argument(int n)                 /* return nth argument for active function */
202   {
203 <    register ACTIVATION  *actp = curact;
204 <    register EPNODE  *ep;
203 >    ACTIVATION  *actp = curact;
204 >    EPNODE  *ep;
205      double  aval;
206  
207 <    if (actp == NULL || --n < 0) {
207 >    if (!actp | (--n < 0)) {
208          eputs("Bad call to argument!\n");
209          quit(1);
210      }
211 <                                                /* already computed? */
204 <    if (n < AFLAGSIZ && 1L<<n & actp->an)
211 >    if ((n < AFLAGSIZ) & actp->an >> n)         /* already computed? */
212          return(actp->ap[n]);
213  
214 <    if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
214 >    if (!actp->fun || !(ep = ekid(actp->fun, n+1))) {
215          eputs(actp->name);
216          eputs(": too few arguments\n");
217          quit(1);
218      }
219 <    curact = actp->prev;                        /* pop environment */
219 >    curact = actp->prev;                        /* previous context */
220      aval = evalue(ep);                          /* compute argument */
221 <    curact = actp;                              /* push back environment */
222 <    if (n < ALISTSIZ) {                         /* save value */
221 >    curact = actp;                              /* put back calling context */
222 >    if (n < ALISTSIZ) {                         /* save value if room */
223          actp->ap[n] = aval;
224          actp->an |= 1L<<n;
225      }
# Line 221 | Line 228 | register int  n;
228  
229  
230   VARDEF *
231 < argf(n)                         /* return function def for nth argument */
225 < int  n;
231 > argf(int n)                     /* return function def for nth argument */
232   {
233 <    register ACTIVATION  *actp;
234 <    register EPNODE  *ep;
233 >    ACTIVATION  *actp;
234 >    EPNODE  *ep;
235  
236      for (actp = curact; actp != NULL; actp = actp->prev) {
237  
# Line 260 | Line 266 | badarg:
266  
267  
268   char *
269 < argfun(n)                       /* return function name for nth argument */
264 < int  n;
269 > argfun(int n)                   /* return function name for nth argument */
270   {
271      return(argf(n)->name);
272   }
273  
274  
275   double
276 < efunc(ep)                               /* evaluate a function */
272 < register EPNODE  *ep;
276 > efunc(EPNODE *ep)                       /* evaluate a function */
277   {
278      ACTIVATION  act;
279      double  alist[ALISTSIZ];
280      double  rval;
281 <    register VARDEF  *dp;
281 >    VARDEF  *dp;
282                                          /* push environment */
283      dp = resolve(ep->v.kid);
284      act.name = dp->name;
# Line 294 | Line 298 | register EPNODE  *ep;
298   }
299  
300  
301 + double
302 + eargument(                              /* evaluate an argument */
303 +    EPNODE      *ep
304 + )
305 + {
306 +    if ((ep->v.chan <= AFLAGSIZ) & curact->an >> (ep->v.chan-1))
307 +        return(curact->ap[ep->v.chan-1]);
308 +
309 +    return(argument(ep->v.chan));
310 + }
311 +
312 +
313   LIBR *
314 < liblookup(fname)                /* look up a library function */
299 < char  *fname;
314 > liblookup(char *fname)          /* look up a library function */
315   {
316      int  upper, lower;
317 <    register int  cm, i;
317 >    int  cm, i;
318  
319      lower = 0;
320      upper = cm = libsize;
# Line 324 | Line 339 | char  *fname;
339  
340  
341   static double
342 < libfunc(fname, vp)                      /* execute library function */
343 < char  *fname;
344 < VARDEF  *vp;
342 > libfunc(                                /* execute library function */
343 >        char  *fname,
344 >        VARDEF  *vp
345 > )
346   {
347 <    register LIBR  *lp;
347 >    LIBR  *lp;
348      double  d;
349      int  lasterrno;
350  
# Line 344 | Line 360 | VARDEF  *vp;
360      lasterrno = errno;
361      errno = 0;
362      d = (*lp->f)(lp->fname);
363 < #ifdef  IEEE
364 <    if (errno == 0)
363 > #ifdef  isnan
364 >    if (errno == 0) {
365          if (isnan(d))
366              errno = EDOM;
367          else if (isinf(d))
368              errno = ERANGE;
369 +    }
370   #endif
371 <    if (errno == EDOM || errno == ERANGE) {
371 >    if ((errno == EDOM) | (errno == ERANGE)) {
372          wputs(fname);
373          if (errno == EDOM)
374                  wputs(": domain error\n");
# Line 385 | Line 402 | l_if(char *nm)         /* if(cond, then, else) conditional ex
402   static double
403   l_select(char *nm)      /* return argument #(A1+1) */
404   {
405 <        register int  n;
405 >        int     narg = nargum();
406 >        double  a1 = argument(1);
407 >        int  n = (int)(a1 + .5);
408  
409 <        n = (int)(argument(1) + .5);
391 <        if (n == 0)
392 <                return(nargum()-1);
393 <        if (n < 1 || n > nargum()-1) {
409 >        if ((a1 < -.5) | (n >= narg)) {
410                  errno = EDOM;
411                  return(0.0);
412          }
413 +        if (!n)         /* asking max index? */
414 +                return(narg-1);
415          return(argument(n+1));
416 + }
417 +
418 +
419 + static double
420 + l_max(char *nm)         /* general maximum function */
421 + {
422 +        int  n = nargum();
423 +        double  vmax = argument(1);
424 +
425 +        while (n > 1) {
426 +                double  v = argument(n--);
427 +                if (vmax < v)
428 +                        vmax = v;
429 +        }
430 +        return(vmax);
431 + }
432 +
433 +
434 + static double
435 + l_min(char *nm)         /* general minimum function */
436 + {
437 +        int  n = nargum();
438 +        double  vmin = argument(1);
439 +
440 +        while (n > 1) {
441 +                double  v = argument(n--);
442 +                if (vmin > v)
443 +                        vmin = v;
444 +        }
445 +        return(vmin);
446   }
447  
448  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines