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.1 by greg, Thu Feb 2 10:34:28 1989 UTC vs.
Revision 2.7 by greg, Sun Nov 22 17:29:46 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20  
21   #include  <errno.h>
22  
23 + #include  <math.h>
24 +
25   #include  "calcomp.h"
26  
27 <
27 >                                /* bits in argument flag (better be right!) */
28 > #define  AFLAGSIZ       (8*sizeof(unsigned long))
29   #define  ALISTSIZ       6       /* maximum saved argument list */
30  
31   typedef struct activation {
# Line 35 | Line 38 | typedef struct activation {
38  
39   static ACTIVATION  *curact = NULL;
40  
41 < extern double  libfunc();
41 > static double  libfunc();
42  
43   #define  MAXLIB         64      /* maximum number of library functions */
44  
45 < extern double  l_if(), l_select(), l_rand();
46 < extern double  l_floor(), l_ceil();
45 > static double  l_if(), l_select(), l_rand();
46 > static double  l_floor(), l_ceil();
47   #ifdef  BIGLIB
48 < extern double  l_sqrt();
49 < extern double  l_sin(), l_cos(), l_tan();
50 < extern double  l_asin(), l_acos(), l_atan(), l_atan2();
51 < extern double  l_exp(), l_log(), l_log10();
48 > static double  l_sqrt();
49 > static double  l_sin(), l_cos(), l_tan();
50 > static double  l_asin(), l_acos(), l_atan(), l_atan2();
51 > static double  l_exp(), l_log(), l_log10();
52   #endif
53  
54   #ifdef  BIGLIB
55                          /* functions must be listed alphabetically */
56   static LIBR  library[MAXLIB] = {
57 <    { "acos", 1, l_acos },
58 <    { "asin", 1, l_asin },
59 <    { "atan", 1, l_atan },
60 <    { "atan2", 2, l_atan2 },
61 <    { "ceil", 1, l_ceil },
62 <    { "cos", 1, l_cos },
63 <    { "exp", 1, l_exp },
64 <    { "floor", 1, l_floor },
65 <    { "if", 3, l_if },
66 <    { "log", 1, l_log },
67 <    { "log10", 1, l_log10 },
68 <    { "rand", 1, l_rand },
69 <    { "select", 1, l_select },
70 <    { "sin", 1, l_sin },
71 <    { "sqrt", 1, l_sqrt },
72 <    { "tan", 1, l_tan },
57 >    { "acos", 1, ':', l_acos },
58 >    { "asin", 1, ':', l_asin },
59 >    { "atan", 1, ':', l_atan },
60 >    { "atan2", 2, ':', l_atan2 },
61 >    { "ceil", 1, ':', l_ceil },
62 >    { "cos", 1, ':', l_cos },
63 >    { "exp", 1, ':', l_exp },
64 >    { "floor", 1, ':', l_floor },
65 >    { "if", 3, ':', l_if },
66 >    { "log", 1, ':', l_log },
67 >    { "log10", 1, ':', l_log10 },
68 >    { "rand", 1, ':', l_rand },
69 >    { "select", 1, ':', l_select },
70 >    { "sin", 1, ':', l_sin },
71 >    { "sqrt", 1, ':', l_sqrt },
72 >    { "tan", 1, ':', l_tan },
73   };
74  
75   static int  libsize = 16;
# Line 74 | Line 77 | static int  libsize = 16;
77   #else
78                          /* functions must be listed alphabetically */
79   static LIBR  library[MAXLIB] = {
80 <    { "ceil", 1, l_ceil },
81 <    { "floor", 1, l_floor },
82 <    { "if", 3, l_if },
83 <    { "rand", 1, l_rand },
84 <    { "select", 1, l_select },
80 >    { "ceil", 1, ':', l_ceil },
81 >    { "floor", 1, ':', l_floor },
82 >    { "if", 3, ':', l_if },
83 >    { "rand", 1, ':', l_rand },
84 >    { "select", 1, ':', l_select },
85   };
86  
87   static int  libsize = 5;
# Line 87 | Line 90 | static int  libsize = 5;
90  
91   extern char  *savestr(), *emalloc();
92  
90 extern LIBR  *liblookup();
91
93   extern VARDEF  *argf();
94  
95   #ifdef  VARIABLE
# Line 103 | Line 104 | int
104   fundefined(fname)               /* return # of arguments for function */
105   char  *fname;
106   {
107 <    LIBR  *lp;
107 >    register LIBR  *lp;
108      register VARDEF  *vp;
109  
110 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
111 <                || vp->def->v.kid->type != FUNC)
111 <        if ((lp = liblookup(fname)) == NULL)
112 <            return(0);
113 <        else
114 <            return(lp->nargs);
115 <    else
110 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
111 >                && vp->def->v.kid->type == FUNC)
112          return(nekids(vp->def->v.kid) - 1);
113 +    lp = vp != NULL ? vp->lib : liblookup(fname);
114 +    if (lp == NULL)
115 +        return(0);
116 +    return(lp->nargs);
117   }
118  
119  
# Line 130 | Line 130 | double  *a;
130      act.name = fname;
131      act.prev = curact;
132      act.ap = a;
133 <    act.an = (1L<<n)-1;
133 >    if (n >= AFLAGSIZ)
134 >        act.an = ~0;
135 >    else
136 >        act.an = (1L<<n)-1;
137      act.fun = NULL;
138      curact = &act;
139  
# Line 145 | Line 148 | double  *a;
148   }
149  
150  
151 < funset(fname, nargs, fptr)              /* set a library function */
151 > funset(fname, nargs, assign, fptr)      /* set a library function */
152   char  *fname;
153   int  nargs;
154 + int  assign;
155   double  (*fptr)();
156   {
157 +    int  oldlibsize = libsize;
158      register LIBR  *lp;
159  
160 <    if ((lp = liblookup(fname)) == NULL) {
160 >    if ((lp = liblookup(fname)) == NULL) {      /* insert */
161          if (libsize >= MAXLIB) {
162              eputs("Too many library functons!\n");
163              quit(1);
# Line 161 | Line 166 | double  (*fptr)();
166              if (strcmp(lp[-1].fname, fname) > 0) {
167                  lp[0].fname = lp[-1].fname;
168                  lp[0].nargs = lp[-1].nargs;
169 +                lp[0].atyp = lp[-1].atyp;
170                  lp[0].f = lp[-1].f;
171              } else
172                  break;
173          libsize++;
174      }
175 <    lp[0].fname = fname;                /* string must be static! */
176 <    lp[0].nargs = nargs;
177 <    lp[0].f = fptr;
175 >    if (fptr == NULL) {                         /* delete */
176 >        while (lp < &library[libsize-1]) {
177 >            lp[0].fname = lp[1].fname;
178 >            lp[0].nargs = lp[1].nargs;
179 >            lp[0].atyp = lp[1].atyp;
180 >            lp[0].f = lp[1].f;
181 >            lp++;
182 >        }
183 >        libsize--;
184 >    } else {                                    /* or assign */
185 >        lp[0].fname = fname;            /* string must be static! */
186 >        lp[0].nargs = nargs;
187 >        lp[0].atyp = assign;
188 >        lp[0].f = fptr;
189 >    }
190 >    if (libsize != oldlibsize)
191 >        libupdate(fname);                       /* relink library */
192   }
193  
194  
# Line 193 | Line 213 | argument(n)                    /* return nth argument for active functi
213   register int  n;
214   {
215      register ACTIVATION  *actp = curact;
216 <    EPNODE  *ep;
216 >    register EPNODE  *ep;
217      double  aval;
218  
219      if (actp == NULL || --n < 0) {
# Line 201 | Line 221 | register int  n;
221          quit(1);
222      }
223                                                  /* already computed? */
224 <    if (1L<<n & actp->an)
224 >    if (n < AFLAGSIZ && 1L<<n & actp->an)
225          return(actp->ap[n]);
226  
227      if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
# Line 320 | Line 340 | char  *fname;
340  
341  
342   #ifndef  VARIABLE
343 + static VARDEF  *varlist = NULL;         /* our list of dummy variables */
344 +
345 +
346   VARDEF *
347   varinsert(vname)                /* dummy variable insert */
348   char  *vname;
# Line 330 | Line 353 | char  *vname;
353      vp->name = savestr(vname);
354      vp->nlinks = 1;
355      vp->def = NULL;
356 <    vp->lib = NULL;
357 <    vp->next = NULL;
356 >    vp->lib = liblookup(vname);
357 >    vp->next = varlist;
358 >    varlist = vp;
359      return(vp);
360   }
361  
# Line 339 | Line 363 | char  *vname;
363   varfree(vp)                     /* free dummy variable */
364   register VARDEF  *vp;
365   {
366 +    register VARDEF  *vp2;
367 +
368 +    if (vp == varlist)
369 +        varlist = vp->next;
370 +    else {
371 +        for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next)
372 +                ;
373 +        vp2->next = vp->next;
374 +    }
375      freestr(vp->name);
376      efree((char *)vp);
377   }
378 +
379 +
380 + libupdate(nm)                   /* update library */
381 + char  *nm;
382 + {
383 +    register VARDEF  *vp;
384 +
385 +    for (vp = varlist; vp != NULL; vp = vp->next)
386 +        vp->lib = liblookup(vp->name);
387 + }
388   #endif
389  
390  
# Line 354 | Line 397 | register VARDEF  *vp;
397   static double
398   libfunc(fname, vp)                      /* execute library function */
399   char  *fname;
400 < register VARDEF  *vp;
400 > VARDEF  *vp;
401   {
402 <    VARDEF  dumdef;
402 >    register LIBR  *lp;
403      double  d;
404      int  lasterrno;
405  
406 <    if (vp == NULL) {
407 <        vp = &dumdef;
408 <        vp->lib = NULL;
409 <    }
410 <    if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) &&
368 <                                (vp->lib = liblookup(fname)) == NULL) ||
369 <                vp->lib->f == NULL) {
406 >    if (vp != NULL)
407 >        lp = vp->lib;
408 >    else
409 >        lp = liblookup(fname);
410 >    if (lp == NULL) {
411          eputs(fname);
412          eputs(": undefined function\n");
413          quit(1);
414      }
415      lasterrno = errno;
416      errno = 0;
417 <    d = (*vp->lib->f)();
417 >    d = (*lp->f)(lp->fname);
418   #ifdef  IEEE
419 <    if (!finite(d))
420 <        errno = EDOM;
419 >    if (errno == 0)
420 >        if (isnan(d))
421 >            errno = EDOM;
422 >        else if (isinf(d))
423 >            errno = ERANGE;
424   #endif
425      if (errno) {
426          wputs(fname);
427 <        wputs(": bad call\n");
427 >        if (errno == EDOM)
428 >                wputs(": domain error\n");
429 >        else if (errno == ERANGE)
430 >                wputs(": range error\n");
431 >        else
432 >                wputs(": error in call\n");
433          return(0.0);
434      }
435      errno = lasterrno;
# Line 423 | Line 472 | l_select()             /* return argument #(A1+1) */
472   static double
473   l_rand()                /* random function between 0 and 1 */
474   {
426    extern double  floor();
475      double  x;
476  
477      x = argument(1);
# Line 437 | Line 485 | l_rand()               /* random function between 0 and 1 */
485   static double
486   l_floor()               /* return largest integer not greater than arg1 */
487   {
440    extern double  floor();
441
488      return(floor(argument(1)));
489   }
490  
# Line 446 | Line 492 | l_floor()              /* return largest integer not greater than
492   static double
493   l_ceil()                /* return smallest integer not less than arg1 */
494   {
449    extern double  ceil();
450
495      return(ceil(argument(1)));
496   }
497  
# Line 456 | Line 500 | l_ceil()               /* return smallest integer not less than arg
500   static double
501   l_sqrt()
502   {
459    extern double  sqrt();
460
503      return(sqrt(argument(1)));
504   }
505  
# Line 465 | Line 507 | l_sqrt()
507   static double
508   l_sin()
509   {
468    extern double  sin();
469
510      return(sin(argument(1)));
511   }
512  
# Line 474 | Line 514 | l_sin()
514   static double
515   l_cos()
516   {
477    extern double  cos();
478
517      return(cos(argument(1)));
518   }
519  
# Line 483 | Line 521 | l_cos()
521   static double
522   l_tan()
523   {
486    extern double  tan();
487
524      return(tan(argument(1)));
525   }
526  
# Line 492 | Line 528 | l_tan()
528   static double
529   l_asin()
530   {
495    extern double  asin();
496
531      return(asin(argument(1)));
532   }
533  
# Line 501 | Line 535 | l_asin()
535   static double
536   l_acos()
537   {
504    extern double  acos();
505
538      return(acos(argument(1)));
539   }
540  
# Line 510 | Line 542 | l_acos()
542   static double
543   l_atan()
544   {
513    extern double  atan();
514
545      return(atan(argument(1)));
546   }
547  
# Line 519 | Line 549 | l_atan()
549   static double
550   l_atan2()
551   {
522    extern double  atan2();
523
552      return(atan2(argument(1), argument(2)));
553   }
554  
# Line 528 | Line 556 | l_atan2()
556   static double
557   l_exp()
558   {
531    extern double  exp();
532
559      return(exp(argument(1)));
560   }
561  
# Line 537 | Line 563 | l_exp()
563   static double
564   l_log()
565   {
540    extern double  log();
541
566      return(log(argument(1)));
567   }
568  
# Line 546 | Line 570 | l_log()
570   static double
571   l_log10()
572   {
549    extern double  log10();
550
573      return(log10(argument(1)));
574   }
575   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines