xref: /csrg-svn/old/lisp/fp/fp.vax/primFp.l (revision 12070)
1 (setq SCCS-primFp.l "@(#)primFp.l	1.1	04/27/83")
2 ;  FP interpreter/compiler
3 ;  Copyright (c) 1982  Scott B. Baden
4 ;  Berkeley, California
5 
6 (include specials.l)
7 (declare (special y_l z_l)
8   (localf ok_pair ok_eqpair rpair$ lpair$ trnspz allNulls
9 	  emptyHeader treeInsWithLen))
10 
11 ; fp addition
12 
13 (defun plus$fp (x)
14   (cond (DynTraceFlg (IncrTimes 'plus$fp)))
15   (cond ((ok_pair x 'numberp) (plus (car x) (cadr x)))
16 	(t (bottom))))
17 
18 ; unit function
19 
20 (defun (u-fnc plus$fp) nil
21   0)
22 
23 ; fp subtraction
24 
25 (defun sub$fp (x)
26   (cond (DynTraceFlg (IncrTimes 'sub$fp)))
27   (cond ((ok_pair x 'numberp) (diff (car x) (cadr x)))
28 	(t (bottom))))
29 
30 
31 ; unit function
32 
33 (defun (u-fnc sub$fp) nil
34   0)
35 
36 ; fp multiplication
37 
38 (defun times$fp (x)
39   (cond (DynTraceFlg (IncrTimes 'times$fp)))
40   (cond ((ok_pair x 'numberp) (product (car x) (cadr x)))
41 	(t (bottom))))
42 
43 ; unit function
44 
45 (defun (u-fnc times$fp) nil
46   1)
47 
48 
49 ; fp division
50 
51 (defun div$fp (x)
52   (cond (DynTraceFlg (IncrTimes 'div$fp)))
53   (cond ((ok_pair x 'numberp)
54 	 (cond ((not (zerop (cadr x)))
55 		(quotient (car x) (cadr x)))
56 	       (t (bottom))))
57 	(t (bottom))))
58 
59 ; unit function
60 
61 (defun (u-fnc div$fp) nil
62   1)
63 
64 
65 
66 ; logical functions, and or xor not
67 
68 (defun and$fp (x)
69   (cond (DynTraceFlg (IncrTimes 'and$fp)))
70   (cond ((ok_pair x 'boolp)
71 	 (cond
72 	  ((eq 'F (car x)) 'F)
73 	  (t (cadr x))))
74 	(t (bottom))))
75 
76 ; unit function
77 
78 (defun (u-fnc and$fp) nil
79   'T)
80 
81 
82 (defun or$fp (x)
83   (cond (DynTraceFlg (IncrTimes 'or$fp)))
84   (cond ((ok_pair x 'boolp)
85 	 (cond
86 	  ((eq 'T (car x)) 'T)
87 	  (t (cadr x))))
88 	(t (bottom))))
89 
90 ; unit function
91 
92 (defun (u-fnc or$fp) nil
93   'F)
94 
95 
96 (defun xor$fp (x)
97   (cond (DynTraceFlg (IncrTimes 'xor$fp)))
98   (cond ((ok_pair x 'boolp)
99 	 (let ((p (car x))
100 	       (q (cadr x)))
101 	      (cond ((or (and (eq p 'T) (eq q 'T))
102 			 (and (eq p 'F) (eq q 'F)))
103 		     'F)
104 		    (t 'T))))
105 	(t (bottom))))
106 
107 ; unit function
108 
109 (defun (u-fnc xor$fp) nil
110   'F)
111 
112 
113 (defun not$fp (x)
114   (cond (DynTraceFlg (IncrTimes 'not$fp)))
115   (cond ((not (atom x)) (bottom))
116 	((boolp x) (cond ((eq x 'T) 'F) (t 'T)))
117 	(t (bottom))))
118 
119 
120 ; relational operators,  <  <=  =  >=  >  ~=
121 
122 (defun lt$fp (x)
123   (cond (DynTraceFlg (IncrTimes 'lt$fp)))
124   (cond ((ok_pair x 'numberp)
125 	 (cond ((lessp (car x) (cadr x)) 'T)
126 	       (t 'F)))
127 	(t (bottom))))
128 
129 (defun le$fp (x)
130   (cond (DynTraceFlg (IncrTimes 'le$fp)))
131   (cond ((ok_pair x 'numberp)
132 	 (cond ((not (greaterp (car x) (cadr x))) 'T)
133 	       (t 'F)))
134 	(t (bottom))))
135 
136 (defun eq$fp (x)
137   (cond (DynTraceFlg (IncrTimes 'eq$fp)))
138   (cond ((ok_eqpair x )
139 	 (cond ((equal  (car x) (cadr x)) 'T)
140 	       (t 'F)))
141 	(t (bottom))))
142 
143 (defun ge$fp (x)
144   (cond (DynTraceFlg (IncrTimes 'ge$fp)))
145   (cond ((ok_pair x 'numberp)
146 	 (cond ((not (lessp (car x) (cadr x))) 'T)
147 	       (t 'F)))
148 	(t (bottom))))
149 
150 (defun gt$fp (x)
151   (cond (DynTraceFlg (IncrTimes 'gt$fp)))
152   (cond ((ok_pair x 'numberp)
153 	 (cond ((greaterp (car x) (cadr x)) 'T)
154 	       (t 'F)))
155 	(t (bottom))))
156 
157 (defun ne$fp (x)
158   (cond (DynTraceFlg (IncrTimes 'ne$fp)))
159   (cond ((ok_eqpair x)
160 	 (cond ((not (equal  (car x) (cadr x))) 'T)
161 	       (t 'F)))
162 	(t (bottom))))
163 
164 
165 
166 ; check arguments for eq and ne
167 
168 (defun ok_eqpair (x)
169   (cond ((not (atom x))
170 	 (cond ((eq (length x) 2) t)))))
171 
172 ; check arguments for binary arithmetics/logicals
173 
174 (defun ok_pair (x typ)
175   (cond ((not (atom x))
176 	 (cond ((eq (length x) 2)
177 		(cond
178 		 ((and (atom (car x)) (atom (cadr x)))
179 		  (cond ((and (funcall typ (car x))
180 			      (funcall typ (cadr x))) t)))))))))
181 
182 ; check if a variable is boolean, 'T' or 'F'
183 
184 (defun boolp (x)
185   (memq x '(T F)))
186 
187 
188 (defun undefp (x)
189   (eq x '?))
190 
191 (defun tl$fp (x)
192   (cond (DynTraceFlg (IncrSize 'tl$fp (size x)) (IncrTimes 'tl$fp)))
193   (cond ((atom x) (bottom))
194 	(t (cdr x))))
195 
196 
197 (defun tlr$fp (x)
198   (cond (DynTraceFlg (IncrSize 'tlr$fp (size x)) (IncrTimes 'tlr$fp)))
199   (cond ((listp x) (cond
200 		    ((onep (length x)) nil)
201 		    (t (reverse (cdr (reverse x))))))
202 	(t (bottom))))
203 
204 ; this function is just like id$fp execept it also prints its
205 ; argument on the stdout. It is meant to be used only for debuging.
206 
207 (defun out$fp (x)
208   (fpPP x)
209   (terpri)
210   x)
211 
212 (defun id$fp (x)
213   (cond (DynTraceFlg (IncrSize 'id$fp (size x)) (IncrTimes 'id$fp)))
214   x)
215 
216 (defun atom$fp (x)
217   (cond (DynTraceFlg (IncrSize 'atom$fp (size x)) (IncrTimes 'atom$fp)))
218   (cond ((atom x) 'T)
219 	(t 'F)))
220 
221 (defun null$fp (x)
222   (cond (DynTraceFlg (IncrSize 'null$fp (size x)) (IncrTimes 'null$fp)))
223   (cond ((null x) 'T)
224 	(t  'F)))
225 
226 (defun reverse$fp (x)
227   (cond (DynTraceFlg (IncrSize 'reverse$fp (size x)) (IncrTimes 'reverse$fp)))
228   (cond  ((null x) x)
229 	 ((listp x) (reverse x))
230 	 (t (bottom))))
231 
232 (defun lpair$ (x)
233   (cond ((or (undefp x) (not (listp x))) nil)
234 	(t
235 	 (setq y_l (car x))
236 	 (setq z_l (cdr x))
237 	 (cond ((null z_l)  t)
238 	       (t (cond ((or (not (listp z_l)) (not (onep (length z_l)))) nil)
239 			(t (listp (setq z_l (car z_l))))))))))
240 
241 (defun rpair$ (x)
242   (cond ((or (undefp x) (not (listp x))) nil)
243 	(t
244 	 (setq y_l (car x))
245 	 (setq z_l (cdr x))
246 	 (cond ((null y_l)  t)
247 	       (t (cond ((not (listp y_l)) nil)
248 			(t (setq z_l (car z_l)) t)))))))
249 
250 
251 (defun distl$fp (x)
252   (let ((y_l nil) (z_l nil))
253        (cond ((lpair$ x)
254 	      (cond (DynTraceFlg
255 		     (IncrSize 'distl$fp (size z_l)) (IncrTimes 'distl$fp)))
256 	      (mapcar '(lambda (u) (list y_l u)) z_l))
257 	     (t (bottom)))))
258 
259 (defun distr$fp (x)
260   (let ((y_l nil) (z_l nil))
261        (cond ((rpair$ x)
262 	      (cond (DynTraceFlg
263 		     (IncrSize 'distr$fp (size y_l)) (IncrTimes 'distr$fp)))
264 	      (mapcar '(lambda (u) (list u z_l)) y_l))
265 	     (t (bottom)))))
266 
267 
268 (defun length$fp (x)
269   (cond (DynTraceFlg (IncrSize 'length$fp (size x)) (IncrTimes 'length$fp)))
270   (cond ((listp x) (length x))
271 	(t (bottom))))
272 
273 (defun apndl$fp (x)
274   (cond ((and (dtpr x) (eq 2 (length x)) (listp (cadr x)))
275 	 (cond (DynTraceFlg
276 		(IncrSize 'apndl$fp (size (cadr x))) (IncrTimes 'apndl$fp)))
277 	 (cons (car x) (cadr x)))
278 	(t (bottom))))
279 
280 
281 (defun apndr$fp (x)
282   (cond ((and (dtpr x) (eq 2 (length x)) (listp (car x)))
283 	 (cond (DynTraceFlg
284 		(IncrSize 'apndr$fp (size (car x))) (IncrTimes 'apndr$fp)))
285 	 (append (car x) (cdr x)))
286 	(t (bottom))))
287 
288 
289 (defun rotl$fp (x)
290   (cond (DynTraceFlg (IncrSize 'rotl$fp (size x)) (IncrTimes 'rotl$fp)))
291   (cond ((null x) x)
292 	((listp x) (cond ((onep (length x)) x)
293 			 (t (append (cdr x) (list (car x))))))
294 	(t (bottom))))
295 
296 (defun rotr$fp (x)
297   (cond (DynTraceFlg (IncrSize 'rotr$fp (size x)) (IncrTimes 'rotr$fp)))
298   (cond ((null x) x)
299 	((listp x) (cond ((onep (length x)) x)
300 			 (t (reverse (rotl$fp (reverse x))))))
301 	(t (bottom))))
302 
303 
304 (defun trans$fp (x)
305   (If (listp x)
306       then (If (allNulls x)
307 	       then
308 	       (cond (DynTraceFlg
309 		      (IncrSize 'trans$fp (size x))
310 		      (IncrTimes 'trans$fp)))
311 	       nil
312 
313 	       else
314 	       (cond (DynTraceFlg
315 		      (IncrSize 'trans$fp
316 				(+ (size (car x))
317 				   (size (cadr x)))) (IncrTimes 'trans$fp)))
318 
319 	       (do ((a x (cdr a))
320 		    (f (length (car x))))
321 		   ((null a) (trnspz x))
322 		   (If (or (not (listp (car a))) (not (eq f (length (car a)))))
323 		       then (bottom))))
324       else
325 
326       (bottom)))
327 
328 (defun allNulls (x)
329   (do ((a x (cdr a)))
330       ((null a) t)
331       (If (car a) then (return nil))))
332 
333 
334 (defun trnspz (l)
335   (do
336    ((h (emptyHeader (length (car l))))
337     (v l (cdr v)))
338    ((null v) (mapcar 'car h))
339    (mapcar #'(lambda (x y) (tconc x y)) h (car v))))
340 
341 
342 (defun emptyHeader (n)
343   (do
344    ((r nil)
345     (c n (1- c)))
346    ((= c 0) r)
347    (setq r (cons (ncons nil) r))))
348 
349 
350 (defun iota$fp (x)
351   (cond (DynTraceFlg  (IncrTimes 'iota$fp)))
352   (cond ((undefp x) x)
353 	((listp x) (bottom))
354 	((not (fixp x)) (bottom))
355 	((lessp x 0) (bottom))
356 	((zerop x) nil)
357 	(t
358 	 (do ((z x (1- z))
359 	      (rslt nil))
360 	     ((zerop z) rslt)
361 	     (setq rslt (cons z rslt))))))
362 
363 ; this is the stuff that was added by dorab patel to make this have
364 ; the same functions as David Lahti's interpreter
365 
366 
367 ;; Modified by SBB to accept nil as a valid input
368 
369 (defun last$fp (x)
370   (cond (DynTraceFlg (IncrSize 'last$fp (size x)) (IncrTimes 'last$fp)))
371     (cond ((null x) nil)
372 	  ((listp x) (car (last x)))
373 	  (t (bottom))))
374 
375 ;; Added by SBB
376 
377 (defun first$fp (x)
378   (If DynTraceFlg then (IncrSize 'first$fp (size x)) (IncrTimes 'first$fp))
379   (If (not (listp x)) then (bottom)
380       else (car x)))
381 
382 (defun front$fp (x)
383   (cond (DynTraceFlg (IncrSize 'front$fp (size x)) (IncrTimes 'front$fp)))
384     (cond ((null x) (bottom))
385 	  ((listp x) (nreverse (cdr (nreverse x))))
386 	  (t (bottom))))
387 
388 (defun pick$fp (sAndX)
389   (let ((s (car sAndX))
390 	(x (cadr sAndX)))
391        (cond (DynTraceFlg (IncrSize 'pick$fp (size x)) (IncrTimes 'pick$fp)))
392 
393        (If (or (not (fixp s)) (zerop s)) then  (bottom)
394 	   else
395 
396 	   (progn
397 	    (cond (DynTraceFlg
398 		   (IncrTimes 'select$fp)
399 		   (IncrSize 'select$fp (size x))))
400 
401 	    (cond ((not (listp x)) (bottom))
402 		  ((plusp s)
403 		   (If (greaterp s (length x)) then (bottom)
404 		       else (nthelem s x)))
405 		  ((minusp s)
406 		   (let  ((len (length x)))
407 			 (If (greaterp (absval s) len) then (bottom)
408 			     else (nthelem (plus len 1 s) x)))))))))
409 
410 
411 
412 (defun concat$fp (x)
413   (cond (DynTraceFlg (IncrSize 'concat$fp (size x)) (IncrTimes 'concat$fp)))
414 
415   (If (listp x)
416       then
417       (do ((a x  (cdr a))
418 	   (y (copy x) (cdr y))
419 	   (rslt (ncons nil)))
420 	  ((null a) (car rslt))
421 	  (If (not (listp (car a))) then (bottom))
422 
423 	  (lconc rslt (car y)))
424 
425       else (bottom)))
426 
427 
428 (defun pair$fp (x)
429   (cond (DynTraceFlg (IncrSize 'pair$fp (size x)) (IncrTimes 'pair$fp)))
430     (cond ((not (listp x)) (bottom))
431 	  ((null x) (bottom))
432 	  (t (do ((count 0 (add count 2)) ; set local vars
433 		  (max (length x))
434 		  (ret nil))
435 		 ((not (lessp count max)) (nreverse ret)) ; return ret at end
436 		 (cond ((equal (diff max count) 1) ; if only one element left
437 			(setq ret (cons (list (car x)) ret)))
438 		       (t (setq ret (cons (list (car x) (cadr x)) ret))
439 			  (setq x (cddr x))))))))
440 
441 
442 (defun split$fp (x)
443   (cond (DynTraceFlg (IncrSize 'split$fp (size x)) (IncrTimes 'split$fp)))
444   (cond ((not (listp x)) (bottom))
445 	((null x) (bottom))
446 	((eq (length x) 1) (list x nil))
447 	(t
448 	 (do ((count 1 (add1 count))
449 	      (mid (fix (plus 0.5 (quotient (length x) 2.0))))
450 	      (ret nil))
451 	     ((greaterp count mid) (cons (nreverse ret) (list x)))
452 	     (setq ret (cons (car x) ret))
453 	     (setq x (cdr x))))))
454 
455 
456 ; Library functions: sin, asin, cos, acos, log, exp, mod
457 
458 (defun sin$fp (x)
459   (cond (DynTraceFlg  (IncrTimes 'sin$fp)))
460   (cond ((numberp x) (sin x))
461 	(t (bottom))))
462 
463 (defun asin$fp (x)
464   (cond (DynTraceFlg  (IncrTimes 'asin$fp)))
465   (cond ((and (numberp x) (not (greaterp (abs x) 1.0))) (asin x))
466 	(t (bottom))))
467 
468 (defun cos$fp (x)
469   (cond (DynTraceFlg  (IncrTimes 'cos$fp)))
470   (cond ((numberp x) (cos x))
471 	(t (bottom))))
472 
473 (defun acos$fp (x)
474   (cond (DynTraceFlg  (IncrTimes 'acos$fp)))
475   (cond ((and (numberp x) (not (greaterp (abs x) 1.0))) (acos x))
476 	(t (bottom))))
477 
478 (defun log$fp (x)
479   (cond (DynTraceFlg  (IncrTimes 'log$fp)))
480   (cond ((and (numberp x) (not (minusp x))) (log x))
481 	(t (bottom))))
482 
483 (defun exp$fp (x)
484   (cond (DynTraceFlg  (IncrTimes 'exp$fp)))
485   (cond ((numberp x) (exp x))
486 	(t (bottom))))
487 
488 (defun mod$fp (x)
489   (cond (DynTraceFlg  (IncrTimes 'mod$fp)))
490   (cond ((ok_pair x 'numberp) (mod (car x) (cadr x)))
491 	(t (bottom))))
492 
493 
494 ;; Tree insert function
495 
496 
497 (defun treeIns$fp (fn x)
498   (If (not (listp x)) then  (bottom)
499       else
500       (If (null x) then  (unitTreeInsert fn)
501 	  else
502 	  (let ((len (length x)))
503 	       (If (onep len) then (car x)
504 		   else
505 		   (If (twop len) then (funcall fn x )
506 		       else (treeInsWithLen fn x len)))))))
507 
508 
509 (defun treeInsWithLen (fn x len)
510   (let* ((r1 (copy x))
511 	 (nLen (fix (plus 0.5 (quotient len 2.0))))
512 	 (p (Cnth r1 nLen))
513 	 (r2 (cdr p)))
514 	(rplacd p nil)
515 	(let ((saveLevel level))
516 	     (setq level (1+ level))
517 	     (let ((R1 (treeIns fn r1 nLen)))
518 		  (setq level (1+ saveLevel))
519 		  (let ((R2 (treeIns fn r2 (diff len nLen))))
520 		       (setq level saveLevel)
521 		       (funcall fn `(,R1 ,R2)))))))
522