ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/calfunc.c
Revision: 2.24
Committed: Wed Jul 24 21:20:28 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +3 -3 lines
Log Message:
Fixed bug in max() and min()

File Contents

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