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 |
+ |
/* ==================================================================== |
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 |
< |
|
78 |
> |
/* bits in argument flag (better be right!) */ |
79 |
> |
#define AFLAGSIZ (8*sizeof(unsigned long)) |
80 |
|
#define ALISTSIZ 6 /* maximum saved argument list */ |
81 |
|
|
82 |
|
typedef struct activation { |
89 |
|
|
90 |
|
static ACTIVATION *curact = NULL; |
91 |
|
|
92 |
< |
extern double libfunc(); |
92 |
> |
static double libfunc(); |
93 |
|
|
94 |
+ |
#ifndef MAXLIB |
95 |
|
#define MAXLIB 64 /* maximum number of library functions */ |
41 |
– |
|
42 |
– |
extern double l_if(), l_select(), l_rand(); |
43 |
– |
extern double l_floor(), l_ceil(); |
44 |
– |
#ifdef BIGLIB |
45 |
– |
extern double l_sqrt(); |
46 |
– |
extern double l_sin(), l_cos(), l_tan(); |
47 |
– |
extern double l_asin(), l_acos(), l_atan(), l_atan2(); |
48 |
– |
extern double l_exp(), l_log(), l_log10(); |
96 |
|
#endif |
97 |
|
|
98 |
< |
#ifdef BIGLIB |
98 |
> |
static double l_if(), l_select(), l_rand(); |
99 |
> |
static double l_floor(), l_ceil(); |
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(); |
104 |
> |
|
105 |
|
/* functions must be listed alphabetically */ |
106 |
|
static LIBR library[MAXLIB] = { |
107 |
< |
{ "acos", 1, l_acos }, |
108 |
< |
{ "asin", 1, l_asin }, |
109 |
< |
{ "atan", 1, l_atan }, |
110 |
< |
{ "atan2", 2, l_atan2 }, |
111 |
< |
{ "ceil", 1, l_ceil }, |
112 |
< |
{ "cos", 1, l_cos }, |
113 |
< |
{ "exp", 1, l_exp }, |
114 |
< |
{ "floor", 1, l_floor }, |
115 |
< |
{ "if", 3, l_if }, |
116 |
< |
{ "log", 1, l_log }, |
117 |
< |
{ "log10", 1, l_log10 }, |
118 |
< |
{ "rand", 1, l_rand }, |
119 |
< |
{ "select", 1, l_select }, |
120 |
< |
{ "sin", 1, l_sin }, |
121 |
< |
{ "sqrt", 1, l_sqrt }, |
122 |
< |
{ "tan", 1, l_tan }, |
107 |
> |
{ "acos", 1, ':', l_acos }, |
108 |
> |
{ "asin", 1, ':', l_asin }, |
109 |
> |
{ "atan", 1, ':', l_atan }, |
110 |
> |
{ "atan2", 2, ':', l_atan2 }, |
111 |
> |
{ "ceil", 1, ':', l_ceil }, |
112 |
> |
{ "cos", 1, ':', l_cos }, |
113 |
> |
{ "exp", 1, ':', l_exp }, |
114 |
> |
{ "floor", 1, ':', l_floor }, |
115 |
> |
{ "if", 3, ':', l_if }, |
116 |
> |
{ "log", 1, ':', l_log }, |
117 |
> |
{ "log10", 1, ':', l_log10 }, |
118 |
> |
{ "rand", 1, ':', l_rand }, |
119 |
> |
{ "select", 1, ':', l_select }, |
120 |
> |
{ "sin", 1, ':', l_sin }, |
121 |
> |
{ "sqrt", 1, ':', l_sqrt }, |
122 |
> |
{ "tan", 1, ':', l_tan }, |
123 |
|
}; |
124 |
|
|
125 |
|
static int libsize = 16; |
126 |
|
|
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 |
127 |
|
#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 |
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) |
111 |
< |
if ((lp = liblookup(fname)) == NULL) |
112 |
< |
return(0); |
113 |
< |
else |
114 |
< |
return(lp->nargs); |
115 |
< |
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 |
|
|
157 |
|
act.name = fname; |
158 |
|
act.prev = curact; |
159 |
|
act.ap = a; |
160 |
< |
act.an = (1L<<n)-1; |
160 |
> |
if (n >= AFLAGSIZ) |
161 |
> |
act.an = ~0; |
162 |
> |
else |
163 |
> |
act.an = (1L<<n)-1; |
164 |
|
act.fun = NULL; |
165 |
|
curact = &act; |
166 |
|
|
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
funset(fname, nargs, fptr) /* set a library function */ |
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); |
201 |
|
if (strcmp(lp[-1].fname, fname) > 0) { |
202 |
|
lp[0].fname = lp[-1].fname; |
203 |
|
lp[0].nargs = lp[-1].nargs; |
204 |
+ |
lp[0].atyp = lp[-1].atyp; |
205 |
|
lp[0].f = lp[-1].f; |
206 |
|
} else |
207 |
|
break; |
208 |
|
libsize++; |
209 |
|
} |
210 |
< |
lp[0].fname = fname; /* string must be static! */ |
211 |
< |
lp[0].nargs = nargs; |
212 |
< |
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 |
|
|
248 |
|
register int n; |
249 |
|
{ |
250 |
|
register ACTIVATION *actp = curact; |
251 |
< |
EPNODE *ep; |
251 |
> |
register EPNODE *ep; |
252 |
|
double aval; |
253 |
|
|
254 |
|
if (actp == NULL || --n < 0) { |
256 |
|
quit(1); |
257 |
|
} |
258 |
|
/* already computed? */ |
259 |
< |
if (1L<<n & actp->an) |
259 |
> |
if (n < AFLAGSIZ && 1L<<n & actp->an) |
260 |
|
return(actp->ap[n]); |
261 |
|
|
262 |
|
if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) { |
275 |
|
} |
276 |
|
|
277 |
|
|
223 |
– |
#ifdef VARIABLE |
278 |
|
VARDEF * |
279 |
|
argf(n) /* return function def for nth argument */ |
280 |
|
int n; |
319 |
|
{ |
320 |
|
return(argf(n)->name); |
321 |
|
} |
268 |
– |
#endif |
322 |
|
|
323 |
|
|
324 |
|
double |
372 |
|
} |
373 |
|
|
374 |
|
|
322 |
– |
#ifndef VARIABLE |
323 |
– |
VARDEF * |
324 |
– |
varinsert(vname) /* dummy variable insert */ |
325 |
– |
char *vname; |
326 |
– |
{ |
327 |
– |
register VARDEF *vp; |
328 |
– |
|
329 |
– |
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
330 |
– |
vp->name = savestr(vname); |
331 |
– |
vp->nlinks = 1; |
332 |
– |
vp->def = NULL; |
333 |
– |
vp->lib = NULL; |
334 |
– |
vp->next = NULL; |
335 |
– |
return(vp); |
336 |
– |
} |
337 |
– |
|
338 |
– |
|
339 |
– |
varfree(vp) /* free dummy variable */ |
340 |
– |
register VARDEF *vp; |
341 |
– |
{ |
342 |
– |
freestr(vp->name); |
343 |
– |
efree((char *)vp); |
344 |
– |
} |
345 |
– |
#endif |
346 |
– |
|
347 |
– |
|
348 |
– |
|
375 |
|
/* |
376 |
|
* The following routines are for internal use: |
377 |
|
*/ |
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)) && |
368 |
< |
(vp->lib = liblookup(fname)) == NULL) || |
369 |
< |
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)(); |
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; |
455 |
|
static double |
456 |
|
l_rand() /* random function between 0 and 1 */ |
457 |
|
{ |
426 |
– |
extern double floor(); |
458 |
|
double x; |
459 |
|
|
460 |
|
x = argument(1); |
468 |
|
static double |
469 |
|
l_floor() /* return largest integer not greater than arg1 */ |
470 |
|
{ |
440 |
– |
extern double floor(); |
441 |
– |
|
471 |
|
return(floor(argument(1))); |
472 |
|
} |
473 |
|
|
475 |
|
static double |
476 |
|
l_ceil() /* return smallest integer not less than arg1 */ |
477 |
|
{ |
449 |
– |
extern double ceil(); |
450 |
– |
|
478 |
|
return(ceil(argument(1))); |
479 |
|
} |
480 |
|
|
481 |
|
|
455 |
– |
#ifdef BIGLIB |
482 |
|
static double |
483 |
|
l_sqrt() |
484 |
|
{ |
459 |
– |
extern double sqrt(); |
460 |
– |
|
485 |
|
return(sqrt(argument(1))); |
486 |
|
} |
487 |
|
|
489 |
|
static double |
490 |
|
l_sin() |
491 |
|
{ |
468 |
– |
extern double sin(); |
469 |
– |
|
492 |
|
return(sin(argument(1))); |
493 |
|
} |
494 |
|
|
496 |
|
static double |
497 |
|
l_cos() |
498 |
|
{ |
477 |
– |
extern double cos(); |
478 |
– |
|
499 |
|
return(cos(argument(1))); |
500 |
|
} |
501 |
|
|
503 |
|
static double |
504 |
|
l_tan() |
505 |
|
{ |
486 |
– |
extern double tan(); |
487 |
– |
|
506 |
|
return(tan(argument(1))); |
507 |
|
} |
508 |
|
|
510 |
|
static double |
511 |
|
l_asin() |
512 |
|
{ |
495 |
– |
extern double asin(); |
496 |
– |
|
513 |
|
return(asin(argument(1))); |
514 |
|
} |
515 |
|
|
517 |
|
static double |
518 |
|
l_acos() |
519 |
|
{ |
504 |
– |
extern double acos(); |
505 |
– |
|
520 |
|
return(acos(argument(1))); |
521 |
|
} |
522 |
|
|
524 |
|
static double |
525 |
|
l_atan() |
526 |
|
{ |
513 |
– |
extern double atan(); |
514 |
– |
|
527 |
|
return(atan(argument(1))); |
528 |
|
} |
529 |
|
|
531 |
|
static double |
532 |
|
l_atan2() |
533 |
|
{ |
522 |
– |
extern double atan2(); |
523 |
– |
|
534 |
|
return(atan2(argument(1), argument(2))); |
535 |
|
} |
536 |
|
|
538 |
|
static double |
539 |
|
l_exp() |
540 |
|
{ |
531 |
– |
extern double exp(); |
532 |
– |
|
541 |
|
return(exp(argument(1))); |
542 |
|
} |
543 |
|
|
545 |
|
static double |
546 |
|
l_log() |
547 |
|
{ |
540 |
– |
extern double log(); |
541 |
– |
|
548 |
|
return(log(argument(1))); |
549 |
|
} |
550 |
|
|
552 |
|
static double |
553 |
|
l_log10() |
554 |
|
{ |
549 |
– |
extern double log10(); |
550 |
– |
|
555 |
|
return(log10(argument(1))); |
556 |
|
} |
553 |
– |
#endif |