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.10 by greg, Wed Mar 5 16:16:52 2003 UTC vs.
Revision 2.18 by greg, Sat Aug 1 23:27:04 2015 UTC

# Line 13 | Line 13 | static const char      RCSid[] = "$Id$";
13   #include "copyright.h"
14  
15   #include  <stdio.h>
16 <
16 > #include  <string.h>
17   #include  <errno.h>
18
18   #include  <math.h>
19  
20 + #include  "rterror.h"
21   #include  "calcomp.h"
22  
23                                  /* bits in argument flag (better be right!) */
# Line 34 | Line 34 | typedef struct activation {
34  
35   static ACTIVATION  *curact = NULL;
36  
37 < static double  libfunc();
37 > static double  libfunc(char *fname, VARDEF *vp);
38  
39   #ifndef  MAXLIB
40   #define  MAXLIB         64      /* maximum number of library functions */
41   #endif
42  
43 < static double  l_if(), l_select(), l_rand();
44 < static double  l_floor(), l_ceil();
45 < static double  l_sqrt();
46 < static double  l_sin(), l_cos(), l_tan();
47 < static double  l_asin(), l_acos(), l_atan(), l_atan2();
48 < static double  l_exp(), l_log(), l_log10();
43 > static double  l_if(char *), l_select(char *), l_rand(char *);
44 > static double  l_floor(char *), l_ceil(char *);
45 > static double  l_sqrt(char *);
46 > static double  l_sin(char *), l_cos(char *), l_tan(char *);
47 > static double  l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *);
48 > static double  l_exp(char *), l_log(char *), l_log10(char *);
49  
50                          /* functions must be listed alphabetically */
51   static LIBR  library[MAXLIB] = {
# Line 73 | Line 73 | static int  libsize = 16;
73  
74  
75   int
76 < fundefined(fname)               /* return # of arguments for function */
77 < char  *fname;
76 > fundefined(                     /* return # of arguments for function */
77 >        char  *fname
78 > )
79   {
80 <    register LIBR  *lp;
81 <    register VARDEF  *vp;
80 >    LIBR  *lp;
81 >    VARDEF  *vp;
82  
83      if ((vp = varlookup(fname)) != NULL && vp->def != NULL
84                  && vp->def->v.kid->type == FUNC)
# Line 90 | Line 91 | char  *fname;
91  
92  
93   double
94 < funvalue(fname, n, a)           /* return a function value to the user */
95 < char  *fname;
96 < int  n;
97 < double  *a;
94 > funvalue(                       /* return a function value to the user */
95 >        char  *fname,
96 >        int  n,
97 >        double  *a
98 > )
99   {
100      ACTIVATION  act;
101 <    register VARDEF  *vp;
101 >    VARDEF  *vp;
102      double  rval;
103                                          /* push environment */
104      act.name = fname;
# Line 121 | Line 123 | double  *a;
123  
124  
125   void
126 < funset(fname, nargs, assign, fptr)      /* set a library function */
127 < char  *fname;
128 < int  nargs;
129 < int  assign;
130 < double  (*fptr)();
126 > funset(                         /* set a library function */
127 >        char  *fname,
128 >        int  nargs,
129 >        int  assign,
130 >        double  (*fptr)(char *)
131 > )
132   {
133      int  oldlibsize = libsize;
134      char *cp;
135 <    register LIBR  *lp;
135 >    LIBR  *lp;
136                                                  /* check for context */
137      for (cp = fname; *cp; cp++)
138          ;
# Line 143 | Line 146 | double  (*fptr)();
146              quit(1);
147          }
148          for (lp = &library[libsize]; lp > library; lp--)
149 <            if (strcmp(lp[-1].fname, fname) > 0) {
150 <                lp[0].fname = lp[-1].fname;
151 <                lp[0].nargs = lp[-1].nargs;
149 <                lp[0].atyp = lp[-1].atyp;
150 <                lp[0].f = lp[-1].f;
151 <            } else
149 >            if (strcmp(lp[-1].fname, fname) > 0)
150 >                lp[0] = lp[-1];
151 >            else
152                  break;
153          libsize++;
154      }
155      if (fptr == NULL) {                         /* delete */
156          while (lp < &library[libsize-1]) {
157 <            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;
157 >            lp[0] = lp[1];
158              lp++;
159          }
160          libsize--;
# Line 173 | Line 170 | double  (*fptr)();
170  
171  
172   int
173 < nargum()                        /* return number of available arguments */
173 > nargum(void)                    /* return number of available arguments */
174   {
175 <    register int  n;
175 >    int  n;
176  
177      if (curact == NULL)
178          return(0);
# Line 189 | Line 186 | nargum()                       /* return number of available arguments */
186  
187  
188   double
189 < argument(n)                     /* return nth argument for active function */
193 < register int  n;
189 > argument(int n)                 /* return nth argument for active function */
190   {
191 <    register ACTIVATION  *actp = curact;
192 <    register EPNODE  *ep;
191 >    ACTIVATION  *actp = curact;
192 >    EPNODE  *ep = NULL;
193      double  aval;
194  
195      if (actp == NULL || --n < 0) {
# Line 221 | Line 217 | register int  n;
217  
218  
219   VARDEF *
220 < argf(n)                         /* return function def for nth argument */
225 < int  n;
220 > argf(int n)                     /* return function def for nth argument */
221   {
222 <    register ACTIVATION  *actp;
223 <    register EPNODE  *ep;
222 >    ACTIVATION  *actp;
223 >    EPNODE  *ep;
224  
225      for (actp = curact; actp != NULL; actp = actp->prev) {
226  
# Line 255 | Line 250 | badarg:
250      eputs(actp->name);
251      eputs(": argument not a function\n");
252      quit(1);
253 +        return NULL; /* pro forma return */
254   }
255  
256  
257   char *
258 < argfun(n)                       /* return function name for nth argument */
263 < int  n;
258 > argfun(int n)                   /* return function name for nth argument */
259   {
260      return(argf(n)->name);
261   }
262  
263  
264   double
265 < efunc(ep)                               /* evaluate a function */
271 < register EPNODE  *ep;
265 > efunc(EPNODE *ep)                       /* evaluate a function */
266   {
267      ACTIVATION  act;
268      double  alist[ALISTSIZ];
269      double  rval;
270 <    register VARDEF  *dp;
270 >    VARDEF  *dp;
271                                          /* push environment */
272      dp = resolve(ep->v.kid);
273      act.name = dp->name;
# Line 294 | Line 288 | register EPNODE  *ep;
288  
289  
290   LIBR *
291 < liblookup(fname)                /* look up a library function */
298 < char  *fname;
291 > liblookup(char *fname)          /* look up a library function */
292   {
293      int  upper, lower;
294 <    register int  cm, i;
294 >    int  cm, i;
295  
296      lower = 0;
297      upper = cm = libsize;
# Line 323 | Line 316 | char  *fname;
316  
317  
318   static double
319 < libfunc(fname, vp)                      /* execute library function */
320 < char  *fname;
321 < VARDEF  *vp;
319 > libfunc(                                /* execute library function */
320 >        char  *fname,
321 >        VARDEF  *vp
322 > )
323   {
324 <    register LIBR  *lp;
324 >    LIBR  *lp;
325      double  d;
326      int  lasterrno;
327  
# Line 343 | Line 337 | VARDEF  *vp;
337      lasterrno = errno;
338      errno = 0;
339      d = (*lp->f)(lp->fname);
340 < #ifdef  IEEE
341 <    if (errno == 0)
340 > #ifdef  isnan
341 >    if (errno == 0) {
342          if (isnan(d))
343              errno = EDOM;
344          else if (isinf(d))
345              errno = ERANGE;
346 +    }
347   #endif
348      if (errno == EDOM || errno == ERANGE) {
349          wputs(fname);
# Line 371 | Line 366 | VARDEF  *vp;
366  
367  
368   static double
369 < l_if()                  /* if(cond, then, else) conditional expression */
369 > l_if(char *nm)          /* if(cond, then, else) conditional expression */
370                          /* cond evaluates true if greater than zero */
371   {
372      if (argument(1) > 0.0)
# Line 382 | Line 377 | l_if()                 /* if(cond, then, else) conditional expressio
377  
378  
379   static double
380 < l_select()              /* return argument #(A1+1) */
380 > l_select(char *nm)      /* return argument #(A1+1) */
381   {
382 <        register int  n;
382 >        int  n;
383  
384 <        n = argument(1) + .5;
384 >        n = (int)(argument(1) + .5);
385          if (n == 0)
386                  return(nargum()-1);
387          if (n < 1 || n > nargum()-1) {
# Line 398 | Line 393 | l_select()             /* return argument #(A1+1) */
393  
394  
395   static double
396 < l_rand()                /* random function between 0 and 1 */
396 > l_rand(char *nm)                /* random function between 0 and 1 */
397   {
398      double  x;
399  
# Line 411 | Line 406 | l_rand()               /* random function between 0 and 1 */
406  
407  
408   static double
409 < l_floor()               /* return largest integer not greater than arg1 */
409 > l_floor(char *nm)               /* return largest integer not greater than arg1 */
410   {
411      return(floor(argument(1)));
412   }
413  
414  
415   static double
416 < l_ceil()                /* return smallest integer not less than arg1 */
416 > l_ceil(char *nm)                /* return smallest integer not less than arg1 */
417   {
418      return(ceil(argument(1)));
419   }
420  
421  
422   static double
423 < l_sqrt()
423 > l_sqrt(char *nm)
424   {
425      return(sqrt(argument(1)));
426   }
427  
428  
429   static double
430 < l_sin()
430 > l_sin(char *nm)
431   {
432      return(sin(argument(1)));
433   }
434  
435  
436   static double
437 < l_cos()
437 > l_cos(char *nm)
438   {
439      return(cos(argument(1)));
440   }
441  
442  
443   static double
444 < l_tan()
444 > l_tan(char *nm)
445   {
446      return(tan(argument(1)));
447   }
448  
449  
450   static double
451 < l_asin()
451 > l_asin(char *nm)
452   {
453      return(asin(argument(1)));
454   }
455  
456  
457   static double
458 < l_acos()
458 > l_acos(char *nm)
459   {
460      return(acos(argument(1)));
461   }
462  
463  
464   static double
465 < l_atan()
465 > l_atan(char *nm)
466   {
467      return(atan(argument(1)));
468   }
469  
470  
471   static double
472 < l_atan2()
472 > l_atan2(char *nm)
473   {
474      return(atan2(argument(1), argument(2)));
475   }
476  
477  
478   static double
479 < l_exp()
479 > l_exp(char *nm)
480   {
481      return(exp(argument(1)));
482   }
483  
484  
485   static double
486 < l_log()
486 > l_log(char *nm)
487   {
488      return(log(argument(1)));
489   }
490  
491  
492   static double
493 < l_log10()
493 > l_log10(char *nm)
494   {
495      return(log10(argument(1)));
496   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines