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.14 by schorsch, Fri Nov 14 17:22:06 2003 UTC vs.
Revision 2.21 by greg, Mon Jun 10 13:56:52 2019 UTC

# Line 22 | Line 22 | static const char      RCSid[] = "$Id$";
22  
23                                  /* bits in argument flag (better be right!) */
24   #define  AFLAGSIZ       (8*sizeof(unsigned long))
25 < #define  ALISTSIZ       6       /* maximum saved argument list */
25 > #define  ALISTSIZ       8       /* maximum saved argument list */
26  
27   typedef struct activation {
28      char  *name;                /* function name */
# Line 40 | Line 40 | static double  libfunc(char *fname, VARDEF *vp);
40   #define  MAXLIB         64      /* maximum number of library functions */
41   #endif
42  
43 < static double  l_if(char *), l_select(char *), l_rand(char *);
43 > static double  l_if(char *), l_select(char *);
44 > static double  l_min(char *), l_max(char *);
45 > static double  l_rand(char *);
46   static double  l_floor(char *), l_ceil(char *);
47   static double  l_sqrt(char *);
48   static double  l_sin(char *), l_cos(char *), l_tan(char *);
# Line 60 | Line 62 | static LIBR  library[MAXLIB] = {
62      { "if", 3, ':', l_if },
63      { "log", 1, ':', l_log },
64      { "log10", 1, ':', l_log10 },
65 +    { "max", 1, ':', l_max },
66 +    { "min", 1, ':', l_min },
67      { "rand", 1, ':', l_rand },
68      { "select", 1, ':', l_select },
69      { "sin", 1, ':', l_sin },
# Line 73 | Line 77 | static int  libsize = 16;
77  
78  
79   int
80 < fundefined(fname)               /* return # of arguments for function */
81 < char  *fname;
80 > fundefined(                     /* return # of req'd arguments for function */
81 >        char  *fname
82 > )
83   {
84 <    register LIBR  *lp;
85 <    register VARDEF  *vp;
84 >    LIBR  *lp;
85 >    VARDEF  *vp;
86  
87      if ((vp = varlookup(fname)) != NULL && vp->def != NULL
88                  && vp->def->v.kid->type == FUNC)
# Line 90 | Line 95 | char  *fname;
95  
96  
97   double
98 < funvalue(fname, n, a)           /* return a function value to the user */
99 < char  *fname;
100 < int  n;
101 < double  *a;
98 > funvalue(                       /* return a function value to the user */
99 >        char  *fname,
100 >        int  n,
101 >        double  *a
102 > )
103   {
104      ACTIVATION  act;
105 <    register VARDEF  *vp;
105 >    VARDEF  *vp;
106      double  rval;
107                                          /* push environment */
108      act.name = fname;
109      act.prev = curact;
110      act.ap = a;
111 <    if (n >= AFLAGSIZ)
106 <        act.an = ~0;
107 <    else
111 >    if (n < AFLAGSIZ)
112          act.an = (1L<<n)-1;
113 +    else {
114 +        act.an = ~0;
115 +        if (n > AFLAGSIZ)
116 +            wputs("Excess arguments in funvalue()\n");
117 +    }
118      act.fun = NULL;
119      curact = &act;
120  
# Line 121 | Line 130 | double  *a;
130  
131  
132   void
133 < funset(fname, nargs, assign, fptr)      /* set a library function */
134 < char  *fname;
135 < int  nargs;
136 < int  assign;
137 < double  (*fptr)(char *);
133 > funset(                         /* set a library function */
134 >        char  *fname,
135 >        int  nargs,
136 >        int  assign,
137 >        double  (*fptr)(char *)
138 > )
139   {
140      int  oldlibsize = libsize;
141      char *cp;
142 <    register LIBR  *lp;
142 >    LIBR  *lp;
143                                                  /* check for context */
144      for (cp = fname; *cp; cp++)
145          ;
146      if (cp == fname)
147          return;
148 <    if (cp[-1] == CNTXMARK)
148 >    while (cp[-1] == CNTXMARK) {
149          *--cp = '\0';
150 +        if (cp == fname) return;
151 +    }
152      if ((lp = liblookup(fname)) == NULL) {      /* insert */
153          if (libsize >= MAXLIB) {
154              eputs("Too many library functons!\n");
155              quit(1);
156          }
157          for (lp = &library[libsize]; lp > library; lp--)
158 <            if (strcmp(lp[-1].fname, fname) > 0) {
159 <                lp[0].fname = lp[-1].fname;
160 <                lp[0].nargs = lp[-1].nargs;
149 <                lp[0].atyp = lp[-1].atyp;
150 <                lp[0].f = lp[-1].f;
151 <            } else
158 >            if (strcmp(lp[-1].fname, fname) > 0)
159 >                lp[0] = lp[-1];
160 >            else
161                  break;
162          libsize++;
163      }
164      if (fptr == NULL) {                         /* delete */
165          while (lp < &library[libsize-1]) {
166 <            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;
166 >            lp[0] = lp[1];
167              lp++;
168          }
169          libsize--;
# Line 173 | Line 179 | double  (*fptr)(char *);
179  
180  
181   int
182 < nargum()                        /* return number of available arguments */
182 > nargum(void)                    /* return number of available arguments */
183   {
184 <    register int  n;
184 >    int  n;
185  
186      if (curact == NULL)
187          return(0);
# Line 189 | Line 195 | nargum()                       /* return number of available arguments */
195  
196  
197   double
198 < argument(n)                     /* return nth argument for active function */
193 < register int  n;
198 > argument(int n)                 /* return nth argument for active function */
199   {
200 <    register ACTIVATION  *actp = curact;
201 <    register EPNODE  *ep = NULL;
200 >    ACTIVATION  *actp = curact;
201 >    EPNODE  *ep = NULL;
202      double  aval;
203  
204 <    if (actp == NULL || --n < 0) {
204 >    if (!n)                                     /* asking for # arguments? */
205 >        return((double)nargum());
206 >
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 295 | Line 299 | register EPNODE  *ep;
299  
300  
301   LIBR *
302 < liblookup(fname)                /* look up a library function */
299 < char  *fname;
302 > liblookup(char *fname)          /* look up a library function */
303   {
304      int  upper, lower;
305 <    register int  cm, i;
305 >    int  cm, i;
306  
307      lower = 0;
308      upper = cm = libsize;
# Line 324 | Line 327 | char  *fname;
327  
328  
329   static double
330 < libfunc(fname, vp)                      /* execute library function */
331 < char  *fname;
332 < VARDEF  *vp;
330 > libfunc(                                /* execute library function */
331 >        char  *fname,
332 >        VARDEF  *vp
333 > )
334   {
335 <    register LIBR  *lp;
335 >    LIBR  *lp;
336      double  d;
337      int  lasterrno;
338  
# Line 344 | Line 348 | VARDEF  *vp;
348      lasterrno = errno;
349      errno = 0;
350      d = (*lp->f)(lp->fname);
351 < #ifdef  IEEE
352 <    if (errno == 0)
351 > #ifdef  isnan
352 >    if (errno == 0) {
353          if (isnan(d))
354              errno = EDOM;
355          else if (isinf(d))
356              errno = ERANGE;
357 +    }
358   #endif
359      if (errno == EDOM || errno == ERANGE) {
360          wputs(fname);
# Line 385 | Line 390 | l_if(char *nm)         /* if(cond, then, else) conditional ex
390   static double
391   l_select(char *nm)      /* return argument #(A1+1) */
392   {
393 <        register int  n;
393 >        int  n;
394  
395          n = (int)(argument(1) + .5);
396          if (n == 0)
# Line 395 | Line 400 | l_select(char *nm)     /* return argument #(A1+1) */
400                  return(0.0);
401          }
402          return(argument(n+1));
403 + }
404 +
405 +
406 + static double
407 + l_max(char *nm)         /* general maximum function */
408 + {
409 +        int  n = nargum();
410 +        int  i = 1;
411 +        int  vmax = argument(1);
412 +
413 +        while (i++ < n) {
414 +                double  v = argument(i);
415 +                if (vmax < v)
416 +                        vmax = v;
417 +        }
418 +        return(vmax);
419 + }
420 +
421 +
422 + static double
423 + l_min(char *nm)         /* general minimum function */
424 + {
425 +        int  n = nargum();
426 +        int  i = 1;
427 +        int  vmin = argument(1);
428 +
429 +        while (i++ < n) {
430 +                double  v = argument(i);
431 +                if (vmin > v)
432 +                        vmin = v;
433 +        }
434 +        return(vmin);
435   }
436  
437  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines