ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/calfunc.c
Revision: 2.16
Committed: Wed Oct 24 00:39:09 2012 UTC (11 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +38 -39 lines
Log Message:
Finished ANSIfication

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.16 static const char RCSid[] = "$Id: calfunc.c,v 2.15 2006/05/10 15:21:20 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * calfunc.c - routines for calcomp using functions.
6     *
7 greg 2.8 * If VARIABLE is not set, only library functions
8     * can be accessed.
9     *
10     * 2/19/03 Eliminated conditional compiles in favor of esupport extern.
11     */
12    
13 greg 2.9 #include "copyright.h"
14 greg 1.1
15     #include <stdio.h>
16 schorsch 2.11 #include <string.h>
17 greg 1.1 #include <errno.h>
18 greg 2.5 #include <math.h>
19    
20 schorsch 2.12 #include "rterror.h"
21 greg 1.1 #include "calcomp.h"
22    
23 greg 1.5 /* bits in argument flag (better be right!) */
24     #define AFLAGSIZ (8*sizeof(unsigned long))
25 greg 1.1 #define ALISTSIZ 6 /* maximum saved argument list */
26    
27     typedef struct activation {
28     char *name; /* function name */
29     struct activation *prev; /* previous activation */
30     double *ap; /* argument list */
31     unsigned long an; /* computed argument flags */
32     EPNODE *fun; /* argument function */
33     } ACTIVATION; /* an activation record */
34    
35     static ACTIVATION *curact = NULL;
36    
37 schorsch 2.12 static double libfunc(char *fname, VARDEF *vp);
38 greg 1.1
39 greg 2.8 #ifndef MAXLIB
40 greg 1.1 #define MAXLIB 64 /* maximum number of library functions */
41 greg 2.8 #endif
42 greg 1.1
43 greg 2.13 static double l_if(char *), l_select(char *), l_rand(char *);
44     static double l_floor(char *), l_ceil(char *);
45     static double l_sqrt(char *);
46     static double l_sin(char *), l_cos(char *), l_tan(char *);
47     static double l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *);
48     static double l_exp(char *), l_log(char *), l_log10(char *);
49 greg 1.1
50     /* functions must be listed alphabetically */
51     static LIBR library[MAXLIB] = {
52 greg 1.4 { "acos", 1, ':', l_acos },
53     { "asin", 1, ':', l_asin },
54     { "atan", 1, ':', l_atan },
55     { "atan2", 2, ':', l_atan2 },
56     { "ceil", 1, ':', l_ceil },
57     { "cos", 1, ':', l_cos },
58     { "exp", 1, ':', l_exp },
59     { "floor", 1, ':', l_floor },
60     { "if", 3, ':', l_if },
61     { "log", 1, ':', l_log },
62     { "log10", 1, ':', l_log10 },
63     { "rand", 1, ':', l_rand },
64     { "select", 1, ':', l_select },
65     { "sin", 1, ':', l_sin },
66     { "sqrt", 1, ':', l_sqrt },
67     { "tan", 1, ':', l_tan },
68 greg 1.1 };
69    
70     static int libsize = 16;
71    
72     #define resolve(ep) ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
73    
74    
75     int
76 greg 2.16 fundefined( /* return # of arguments for function */
77     char *fname
78     )
79 greg 1.1 {
80 greg 2.16 LIBR *lp;
81     VARDEF *vp;
82 greg 1.1
83 greg 2.7 if ((vp = varlookup(fname)) != NULL && vp->def != NULL
84     && vp->def->v.kid->type == FUNC)
85 greg 1.1 return(nekids(vp->def->v.kid) - 1);
86 greg 2.7 lp = vp != NULL ? vp->lib : liblookup(fname);
87     if (lp == NULL)
88     return(0);
89     return(lp->nargs);
90 greg 1.1 }
91    
92    
93     double
94 greg 2.16 funvalue( /* return a function value to the user */
95     char *fname,
96     int n,
97     double *a
98     )
99 greg 1.1 {
100     ACTIVATION act;
101 greg 2.16 VARDEF *vp;
102 greg 1.1 double rval;
103     /* push environment */
104     act.name = fname;
105     act.prev = curact;
106     act.ap = a;
107 greg 1.5 if (n >= AFLAGSIZ)
108     act.an = ~0;
109     else
110     act.an = (1L<<n)-1;
111 greg 1.1 act.fun = NULL;
112     curact = &act;
113    
114     if ((vp = varlookup(fname)) == NULL || vp->def == NULL
115     || vp->def->v.kid->type != FUNC)
116     rval = libfunc(fname, vp);
117     else
118     rval = evalue(vp->def->v.kid->sibling);
119    
120     curact = act.prev; /* pop environment */
121     return(rval);
122     }
123    
124    
125 greg 2.8 void
126 greg 2.16 funset( /* set a library function */
127     char *fname,
128     int nargs,
129     int assign,
130     double (*fptr)(char *)
131     )
132 greg 1.1 {
133 greg 2.6 int oldlibsize = libsize;
134 greg 2.8 char *cp;
135 greg 2.16 LIBR *lp;
136 greg 2.8 /* check for context */
137     for (cp = fname; *cp; cp++)
138     ;
139     if (cp == fname)
140     return;
141     if (cp[-1] == CNTXMARK)
142     *--cp = '\0';
143 greg 2.2 if ((lp = liblookup(fname)) == NULL) { /* insert */
144 greg 1.1 if (libsize >= MAXLIB) {
145     eputs("Too many library functons!\n");
146     quit(1);
147     }
148     for (lp = &library[libsize]; lp > library; lp--)
149     if (strcmp(lp[-1].fname, fname) > 0) {
150     lp[0].fname = lp[-1].fname;
151     lp[0].nargs = lp[-1].nargs;
152 greg 1.4 lp[0].atyp = lp[-1].atyp;
153 greg 1.1 lp[0].f = lp[-1].f;
154     } else
155     break;
156     libsize++;
157     }
158 greg 2.2 if (fptr == NULL) { /* delete */
159     while (lp < &library[libsize-1]) {
160     lp[0].fname = lp[1].fname;
161     lp[0].nargs = lp[1].nargs;
162     lp[0].atyp = lp[1].atyp;
163     lp[0].f = lp[1].f;
164     lp++;
165     }
166     libsize--;
167     } else { /* or assign */
168     lp[0].fname = fname; /* string must be static! */
169     lp[0].nargs = nargs;
170     lp[0].atyp = assign;
171     lp[0].f = fptr;
172     }
173 greg 2.6 if (libsize != oldlibsize)
174     libupdate(fname); /* relink library */
175 greg 1.1 }
176    
177    
178     int
179 greg 2.16 nargum(void) /* return number of available arguments */
180 greg 1.1 {
181 greg 2.16 int n;
182 greg 1.1
183     if (curact == NULL)
184     return(0);
185     if (curact->fun == NULL) {
186     for (n = 0; (1L<<n) & curact->an; n++)
187     ;
188     return(n);
189     }
190     return(nekids(curact->fun) - 1);
191     }
192    
193    
194     double
195 greg 2.16 argument(int n) /* return nth argument for active function */
196 greg 1.1 {
197 greg 2.16 ACTIVATION *actp = curact;
198     EPNODE *ep = NULL;
199 greg 1.1 double aval;
200    
201     if (actp == NULL || --n < 0) {
202     eputs("Bad call to argument!\n");
203     quit(1);
204     }
205     /* already computed? */
206 greg 1.5 if (n < AFLAGSIZ && 1L<<n & actp->an)
207 greg 1.1 return(actp->ap[n]);
208    
209     if (actp->fun == NULL || (ep = ekid(actp->fun, n+1)) == NULL) {
210     eputs(actp->name);
211     eputs(": too few arguments\n");
212     quit(1);
213     }
214     curact = actp->prev; /* pop environment */
215     aval = evalue(ep); /* compute argument */
216     curact = actp; /* push back environment */
217     if (n < ALISTSIZ) { /* save value */
218     actp->ap[n] = aval;
219     actp->an |= 1L<<n;
220     }
221     return(aval);
222     }
223    
224    
225     VARDEF *
226 greg 2.16 argf(int n) /* return function def for nth argument */
227 greg 1.1 {
228 greg 2.16 ACTIVATION *actp;
229     EPNODE *ep;
230 greg 1.1
231     for (actp = curact; actp != NULL; actp = actp->prev) {
232    
233     if (n <= 0)
234     break;
235    
236     if (actp->fun == NULL)
237     goto badarg;
238    
239     if ((ep = ekid(actp->fun, n)) == NULL) {
240     eputs(actp->name);
241     eputs(": too few arguments\n");
242     quit(1);
243     }
244     if (ep->type == VAR)
245     return(ep->v.ln); /* found it */
246    
247     if (ep->type != ARG)
248     goto badarg;
249    
250     n = ep->v.chan; /* try previous context */
251     }
252     eputs("Bad call to argf!\n");
253     quit(1);
254    
255     badarg:
256     eputs(actp->name);
257     eputs(": argument not a function\n");
258     quit(1);
259 schorsch 2.11 return NULL; /* pro forma return */
260 greg 1.1 }
261    
262    
263     char *
264 greg 2.16 argfun(int n) /* return function name for nth argument */
265 greg 1.1 {
266     return(argf(n)->name);
267     }
268    
269    
270     double
271 greg 2.16 efunc(EPNODE *ep) /* evaluate a function */
272 greg 1.1 {
273     ACTIVATION act;
274     double alist[ALISTSIZ];
275     double rval;
276 greg 2.16 VARDEF *dp;
277 greg 1.1 /* push environment */
278     dp = resolve(ep->v.kid);
279     act.name = dp->name;
280     act.prev = curact;
281     act.ap = alist;
282     act.an = 0;
283     act.fun = ep;
284     curact = &act;
285    
286     if (dp->def == NULL || dp->def->v.kid->type != FUNC)
287     rval = libfunc(act.name, dp);
288     else
289     rval = evalue(dp->def->v.kid->sibling);
290    
291     curact = act.prev; /* pop environment */
292     return(rval);
293     }
294    
295    
296     LIBR *
297 greg 2.16 liblookup(char *fname) /* look up a library function */
298 greg 1.1 {
299     int upper, lower;
300 greg 2.16 int cm, i;
301 greg 1.1
302     lower = 0;
303     upper = cm = libsize;
304    
305     while ((i = (lower + upper) >> 1) != cm) {
306     cm = strcmp(fname, library[i].fname);
307     if (cm > 0)
308     lower = i;
309     else if (cm < 0)
310     upper = i;
311     else
312     return(&library[i]);
313     cm = i;
314     }
315     return(NULL);
316     }
317    
318    
319     /*
320     * The following routines are for internal use:
321     */
322    
323    
324     static double
325 greg 2.16 libfunc( /* execute library function */
326     char *fname,
327     VARDEF *vp
328     )
329 greg 1.1 {
330 greg 2.16 LIBR *lp;
331 greg 1.1 double d;
332     int lasterrno;
333    
334 greg 2.3 if (vp != NULL)
335     lp = vp->lib;
336     else
337     lp = liblookup(fname);
338     if (lp == NULL) {
339 greg 1.1 eputs(fname);
340     eputs(": undefined function\n");
341     quit(1);
342     }
343     lasterrno = errno;
344     errno = 0;
345 greg 2.3 d = (*lp->f)(lp->fname);
346 greg 2.15 #ifdef isnan
347 greg 1.9 if (errno == 0)
348     if (isnan(d))
349     errno = EDOM;
350     else if (isinf(d))
351     errno = ERANGE;
352 greg 1.1 #endif
353 greg 2.10 if (errno == EDOM || errno == ERANGE) {
354 greg 1.1 wputs(fname);
355 greg 1.8 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 greg 1.1 return(0.0);
362     }
363     errno = lasterrno;
364     return(d);
365     }
366    
367    
368     /*
369     * Library functions:
370     */
371    
372    
373     static double
374 greg 2.13 l_if(char *nm) /* if(cond, then, else) conditional expression */
375 greg 1.1 /* cond evaluates true if greater than zero */
376     {
377     if (argument(1) > 0.0)
378     return(argument(2));
379     else
380     return(argument(3));
381     }
382    
383    
384     static double
385 greg 2.13 l_select(char *nm) /* return argument #(A1+1) */
386 greg 1.1 {
387 greg 2.16 int n;
388 greg 1.1
389 schorsch 2.11 n = (int)(argument(1) + .5);
390 greg 1.1 if (n == 0)
391     return(nargum()-1);
392     if (n < 1 || n > nargum()-1) {
393     errno = EDOM;
394     return(0.0);
395     }
396     return(argument(n+1));
397     }
398    
399    
400     static double
401 greg 2.13 l_rand(char *nm) /* random function between 0 and 1 */
402 greg 1.1 {
403     double x;
404    
405     x = argument(1);
406     x *= 1.0/(1.0 + x*x) + 2.71828182845904;
407     x += .785398163397447 - floor(x);
408     x = 1e5 / x;
409     return(x - floor(x));
410     }
411    
412    
413     static double
414 greg 2.13 l_floor(char *nm) /* return largest integer not greater than arg1 */
415 greg 1.1 {
416     return(floor(argument(1)));
417     }
418    
419    
420     static double
421 greg 2.13 l_ceil(char *nm) /* return smallest integer not less than arg1 */
422 greg 1.1 {
423     return(ceil(argument(1)));
424     }
425    
426    
427     static double
428 greg 2.13 l_sqrt(char *nm)
429 greg 1.1 {
430     return(sqrt(argument(1)));
431     }
432    
433    
434     static double
435 greg 2.13 l_sin(char *nm)
436 greg 1.1 {
437     return(sin(argument(1)));
438     }
439    
440    
441     static double
442 greg 2.13 l_cos(char *nm)
443 greg 1.1 {
444     return(cos(argument(1)));
445     }
446    
447    
448     static double
449 greg 2.13 l_tan(char *nm)
450 greg 1.1 {
451     return(tan(argument(1)));
452     }
453    
454    
455     static double
456 greg 2.13 l_asin(char *nm)
457 greg 1.1 {
458     return(asin(argument(1)));
459     }
460    
461    
462     static double
463 greg 2.13 l_acos(char *nm)
464 greg 1.1 {
465     return(acos(argument(1)));
466     }
467    
468    
469     static double
470 greg 2.13 l_atan(char *nm)
471 greg 1.1 {
472     return(atan(argument(1)));
473     }
474    
475    
476     static double
477 greg 2.13 l_atan2(char *nm)
478 greg 1.1 {
479     return(atan2(argument(1), argument(2)));
480     }
481    
482    
483     static double
484 greg 2.13 l_exp(char *nm)
485 greg 1.1 {
486     return(exp(argument(1)));
487     }
488    
489    
490     static double
491 greg 2.13 l_log(char *nm)
492 greg 1.1 {
493     return(log(argument(1)));
494     }
495    
496    
497     static double
498 greg 2.13 l_log10(char *nm)
499 greg 1.1 {
500     return(log10(argument(1)));
501     }