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 2.2 by greg, Fri May 15 16:38:47 1992 UTC vs.
Revision 2.11 by schorsch, Sat Jun 7 12:50:20 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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  "calcomp.h"
21  
# Line 38 | Line 35 | static ACTIVATION  *curact = NULL;
35  
36   static double  libfunc();
37  
38 + #ifndef  MAXLIB
39   #define  MAXLIB         64      /* maximum number of library functions */
40 + #endif
41  
42   static double  l_if(), l_select(), l_rand();
43   static double  l_floor(), l_ceil();
45 #ifdef  BIGLIB
44   static double  l_sqrt();
45   static double  l_sin(), l_cos(), l_tan();
46   static double  l_asin(), l_acos(), l_atan(), l_atan2();
47   static double  l_exp(), l_log(), l_log10();
50 #endif
48  
52 #ifdef  BIGLIB
49                          /* functions must be listed alphabetically */
50   static LIBR  library[MAXLIB] = {
51      { "acos", 1, ':', l_acos },
# Line 72 | Line 68 | static LIBR  library[MAXLIB] = {
68  
69   static int  libsize = 16;
70  
75 #else
76                        /* functions must be listed alphabetically */
77 static LIBR  library[MAXLIB] = {
78    { "ceil", 1, ':', l_ceil },
79    { "floor", 1, ':', l_floor },
80    { "if", 3, ':', l_if },
81    { "rand", 1, ':', l_rand },
82    { "select", 1, ':', l_select },
83 };
84
85 static int  libsize = 5;
86
87 #endif
88
89 extern char  *savestr(), *emalloc();
90
91 extern VARDEF  *argf();
92
93 #ifdef  VARIABLE
71   #define  resolve(ep)    ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
95 #else
96 #define  resolve(ep)    ((ep)->v.ln)
97 #define varlookup(name) NULL
98 #endif
72  
73  
74   int
75   fundefined(fname)               /* return # of arguments for function */
76   char  *fname;
77   {
78 <    LIBR  *lp;
78 >    register LIBR  *lp;
79      register VARDEF  *vp;
80  
81 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
82 <                || vp->def->v.kid->type != FUNC)
110 <        if ((lp = liblookup(fname)) == NULL)
111 <            return(0);
112 <        else
113 <            return(lp->nargs);
114 <    else
81 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
82 >                && vp->def->v.kid->type == FUNC)
83          return(nekids(vp->def->v.kid) - 1);
84 +    lp = vp != NULL ? vp->lib : liblookup(fname);
85 +    if (lp == NULL)
86 +        return(0);
87 +    return(lp->nargs);
88   }
89  
90  
# Line 147 | Line 119 | double  *a;
119   }
120  
121  
122 + void
123   funset(fname, nargs, assign, fptr)      /* set a library function */
124   char  *fname;
125   int  nargs;
126   int  assign;
127   double  (*fptr)();
128   {
129 +    int  oldlibsize = libsize;
130 +    char *cp;
131      register LIBR  *lp;
132 <
132 >                                                /* check for context */
133 >    for (cp = fname; *cp; cp++)
134 >        ;
135 >    if (cp == fname)
136 >        return;
137 >    if (cp[-1] == CNTXMARK)
138 >        *--cp = '\0';
139      if ((lp = liblookup(fname)) == NULL) {      /* insert */
140          if (libsize >= MAXLIB) {
141              eputs("Too many library functons!\n");
# Line 185 | Line 166 | double  (*fptr)();
166          lp[0].atyp = assign;
167          lp[0].f = fptr;
168      }
169 <    libupdate(fname);                   /* relink library */
169 >    if (libsize != oldlibsize)
170 >        libupdate(fname);                       /* relink library */
171   }
172  
173  
# Line 237 | Line 219 | register int  n;
219   }
220  
221  
240 #ifdef  VARIABLE
222   VARDEF *
223   argf(n)                         /* return function def for nth argument */
224   int  n;
# Line 273 | Line 254 | badarg:
254      eputs(actp->name);
255      eputs(": argument not a function\n");
256      quit(1);
257 +        return NULL; /* pro forma return */
258   }
259  
260  
# Line 282 | Line 264 | int  n;
264   {
265      return(argf(n)->name);
266   }
285 #endif
267  
268  
269   double
# Line 336 | Line 317 | char  *fname;
317   }
318  
319  
339 #ifndef  VARIABLE
340 VARDEF *
341 varinsert(vname)                /* dummy variable insert */
342 char  *vname;
343 {
344    register VARDEF  *vp;
345
346    vp = (VARDEF *)emalloc(sizeof(VARDEF));
347    vp->name = savestr(vname);
348    vp->nlinks = 1;
349    vp->def = NULL;
350    vp->lib = NULL;
351    vp->next = NULL;
352    return(vp);
353 }
354
355
356 varfree(vp)                     /* free dummy variable */
357 register VARDEF  *vp;
358 {
359    freestr(vp->name);
360    efree((char *)vp);
361 }
362 #endif
363
364
365
320   /*
321   *  The following routines are for internal use:
322   */
# Line 371 | 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 = liblookup(fname);
337 <    }
338 <    if (vp->lib == 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)(vp->lib->fname);
345 >    d = (*lp->f)(lp->fname);
346   #ifdef  IEEE
347      if (errno == 0)
348          if (isnan(d))
# Line 396 | Line 350 | register VARDEF  *vp;
350          else if (isinf(d))
351              errno = ERANGE;
352   #endif
353 <    if (errno) {
353 >    if (errno == EDOM || errno == ERANGE) {
354          wputs(fname);
355          if (errno == EDOM)
356                  wputs(": domain error\n");
# Line 432 | Line 386 | l_select()             /* return argument #(A1+1) */
386   {
387          register int  n;
388  
389 <        n = argument(1) + .5;
389 >        n = (int)(argument(1) + .5);
390          if (n == 0)
391                  return(nargum()-1);
392          if (n < 1 || n > nargum()-1) {
# Line 446 | Line 400 | l_select()             /* return argument #(A1+1) */
400   static double
401   l_rand()                /* random function between 0 and 1 */
402   {
449    extern double  floor();
403      double  x;
404  
405      x = argument(1);
# Line 460 | Line 413 | l_rand()               /* random function between 0 and 1 */
413   static double
414   l_floor()               /* return largest integer not greater than arg1 */
415   {
463    extern double  floor();
464
416      return(floor(argument(1)));
417   }
418  
# Line 469 | Line 420 | l_floor()              /* return largest integer not greater than
420   static double
421   l_ceil()                /* return smallest integer not less than arg1 */
422   {
472    extern double  ceil();
473
423      return(ceil(argument(1)));
424   }
425  
426  
478 #ifdef  BIGLIB
427   static double
428   l_sqrt()
429   {
482    extern double  sqrt();
483
430      return(sqrt(argument(1)));
431   }
432  
# Line 488 | Line 434 | l_sqrt()
434   static double
435   l_sin()
436   {
491    extern double  sin();
492
437      return(sin(argument(1)));
438   }
439  
# Line 497 | Line 441 | l_sin()
441   static double
442   l_cos()
443   {
500    extern double  cos();
501
444      return(cos(argument(1)));
445   }
446  
# Line 506 | Line 448 | l_cos()
448   static double
449   l_tan()
450   {
509    extern double  tan();
510
451      return(tan(argument(1)));
452   }
453  
# Line 515 | Line 455 | l_tan()
455   static double
456   l_asin()
457   {
518    extern double  asin();
519
458      return(asin(argument(1)));
459   }
460  
# Line 524 | Line 462 | l_asin()
462   static double
463   l_acos()
464   {
527    extern double  acos();
528
465      return(acos(argument(1)));
466   }
467  
# Line 533 | Line 469 | l_acos()
469   static double
470   l_atan()
471   {
536    extern double  atan();
537
472      return(atan(argument(1)));
473   }
474  
# Line 542 | Line 476 | l_atan()
476   static double
477   l_atan2()
478   {
545    extern double  atan2();
546
479      return(atan2(argument(1), argument(2)));
480   }
481  
# Line 551 | Line 483 | l_atan2()
483   static double
484   l_exp()
485   {
554    extern double  exp();
555
486      return(exp(argument(1)));
487   }
488  
# Line 560 | Line 490 | l_exp()
490   static double
491   l_log()
492   {
563    extern double  log();
564
493      return(log(argument(1)));
494   }
495  
# Line 569 | Line 497 | l_log()
497   static double
498   l_log10()
499   {
572    extern double  log10();
573
500      return(log10(argument(1)));
501   }
576 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines