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 1.3 by greg, Sat Sep 29 11:18:34 1990 UTC vs.
Revision 2.20 by greg, Mon Mar 4 20:34:13 2019 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  calfunc.c - routines for calcomp using functions.
6   *
7 < *      The define BIGLIB pulls in a large number of the
11 < *  available math routines.
12 < *
13 < *      If VARIABLE is not defined, only library functions
7 > *      If VARIABLE is not set, only library functions
8   *  can be accessed.
9   *
10 < *     4/2/86
10 > *  2/19/03     Eliminated conditional compiles in favor of esupport extern.
11   */
12  
13 < #include  <stdio.h>
13 > #include "copyright.h"
14  
15 + #include  <stdio.h>
16 + #include  <string.h>
17   #include  <errno.h>
18 + #include  <math.h>
19  
20 + #include  "rterror.h"
21   #include  "calcomp.h"
22  
23 +                                /* bits in argument flag (better be right!) */
24 + #define  AFLAGSIZ       (8*sizeof(unsigned long))
25 + #define  ALISTSIZ       8       /* maximum saved argument list */
26  
26 #define  ALISTSIZ       6       /* maximum saved argument list */
27
27   typedef struct activation {
28      char  *name;                /* function name */
29      struct activation  *prev;   /* previous activation */
# Line 35 | 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
42 static double  l_if(), l_select(), l_rand();
43 static double  l_floor(), l_ceil();
44 #ifdef  BIGLIB
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();
41   #endif
42  
43 < #ifdef  BIGLIB
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] = {
52 <    { "acos", 1, l_acos },
53 <    { "asin", 1, l_asin },
54 <    { "atan", 1, l_atan },
55 <    { "atan2", 2, l_atan2 },
56 <    { "ceil", 1, l_ceil },
57 <    { "cos", 1, l_cos },
58 <    { "exp", 1, l_exp },
59 <    { "floor", 1, l_floor },
60 <    { "if", 3, l_if },
61 <    { "log", 1, l_log },
62 <    { "log10", 1, l_log10 },
63 <    { "rand", 1, l_rand },
64 <    { "select", 1, l_select },
65 <    { "sin", 1, l_sin },
66 <    { "sqrt", 1, l_sqrt },
67 <    { "tan", 1, l_tan },
52 >    { "acos", 1, ':', l_acos },
53 >    { "asin", 1, ':', l_asin },
54 >    { "atan", 1, ':', l_atan },
55 >    { "atan2", 2, ':', l_atan2 },
56 >    { "ceil", 1, ':', l_ceil },
57 >    { "cos", 1, ':', l_cos },
58 >    { "exp", 1, ':', l_exp },
59 >    { "floor", 1, ':', l_floor },
60 >    { "if", 3, ':', l_if },
61 >    { "log", 1, ':', l_log },
62 >    { "log10", 1, ':', l_log10 },
63 >    { "rand", 1, ':', l_rand },
64 >    { "select", 1, ':', l_select },
65 >    { "sin", 1, ':', l_sin },
66 >    { "sqrt", 1, ':', l_sqrt },
67 >    { "tan", 1, ':', l_tan },
68   };
69  
70   static int  libsize = 16;
71  
74 #else
75                        /* functions must be listed alphabetically */
76 static LIBR  library[MAXLIB] = {
77    { "ceil", 1, l_ceil },
78    { "floor", 1, l_floor },
79    { "if", 3, l_if },
80    { "rand", 1, l_rand },
81    { "select", 1, l_select },
82 };
83
84 static int  libsize = 5;
85
86 #endif
87
88 extern char  *savestr(), *emalloc();
89
90 extern LIBR  *liblookup();
91
92 extern VARDEF  *argf();
93
94 #ifdef  VARIABLE
72   #define  resolve(ep)    ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
96 #else
97 #define  resolve(ep)    ((ep)->v.ln)
98 #define varlookup(name) NULL
99 #endif
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      LIBR  *lp;
81 <    register VARDEF  *vp;
81 >    VARDEF  *vp;
82  
83 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
84 <                || vp->def->v.kid->type != FUNC)
111 <        if ((lp = liblookup(fname)) == NULL)
112 <            return(0);
113 <        else
114 <            return(lp->nargs);
115 <    else
83 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
84 >                && vp->def->v.kid->type == FUNC)
85          return(nekids(vp->def->v.kid) - 1);
86 +    lp = vp != NULL ? vp->lib : liblookup(fname);
87 +    if (lp == NULL)
88 +        return(0);
89 +    return(lp->nargs);
90   }
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;
105      act.prev = curact;
106      act.ap = a;
107 <    act.an = (1L<<n)-1;
107 >    if (n < AFLAGSIZ)
108 >        act.an = (1L<<n)-1;
109 >    else {
110 >        act.an = ~0;
111 >        if (n > AFLAGSIZ)
112 >            wputs("Excess arguments in funvalue()\n");
113 >    }
114      act.fun = NULL;
115      curact = &act;
116  
# Line 145 | Line 125 | double  *a;
125   }
126  
127  
128 < funset(fname, nargs, fptr)              /* set a library function */
129 < char  *fname;
130 < int  nargs;
131 < double  (*fptr)();
128 > void
129 > funset(                         /* set a library function */
130 >        char  *fname,
131 >        int  nargs,
132 >        int  assign,
133 >        double  (*fptr)(char *)
134 > )
135   {
136 <    register LIBR  *lp;
137 <
138 <    if ((lp = liblookup(fname)) == NULL) {
136 >    int  oldlibsize = libsize;
137 >    char *cp;
138 >    LIBR  *lp;
139 >                                                /* check for context */
140 >    for (cp = fname; *cp; cp++)
141 >        ;
142 >    if (cp == fname)
143 >        return;
144 >    if (cp[-1] == CNTXMARK)
145 >        *--cp = '\0';
146 >    if ((lp = liblookup(fname)) == NULL) {      /* insert */
147          if (libsize >= MAXLIB) {
148              eputs("Too many library functons!\n");
149              quit(1);
150          }
151          for (lp = &library[libsize]; lp > library; lp--)
152 <            if (strcmp(lp[-1].fname, fname) > 0) {
153 <                lp[0].fname = lp[-1].fname;
154 <                lp[0].nargs = lp[-1].nargs;
164 <                lp[0].f = lp[-1].f;
165 <            } else
152 >            if (strcmp(lp[-1].fname, fname) > 0)
153 >                lp[0] = lp[-1];
154 >            else
155                  break;
156          libsize++;
157      }
158 <    lp[0].fname = savestr(fname);
159 <    lp[0].nargs = nargs;
160 <    lp[0].f = fptr;
158 >    if (fptr == NULL) {                         /* delete */
159 >        while (lp < &library[libsize-1]) {
160 >            lp[0] = lp[1];
161 >            lp++;
162 >        }
163 >        libsize--;
164 >    } else {                                    /* or assign */
165 >        lp[0].fname = fname;            /* string must be static! */
166 >        lp[0].nargs = nargs;
167 >        lp[0].atyp = assign;
168 >        lp[0].f = fptr;
169 >    }
170 >    if (libsize != oldlibsize)
171 >        libupdate(fname);                       /* relink library */
172   }
173  
174  
175   int
176 < nargum()                        /* return number of available arguments */
176 > nargum(void)                    /* return number of available arguments */
177   {
178 <    register int  n;
178 >    int  n;
179  
180      if (curact == NULL)
181          return(0);
# Line 189 | Line 189 | nargum()                       /* return number of available arguments */
189  
190  
191   double
192 < argument(n)                     /* return nth argument for active function */
193 < register int  n;
192 > argument(int n)                 /* return nth argument for active function */
193   {
194 <    register ACTIVATION  *actp = curact;
195 <    EPNODE  *ep;
194 >    ACTIVATION  *actp = curact;
195 >    EPNODE  *ep = NULL;
196      double  aval;
197  
198 <    if (actp == NULL || --n < 0) {
198 >    if (!actp | (--n < 0)) {
199          eputs("Bad call to argument!\n");
200          quit(1);
201      }
202 <                                                /* already computed? */
204 <    if (1L<<n & actp->an)
202 >    if ((n < AFLAGSIZ) & actp->an >> n)         /* already computed? */
203          return(actp->ap[n]);
204  
205 <    if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
205 >    if (!actp->fun || !(ep = ekid(actp->fun, n+1))) {
206          eputs(actp->name);
207          eputs(": too few arguments\n");
208          quit(1);
209      }
210 <    curact = actp->prev;                        /* pop environment */
210 >    curact = actp->prev;                        /* previous context */
211      aval = evalue(ep);                          /* compute argument */
212 <    curact = actp;                              /* push back environment */
213 <    if (n < ALISTSIZ) {                         /* save value */
212 >    curact = actp;                              /* put back calling context */
213 >    if (n < ALISTSIZ) {                         /* save value if room */
214          actp->ap[n] = aval;
215          actp->an |= 1L<<n;
216      }
# Line 220 | Line 218 | register int  n;
218   }
219  
220  
223 #ifdef  VARIABLE
221   VARDEF *
222 < argf(n)                         /* return function def for nth argument */
226 < int  n;
222 > argf(int n)                     /* return function def for nth argument */
223   {
224 <    register ACTIVATION  *actp;
225 <    register EPNODE  *ep;
224 >    ACTIVATION  *actp;
225 >    EPNODE  *ep;
226  
227      for (actp = curact; actp != NULL; actp = actp->prev) {
228  
# Line 256 | Line 252 | badarg:
252      eputs(actp->name);
253      eputs(": argument not a function\n");
254      quit(1);
255 +        return NULL; /* pro forma return */
256   }
257  
258  
259   char *
260 < argfun(n)                       /* return function name for nth argument */
264 < int  n;
260 > argfun(int n)                   /* return function name for nth argument */
261   {
262      return(argf(n)->name);
263   }
268 #endif
264  
265  
266   double
267 < efunc(ep)                               /* evaluate a function */
273 < register EPNODE  *ep;
267 > efunc(EPNODE *ep)                       /* evaluate a function */
268   {
269      ACTIVATION  act;
270      double  alist[ALISTSIZ];
271      double  rval;
272 <    register VARDEF  *dp;
272 >    VARDEF  *dp;
273                                          /* push environment */
274      dp = resolve(ep->v.kid);
275      act.name = dp->name;
# Line 296 | Line 290 | register EPNODE  *ep;
290  
291  
292   LIBR *
293 < liblookup(fname)                /* look up a library function */
300 < char  *fname;
293 > liblookup(char *fname)          /* look up a library function */
294   {
295      int  upper, lower;
296 <    register int  cm, i;
296 >    int  cm, i;
297  
298      lower = 0;
299      upper = cm = libsize;
# Line 319 | Line 312 | char  *fname;
312   }
313  
314  
322 #ifndef  VARIABLE
323 VARDEF *
324 varinsert(vname)                /* dummy variable insert */
325 char  *vname;
326 {
327    register VARDEF  *vp;
328
329    vp = (VARDEF *)emalloc(sizeof(VARDEF));
330    vp->name = savestr(vname);
331    vp->nlinks = 1;
332    vp->def = NULL;
333    vp->lib = NULL;
334    vp->next = NULL;
335    return(vp);
336 }
337
338
339 varfree(vp)                     /* free dummy variable */
340 register VARDEF  *vp;
341 {
342    freestr(vp->name);
343    efree((char *)vp);
344 }
345 #endif
346
347
348
315   /*
316   *  The following routines are for internal use:
317   */
318  
319  
320   static double
321 < libfunc(fname, vp)                      /* execute library function */
322 < char  *fname;
323 < register VARDEF  *vp;
321 > libfunc(                                /* execute library function */
322 >        char  *fname,
323 >        VARDEF  *vp
324 > )
325   {
326 <    VARDEF  dumdef;
326 >    LIBR  *lp;
327      double  d;
328      int  lasterrno;
329  
330 <    if (vp == NULL) {
331 <        vp = &dumdef;
332 <        vp->lib = NULL;
333 <    }
334 <    if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) &&
368 <                                (vp->lib = liblookup(fname)) == NULL) ||
369 <                vp->lib->f == NULL) {
330 >    if (vp != NULL)
331 >        lp = vp->lib;
332 >    else
333 >        lp = liblookup(fname);
334 >    if (lp == NULL) {
335          eputs(fname);
336          eputs(": undefined function\n");
337          quit(1);
338      }
339      lasterrno = errno;
340      errno = 0;
341 <    d = (*vp->lib->f)();
342 < #ifdef  IEEE
343 <    if (!finite(d))
344 <        errno = EDOM;
341 >    d = (*lp->f)(lp->fname);
342 > #ifdef  isnan
343 >    if (errno == 0) {
344 >        if (isnan(d))
345 >            errno = EDOM;
346 >        else if (isinf(d))
347 >            errno = ERANGE;
348 >    }
349   #endif
350 <    if (errno) {
350 >    if (errno == EDOM || errno == ERANGE) {
351          wputs(fname);
352 <        wputs(": bad call\n");
352 >        if (errno == EDOM)
353 >                wputs(": domain error\n");
354 >        else if (errno == ERANGE)
355 >                wputs(": range error\n");
356 >        else
357 >                wputs(": error in call\n");
358          return(0.0);
359      }
360      errno = lasterrno;
# Line 394 | Line 368 | register VARDEF  *vp;
368  
369  
370   static double
371 < l_if()                  /* if(cond, then, else) conditional expression */
371 > l_if(char *nm)          /* if(cond, then, else) conditional expression */
372                          /* cond evaluates true if greater than zero */
373   {
374      if (argument(1) > 0.0)
# Line 405 | Line 379 | l_if()                 /* if(cond, then, else) conditional expressio
379  
380  
381   static double
382 < l_select()              /* return argument #(A1+1) */
382 > l_select(char *nm)      /* return argument #(A1+1) */
383   {
384 <        register int  n;
384 >        int  n;
385  
386 <        n = argument(1) + .5;
386 >        n = (int)(argument(1) + .5);
387          if (n == 0)
388                  return(nargum()-1);
389          if (n < 1 || n > nargum()-1) {
# Line 421 | Line 395 | l_select()             /* return argument #(A1+1) */
395  
396  
397   static double
398 < l_rand()                /* random function between 0 and 1 */
398 > l_rand(char *nm)                /* random function between 0 and 1 */
399   {
426    extern double  floor();
400      double  x;
401  
402      x = argument(1);
# Line 435 | Line 408 | l_rand()               /* random function between 0 and 1 */
408  
409  
410   static double
411 < l_floor()               /* return largest integer not greater than arg1 */
411 > l_floor(char *nm)               /* return largest integer not greater than arg1 */
412   {
440    extern double  floor();
441
413      return(floor(argument(1)));
414   }
415  
416  
417   static double
418 < l_ceil()                /* return smallest integer not less than arg1 */
418 > l_ceil(char *nm)                /* return smallest integer not less than arg1 */
419   {
449    extern double  ceil();
450
420      return(ceil(argument(1)));
421   }
422  
423  
455 #ifdef  BIGLIB
424   static double
425 < l_sqrt()
425 > l_sqrt(char *nm)
426   {
459    extern double  sqrt();
460
427      return(sqrt(argument(1)));
428   }
429  
430  
431   static double
432 < l_sin()
432 > l_sin(char *nm)
433   {
468    extern double  sin();
469
434      return(sin(argument(1)));
435   }
436  
437  
438   static double
439 < l_cos()
439 > l_cos(char *nm)
440   {
477    extern double  cos();
478
441      return(cos(argument(1)));
442   }
443  
444  
445   static double
446 < l_tan()
446 > l_tan(char *nm)
447   {
486    extern double  tan();
487
448      return(tan(argument(1)));
449   }
450  
451  
452   static double
453 < l_asin()
453 > l_asin(char *nm)
454   {
495    extern double  asin();
496
455      return(asin(argument(1)));
456   }
457  
458  
459   static double
460 < l_acos()
460 > l_acos(char *nm)
461   {
504    extern double  acos();
505
462      return(acos(argument(1)));
463   }
464  
465  
466   static double
467 < l_atan()
467 > l_atan(char *nm)
468   {
513    extern double  atan();
514
469      return(atan(argument(1)));
470   }
471  
472  
473   static double
474 < l_atan2()
474 > l_atan2(char *nm)
475   {
522    extern double  atan2();
523
476      return(atan2(argument(1), argument(2)));
477   }
478  
479  
480   static double
481 < l_exp()
481 > l_exp(char *nm)
482   {
531    extern double  exp();
532
483      return(exp(argument(1)));
484   }
485  
486  
487   static double
488 < l_log()
488 > l_log(char *nm)
489   {
540    extern double  log();
541
490      return(log(argument(1)));
491   }
492  
493  
494   static double
495 < l_log10()
495 > l_log10(char *nm)
496   {
549    extern double  log10();
550
497      return(log10(argument(1)));
498   }
553 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines