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.2 by greg, Tue Jun 26 09:15:13 1990 UTC vs.
Revision 2.29 by greg, Sun Feb 25 18:36:27 2024 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       10      /* 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
44 <                        /* functions must be listed alphabetically */
45 < static LIBR  library[MAXLIB] = {
46 <    { "acos", 1, l_acos },
47 <    { "asin", 1, l_asin },
48 <    { "atan", 1, l_atan },
49 <    { "atan2", 2, l_atan2 },
50 <    { "ceil", 1, l_ceil },
59 <    { "cos", 1, l_cos },
60 <    { "exp", 1, l_exp },
61 <    { "floor", 1, l_floor },
62 <    { "if", 3, l_if },
63 <    { "log", 1, l_log },
64 <    { "log10", 1, l_log10 },
65 <    { "rand", 1, l_rand },
66 <    { "select", 1, l_select },
67 <    { "sin", 1, l_sin },
68 <    { "sqrt", 1, l_sqrt },
69 <    { "tan", 1, l_tan },
70 < };
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 *);
49 > static double  l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *);
50 > static double  l_exp(char *), l_log(char *), l_log10(char *);
51  
72 static int  libsize = 16;
73
74 #else
52                          /* functions must be listed alphabetically */
53   static LIBR  library[MAXLIB] = {
54 <    { "ceil", 1, l_ceil },
55 <    { "floor", 1, l_floor },
56 <    { "if", 3, l_if },
57 <    { "rand", 1, l_rand },
58 <    { "select", 1, l_select },
54 >    { "acos", 1, ':', l_acos },
55 >    { "asin", 1, ':', l_asin },
56 >    { "atan", 1, ':', l_atan },
57 >    { "atan2", 2, ':', l_atan2 },
58 >    { "ceil", 1, ':', l_ceil },
59 >    { "cos", 1, ':', l_cos },
60 >    { "exp", 1, ':', l_exp },
61 >    { "floor", 1, ':', l_floor },
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 },
70 >    { "sqrt", 1, ':', l_sqrt },
71 >    { "tan", 1, ':', l_tan },
72   };
73  
74 < static int  libsize = 5;
74 > static int  libsize = 18;
75  
86 #endif
87
88 extern char  *savestr(), *emalloc();
89
90 extern LIBR  *liblookup();
91
92 extern VARDEF  *argf();
93
94 #ifdef  VARIABLE
76   #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
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      LIBR  *lp;
85 <    register VARDEF  *vp;
85 >    VARDEF  *vp;
86  
87 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
88 <                || vp->def->v.kid->type != FUNC)
111 <        if ((lp = liblookup(fname)) == NULL)
112 <            return(0);
113 <        else
114 <            return(lp->nargs);
115 <    else
87 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
88 >                && vp->def->v.kid->type == FUNC)
89          return(nekids(vp->def->v.kid) - 1);
90 +    lp = vp != NULL ? vp->lib : liblookup(fname);
91 +    if (lp == NULL)
92 +        return(0);
93 +    return(lp->nargs);
94   }
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 <    act.an = (1L<<n)-1;
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 145 | Line 129 | double  *a;
129   }
130  
131  
132 < funset(fname, nargs, fptr)              /* set a library function */
133 < char  *fname;
134 < int  nargs;
135 < double  (*fptr)();
132 > void
133 > funset(                         /* set a library function */
134 >        char  *fname,
135 >        int  nargs,
136 >        int  assign,
137 >        double  (*fptr)(char *)
138 > )
139   {
140 <    register LIBR  *lp;
141 <
142 <    if ((lp = liblookup(fname)) == NULL) {
140 >    int  oldlibsize = libsize;
141 >    char *cp;
142 >    LIBR  *lp;
143 >                                                /* check for context */
144 >    for (cp = fname; *cp; cp++)
145 >        ;
146 >    if (cp == fname)
147 >        return;
148 >    while (cp[-1] == CNTXMARK) {
149 >        *--cp = '\0';
150 >        if (cp == fname) return;
151 >    }
152 >    if ((lp = liblookup(fname)) == NULL) {      /* insert */
153 >        if (fptr == NULL)
154 >                return;                         /* nothing! */
155          if (libsize >= MAXLIB) {
156              eputs("Too many library functons!\n");
157              quit(1);
158          }
159          for (lp = &library[libsize]; lp > library; lp--)
160 <            if (strcmp(lp[-1].fname, fname) > 0) {
161 <                lp[0].fname = lp[-1].fname;
162 <                lp[0].nargs = lp[-1].nargs;
164 <                lp[0].f = lp[-1].f;
165 <            } else
160 >            if (strcmp(lp[-1].fname, fname) > 0)
161 >                lp[0] = lp[-1];
162 >            else
163                  break;
164          libsize++;
165      }
166 <    lp[0].fname = fname;                /* string must be static! */
167 <    lp[0].nargs = nargs;
168 <    lp[0].f = fptr;
166 >    if (fptr == NULL) {                         /* delete */
167 >        while (lp < &library[libsize-1]) {
168 >            lp[0] = lp[1];
169 >            lp++;
170 >        }
171 >        libsize--;
172 >    } else {                                    /* or assign */
173 >        lp[0].fname = fname;            /* string must be static! */
174 >        lp[0].nargs = nargs;
175 >        lp[0].atyp = assign;
176 >        lp[0].f = fptr;
177 >    }
178 >    if (libsize != oldlibsize)
179 >        libupdate(fname);                       /* relink library */
180   }
181  
182  
183   int
184 < nargum()                        /* return number of available arguments */
184 > nargum(void)                    /* return number of available arguments */
185   {
186 <    register int  n;
186 >    int  n;
187  
188      if (curact == NULL)
189          return(0);
# Line 189 | Line 197 | nargum()                       /* return number of available arguments */
197  
198  
199   double
200 < argument(n)                     /* return nth argument for active function */
193 < register int  n;
200 > argument(int n)                 /* return nth argument for active function */
201   {
202 <    register ACTIVATION  *actp = curact;
203 <    EPNODE  *ep;
202 >    ACTIVATION  *actp = curact;
203 >    EPNODE  *ep = NULL;
204      double  aval;
205  
206 <    if (actp == NULL || --n < 0) {
206 >    if (!actp | (--n < 0)) {
207          eputs("Bad call to argument!\n");
208          quit(1);
209      }
210 <                                                /* already computed? */
204 <    if (1L<<n & actp->an)
210 >    if ((n < AFLAGSIZ) & actp->an >> n)         /* already computed? */
211          return(actp->ap[n]);
212  
213 <    if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
213 >    if (!actp->fun || !(ep = ekid(actp->fun, n+1))) {
214          eputs(actp->name);
215          eputs(": too few arguments\n");
216          quit(1);
217      }
218 <    curact = actp->prev;                        /* pop environment */
218 >    curact = actp->prev;                        /* previous context */
219      aval = evalue(ep);                          /* compute argument */
220 <    curact = actp;                              /* push back environment */
221 <    if (n < ALISTSIZ) {                         /* save value */
220 >    curact = actp;                              /* put back calling context */
221 >    if (n < ALISTSIZ) {                         /* save value if room */
222          actp->ap[n] = aval;
223          actp->an |= 1L<<n;
224      }
# Line 220 | Line 226 | register int  n;
226   }
227  
228  
223 #ifdef  VARIABLE
229   VARDEF *
230 < argf(n)                         /* return function def for nth argument */
226 < int  n;
230 > argf(int n)                     /* return function def for nth argument */
231   {
232 <    register ACTIVATION  *actp;
233 <    register EPNODE  *ep;
232 >    ACTIVATION  *actp;
233 >    EPNODE  *ep;
234  
235      for (actp = curact; actp != NULL; actp = actp->prev) {
236  
# Line 256 | Line 260 | badarg:
260      eputs(actp->name);
261      eputs(": argument not a function\n");
262      quit(1);
263 +        return NULL; /* pro forma return */
264   }
265  
266  
267   char *
268 < argfun(n)                       /* return function name for nth argument */
264 < int  n;
268 > argfun(int n)                   /* return function name for nth argument */
269   {
270      return(argf(n)->name);
271   }
268 #endif
272  
273  
274   double
275 < efunc(ep)                               /* evaluate a function */
273 < register EPNODE  *ep;
275 > efunc(EPNODE *ep)                       /* evaluate a function */
276   {
277      ACTIVATION  act;
278      double  alist[ALISTSIZ];
279      double  rval;
280 <    register VARDEF  *dp;
280 >    VARDEF  *dp;
281                                          /* push environment */
282      dp = resolve(ep->v.kid);
283      act.name = dp->name;
# Line 296 | Line 298 | register EPNODE  *ep;
298  
299  
300   LIBR *
301 < liblookup(fname)                /* look up a library function */
300 < char  *fname;
301 > liblookup(char *fname)          /* look up a library function */
302   {
303      int  upper, lower;
304 <    register int  cm, i;
304 >    int  cm, i;
305  
306      lower = 0;
307      upper = cm = libsize;
# Line 319 | Line 320 | char  *fname;
320   }
321  
322  
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
323   /*
324   *  The following routines are for internal use:
325   */
326  
327  
328   static double
329 < libfunc(fname, vp)                      /* execute library function */
330 < char  *fname;
331 < register VARDEF  *vp;
329 > libfunc(                                /* execute library function */
330 >        char  *fname,
331 >        VARDEF  *vp
332 > )
333   {
334 <    VARDEF  dumdef;
334 >    LIBR  *lp;
335      double  d;
336      int  lasterrno;
337  
338 <    if (vp == NULL) {
339 <        vp = &dumdef;
340 <        vp->lib = NULL;
341 <    }
342 <    if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) &&
368 <                                (vp->lib = liblookup(fname)) == NULL) ||
369 <                vp->lib->f == NULL) {
338 >    if (vp != NULL)
339 >        lp = vp->lib;
340 >    else
341 >        lp = liblookup(fname);
342 >    if (lp == NULL) {
343          eputs(fname);
344          eputs(": undefined function\n");
345          quit(1);
346      }
347      lasterrno = errno;
348      errno = 0;
349 <    d = (*vp->lib->f)();
350 < #ifdef  IEEE
351 <    if (!finite(d))
352 <        errno = EDOM;
349 >    d = (*lp->f)(lp->fname);
350 > #ifdef  isnan
351 >    if (errno == 0) {
352 >        if (isnan(d))
353 >            errno = EDOM;
354 >        else if (isinf(d))
355 >            errno = ERANGE;
356 >    }
357   #endif
358 <    if (errno) {
358 >    if ((errno == EDOM) | (errno == ERANGE)) {
359          wputs(fname);
360 <        wputs(": bad call\n");
360 >        if (errno == EDOM)
361 >                wputs(": domain error\n");
362 >        else if (errno == ERANGE)
363 >                wputs(": range error\n");
364 >        else
365 >                wputs(": error in call\n");
366          return(0.0);
367      }
368      errno = lasterrno;
# Line 394 | Line 376 | register VARDEF  *vp;
376  
377  
378   static double
379 < l_if()                  /* if(cond, then, else) conditional expression */
379 > l_if(char *nm)          /* if(cond, then, else) conditional expression */
380                          /* cond evaluates true if greater than zero */
381   {
382      if (argument(1) > 0.0)
# Line 405 | Line 387 | l_if()                 /* if(cond, then, else) conditional expressio
387  
388  
389   static double
390 < l_select()              /* return argument #(A1+1) */
390 > l_select(char *nm)      /* return argument #(A1+1) */
391   {
392 <        register int  n;
392 >        int     narg = nargum();
393 >        double  a1 = argument(1);
394 >        int  n = (int)(a1 + .5);
395  
396 <        n = argument(1) + .5;
413 <        if (n == 0)
414 <                return(nargum()-1);
415 <        if (n < 1 || n > nargum()-1) {
396 >        if ((a1 < -.5) | (n >= narg)) {
397                  errno = EDOM;
398                  return(0.0);
399          }
400 +        if (!n)         /* asking max index? */
401 +                return(narg-1);
402          return(argument(n+1));
403   }
404  
405  
406   static double
407 < l_rand()                /* random function between 0 and 1 */
407 > l_max(char *nm)         /* general maximum function */
408   {
409 <    extern double  floor();
409 >        int  n = nargum();
410 >        double  vmax = argument(1);
411 >
412 >        while (n > 1) {
413 >                double  v = argument(n--);
414 >                if (vmax < v)
415 >                        vmax = v;
416 >        }
417 >        return(vmax);
418 > }
419 >
420 >
421 > static double
422 > l_min(char *nm)         /* general minimum function */
423 > {
424 >        int  n = nargum();
425 >        double  vmin = argument(1);
426 >
427 >        while (n > 1) {
428 >                double  v = argument(n--);
429 >                if (vmin > v)
430 >                        vmin = v;
431 >        }
432 >        return(vmin);
433 > }
434 >
435 >
436 > static double
437 > l_rand(char *nm)                /* random function between 0 and 1 */
438 > {
439      double  x;
440  
441      x = argument(1);
# Line 435 | Line 447 | l_rand()               /* random function between 0 and 1 */
447  
448  
449   static double
450 < l_floor()               /* return largest integer not greater than arg1 */
450 > l_floor(char *nm)               /* return largest integer not greater than arg1 */
451   {
440    extern double  floor();
441
452      return(floor(argument(1)));
453   }
454  
455  
456   static double
457 < l_ceil()                /* return smallest integer not less than arg1 */
457 > l_ceil(char *nm)                /* return smallest integer not less than arg1 */
458   {
449    extern double  ceil();
450
459      return(ceil(argument(1)));
460   }
461  
462  
455 #ifdef  BIGLIB
463   static double
464 < l_sqrt()
464 > l_sqrt(char *nm)
465   {
459    extern double  sqrt();
460
466      return(sqrt(argument(1)));
467   }
468  
469  
470   static double
471 < l_sin()
471 > l_sin(char *nm)
472   {
468    extern double  sin();
469
473      return(sin(argument(1)));
474   }
475  
476  
477   static double
478 < l_cos()
478 > l_cos(char *nm)
479   {
477    extern double  cos();
478
480      return(cos(argument(1)));
481   }
482  
483  
484   static double
485 < l_tan()
485 > l_tan(char *nm)
486   {
486    extern double  tan();
487
487      return(tan(argument(1)));
488   }
489  
490  
491   static double
492 < l_asin()
492 > l_asin(char *nm)
493   {
495    extern double  asin();
496
494      return(asin(argument(1)));
495   }
496  
497  
498   static double
499 < l_acos()
499 > l_acos(char *nm)
500   {
504    extern double  acos();
505
501      return(acos(argument(1)));
502   }
503  
504  
505   static double
506 < l_atan()
506 > l_atan(char *nm)
507   {
513    extern double  atan();
514
508      return(atan(argument(1)));
509   }
510  
511  
512   static double
513 < l_atan2()
513 > l_atan2(char *nm)
514   {
522    extern double  atan2();
523
515      return(atan2(argument(1), argument(2)));
516   }
517  
518  
519   static double
520 < l_exp()
520 > l_exp(char *nm)
521   {
531    extern double  exp();
532
522      return(exp(argument(1)));
523   }
524  
525  
526   static double
527 < l_log()
527 > l_log(char *nm)
528   {
540    extern double  log();
541
529      return(log(argument(1)));
530   }
531  
532  
533   static double
534 < l_log10()
534 > l_log10(char *nm)
535   {
549    extern double  log10();
550
536      return(log10(argument(1)));
537   }
553 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines