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.7 by greg, Fri Jun 28 09:31:59 1991 UTC vs.
Revision 2.8 by greg, Sat Feb 22 02:07:21 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 + /* ====================================================================
14 + * The Radiance Software License, Version 1.0
15 + *
16 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
17 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
18 + *
19 + * Redistribution and use in source and binary forms, with or without
20 + * modification, are permitted provided that the following conditions
21 + * are met:
22 + *
23 + * 1. Redistributions of source code must retain the above copyright
24 + *         notice, this list of conditions and the following disclaimer.
25 + *
26 + * 2. Redistributions in binary form must reproduce the above copyright
27 + *       notice, this list of conditions and the following disclaimer in
28 + *       the documentation and/or other materials provided with the
29 + *       distribution.
30 + *
31 + * 3. The end-user documentation included with the redistribution,
32 + *           if any, must include the following acknowledgment:
33 + *             "This product includes Radiance software
34 + *                 (http://radsite.lbl.gov/)
35 + *                 developed by the Lawrence Berkeley National Laboratory
36 + *               (http://www.lbl.gov/)."
37 + *       Alternately, this acknowledgment may appear in the software itself,
38 + *       if and wherever such third-party acknowledgments normally appear.
39 + *
40 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
41 + *       and "The Regents of the University of California" must
42 + *       not be used to endorse or promote products derived from this
43 + *       software without prior written permission. For written
44 + *       permission, please contact [email protected].
45 + *
46 + * 5. Products derived from this software may not be called "Radiance",
47 + *       nor may "Radiance" appear in their name, without prior written
48 + *       permission of Lawrence Berkeley National Laboratory.
49 + *
50 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
51 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
54 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
57 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
60 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 + * SUCH DAMAGE.
62 + * ====================================================================
63 + *
64 + * This software consists of voluntary contributions made by many
65 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
66 + * information on Lawrence Berkeley National Laboratory, please see
67 + * <http://www.lbl.gov/>.
68 + */
69 +
70   #include  <stdio.h>
71  
72   #include  <errno.h>
73  
74 + #include  <math.h>
75 +
76   #include  "calcomp.h"
77  
78                                  /* bits in argument flag (better be right!) */
# Line 38 | Line 91 | static ACTIVATION  *curact = NULL;
91  
92   static double  libfunc();
93  
94 + #ifndef  MAXLIB
95   #define  MAXLIB         64      /* maximum number of library functions */
96 + #endif
97  
98   static double  l_if(), l_select(), l_rand();
99   static double  l_floor(), l_ceil();
45 #ifdef  BIGLIB
100   static double  l_sqrt();
101   static double  l_sin(), l_cos(), l_tan();
102   static double  l_asin(), l_acos(), l_atan(), l_atan2();
103   static double  l_exp(), l_log(), l_log10();
50 #endif
104  
52 #ifdef  BIGLIB
105                          /* functions must be listed alphabetically */
106   static LIBR  library[MAXLIB] = {
107      { "acos", 1, ':', l_acos },
# Line 72 | Line 124 | static LIBR  library[MAXLIB] = {
124  
125   static int  libsize = 16;
126  
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 LIBR  *liblookup();
92
93 extern VARDEF  *argf();
94
95 #ifdef  VARIABLE
127   #define  resolve(ep)    ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
97 #else
98 #define  resolve(ep)    ((ep)->v.ln)
99 #define varlookup(name) NULL
100 #endif
128  
129  
130   int
131   fundefined(fname)               /* return # of arguments for function */
132   char  *fname;
133   {
134 <    LIBR  *lp;
134 >    register LIBR  *lp;
135      register VARDEF  *vp;
136  
137 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
138 <                || vp->def->v.kid->type != FUNC)
112 <        if ((lp = liblookup(fname)) == NULL)
113 <            return(0);
114 <        else
115 <            return(lp->nargs);
116 <    else
137 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
138 >                && vp->def->v.kid->type == FUNC)
139          return(nekids(vp->def->v.kid) - 1);
140 +    lp = vp != NULL ? vp->lib : liblookup(fname);
141 +    if (lp == NULL)
142 +        return(0);
143 +    return(lp->nargs);
144   }
145  
146  
# Line 149 | Line 175 | double  *a;
175   }
176  
177  
178 + void
179   funset(fname, nargs, assign, fptr)      /* set a library function */
180   char  *fname;
181   int  nargs;
182   int  assign;
183   double  (*fptr)();
184   {
185 +    int  oldlibsize = libsize;
186 +    char *cp;
187      register LIBR  *lp;
188 <
189 <    if ((lp = liblookup(fname)) == NULL) {
188 >                                                /* check for context */
189 >    for (cp = fname; *cp; cp++)
190 >        ;
191 >    if (cp == fname)
192 >        return;
193 >    if (cp[-1] == CNTXMARK)
194 >        *--cp = '\0';
195 >    if ((lp = liblookup(fname)) == NULL) {      /* insert */
196          if (libsize >= MAXLIB) {
197              eputs("Too many library functons!\n");
198              quit(1);
# Line 172 | Line 207 | double  (*fptr)();
207                  break;
208          libsize++;
209      }
210 <    lp[0].fname = fname;                /* must be static! */
211 <    lp[0].nargs = nargs;
212 <    lp[0].atyp = assign;
213 <    lp[0].f = fptr;
210 >    if (fptr == NULL) {                         /* delete */
211 >        while (lp < &library[libsize-1]) {
212 >            lp[0].fname = lp[1].fname;
213 >            lp[0].nargs = lp[1].nargs;
214 >            lp[0].atyp = lp[1].atyp;
215 >            lp[0].f = lp[1].f;
216 >            lp++;
217 >        }
218 >        libsize--;
219 >    } else {                                    /* or assign */
220 >        lp[0].fname = fname;            /* string must be static! */
221 >        lp[0].nargs = nargs;
222 >        lp[0].atyp = assign;
223 >        lp[0].f = fptr;
224 >    }
225 >    if (libsize != oldlibsize)
226 >        libupdate(fname);                       /* relink library */
227   }
228  
229  
# Line 227 | Line 275 | register int  n;
275   }
276  
277  
230 #ifdef  VARIABLE
278   VARDEF *
279   argf(n)                         /* return function def for nth argument */
280   int  n;
# Line 272 | Line 319 | int  n;
319   {
320      return(argf(n)->name);
321   }
275 #endif
322  
323  
324   double
# Line 326 | Line 372 | char  *fname;
372   }
373  
374  
329 #ifndef  VARIABLE
330 VARDEF *
331 varinsert(vname)                /* dummy variable insert */
332 char  *vname;
333 {
334    register VARDEF  *vp;
335
336    vp = (VARDEF *)emalloc(sizeof(VARDEF));
337    vp->name = savestr(vname);
338    vp->nlinks = 1;
339    vp->def = NULL;
340    vp->lib = NULL;
341    vp->next = NULL;
342    return(vp);
343 }
344
345
346 varfree(vp)                     /* free dummy variable */
347 register VARDEF  *vp;
348 {
349    freestr(vp->name);
350    efree((char *)vp);
351 }
352 #endif
353
354
355
375   /*
376   *  The following routines are for internal use:
377   */
# Line 361 | Line 380 | register VARDEF  *vp;
380   static double
381   libfunc(fname, vp)                      /* execute library function */
382   char  *fname;
383 < register VARDEF  *vp;
383 > VARDEF  *vp;
384   {
385 <    VARDEF  dumdef;
385 >    register LIBR  *lp;
386      double  d;
387      int  lasterrno;
388  
389 <    if (vp == NULL) {
390 <        vp = &dumdef;
391 <        vp->lib = NULL;
392 <    }
393 <    if (((vp->lib == NULL || strcmp(fname, vp->lib->fname)) &&
375 <                                (vp->lib = liblookup(fname)) == NULL) ||
376 <                vp->lib->f == NULL) {
389 >    if (vp != NULL)
390 >        lp = vp->lib;
391 >    else
392 >        lp = liblookup(fname);
393 >    if (lp == NULL) {
394          eputs(fname);
395          eputs(": undefined function\n");
396          quit(1);
397      }
398      lasterrno = errno;
399      errno = 0;
400 <    d = (*vp->lib->f)(vp->lib->fname);
400 >    d = (*lp->f)(lp->fname);
401   #ifdef  IEEE
402 <    if (!finite(d))
403 <        errno = EDOM;
402 >    if (errno == 0)
403 >        if (isnan(d))
404 >            errno = EDOM;
405 >        else if (isinf(d))
406 >            errno = ERANGE;
407   #endif
408      if (errno) {
409          wputs(fname);
410 <        wputs(": bad call\n");
410 >        if (errno == EDOM)
411 >                wputs(": domain error\n");
412 >        else if (errno == ERANGE)
413 >                wputs(": range error\n");
414 >        else
415 >                wputs(": error in call\n");
416          return(0.0);
417      }
418      errno = lasterrno;
# Line 430 | Line 455 | l_select()             /* return argument #(A1+1) */
455   static double
456   l_rand()                /* random function between 0 and 1 */
457   {
433    extern double  floor();
458      double  x;
459  
460      x = argument(1);
# Line 444 | Line 468 | l_rand()               /* random function between 0 and 1 */
468   static double
469   l_floor()               /* return largest integer not greater than arg1 */
470   {
447    extern double  floor();
448
471      return(floor(argument(1)));
472   }
473  
# Line 453 | Line 475 | l_floor()              /* return largest integer not greater than
475   static double
476   l_ceil()                /* return smallest integer not less than arg1 */
477   {
456    extern double  ceil();
457
478      return(ceil(argument(1)));
479   }
480  
481  
462 #ifdef  BIGLIB
482   static double
483   l_sqrt()
484   {
466    extern double  sqrt();
467
485      return(sqrt(argument(1)));
486   }
487  
# Line 472 | Line 489 | l_sqrt()
489   static double
490   l_sin()
491   {
475    extern double  sin();
476
492      return(sin(argument(1)));
493   }
494  
# Line 481 | Line 496 | l_sin()
496   static double
497   l_cos()
498   {
484    extern double  cos();
485
499      return(cos(argument(1)));
500   }
501  
# Line 490 | Line 503 | l_cos()
503   static double
504   l_tan()
505   {
493    extern double  tan();
494
506      return(tan(argument(1)));
507   }
508  
# Line 499 | Line 510 | l_tan()
510   static double
511   l_asin()
512   {
502    extern double  asin();
503
513      return(asin(argument(1)));
514   }
515  
# Line 508 | Line 517 | l_asin()
517   static double
518   l_acos()
519   {
511    extern double  acos();
512
520      return(acos(argument(1)));
521   }
522  
# Line 517 | Line 524 | l_acos()
524   static double
525   l_atan()
526   {
520    extern double  atan();
521
527      return(atan(argument(1)));
528   }
529  
# Line 526 | Line 531 | l_atan()
531   static double
532   l_atan2()
533   {
529    extern double  atan2();
530
534      return(atan2(argument(1), argument(2)));
535   }
536  
# Line 535 | Line 538 | l_atan2()
538   static double
539   l_exp()
540   {
538    extern double  exp();
539
541      return(exp(argument(1)));
542   }
543  
# Line 544 | Line 545 | l_exp()
545   static double
546   l_log()
547   {
547    extern double  log();
548
548      return(log(argument(1)));
549   }
550  
# Line 553 | Line 552 | l_log()
552   static double
553   l_log10()
554   {
556    extern double  log10();
557
555      return(log10(argument(1)));
556   }
560 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines