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.4 by greg, Tue Apr 23 15:44:39 1991 UTC vs.
Revision 2.9 by greg, Tue Feb 25 02:47:21 2003 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 "copyright.h"
14 +
15   #include  <stdio.h>
16  
17   #include  <errno.h>
18  
19 + #include  <math.h>
20 +
21   #include  "calcomp.h"
22  
23 <
23 >                                /* bits in argument flag (better be right!) */
24 > #define  AFLAGSIZ       (8*sizeof(unsigned long))
25   #define  ALISTSIZ       6       /* maximum saved argument list */
26  
27   typedef struct activation {
# Line 37 | Line 36 | static ACTIVATION  *curact = NULL;
36  
37   static double  libfunc();
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();
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();
49 #endif
49  
51 #ifdef  BIGLIB
50                          /* functions must be listed alphabetically */
51   static LIBR  library[MAXLIB] = {
52      { "acos", 1, ':', l_acos },
# Line 71 | Line 69 | static LIBR  library[MAXLIB] = {
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;
78   {
79 <    LIBR  *lp;
79 >    register LIBR  *lp;
80      register VARDEF  *vp;
81  
82 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
83 <                || vp->def->v.kid->type != FUNC)
111 <        if ((lp = liblookup(fname)) == NULL)
112 <            return(0);
113 <        else
114 <            return(lp->nargs);
115 <    else
82 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
83 >                && vp->def->v.kid->type == FUNC)
84          return(nekids(vp->def->v.kid) - 1);
85 +    lp = vp != NULL ? vp->lib : liblookup(fname);
86 +    if (lp == NULL)
87 +        return(0);
88 +    return(lp->nargs);
89   }
90  
91  
# Line 130 | Line 102 | double  *a;
102      act.name = fname;
103      act.prev = curact;
104      act.ap = a;
105 <    act.an = (1L<<n)-1;
105 >    if (n >= AFLAGSIZ)
106 >        act.an = ~0;
107 >    else
108 >        act.an = (1L<<n)-1;
109      act.fun = NULL;
110      curact = &act;
111  
# Line 145 | Line 120 | double  *a;
120   }
121  
122  
123 + void
124   funset(fname, nargs, assign, fptr)      /* set a library function */
125   char  *fname;
126   int  nargs;
127   int  assign;
128   double  (*fptr)();
129   {
130 +    int  oldlibsize = libsize;
131 +    char *cp;
132      register LIBR  *lp;
133 <
134 <    if ((lp = liblookup(fname)) == NULL) {
133 >                                                /* check for context */
134 >    for (cp = fname; *cp; cp++)
135 >        ;
136 >    if (cp == fname)
137 >        return;
138 >    if (cp[-1] == CNTXMARK)
139 >        *--cp = '\0';
140 >    if ((lp = liblookup(fname)) == NULL) {      /* insert */
141          if (libsize >= MAXLIB) {
142              eputs("Too many library functons!\n");
143              quit(1);
# Line 168 | Line 152 | double  (*fptr)();
152                  break;
153          libsize++;
154      }
155 <    lp[0].fname = savestr(fname);
156 <    lp[0].nargs = nargs;
157 <    lp[0].atyp = assign;
158 <    lp[0].f = fptr;
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;
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  
# Line 196 | Line 193 | argument(n)                    /* return nth argument for active functi
193   register int  n;
194   {
195      register ACTIVATION  *actp = curact;
196 <    EPNODE  *ep;
196 >    register EPNODE  *ep;
197      double  aval;
198  
199      if (actp == NULL || --n < 0) {
# Line 204 | Line 201 | register int  n;
201          quit(1);
202      }
203                                                  /* already computed? */
204 <    if (1L<<n & actp->an)
204 >    if (n < AFLAGSIZ && 1L<<n & actp->an)
205          return(actp->ap[n]);
206  
207      if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
# Line 223 | Line 220 | register int  n;
220   }
221  
222  
226 #ifdef  VARIABLE
223   VARDEF *
224   argf(n)                         /* return function def for nth argument */
225   int  n;
# Line 268 | Line 264 | int  n;
264   {
265      return(argf(n)->name);
266   }
271 #endif
267  
268  
269   double
# Line 322 | Line 317 | char  *fname;
317   }
318  
319  
325 #ifndef  VARIABLE
326 VARDEF *
327 varinsert(vname)                /* dummy variable insert */
328 char  *vname;
329 {
330    register VARDEF  *vp;
331
332    vp = (VARDEF *)emalloc(sizeof(VARDEF));
333    vp->name = savestr(vname);
334    vp->nlinks = 1;
335    vp->def = NULL;
336    vp->lib = NULL;
337    vp->next = NULL;
338    return(vp);
339 }
340
341
342 varfree(vp)                     /* free dummy variable */
343 register VARDEF  *vp;
344 {
345    freestr(vp->name);
346    efree((char *)vp);
347 }
348 #endif
349
350
351
320   /*
321   *  The following routines are for internal use:
322   */
# Line 357 | Line 325 | register VARDEF  *vp;
325   static double
326   libfunc(fname, vp)                      /* execute library function */
327   char  *fname;
328 < register VARDEF  *vp;
328 > VARDEF  *vp;
329   {
330 <    VARDEF  dumdef;
330 >    register LIBR  *lp;
331      double  d;
332      int  lasterrno;
333  
334 <    if (vp == NULL) {
335 <        vp = &dumdef;
336 <        vp->lib = NULL;
337 <    }
338 <    if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) &&
371 <                                (vp->lib = liblookup(fname)) == NULL) ||
372 <                vp->lib->f == NULL) {
334 >    if (vp != NULL)
335 >        lp = vp->lib;
336 >    else
337 >        lp = liblookup(fname);
338 >    if (lp == NULL) {
339          eputs(fname);
340          eputs(": undefined function\n");
341          quit(1);
342      }
343      lasterrno = errno;
344      errno = 0;
345 <    d = (*vp->lib->f)();
345 >    d = (*lp->f)(lp->fname);
346   #ifdef  IEEE
347 <    if (!finite(d))
348 <        errno = EDOM;
347 >    if (errno == 0)
348 >        if (isnan(d))
349 >            errno = EDOM;
350 >        else if (isinf(d))
351 >            errno = ERANGE;
352   #endif
353      if (errno) {
354          wputs(fname);
355 <        wputs(": bad call\n");
355 >        if (errno == EDOM)
356 >                wputs(": domain error\n");
357 >        else if (errno == ERANGE)
358 >                wputs(": range error\n");
359 >        else
360 >                wputs(": error in call\n");
361          return(0.0);
362      }
363      errno = lasterrno;
# Line 426 | Line 400 | l_select()             /* return argument #(A1+1) */
400   static double
401   l_rand()                /* random function between 0 and 1 */
402   {
429    extern double  floor();
403      double  x;
404  
405      x = argument(1);
# Line 440 | Line 413 | l_rand()               /* random function between 0 and 1 */
413   static double
414   l_floor()               /* return largest integer not greater than arg1 */
415   {
443    extern double  floor();
444
416      return(floor(argument(1)));
417   }
418  
# Line 449 | Line 420 | l_floor()              /* return largest integer not greater than
420   static double
421   l_ceil()                /* return smallest integer not less than arg1 */
422   {
452    extern double  ceil();
453
423      return(ceil(argument(1)));
424   }
425  
426  
458 #ifdef  BIGLIB
427   static double
428   l_sqrt()
429   {
462    extern double  sqrt();
463
430      return(sqrt(argument(1)));
431   }
432  
# Line 468 | Line 434 | l_sqrt()
434   static double
435   l_sin()
436   {
471    extern double  sin();
472
437      return(sin(argument(1)));
438   }
439  
# Line 477 | Line 441 | l_sin()
441   static double
442   l_cos()
443   {
480    extern double  cos();
481
444      return(cos(argument(1)));
445   }
446  
# Line 486 | Line 448 | l_cos()
448   static double
449   l_tan()
450   {
489    extern double  tan();
490
451      return(tan(argument(1)));
452   }
453  
# Line 495 | Line 455 | l_tan()
455   static double
456   l_asin()
457   {
498    extern double  asin();
499
458      return(asin(argument(1)));
459   }
460  
# Line 504 | Line 462 | l_asin()
462   static double
463   l_acos()
464   {
507    extern double  acos();
508
465      return(acos(argument(1)));
466   }
467  
# Line 513 | Line 469 | l_acos()
469   static double
470   l_atan()
471   {
516    extern double  atan();
517
472      return(atan(argument(1)));
473   }
474  
# Line 522 | Line 476 | l_atan()
476   static double
477   l_atan2()
478   {
525    extern double  atan2();
526
479      return(atan2(argument(1), argument(2)));
480   }
481  
# Line 531 | Line 483 | l_atan2()
483   static double
484   l_exp()
485   {
534    extern double  exp();
535
486      return(exp(argument(1)));
487   }
488  
# Line 540 | Line 490 | l_exp()
490   static double
491   l_log()
492   {
543    extern double  log();
544
493      return(log(argument(1)));
494   }
495  
# Line 549 | Line 497 | l_log()
497   static double
498   l_log10()
499   {
552    extern double  log10();
553
500      return(log10(argument(1)));
501   }
556 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines