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.19 by greg, Mon Mar 4 18:16:18 2019 UTC vs.
Revision 2.23 by greg, Tue Jun 11 22:13:50 2019 UTC

# 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 67 | Line 71 | static LIBR  library[MAXLIB] = {
71      { "tan", 1, ':', l_tan },
72   };
73  
74 < static int  libsize = 16;
74 > static int  libsize = 18;
75  
76   #define  resolve(ep)    ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
77  
78  
79   int
80 < fundefined(                     /* return # of arguments for function */
80 > fundefined(                     /* return # of req'd arguments for function */
81          char  *fname
82   )
83   {
# Line 141 | Line 145 | funset(                                /* set a library function */
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");
# Line 195 | Line 201 | argument(int n)                        /* return nth argument for active fu
201      EPNODE  *ep = NULL;
202      double  aval;
203  
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 <    if ((n < AFLAGSIZ) & (actp->an >> n))       /* already computed? */
211 >    if ((n < AFLAGSIZ) & actp->an >> n)         /* already computed? */
212          return(actp->ap[n]);
213  
214      if (!actp->fun || !(ep = ekid(actp->fun, n+1))) {
# Line 209 | Line 218 | argument(int n)                        /* return nth argument for active fu
218      }
219      curact = actp->prev;                        /* previous context */
220      aval = evalue(ep);                          /* compute argument */
221 <    curact = actp;                              /* push back context */
222 <    if (n < ALISTSIZ) {                         /* save value if we can */
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 381 | 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 <        int  n;
393 >        int     narg = nargum();
394 >        double  a1 = argument(1);
395 >        int  n = (int)(a1 + .5);
396  
397 <        n = (int)(argument(1) + .5);
387 <        if (n == 0)
388 <                return(nargum()-1);
389 <        if (n < 1 || n > nargum()-1) {
397 >        if (a1 < -.5 || n >= narg) {
398                  errno = EDOM;
399                  return(0.0);
400          }
401 +        if (!n)         /* asking max index? */
402 +                return(narg-1);
403          return(argument(n+1));
404 + }
405 +
406 +
407 + static double
408 + l_max(char *nm)         /* general maximum function */
409 + {
410 +        int  n = nargum();
411 +        int  i = 1;
412 +        int  vmax = argument(1);
413 +
414 +        while (i++ < n) {
415 +                double  v = argument(i);
416 +                if (vmax < v)
417 +                        vmax = v;
418 +        }
419 +        return(vmax);
420 + }
421 +
422 +
423 + static double
424 + l_min(char *nm)         /* general minimum function */
425 + {
426 +        int  n = nargum();
427 +        int  i = 1;
428 +        int  vmin = argument(1);
429 +
430 +        while (i++ < n) {
431 +                double  v = argument(i);
432 +                if (vmin > v)
433 +                        vmin = v;
434 +        }
435 +        return(vmin);
436   }
437  
438  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines