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.5 by greg, Fri Oct 2 15:58:31 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 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 51 | Line 54 | static double  l_exp(), l_log(), l_log10();
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 130 | Line 131 | double  *a;
131      act.name = fname;
132      act.prev = curact;
133      act.ap = a;
134 <    act.an = (1L<<n)-1;
134 >    if (n >= AFLAGSIZ)
135 >        act.an = ~0;
136 >    else
137 >        act.an = (1L<<n)-1;
138      act.fun = NULL;
139      curact = &act;
140  
# Line 145 | Line 149 | double  *a;
149   }
150  
151  
152 < funset(fname, nargs, fptr)              /* set a library function */
152 > funset(fname, nargs, assign, fptr)      /* set a library function */
153   char  *fname;
154   int  nargs;
155 + int  assign;
156   double  (*fptr)();
157   {
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 = savestr(fname);
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 >    libupdate(fname);                   /* relink library */
191   }
192  
193  
# Line 193 | Line 212 | argument(n)                    /* return nth argument for active functi
212   register int  n;
213   {
214      register ACTIVATION  *actp = curact;
215 <    EPNODE  *ep;
215 >    register EPNODE  *ep;
216      double  aval;
217  
218      if (actp == NULL || --n < 0) {
# Line 201 | Line 220 | register int  n;
220          quit(1);
221      }
222                                                  /* already computed? */
223 <    if (1L<<n & actp->an)
223 >    if (n < AFLAGSIZ && 1L<<n & actp->an)
224          return(actp->ap[n]);
225  
226      if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
# Line 320 | Line 339 | char  *fname;
339  
340  
341   #ifndef  VARIABLE
342 + static VARDEF  *varlist = NULL;         /* our list of dummy variables */
343 +
344 +
345   VARDEF *
346   varinsert(vname)                /* dummy variable insert */
347   char  *vname;
# Line 330 | Line 352 | char  *vname;
352      vp->name = savestr(vname);
353      vp->nlinks = 1;
354      vp->def = NULL;
355 <    vp->lib = NULL;
356 <    vp->next = NULL;
355 >    vp->lib = liblookup(vname);
356 >    vp->next = varlist;
357 >    varlist = vp;
358      return(vp);
359   }
360  
# Line 339 | Line 362 | char  *vname;
362   varfree(vp)                     /* free dummy variable */
363   register VARDEF  *vp;
364   {
365 +    register VARDEF  *vp2;
366 +
367 +    if (vp == varlist)
368 +        varlist = vp->next;
369 +    else {
370 +        for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next)
371 +                ;
372 +        vp2->next = vp->next;
373 +    }
374      freestr(vp->name);
375      efree((char *)vp);
376   }
377 +
378 +
379 + libupdate(nm)                   /* update library */
380 + char  *nm;
381 + {
382 +    register VARDEF  *vp;
383 +
384 +    for (vp = varlist; vp != NULL; vp = vp->next)
385 +        vp->lib = liblookup(vp->name);
386 + }
387   #endif
388  
389  
# Line 354 | Line 396 | register VARDEF  *vp;
396   static double
397   libfunc(fname, vp)                      /* execute library function */
398   char  *fname;
399 < register VARDEF  *vp;
399 > VARDEF  *vp;
400   {
401 <    VARDEF  dumdef;
401 >    register LIBR  *lp;
402      double  d;
403      int  lasterrno;
404  
405 <    if (vp == NULL) {
406 <        vp = &dumdef;
407 <        vp->lib = NULL;
408 <    }
409 <    if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) &&
368 <                                (vp->lib = liblookup(fname)) == NULL) ||
369 <                vp->lib->f == NULL) {
405 >    if (vp != NULL)
406 >        lp = vp->lib;
407 >    else
408 >        lp = liblookup(fname);
409 >    if (lp == NULL) {
410          eputs(fname);
411          eputs(": undefined function\n");
412          quit(1);
413      }
414      lasterrno = errno;
415      errno = 0;
416 <    d = (*vp->lib->f)();
416 >    d = (*lp->f)(lp->fname);
417   #ifdef  IEEE
418 <    if (!finite(d))
419 <        errno = EDOM;
418 >    if (errno == 0)
419 >        if (isnan(d))
420 >            errno = EDOM;
421 >        else if (isinf(d))
422 >            errno = ERANGE;
423   #endif
424      if (errno) {
425          wputs(fname);
426 <        wputs(": bad call\n");
426 >        if (errno == EDOM)
427 >                wputs(": domain error\n");
428 >        else if (errno == ERANGE)
429 >                wputs(": range error\n");
430 >        else
431 >                wputs(": error in call\n");
432          return(0.0);
433      }
434      errno = lasterrno;
# Line 423 | Line 471 | l_select()             /* return argument #(A1+1) */
471   static double
472   l_rand()                /* random function between 0 and 1 */
473   {
426    extern double  floor();
474      double  x;
475  
476      x = argument(1);
# Line 437 | Line 484 | l_rand()               /* random function between 0 and 1 */
484   static double
485   l_floor()               /* return largest integer not greater than arg1 */
486   {
440    extern double  floor();
441
487      return(floor(argument(1)));
488   }
489  
# Line 446 | Line 491 | l_floor()              /* return largest integer not greater than
491   static double
492   l_ceil()                /* return smallest integer not less than arg1 */
493   {
449    extern double  ceil();
450
494      return(ceil(argument(1)));
495   }
496  
# Line 456 | Line 499 | l_ceil()               /* return smallest integer not less than arg
499   static double
500   l_sqrt()
501   {
459    extern double  sqrt();
460
502      return(sqrt(argument(1)));
503   }
504  
# Line 465 | Line 506 | l_sqrt()
506   static double
507   l_sin()
508   {
468    extern double  sin();
469
509      return(sin(argument(1)));
510   }
511  
# Line 474 | Line 513 | l_sin()
513   static double
514   l_cos()
515   {
477    extern double  cos();
478
516      return(cos(argument(1)));
517   }
518  
# Line 483 | Line 520 | l_cos()
520   static double
521   l_tan()
522   {
486    extern double  tan();
487
523      return(tan(argument(1)));
524   }
525  
# Line 492 | Line 527 | l_tan()
527   static double
528   l_asin()
529   {
495    extern double  asin();
496
530      return(asin(argument(1)));
531   }
532  
# Line 501 | Line 534 | l_asin()
534   static double
535   l_acos()
536   {
504    extern double  acos();
505
537      return(acos(argument(1)));
538   }
539  
# Line 510 | Line 541 | l_acos()
541   static double
542   l_atan()
543   {
513    extern double  atan();
514
544      return(atan(argument(1)));
545   }
546  
# Line 519 | Line 548 | l_atan()
548   static double
549   l_atan2()
550   {
522    extern double  atan2();
523
551      return(atan2(argument(1), argument(2)));
552   }
553  
# Line 528 | Line 555 | l_atan2()
555   static double
556   l_exp()
557   {
531    extern double  exp();
532
558      return(exp(argument(1)));
559   }
560  
# Line 537 | Line 562 | l_exp()
562   static double
563   l_log()
564   {
540    extern double  log();
541
565      return(log(argument(1)));
566   }
567  
# Line 546 | Line 569 | l_log()
569   static double
570   l_log10()
571   {
549    extern double  log10();
550
572      return(log10(argument(1)));
573   }
574   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines