1 ;;;; This file contains macro-like source transformations which
2 ;;;; convert uses of certain functions into the canonical form desired
3 ;;;; within the compiler. ### and other IR1 transforms and stuff.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
19 ;;; Convert into an IF so that IF optimizations will eliminate redundant
21 (def-source-transform not (x) `(if ,x nil t))
22 (def-source-transform null (x) `(if ,x nil t))
24 ;;; ENDP is just NULL with a LIST assertion.
25 (def-source-transform endp (x) `(null (the list ,x)))
26 ;;; FIXME: Is THE LIST a strong enough assertion for ANSI's "should
27 ;;; return an error"? (THE LIST is optimized away when safety is low;
28 ;;; does that satisfy the spec?)
30 ;;; We turn IDENTITY into PROG1 so that it is obvious that it just
31 ;;; returns the first value of its argument. Ditto for VALUES with one
33 (def-source-transform identity (x) `(prog1 ,x))
34 (def-source-transform values (x) `(prog1 ,x))
36 ;;; Bind the values and make a closure that returns them.
37 (def-source-transform constantly (value &rest values)
38 (let ((temps (make-gensym-list (1+ (length values))))
40 `(let ,(loop for temp in temps and
41 value in (list* value values)
42 collect `(,temp ,value))
43 #'(lambda (&rest ,dum)
44 (declare (ignore ,dum))
47 ;;; If the function has a known number of arguments, then return a
48 ;;; lambda with the appropriate fixed number of args. If the
49 ;;; destination is a FUNCALL, then do the &REST APPLY thing, and let
50 ;;; MV optimization figure things out.
51 (deftransform complement ((fun) * * :node node :when :both)
53 (multiple-value-bind (min max)
54 (function-type-nargs (continuation-type fun))
56 ((and min (eql min max))
57 (let ((dums (make-gensym-list min)))
58 `#'(lambda ,dums (not (funcall fun ,@dums)))))
59 ((let* ((cont (node-cont node))
60 (dest (continuation-dest cont)))
61 (and (combination-p dest)
62 (eq (combination-fun dest) cont)))
63 '#'(lambda (&rest args)
64 (not (apply fun args))))
66 (give-up-ir1-transform
67 "The function doesn't have a fixed argument count.")))))
71 ;;; Translate CxxR into CAR/CDR combos.
73 (defun source-transform-cxr (form)
74 (if (or (byte-compiling) (/= (length form) 2))
76 (let ((name (symbol-name (car form))))
77 (do ((i (- (length name) 2) (1- i))
79 `(,(ecase (char name i)
86 (b '(1 0) (cons i b)))
88 (dotimes (j (ash 1 i))
89 (setf (info :function :source-transform
90 (intern (format nil "C~{~:[A~;D~]~}R"
91 (mapcar #'(lambda (x) (logbitp x j)) b))))
92 #'source-transform-cxr)))
94 ;;; Turn FIRST..FOURTH and REST into the obvious synonym, assuming
95 ;;; whatever is right for them is right for us. FIFTH..TENTH turn into
96 ;;; Nth, which can be expanded into a CAR/CDR later on if policy
98 (def-source-transform first (x) `(car ,x))
99 (def-source-transform rest (x) `(cdr ,x))
100 (def-source-transform second (x) `(cadr ,x))
101 (def-source-transform third (x) `(caddr ,x))
102 (def-source-transform fourth (x) `(cadddr ,x))
103 (def-source-transform fifth (x) `(nth 4 ,x))
104 (def-source-transform sixth (x) `(nth 5 ,x))
105 (def-source-transform seventh (x) `(nth 6 ,x))
106 (def-source-transform eighth (x) `(nth 7 ,x))
107 (def-source-transform ninth (x) `(nth 8 ,x))
108 (def-source-transform tenth (x) `(nth 9 ,x))
110 ;;; Translate RPLACx to LET and SETF.
111 (def-source-transform rplaca (x y)
116 (def-source-transform rplacd (x y)
122 (def-source-transform nth (n l) `(car (nthcdr ,n ,l)))
124 (defvar *default-nthcdr-open-code-limit* 6)
125 (defvar *extreme-nthcdr-open-code-limit* 20)
127 (deftransform nthcdr ((n l) (unsigned-byte t) * :node node)
128 "convert NTHCDR to CAxxR"
129 (unless (constant-continuation-p n)
130 (give-up-ir1-transform))
131 (let ((n (continuation-value n)))
133 (if (policy node (= speed 3) (= space 0))
134 *extreme-nthcdr-open-code-limit*
135 *default-nthcdr-open-code-limit*))
136 (give-up-ir1-transform))
141 `(cdr ,(frob (1- n))))))
144 ;;;; arithmetic and numerology
146 (def-source-transform plusp (x) `(> ,x 0))
147 (def-source-transform minusp (x) `(< ,x 0))
148 (def-source-transform zerop (x) `(= ,x 0))
150 (def-source-transform 1+ (x) `(+ ,x 1))
151 (def-source-transform 1- (x) `(- ,x 1))
153 (def-source-transform oddp (x) `(not (zerop (logand ,x 1))))
154 (def-source-transform evenp (x) `(zerop (logand ,x 1)))
156 ;;; Note that all the integer division functions are available for
157 ;;; inline expansion.
159 ;;; FIXME: DEF-FROB instead of FROB
160 (macrolet ((frob (fun)
161 `(def-source-transform ,fun (x &optional (y nil y-p))
168 #!+propagate-float-type
170 #!+propagate-float-type
173 (def-source-transform lognand (x y) `(lognot (logand ,x ,y)))
174 (def-source-transform lognor (x y) `(lognot (logior ,x ,y)))
175 (def-source-transform logandc1 (x y) `(logand (lognot ,x) ,y))
176 (def-source-transform logandc2 (x y) `(logand ,x (lognot ,y)))
177 (def-source-transform logorc1 (x y) `(logior (lognot ,x) ,y))
178 (def-source-transform logorc2 (x y) `(logior ,x (lognot ,y)))
179 (def-source-transform logtest (x y) `(not (zerop (logand ,x ,y))))
180 (def-source-transform logbitp (index integer)
181 `(not (zerop (logand (ash 1 ,index) ,integer))))
182 (def-source-transform byte (size position) `(cons ,size ,position))
183 (def-source-transform byte-size (spec) `(car ,spec))
184 (def-source-transform byte-position (spec) `(cdr ,spec))
185 (def-source-transform ldb-test (bytespec integer)
186 `(not (zerop (mask-field ,bytespec ,integer))))
188 ;;; With the ratio and complex accessors, we pick off the "identity"
189 ;;; case, and use a primitive to handle the cell access case.
190 (def-source-transform numerator (num)
191 (once-only ((n-num `(the rational ,num)))
195 (def-source-transform denominator (num)
196 (once-only ((n-num `(the rational ,num)))
198 (%denominator ,n-num)
201 ;;;; Interval arithmetic for computing bounds
202 ;;;; (toy@rtp.ericsson.se)
204 ;;;; This is a set of routines for operating on intervals. It
205 ;;;; implements a simple interval arithmetic package. Although SBCL
206 ;;;; has an interval type in numeric-type, we choose to use our own
207 ;;;; for two reasons:
209 ;;;; 1. This package is simpler than numeric-type
211 ;;;; 2. It makes debugging much easier because you can just strip
212 ;;;; out these routines and test them independently of SBCL. (a
215 ;;;; One disadvantage is a probable increase in consing because we
216 ;;;; have to create these new interval structures even though
217 ;;;; numeric-type has everything we want to know. Reason 2 wins for
220 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
222 #!+propagate-float-type
225 ;;; The basic interval type. It can handle open and closed intervals.
226 ;;; A bound is open if it is a list containing a number, just like
227 ;;; Lisp says. NIL means unbounded.
229 (:constructor %make-interval))
232 (defun make-interval (&key low high)
233 (labels ((normalize-bound (val)
234 (cond ((and (floatp val)
235 (float-infinity-p val))
240 ;; Handle any closed bounds
243 ;; We have an open bound. Normalize the numeric
244 ;; bound. If the normalized bound is still a number
245 ;; (not nil), keep the bound open. Otherwise, the
246 ;; bound is really unbounded, so drop the openness.
247 (let ((new-val (normalize-bound (first val))))
249 ;; Bound exists, so keep it open still
252 (error "Unknown bound type in make-interval!")))))
253 (%make-interval :low (normalize-bound low)
254 :high (normalize-bound high))))
256 #!-sb-fluid (declaim (inline bound-value set-bound))
258 ;;; Extract the numeric value of a bound. Return NIL, if X is NIL.
259 (defun bound-value (x)
260 (if (consp x) (car x) x))
262 ;;; Given a number X, create a form suitable as a bound for an
263 ;;; interval. Make the bound open if OPEN-P is T. NIL remains NIL.
264 (defun set-bound (x open-p)
265 (if (and x open-p) (list x) x))
267 ;;; Apply the function F to a bound X. If X is an open bound, then
268 ;;; the result will be open. IF X is NIL, the result is NIL.
269 (defun bound-func (f x)
271 (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)
272 ;; With these traps masked, we might get things like infinity
273 ;; or negative infinity returned. Check for this and return
274 ;; NIL to indicate unbounded.
275 (let ((y (funcall f (bound-value x))))
277 (float-infinity-p y))
279 (set-bound (funcall f (bound-value x)) (consp x)))))))
281 ;;; Apply a binary operator OP to two bounds X and Y. The result is
282 ;;; NIL if either is NIL. Otherwise bound is computed and the result
283 ;;; is open if either X or Y is open.
285 ;;; FIXME: only used in this file, not needed in target runtime
286 (defmacro bound-binop (op x y)
288 (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)
289 (set-bound (,op (bound-value ,x)
291 (or (consp ,x) (consp ,y))))))
293 ;;; NUMERIC-TYPE->INTERVAL
295 ;;; Convert a numeric-type object to an interval object.
297 (defun numeric-type->interval (x)
298 (declare (type numeric-type x))
299 (make-interval :low (numeric-type-low x)
300 :high (numeric-type-high x)))
302 (defun copy-interval-limit (limit)
307 (defun copy-interval (x)
308 (declare (type interval x))
309 (make-interval :low (copy-interval-limit (interval-low x))
310 :high (copy-interval-limit (interval-high x))))
314 ;;; Given a point P contained in the interval X, split X into two
315 ;;; interval at the point P. If CLOSE-LOWER is T, then the left
316 ;;; interval contains P. If CLOSE-UPPER is T, the right interval
317 ;;; contains P. You can specify both to be T or NIL.
318 (defun interval-split (p x &optional close-lower close-upper)
319 (declare (type number p)
321 (list (make-interval :low (copy-interval-limit (interval-low x))
322 :high (if close-lower p (list p)))
323 (make-interval :low (if close-upper (list p) p)
324 :high (copy-interval-limit (interval-high x)))))
328 ;;; Return the closure of the interval. That is, convert open bounds
329 ;;; to closed bounds.
330 (defun interval-closure (x)
331 (declare (type interval x))
332 (make-interval :low (bound-value (interval-low x))
333 :high (bound-value (interval-high x))))
335 (defun signed-zero->= (x y)
339 (>= (float-sign (float x))
340 (float-sign (float y))))))
342 ;;; INTERVAL-RANGE-INFO
344 ;;; For an interval X, if X >= POINT, return '+. If X <= POINT, return
345 ;;; '-. Otherwise return NIL.
347 (defun interval-range-info (x &optional (point 0))
348 (declare (type interval x))
349 (let ((lo (interval-low x))
350 (hi (interval-high x)))
351 (cond ((and lo (signed-zero->= (bound-value lo) point))
353 ((and hi (signed-zero->= point (bound-value hi)))
357 (defun interval-range-info (x &optional (point 0))
358 (declare (type interval x))
359 (labels ((signed->= (x y)
360 (if (and (zerop x) (zerop y) (floatp x) (floatp y))
361 (>= (float-sign x) (float-sign y))
363 (let ((lo (interval-low x))
364 (hi (interval-high x)))
365 (cond ((and lo (signed->= (bound-value lo) point))
367 ((and hi (signed->= point (bound-value hi)))
372 ;;; INTERVAL-BOUNDED-P
374 ;;; Test to see whether the interval X is bounded. HOW determines the
375 ;;; test, and should be either ABOVE, BELOW, or BOTH.
376 (defun interval-bounded-p (x how)
377 (declare (type interval x))
384 (and (interval-low x) (interval-high x)))))
386 ;;; Signed zero comparison functions. Use these functions if we need
387 ;;; to distinguish between signed zeroes.
389 (defun signed-zero-< (x y)
393 (< (float-sign (float x))
394 (float-sign (float y))))))
395 (defun signed-zero-> (x y)
399 (> (float-sign (float x))
400 (float-sign (float y))))))
402 (defun signed-zero-= (x y)
405 (= (float-sign (float x))
406 (float-sign (float y)))))
408 (defun signed-zero-<= (x y)
412 (<= (float-sign (float x))
413 (float-sign (float y))))))
415 ;;; INTERVAL-CONTAINS-P
417 ;;; See whether the interval X contains the number P, taking into account
418 ;;; that the interval might not be closed.
419 (defun interval-contains-p (p x)
420 (declare (type number p)
422 ;; Does the interval X contain the number P? This would be a lot
423 ;; easier if all intervals were closed!
424 (let ((lo (interval-low x))
425 (hi (interval-high x)))
427 ;; The interval is bounded
428 (if (and (signed-zero-<= (bound-value lo) p)
429 (signed-zero-<= p (bound-value hi)))
430 ;; P is definitely in the closure of the interval.
431 ;; We just need to check the end points now.
432 (cond ((signed-zero-= p (bound-value lo))
434 ((signed-zero-= p (bound-value hi))
439 ;; Interval with upper bound
440 (if (signed-zero-< p (bound-value hi))
442 (and (numberp hi) (signed-zero-= p hi))))
444 ;; Interval with lower bound
445 (if (signed-zero-> p (bound-value lo))
447 (and (numberp lo) (signed-zero-= p lo))))
449 ;; Interval with no bounds
452 ;;; INTERVAL-INTERSECT-P
454 ;;; Determine if two intervals X and Y intersect. Return T if so. If
455 ;;; CLOSED-INTERVALS-P is T, the treat the intervals as if they were
456 ;;; closed. Otherwise the intervals are treated as they are.
458 ;;; Thus if X = [0, 1) and Y = (1, 2), then they do not intersect
459 ;;; because no element in X is in Y. However, if CLOSED-INTERVALS-P
460 ;;; is T, then they do intersect because we use the closure of X = [0,
461 ;;; 1] and Y = [1, 2] to determine intersection.
462 (defun interval-intersect-p (x y &optional closed-intervals-p)
463 (declare (type interval x y))
464 (multiple-value-bind (intersect diff)
465 (interval-intersection/difference (if closed-intervals-p
468 (if closed-intervals-p
471 (declare (ignore diff))
474 ;;; Are the two intervals adjacent? That is, is there a number
475 ;;; between the two intervals that is not an element of either
476 ;;; interval? If so, they are not adjacent. For example [0, 1) and
477 ;;; [1, 2] are adjacent but [0, 1) and (1, 2] are not because 1 lies
478 ;;; between both intervals.
479 (defun interval-adjacent-p (x y)
480 (declare (type interval x y))
481 (flet ((adjacent (lo hi)
482 ;; Check to see whether lo and hi are adjacent. If either is
483 ;; nil, they can't be adjacent.
484 (when (and lo hi (= (bound-value lo) (bound-value hi)))
485 ;; The bounds are equal. They are adjacent if one of
486 ;; them is closed (a number). If both are open (consp),
487 ;; then there is a number that lies between them.
488 (or (numberp lo) (numberp hi)))))
489 (or (adjacent (interval-low y) (interval-high x))
490 (adjacent (interval-low x) (interval-high y)))))
492 ;;; INTERVAL-INTERSECTION/DIFFERENCE
494 ;;; Compute the intersection and difference between two intervals.
495 ;;; Two values are returned: the intersection and the difference.
497 ;;; Let the two intervals be X and Y, and let I and D be the two
498 ;;; values returned by this function. Then I = X intersect Y. If I
499 ;;; is NIL (the empty set), then D is X union Y, represented as the
500 ;;; list of X and Y. If I is not the empty set, then D is (X union Y)
501 ;;; - I, which is a list of two intervals.
503 ;;; For example, let X = [1,5] and Y = [-1,3). Then I = [1,3) and D =
504 ;;; [-1,1) union [3,5], which is returned as a list of two intervals.
505 (defun interval-intersection/difference (x y)
506 (declare (type interval x y))
507 (let ((x-lo (interval-low x))
508 (x-hi (interval-high x))
509 (y-lo (interval-low y))
510 (y-hi (interval-high y)))
513 ;; If p is an open bound, make it closed. If p is a closed
514 ;; bound, make it open.
519 ;; Test whether P is in the interval.
520 (when (interval-contains-p (bound-value p)
521 (interval-closure int))
522 (let ((lo (interval-low int))
523 (hi (interval-high int)))
524 ;; Check for endpoints
525 (cond ((and lo (= (bound-value p) (bound-value lo)))
526 (not (and (consp p) (numberp lo))))
527 ((and hi (= (bound-value p) (bound-value hi)))
528 (not (and (numberp p) (consp hi))))
530 (test-lower-bound (p int)
531 ;; P is a lower bound of an interval.
534 (not (interval-bounded-p int 'below))))
535 (test-upper-bound (p int)
536 ;; P is an upper bound of an interval
539 (not (interval-bounded-p int 'above)))))
540 (let ((x-lo-in-y (test-lower-bound x-lo y))
541 (x-hi-in-y (test-upper-bound x-hi y))
542 (y-lo-in-x (test-lower-bound y-lo x))
543 (y-hi-in-x (test-upper-bound y-hi x)))
544 (cond ((or x-lo-in-y x-hi-in-y y-lo-in-x y-hi-in-x)
545 ;; Intervals intersect. Let's compute the intersection
546 ;; and the difference.
547 (multiple-value-bind (lo left-lo left-hi)
548 (cond (x-lo-in-y (values x-lo y-lo (opposite-bound x-lo)))
549 (y-lo-in-x (values y-lo x-lo (opposite-bound y-lo))))
550 (multiple-value-bind (hi right-lo right-hi)
552 (values x-hi (opposite-bound x-hi) y-hi))
554 (values y-hi (opposite-bound y-hi) x-hi)))
555 (values (make-interval :low lo :high hi)
556 (list (make-interval :low left-lo :high left-hi)
557 (make-interval :low right-lo :high right-hi))))))
559 (values nil (list x y))))))))
561 ;;; INTERVAL-MERGE-PAIR
563 ;;; If intervals X and Y intersect, return a new interval that is the
564 ;;; union of the two. If they do not intersect, return NIL.
565 (defun interval-merge-pair (x y)
566 (declare (type interval x y))
567 ;; If x and y intersect or are adjacent, create the union.
568 ;; Otherwise return nil
569 (when (or (interval-intersect-p x y)
570 (interval-adjacent-p x y))
571 (flet ((select-bound (x1 x2 min-op max-op)
572 (let ((x1-val (bound-value x1))
573 (x2-val (bound-value x2)))
575 ;; Both bounds are finite. Select the right one.
576 (cond ((funcall min-op x1-val x2-val)
577 ;; x1 definitely better
579 ((funcall max-op x1-val x2-val)
580 ;; x2 definitely better
583 ;; Bounds are equal. Select either
584 ;; value and make it open only if
586 (set-bound x1-val (and (consp x1) (consp x2))))))
588 ;; At least one bound is not finite. The
589 ;; non-finite bound always wins.
591 (let* ((x-lo (copy-interval-limit (interval-low x)))
592 (x-hi (copy-interval-limit (interval-high x)))
593 (y-lo (copy-interval-limit (interval-low y)))
594 (y-hi (copy-interval-limit (interval-high y))))
595 (make-interval :low (select-bound x-lo y-lo #'< #'>)
596 :high (select-bound x-hi y-hi #'> #'<))))))
598 ;;; Basic arithmetic operations on intervals. We probably should do
599 ;;; true interval arithmetic here, but it's complicated because we
600 ;;; have float and integer types and bounds can be open or closed.
604 ;;; The negative of an interval
605 (defun interval-neg (x)
606 (declare (type interval x))
607 (make-interval :low (bound-func #'- (interval-high x))
608 :high (bound-func #'- (interval-low x))))
612 ;;; Add two intervals
613 (defun interval-add (x y)
614 (declare (type interval x y))
615 (make-interval :low (bound-binop + (interval-low x) (interval-low y))
616 :high (bound-binop + (interval-high x) (interval-high y))))
620 ;;; Subtract two intervals
621 (defun interval-sub (x y)
622 (declare (type interval x y))
623 (make-interval :low (bound-binop - (interval-low x) (interval-high y))
624 :high (bound-binop - (interval-high x) (interval-low y))))
628 ;;; Multiply two intervals
629 (defun interval-mul (x y)
630 (declare (type interval x y))
631 (flet ((bound-mul (x y)
632 (cond ((or (null x) (null y))
633 ;; Multiply by infinity is infinity
635 ((or (and (numberp x) (zerop x))
636 (and (numberp y) (zerop y)))
637 ;; Multiply by closed zero is special. The result
638 ;; is always a closed bound. But don't replace this
639 ;; with zero; we want the multiplication to produce
640 ;; the correct signed zero, if needed.
641 (* (bound-value x) (bound-value y)))
642 ((or (and (floatp x) (float-infinity-p x))
643 (and (floatp y) (float-infinity-p y)))
644 ;; Infinity times anything is infinity
647 ;; General multiply. The result is open if either is open.
648 (bound-binop * x y)))))
649 (let ((x-range (interval-range-info x))
650 (y-range (interval-range-info y)))
651 (cond ((null x-range)
652 ;; Split x into two and multiply each separately
653 (destructuring-bind (x- x+) (interval-split 0 x t t)
654 (interval-merge-pair (interval-mul x- y)
655 (interval-mul x+ y))))
657 ;; Split y into two and multiply each separately
658 (destructuring-bind (y- y+) (interval-split 0 y t t)
659 (interval-merge-pair (interval-mul x y-)
660 (interval-mul x y+))))
662 (interval-neg (interval-mul (interval-neg x) y)))
664 (interval-neg (interval-mul x (interval-neg y))))
665 ((and (eq x-range '+) (eq y-range '+))
666 ;; If we are here, X and Y are both positive
667 (make-interval :low (bound-mul (interval-low x) (interval-low y))
668 :high (bound-mul (interval-high x) (interval-high y))))
670 (error "This shouldn't happen!"))))))
674 ;;; Divide two intervals.
675 (defun interval-div (top bot)
676 (declare (type interval top bot))
677 (flet ((bound-div (x y y-low-p)
680 ;; Divide by infinity means result is 0. However,
681 ;; we need to watch out for the sign of the result,
682 ;; to correctly handle signed zeros. We also need
683 ;; to watch out for positive or negative infinity.
684 (if (floatp (bound-value x))
686 (- (float-sign (bound-value x) 0.0))
687 (float-sign (bound-value x) 0.0))
689 ((zerop (bound-value y))
690 ;; Divide by zero means result is infinity
692 ((and (numberp x) (zerop x))
693 ;; Zero divided by anything is zero.
696 (bound-binop / x y)))))
697 (let ((top-range (interval-range-info top))
698 (bot-range (interval-range-info bot)))
699 (cond ((null bot-range)
700 ;; The denominator contains zero, so anything goes!
701 (make-interval :low nil :high nil))
703 ;; Denominator is negative so flip the sign, compute the
704 ;; result, and flip it back.
705 (interval-neg (interval-div top (interval-neg bot))))
707 ;; Split top into two positive and negative parts, and
708 ;; divide each separately
709 (destructuring-bind (top- top+) (interval-split 0 top t t)
710 (interval-merge-pair (interval-div top- bot)
711 (interval-div top+ bot))))
713 ;; Top is negative so flip the sign, divide, and flip the
714 ;; sign of the result.
715 (interval-neg (interval-div (interval-neg top) bot)))
716 ((and (eq top-range '+) (eq bot-range '+))
718 (make-interval :low (bound-div (interval-low top) (interval-high bot) t)
719 :high (bound-div (interval-high top) (interval-low bot) nil)))
721 (error "This shouldn't happen!"))))))
725 ;;; Apply the function F to the interval X. If X = [a, b], then the
726 ;;; result is [f(a), f(b)]. It is up to the user to make sure the
727 ;;; result makes sense. It will if F is monotonic increasing (or
729 (defun interval-func (f x)
730 (declare (type interval x))
731 (let ((lo (bound-func f (interval-low x)))
732 (hi (bound-func f (interval-high x))))
733 (make-interval :low lo :high hi)))
737 ;;; Return T if X < Y. That is every number in the interval X is
738 ;;; always less than any number in the interval Y.
739 (defun interval-< (x y)
740 (declare (type interval x y))
741 ;; X < Y only if X is bounded above, Y is bounded below, and they
743 (when (and (interval-bounded-p x 'above)
744 (interval-bounded-p y 'below))
745 ;; Intervals are bounded in the appropriate way. Make sure they
747 (let ((left (interval-high x))
748 (right (interval-low y)))
749 (cond ((> (bound-value left)
751 ;; Definitely overlap so result is NIL
753 ((< (bound-value left)
755 ;; Definitely don't touch, so result is T
758 ;; Limits are equal. Check for open or closed bounds.
759 ;; Don't overlap if one or the other are open.
760 (or (consp left) (consp right)))))))
764 ;;; Return T if X >= Y. That is, every number in the interval X is
765 ;;; always greater than any number in the interval Y.
766 (defun interval->= (x y)
767 (declare (type interval x y))
768 ;; X >= Y if lower bound of X >= upper bound of Y
769 (when (and (interval-bounded-p x 'below)
770 (interval-bounded-p y 'above))
771 (>= (bound-value (interval-low x)) (bound-value (interval-high y)))))
775 ;;; Return an interval that is the absolute value of X. Thus, if X =
776 ;;; [-1 10], the result is [0, 10].
777 (defun interval-abs (x)
778 (declare (type interval x))
779 (case (interval-range-info x)
785 (destructuring-bind (x- x+) (interval-split 0 x t t)
786 (interval-merge-pair (interval-neg x-) x+)))))
790 ;;; Compute the square of an interval.
791 (defun interval-sqr (x)
792 (declare (type interval x))
793 (interval-func #'(lambda (x) (* x x))
797 ;;;; numeric derive-type methods
799 ;;; Utility for defining derive-type methods of integer operations. If the
800 ;;; types of both X and Y are integer types, then we compute a new integer type
801 ;;; with bounds determined Fun when applied to X and Y. Otherwise, we use
802 ;;; Numeric-Contagion.
803 (defun derive-integer-type (x y fun)
804 (declare (type continuation x y) (type function fun))
805 (let ((x (continuation-type x))
806 (y (continuation-type y)))
807 (if (and (numeric-type-p x) (numeric-type-p y)
808 (eq (numeric-type-class x) 'integer)
809 (eq (numeric-type-class y) 'integer)
810 (eq (numeric-type-complexp x) :real)
811 (eq (numeric-type-complexp y) :real))
812 (multiple-value-bind (low high) (funcall fun x y)
813 (make-numeric-type :class 'integer
817 (numeric-contagion x y))))
819 #!+(or propagate-float-type propagate-fun-type)
822 ;; Simple utility to flatten a list
823 (defun flatten-list (x)
824 (labels ((flatten-helper (x r);; 'r' is the stuff to the 'right'.
828 (t (flatten-helper (car x)
829 (flatten-helper (cdr x) r))))))
830 (flatten-helper x nil)))
832 ;;; Take some type of continuation and massage it so that we get a
833 ;;; list of the constituent types. If ARG is *EMPTY-TYPE*, return NIL
834 ;;; to indicate failure.
835 (defun prepare-arg-for-derive-type (arg)
836 (flet ((listify (arg)
841 (union-type-types arg))
844 (unless (eq arg *empty-type*)
845 ;; Make sure all args are some type of numeric-type. For member
846 ;; types, convert the list of members into a union of equivalent
847 ;; single-element member-type's.
848 (let ((new-args nil))
849 (dolist (arg (listify arg))
850 (if (member-type-p arg)
851 ;; Run down the list of members and convert to a list of
853 (dolist (member (member-type-members arg))
854 (push (if (numberp member)
855 (make-member-type :members (list member))
858 (push arg new-args)))
859 (unless (member *empty-type* new-args)
862 ;;; Convert from the standard type convention for which -0.0 and 0.0
863 ;;; and equal to an intermediate convention for which they are
864 ;;; considered different which is more natural for some of the
866 #!-negative-zero-is-not-zero
867 (defun convert-numeric-type (type)
868 (declare (type numeric-type type))
869 ;;; Only convert real float interval delimiters types.
870 (if (eq (numeric-type-complexp type) :real)
871 (let* ((lo (numeric-type-low type))
872 (lo-val (bound-value lo))
873 (lo-float-zero-p (and lo (floatp lo-val) (= lo-val 0.0)))
874 (hi (numeric-type-high type))
875 (hi-val (bound-value hi))
876 (hi-float-zero-p (and hi (floatp hi-val) (= hi-val 0.0))))
877 (if (or lo-float-zero-p hi-float-zero-p)
879 :class (numeric-type-class type)
880 :format (numeric-type-format type)
882 :low (if lo-float-zero-p
884 (list (float 0.0 lo-val))
887 :high (if hi-float-zero-p
889 (list (float -0.0 hi-val))
896 ;;; Convert back from the intermediate convention for which -0.0 and
897 ;;; 0.0 are considered different to the standard type convention for
899 #!-negative-zero-is-not-zero
900 (defun convert-back-numeric-type (type)
901 (declare (type numeric-type type))
902 ;;; Only convert real float interval delimiters types.
903 (if (eq (numeric-type-complexp type) :real)
904 (let* ((lo (numeric-type-low type))
905 (lo-val (bound-value lo))
907 (and lo (floatp lo-val) (= lo-val 0.0)
908 (float-sign lo-val)))
909 (hi (numeric-type-high type))
910 (hi-val (bound-value hi))
912 (and hi (floatp hi-val) (= hi-val 0.0)
913 (float-sign hi-val))))
915 ;; (float +0.0 +0.0) => (member 0.0)
916 ;; (float -0.0 -0.0) => (member -0.0)
917 ((and lo-float-zero-p hi-float-zero-p)
918 ;; Shouldn't have exclusive bounds here.
919 (assert (and (not (consp lo)) (not (consp hi))))
920 (if (= lo-float-zero-p hi-float-zero-p)
921 ;; (float +0.0 +0.0) => (member 0.0)
922 ;; (float -0.0 -0.0) => (member -0.0)
923 (specifier-type `(member ,lo-val))
924 ;; (float -0.0 +0.0) => (float 0.0 0.0)
925 ;; (float +0.0 -0.0) => (float 0.0 0.0)
926 (make-numeric-type :class (numeric-type-class type)
927 :format (numeric-type-format type)
933 ;; (float -0.0 x) => (float 0.0 x)
934 ((and (not (consp lo)) (minusp lo-float-zero-p))
935 (make-numeric-type :class (numeric-type-class type)
936 :format (numeric-type-format type)
938 :low (float 0.0 lo-val)
940 ;; (float (+0.0) x) => (float (0.0) x)
941 ((and (consp lo) (plusp lo-float-zero-p))
942 (make-numeric-type :class (numeric-type-class type)
943 :format (numeric-type-format type)
945 :low (list (float 0.0 lo-val))
948 ;; (float +0.0 x) => (or (member 0.0) (float (0.0) x))
949 ;; (float (-0.0) x) => (or (member 0.0) (float (0.0) x))
950 (list (make-member-type :members (list (float 0.0 lo-val)))
951 (make-numeric-type :class (numeric-type-class type)
952 :format (numeric-type-format type)
954 :low (list (float 0.0 lo-val))
958 ;; (float x +0.0) => (float x 0.0)
959 ((and (not (consp hi)) (plusp hi-float-zero-p))
960 (make-numeric-type :class (numeric-type-class type)
961 :format (numeric-type-format type)
964 :high (float 0.0 hi-val)))
965 ;; (float x (-0.0)) => (float x (0.0))
966 ((and (consp hi) (minusp hi-float-zero-p))
967 (make-numeric-type :class (numeric-type-class type)
968 :format (numeric-type-format type)
971 :high (list (float 0.0 hi-val))))
973 ;; (float x (+0.0)) => (or (member -0.0) (float x (0.0)))
974 ;; (float x -0.0) => (or (member -0.0) (float x (0.0)))
975 (list (make-member-type :members (list (float -0.0 hi-val)))
976 (make-numeric-type :class (numeric-type-class type)
977 :format (numeric-type-format type)
980 :high (list (float 0.0 hi-val)))))))
986 ;;; Convert back a possible list of numeric types.
987 #!-negative-zero-is-not-zero
988 (defun convert-back-numeric-type-list (type-list)
992 (dolist (type type-list)
993 (if (numeric-type-p type)
994 (let ((result (convert-back-numeric-type type)))
996 (setf results (append results result))
997 (push result results)))
998 (push type results)))
1001 (convert-back-numeric-type type-list))
1003 (convert-back-numeric-type-list (union-type-types type-list)))
1007 ;;; Make-Canonical-Union-Type
1009 ;;; Take a list of types and return a canonical type specifier,
1010 ;;; combining any members types together. If both positive and
1011 ;;; negative members types are present they are converted to a float
1012 ;;; type. X This would be far simpler if the type-union methods could
1013 ;;; handle member/number unions.
1014 (defun make-canonical-union-type (type-list)
1017 (dolist (type type-list)
1018 (if (member-type-p type)
1019 (setf members (union members (member-type-members type)))
1020 (push type misc-types)))
1022 (when (null (set-difference '(-0l0 0l0) members))
1023 #!-negative-zero-is-not-zero
1024 (push (specifier-type '(long-float 0l0 0l0)) misc-types)
1025 #!+negative-zero-is-not-zero
1026 (push (specifier-type '(long-float -0l0 0l0)) misc-types)
1027 (setf members (set-difference members '(-0l0 0l0))))
1028 (when (null (set-difference '(-0d0 0d0) members))
1029 #!-negative-zero-is-not-zero
1030 (push (specifier-type '(double-float 0d0 0d0)) misc-types)
1031 #!+negative-zero-is-not-zero
1032 (push (specifier-type '(double-float -0d0 0d0)) misc-types)
1033 (setf members (set-difference members '(-0d0 0d0))))
1034 (when (null (set-difference '(-0f0 0f0) members))
1035 #!-negative-zero-is-not-zero
1036 (push (specifier-type '(single-float 0f0 0f0)) misc-types)
1037 #!+negative-zero-is-not-zero
1038 (push (specifier-type '(single-float -0f0 0f0)) misc-types)
1039 (setf members (set-difference members '(-0f0 0f0))))
1040 (cond ((null members)
1041 (let ((res (first misc-types)))
1042 (dolist (type (rest misc-types))
1043 (setq res (type-union res type)))
1046 (make-member-type :members members))
1048 (let ((res (first misc-types)))
1049 (dolist (type (rest misc-types))
1050 (setq res (type-union res type)))
1051 (dolist (type members)
1052 (setq res (type-union
1053 res (make-member-type :members (list type)))))
1056 ;;; Convert-Member-Type
1058 ;;; Convert a member type with a single member to a numeric type.
1059 (defun convert-member-type (arg)
1060 (let* ((members (member-type-members arg))
1061 (member (first members))
1062 (member-type (type-of member)))
1063 (assert (not (rest members)))
1064 (specifier-type `(,(if (subtypep member-type 'integer)
1069 ;;; ONE-ARG-DERIVE-TYPE
1071 ;;; This is used in defoptimizers for computing the resulting type of
1074 ;;; Given the continuation ARG, derive the resulting type using the
1075 ;;; DERIVE-FCN. DERIVE-FCN takes exactly one argument which is some
1076 ;;; "atomic" continuation type like numeric-type or member-type
1077 ;;; (containing just one element). It should return the resulting
1078 ;;; type, which can be a list of types.
1080 ;;; For the case of member types, if a member-fcn is given it is
1081 ;;; called to compute the result otherwise the member type is first
1082 ;;; converted to a numeric type and the derive-fcn is call.
1083 (defun one-arg-derive-type (arg derive-fcn member-fcn
1084 &optional (convert-type t))
1085 (declare (type function derive-fcn)
1086 (type (or null function) member-fcn)
1087 #!+negative-zero-is-not-zero (ignore convert-type))
1088 (let ((arg-list (prepare-arg-for-derive-type (continuation-type arg))))
1094 (with-float-traps-masked
1095 (:underflow :overflow :divide-by-zero)
1099 (first (member-type-members x))))))
1100 ;; Otherwise convert to a numeric type.
1101 (let ((result-type-list
1102 (funcall derive-fcn (convert-member-type x))))
1103 #!-negative-zero-is-not-zero
1105 (convert-back-numeric-type-list result-type-list)
1107 #!+negative-zero-is-not-zero
1110 #!-negative-zero-is-not-zero
1112 (convert-back-numeric-type-list
1113 (funcall derive-fcn (convert-numeric-type x)))
1114 (funcall derive-fcn x))
1115 #!+negative-zero-is-not-zero
1116 (funcall derive-fcn x))
1118 *universal-type*))))
1119 ;; Run down the list of args and derive the type of each one,
1120 ;; saving all of the results in a list.
1121 (let ((results nil))
1122 (dolist (arg arg-list)
1123 (let ((result (deriver arg)))
1125 (setf results (append results result))
1126 (push result results))))
1128 (make-canonical-union-type results)
1129 (first results)))))))
1131 ;;; TWO-ARG-DERIVE-TYPE
1133 ;;; Same as ONE-ARG-DERIVE-TYPE, except we assume the function takes
1134 ;;; two arguments. DERIVE-FCN takes 3 args in this case: the two
1135 ;;; original args and a third which is T to indicate if the two args
1136 ;;; really represent the same continuation. This is useful for
1137 ;;; deriving the type of things like (* x x), which should always be
1138 ;;; positive. If we didn't do this, we wouldn't be able to tell.
1139 (defun two-arg-derive-type (arg1 arg2 derive-fcn fcn
1140 &optional (convert-type t))
1141 #!+negative-zero-is-not-zero
1142 (declare (ignore convert-type))
1143 (flet (#!-negative-zero-is-not-zero
1144 (deriver (x y same-arg)
1145 (cond ((and (member-type-p x) (member-type-p y))
1146 (let* ((x (first (member-type-members x)))
1147 (y (first (member-type-members y)))
1148 (result (with-float-traps-masked
1149 (:underflow :overflow :divide-by-zero
1151 (funcall fcn x y))))
1152 (cond ((null result))
1153 ((and (floatp result) (float-nan-p result))
1156 :format (type-of result)
1159 (make-member-type :members (list result))))))
1160 ((and (member-type-p x) (numeric-type-p y))
1161 (let* ((x (convert-member-type x))
1162 (y (if convert-type (convert-numeric-type y) y))
1163 (result (funcall derive-fcn x y same-arg)))
1165 (convert-back-numeric-type-list result)
1167 ((and (numeric-type-p x) (member-type-p y))
1168 (let* ((x (if convert-type (convert-numeric-type x) x))
1169 (y (convert-member-type y))
1170 (result (funcall derive-fcn x y same-arg)))
1172 (convert-back-numeric-type-list result)
1174 ((and (numeric-type-p x) (numeric-type-p y))
1175 (let* ((x (if convert-type (convert-numeric-type x) x))
1176 (y (if convert-type (convert-numeric-type y) y))
1177 (result (funcall derive-fcn x y same-arg)))
1179 (convert-back-numeric-type-list result)
1183 #!+negative-zero-is-not-zero
1184 (deriver (x y same-arg)
1185 (cond ((and (member-type-p x) (member-type-p y))
1186 (let* ((x (first (member-type-members x)))
1187 (y (first (member-type-members y)))
1188 (result (with-float-traps-masked
1189 (:underflow :overflow :divide-by-zero)
1190 (funcall fcn x y))))
1192 (make-member-type :members (list result)))))
1193 ((and (member-type-p x) (numeric-type-p y))
1194 (let ((x (convert-member-type x)))
1195 (funcall derive-fcn x y same-arg)))
1196 ((and (numeric-type-p x) (member-type-p y))
1197 (let ((y (convert-member-type y)))
1198 (funcall derive-fcn x y same-arg)))
1199 ((and (numeric-type-p x) (numeric-type-p y))
1200 (funcall derive-fcn x y same-arg))
1202 *universal-type*))))
1203 (let ((same-arg (same-leaf-ref-p arg1 arg2))
1204 (a1 (prepare-arg-for-derive-type (continuation-type arg1)))
1205 (a2 (prepare-arg-for-derive-type (continuation-type arg2))))
1207 (let ((results nil))
1209 ;; Since the args are the same continuation, just run
1212 (let ((result (deriver x x same-arg)))
1214 (setf results (append results result))
1215 (push result results))))
1216 ;; Try all pairwise combinations.
1219 (let ((result (or (deriver x y same-arg)
1220 (numeric-contagion x y))))
1222 (setf results (append results result))
1223 (push result results))))))
1225 (make-canonical-union-type results)
1226 (first results)))))))
1230 #!-propagate-float-type
1232 (defoptimizer (+ derive-type) ((x y))
1233 (derive-integer-type
1240 (values (frob (numeric-type-low x) (numeric-type-low y))
1241 (frob (numeric-type-high x) (numeric-type-high y)))))))
1243 (defoptimizer (- derive-type) ((x y))
1244 (derive-integer-type
1251 (values (frob (numeric-type-low x) (numeric-type-high y))
1252 (frob (numeric-type-high x) (numeric-type-low y)))))))
1254 (defoptimizer (* derive-type) ((x y))
1255 (derive-integer-type
1258 (let ((x-low (numeric-type-low x))
1259 (x-high (numeric-type-high x))
1260 (y-low (numeric-type-low y))
1261 (y-high (numeric-type-high y)))
1262 (cond ((not (and x-low y-low))
1264 ((or (minusp x-low) (minusp y-low))
1265 (if (and x-high y-high)
1266 (let ((max (* (max (abs x-low) (abs x-high))
1267 (max (abs y-low) (abs y-high)))))
1268 (values (- max) max))
1271 (values (* x-low y-low)
1272 (if (and x-high y-high)
1276 (defoptimizer (/ derive-type) ((x y))
1277 (numeric-contagion (continuation-type x) (continuation-type y)))
1281 #!+propagate-float-type
1283 (defun +-derive-type-aux (x y same-arg)
1284 (if (and (numeric-type-real-p x)
1285 (numeric-type-real-p y))
1288 (let ((x-int (numeric-type->interval x)))
1289 (interval-add x-int x-int))
1290 (interval-add (numeric-type->interval x)
1291 (numeric-type->interval y))))
1292 (result-type (numeric-contagion x y)))
1293 ;; If the result type is a float, we need to be sure to coerce
1294 ;; the bounds into the correct type.
1295 (when (eq (numeric-type-class result-type) 'float)
1296 (setf result (interval-func
1298 (coerce x (or (numeric-type-format result-type)
1302 :class (if (and (eq (numeric-type-class x) 'integer)
1303 (eq (numeric-type-class y) 'integer))
1304 ;; The sum of integers is always an integer
1306 (numeric-type-class result-type))
1307 :format (numeric-type-format result-type)
1308 :low (interval-low result)
1309 :high (interval-high result)))
1310 ;; General contagion
1311 (numeric-contagion x y)))
1313 (defoptimizer (+ derive-type) ((x y))
1314 (two-arg-derive-type x y #'+-derive-type-aux #'+))
1316 (defun --derive-type-aux (x y same-arg)
1317 (if (and (numeric-type-real-p x)
1318 (numeric-type-real-p y))
1320 ;; (- x x) is always 0.
1322 (make-interval :low 0 :high 0)
1323 (interval-sub (numeric-type->interval x)
1324 (numeric-type->interval y))))
1325 (result-type (numeric-contagion x y)))
1326 ;; If the result type is a float, we need to be sure to coerce
1327 ;; the bounds into the correct type.
1328 (when (eq (numeric-type-class result-type) 'float)
1329 (setf result (interval-func
1331 (coerce x (or (numeric-type-format result-type)
1335 :class (if (and (eq (numeric-type-class x) 'integer)
1336 (eq (numeric-type-class y) 'integer))
1337 ;; The difference of integers is always an integer
1339 (numeric-type-class result-type))
1340 :format (numeric-type-format result-type)
1341 :low (interval-low result)
1342 :high (interval-high result)))
1343 ;; General contagion
1344 (numeric-contagion x y)))
1346 (defoptimizer (- derive-type) ((x y))
1347 (two-arg-derive-type x y #'--derive-type-aux #'-))
1349 (defun *-derive-type-aux (x y same-arg)
1350 (if (and (numeric-type-real-p x)
1351 (numeric-type-real-p y))
1353 ;; (* x x) is always positive, so take care to do it
1356 (interval-sqr (numeric-type->interval x))
1357 (interval-mul (numeric-type->interval x)
1358 (numeric-type->interval y))))
1359 (result-type (numeric-contagion x y)))
1360 ;; If the result type is a float, we need to be sure to coerce
1361 ;; the bounds into the correct type.
1362 (when (eq (numeric-type-class result-type) 'float)
1363 (setf result (interval-func
1365 (coerce x (or (numeric-type-format result-type)
1369 :class (if (and (eq (numeric-type-class x) 'integer)
1370 (eq (numeric-type-class y) 'integer))
1371 ;; The product of integers is always an integer
1373 (numeric-type-class result-type))
1374 :format (numeric-type-format result-type)
1375 :low (interval-low result)
1376 :high (interval-high result)))
1377 (numeric-contagion x y)))
1379 (defoptimizer (* derive-type) ((x y))
1380 (two-arg-derive-type x y #'*-derive-type-aux #'*))
1382 (defun /-derive-type-aux (x y same-arg)
1383 (if (and (numeric-type-real-p x)
1384 (numeric-type-real-p y))
1386 ;; (/ x x) is always 1, except if x can contain 0. In
1387 ;; that case, we shouldn't optimize the division away
1388 ;; because we want 0/0 to signal an error.
1390 (not (interval-contains-p
1391 0 (interval-closure (numeric-type->interval y)))))
1392 (make-interval :low 1 :high 1)
1393 (interval-div (numeric-type->interval x)
1394 (numeric-type->interval y))))
1395 (result-type (numeric-contagion x y)))
1396 ;; If the result type is a float, we need to be sure to coerce
1397 ;; the bounds into the correct type.
1398 (when (eq (numeric-type-class result-type) 'float)
1399 (setf result (interval-func
1401 (coerce x (or (numeric-type-format result-type)
1404 (make-numeric-type :class (numeric-type-class result-type)
1405 :format (numeric-type-format result-type)
1406 :low (interval-low result)
1407 :high (interval-high result)))
1408 (numeric-contagion x y)))
1410 (defoptimizer (/ derive-type) ((x y))
1411 (two-arg-derive-type x y #'/-derive-type-aux #'/))
1415 ;;; KLUDGE: All this ASH optimization is suppressed under CMU CL
1416 ;;; because as of version 2.4.6 for Debian, CMU CL blows up on (ASH
1417 ;;; 1000000000 -100000000000) (i.e. ASH of two bignums yielding zero)
1418 ;;; and it's hard to avoid that calculation in here.
1419 #-(and cmu sb-xc-host)
1421 #!-propagate-fun-type
1422 (defoptimizer (ash derive-type) ((n shift))
1423 (or (let ((n-type (continuation-type n)))
1424 (when (numeric-type-p n-type)
1425 (let ((n-low (numeric-type-low n-type))
1426 (n-high (numeric-type-high n-type)))
1427 (if (constant-continuation-p shift)
1428 (let ((shift (continuation-value shift)))
1429 (make-numeric-type :class 'integer
1431 :low (when n-low (ash n-low shift))
1432 :high (when n-high (ash n-high shift))))
1433 (let ((s-type (continuation-type shift)))
1434 (when (numeric-type-p s-type)
1435 (let ((s-low (numeric-type-low s-type))
1436 (s-high (numeric-type-high s-type)))
1437 (if (and s-low s-high (<= s-low 64) (<= s-high 64))
1438 (make-numeric-type :class 'integer
1441 (min (ash n-low s-high)
1444 (max (ash n-high s-high)
1445 (ash n-high s-low))))
1446 (make-numeric-type :class 'integer
1447 :complexp :real)))))))))
1449 #!+propagate-fun-type
1450 (defun ash-derive-type-aux (n-type shift same-arg)
1451 (declare (ignore same-arg))
1452 (or (and (csubtypep n-type (specifier-type 'integer))
1453 (csubtypep shift (specifier-type 'integer))
1454 (let ((n-low (numeric-type-low n-type))
1455 (n-high (numeric-type-high n-type))
1456 (s-low (numeric-type-low shift))
1457 (s-high (numeric-type-high shift)))
1458 ;; KLUDGE: The bare 64's here should be related to
1459 ;; symbolic machine word size values somehow.
1460 (if (and s-low s-high (<= s-low 64) (<= s-high 64))
1461 (make-numeric-type :class 'integer :complexp :real
1463 (min (ash n-low s-high)
1466 (max (ash n-high s-high)
1467 (ash n-high s-low))))
1468 (make-numeric-type :class 'integer
1471 #!+propagate-fun-type
1472 (defoptimizer (ash derive-type) ((n shift))
1473 (two-arg-derive-type n shift #'ash-derive-type-aux #'ash))
1476 #!-propagate-float-type
1477 (macrolet ((frob (fun)
1478 `#'(lambda (type type2)
1479 (declare (ignore type2))
1480 (let ((lo (numeric-type-low type))
1481 (hi (numeric-type-high type)))
1482 (values (if hi (,fun hi) nil) (if lo (,fun lo) nil))))))
1484 (defoptimizer (%negate derive-type) ((num))
1485 (derive-integer-type num num (frob -)))
1487 (defoptimizer (lognot derive-type) ((int))
1488 (derive-integer-type int int (frob lognot))))
1490 #!+propagate-float-type
1491 (defoptimizer (lognot derive-type) ((int))
1492 (derive-integer-type int int
1493 #'(lambda (type type2)
1494 (declare (ignore type2))
1495 (let ((lo (numeric-type-low type))
1496 (hi (numeric-type-high type)))
1497 (values (if hi (lognot hi) nil)
1498 (if lo (lognot lo) nil)
1499 (numeric-type-class type)
1500 (numeric-type-format type))))))
1502 #!+propagate-float-type
1503 (defoptimizer (%negate derive-type) ((num))
1504 (flet ((negate-bound (b)
1505 (set-bound (- (bound-value b)) (consp b))))
1506 (one-arg-derive-type num
1508 (let ((lo (numeric-type-low type))
1509 (hi (numeric-type-high type))
1510 (result (copy-numeric-type type)))
1511 (setf (numeric-type-low result)
1512 (if hi (negate-bound hi) nil))
1513 (setf (numeric-type-high result)
1514 (if lo (negate-bound lo) nil))
1518 #!-propagate-float-type
1519 (defoptimizer (abs derive-type) ((num))
1520 (let ((type (continuation-type num)))
1521 (if (and (numeric-type-p type)
1522 (eq (numeric-type-class type) 'integer)
1523 (eq (numeric-type-complexp type) :real))
1524 (let ((lo (numeric-type-low type))
1525 (hi (numeric-type-high type)))
1526 (make-numeric-type :class 'integer :complexp :real
1527 :low (cond ((and hi (minusp hi))
1533 :high (if (and hi lo)
1534 (max (abs hi) (abs lo))
1536 (numeric-contagion type type))))
1538 #!+propagate-float-type
1539 (defun abs-derive-type-aux (type)
1540 (cond ((eq (numeric-type-complexp type) :complex)
1541 ;; The absolute value of a complex number is always a
1542 ;; non-negative float.
1543 (let* ((format (case (numeric-type-class type)
1544 ((integer rational) 'single-float)
1545 (t (numeric-type-format type))))
1546 (bound-format (or format 'float)))
1547 (make-numeric-type :class 'float
1550 :low (coerce 0 bound-format)
1553 ;; The absolute value of a real number is a non-negative real
1554 ;; of the same type.
1555 (let* ((abs-bnd (interval-abs (numeric-type->interval type)))
1556 (class (numeric-type-class type))
1557 (format (numeric-type-format type))
1558 (bound-type (or format class 'real)))
1563 :low (coerce-numeric-bound (interval-low abs-bnd) bound-type)
1564 :high (coerce-numeric-bound
1565 (interval-high abs-bnd) bound-type))))))
1567 #!+propagate-float-type
1568 (defoptimizer (abs derive-type) ((num))
1569 (one-arg-derive-type num #'abs-derive-type-aux #'abs))
1571 #!-propagate-float-type
1572 (defoptimizer (truncate derive-type) ((number divisor))
1573 (let ((number-type (continuation-type number))
1574 (divisor-type (continuation-type divisor))
1575 (integer-type (specifier-type 'integer)))
1576 (if (and (numeric-type-p number-type)
1577 (csubtypep number-type integer-type)
1578 (numeric-type-p divisor-type)
1579 (csubtypep divisor-type integer-type))
1580 (let ((number-low (numeric-type-low number-type))
1581 (number-high (numeric-type-high number-type))
1582 (divisor-low (numeric-type-low divisor-type))
1583 (divisor-high (numeric-type-high divisor-type)))
1584 (values-specifier-type
1585 `(values ,(integer-truncate-derive-type number-low number-high
1586 divisor-low divisor-high)
1587 ,(integer-rem-derive-type number-low number-high
1588 divisor-low divisor-high))))
1591 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
1593 #!+propagate-float-type
1596 (defun rem-result-type (number-type divisor-type)
1597 ;; Figure out what the remainder type is. The remainder is an
1598 ;; integer if both args are integers; a rational if both args are
1599 ;; rational; and a float otherwise.
1600 (cond ((and (csubtypep number-type (specifier-type 'integer))
1601 (csubtypep divisor-type (specifier-type 'integer)))
1603 ((and (csubtypep number-type (specifier-type 'rational))
1604 (csubtypep divisor-type (specifier-type 'rational)))
1606 ((and (csubtypep number-type (specifier-type 'float))
1607 (csubtypep divisor-type (specifier-type 'float)))
1608 ;; Both are floats so the result is also a float, of
1609 ;; the largest type.
1610 (or (float-format-max (numeric-type-format number-type)
1611 (numeric-type-format divisor-type))
1613 ((and (csubtypep number-type (specifier-type 'float))
1614 (csubtypep divisor-type (specifier-type 'rational)))
1615 ;; One of the arguments is a float and the other is a
1616 ;; rational. The remainder is a float of the same
1618 (or (numeric-type-format number-type) 'float))
1619 ((and (csubtypep divisor-type (specifier-type 'float))
1620 (csubtypep number-type (specifier-type 'rational)))
1621 ;; One of the arguments is a float and the other is a
1622 ;; rational. The remainder is a float of the same
1624 (or (numeric-type-format divisor-type) 'float))
1626 ;; Some unhandled combination. This usually means both args
1627 ;; are REAL so the result is a REAL.
1630 (defun truncate-derive-type-quot (number-type divisor-type)
1631 (let* ((rem-type (rem-result-type number-type divisor-type))
1632 (number-interval (numeric-type->interval number-type))
1633 (divisor-interval (numeric-type->interval divisor-type)))
1634 ;;(declare (type (member '(integer rational float)) rem-type))
1635 ;; We have real numbers now.
1636 (cond ((eq rem-type 'integer)
1637 ;; Since the remainder type is INTEGER, both args are
1639 (let* ((res (integer-truncate-derive-type
1640 (interval-low number-interval)
1641 (interval-high number-interval)
1642 (interval-low divisor-interval)
1643 (interval-high divisor-interval))))
1644 (specifier-type (if (listp res) res 'integer))))
1646 (let ((quot (truncate-quotient-bound
1647 (interval-div number-interval
1648 divisor-interval))))
1649 (specifier-type `(integer ,(or (interval-low quot) '*)
1650 ,(or (interval-high quot) '*))))))))
1652 (defun truncate-derive-type-rem (number-type divisor-type)
1653 (let* ((rem-type (rem-result-type number-type divisor-type))
1654 (number-interval (numeric-type->interval number-type))
1655 (divisor-interval (numeric-type->interval divisor-type))
1656 (rem (truncate-rem-bound number-interval divisor-interval)))
1657 ;;(declare (type (member '(integer rational float)) rem-type))
1658 ;; We have real numbers now.
1659 (cond ((eq rem-type 'integer)
1660 ;; Since the remainder type is INTEGER, both args are
1662 (specifier-type `(,rem-type ,(or (interval-low rem) '*)
1663 ,(or (interval-high rem) '*))))
1665 (multiple-value-bind (class format)
1668 (values 'integer nil))
1670 (values 'rational nil))
1671 ((or single-float double-float #!+long-float long-float)
1672 (values 'float rem-type))
1674 (values 'float nil))
1677 (when (member rem-type '(float single-float double-float
1678 #!+long-float long-float))
1679 (setf rem (interval-func #'(lambda (x)
1680 (coerce x rem-type))
1682 (make-numeric-type :class class
1684 :low (interval-low rem)
1685 :high (interval-high rem)))))))
1687 (defun truncate-derive-type-quot-aux (num div same-arg)
1688 (declare (ignore same-arg))
1689 (if (and (numeric-type-real-p num)
1690 (numeric-type-real-p div))
1691 (truncate-derive-type-quot num div)
1694 (defun truncate-derive-type-rem-aux (num div same-arg)
1695 (declare (ignore same-arg))
1696 (if (and (numeric-type-real-p num)
1697 (numeric-type-real-p div))
1698 (truncate-derive-type-rem num div)
1701 (defoptimizer (truncate derive-type) ((number divisor))
1702 (let ((quot (two-arg-derive-type number divisor
1703 #'truncate-derive-type-quot-aux #'truncate))
1704 (rem (two-arg-derive-type number divisor
1705 #'truncate-derive-type-rem-aux #'rem)))
1706 (when (and quot rem)
1707 (make-values-type :required (list quot rem)))))
1709 (defun ftruncate-derive-type-quot (number-type divisor-type)
1710 ;; The bounds are the same as for truncate. However, the first
1711 ;; result is a float of some type. We need to determine what that
1712 ;; type is. Basically it's the more contagious of the two types.
1713 (let ((q-type (truncate-derive-type-quot number-type divisor-type))
1714 (res-type (numeric-contagion number-type divisor-type)))
1715 (make-numeric-type :class 'float
1716 :format (numeric-type-format res-type)
1717 :low (numeric-type-low q-type)
1718 :high (numeric-type-high q-type))))
1720 (defun ftruncate-derive-type-quot-aux (n d same-arg)
1721 (declare (ignore same-arg))
1722 (if (and (numeric-type-real-p n)
1723 (numeric-type-real-p d))
1724 (ftruncate-derive-type-quot n d)
1727 (defoptimizer (ftruncate derive-type) ((number divisor))
1729 (two-arg-derive-type number divisor
1730 #'ftruncate-derive-type-quot-aux #'ftruncate))
1731 (rem (two-arg-derive-type number divisor
1732 #'truncate-derive-type-rem-aux #'rem)))
1733 (when (and quot rem)
1734 (make-values-type :required (list quot rem)))))
1736 (defun %unary-truncate-derive-type-aux (number)
1737 (truncate-derive-type-quot number (specifier-type '(integer 1 1))))
1739 (defoptimizer (%unary-truncate derive-type) ((number))
1740 (one-arg-derive-type number
1741 #'%unary-truncate-derive-type-aux
1744 ;;; Define optimizers for FLOOR and CEILING.
1746 ((frob-opt (name q-name r-name)
1747 (let ((q-aux (symbolicate q-name "-AUX"))
1748 (r-aux (symbolicate r-name "-AUX")))
1750 ;; Compute type of quotient (first) result
1751 (defun ,q-aux (number-type divisor-type)
1752 (let* ((number-interval
1753 (numeric-type->interval number-type))
1755 (numeric-type->interval divisor-type))
1756 (quot (,q-name (interval-div number-interval
1757 divisor-interval))))
1758 (specifier-type `(integer ,(or (interval-low quot) '*)
1759 ,(or (interval-high quot) '*)))))
1760 ;; Compute type of remainder
1761 (defun ,r-aux (number-type divisor-type)
1762 (let* ((divisor-interval
1763 (numeric-type->interval divisor-type))
1764 (rem (,r-name divisor-interval))
1765 (result-type (rem-result-type number-type divisor-type)))
1766 (multiple-value-bind (class format)
1769 (values 'integer nil))
1771 (values 'rational nil))
1772 ((or single-float double-float #!+long-float long-float)
1773 (values 'float result-type))
1775 (values 'float nil))
1778 (when (member result-type '(float single-float double-float
1779 #!+long-float long-float))
1780 ;; Make sure the limits on the interval have
1782 (setf rem (interval-func #'(lambda (x)
1783 (coerce x result-type))
1785 (make-numeric-type :class class
1787 :low (interval-low rem)
1788 :high (interval-high rem)))))
1789 ;; The optimizer itself
1790 (defoptimizer (,name derive-type) ((number divisor))
1791 (flet ((derive-q (n d same-arg)
1792 (declare (ignore same-arg))
1793 (if (and (numeric-type-real-p n)
1794 (numeric-type-real-p d))
1797 (derive-r (n d same-arg)
1798 (declare (ignore same-arg))
1799 (if (and (numeric-type-real-p n)
1800 (numeric-type-real-p d))
1803 (let ((quot (two-arg-derive-type
1804 number divisor #'derive-q #',name))
1805 (rem (two-arg-derive-type
1806 number divisor #'derive-r #'mod)))
1807 (when (and quot rem)
1808 (make-values-type :required (list quot rem))))))
1811 ;; FIXME: DEF-FROB-OPT, not just FROB-OPT
1812 (frob-opt floor floor-quotient-bound floor-rem-bound)
1813 (frob-opt ceiling ceiling-quotient-bound ceiling-rem-bound))
1815 ;;; Define optimizers for FFLOOR and FCEILING
1817 ((frob-opt (name q-name r-name)
1818 (let ((q-aux (symbolicate "F" q-name "-AUX"))
1819 (r-aux (symbolicate r-name "-AUX")))
1821 ;; Compute type of quotient (first) result
1822 (defun ,q-aux (number-type divisor-type)
1823 (let* ((number-interval
1824 (numeric-type->interval number-type))
1826 (numeric-type->interval divisor-type))
1827 (quot (,q-name (interval-div number-interval
1829 (res-type (numeric-contagion number-type divisor-type)))
1831 :class (numeric-type-class res-type)
1832 :format (numeric-type-format res-type)
1833 :low (interval-low quot)
1834 :high (interval-high quot))))
1836 (defoptimizer (,name derive-type) ((number divisor))
1837 (flet ((derive-q (n d same-arg)
1838 (declare (ignore same-arg))
1839 (if (and (numeric-type-real-p n)
1840 (numeric-type-real-p d))
1843 (derive-r (n d same-arg)
1844 (declare (ignore same-arg))
1845 (if (and (numeric-type-real-p n)
1846 (numeric-type-real-p d))
1849 (let ((quot (two-arg-derive-type
1850 number divisor #'derive-q #',name))
1851 (rem (two-arg-derive-type
1852 number divisor #'derive-r #'mod)))
1853 (when (and quot rem)
1854 (make-values-type :required (list quot rem))))))))))
1856 ;; FIXME: DEF-FROB-OPT, not just FROB-OPT
1857 (frob-opt ffloor floor-quotient-bound floor-rem-bound)
1858 (frob-opt fceiling ceiling-quotient-bound ceiling-rem-bound))
1860 ;;; Functions to compute the bounds on the quotient and remainder for
1861 ;;; the FLOOR function.
1862 (defun floor-quotient-bound (quot)
1863 ;; Take the floor of the quotient and then massage it into what we
1865 (let ((lo (interval-low quot))
1866 (hi (interval-high quot)))
1867 ;; Take the floor of the lower bound. The result is always a
1868 ;; closed lower bound.
1870 (floor (bound-value lo))
1872 ;; For the upper bound, we need to be careful
1875 ;; An open bound. We need to be careful here because
1876 ;; the floor of '(10.0) is 9, but the floor of
1878 (multiple-value-bind (q r) (floor (first hi))
1883 ;; A closed bound, so the answer is obvious.
1887 (make-interval :low lo :high hi)))
1888 (defun floor-rem-bound (div)
1889 ;; The remainder depends only on the divisor. Try to get the
1890 ;; correct sign for the remainder if we can.
1891 (case (interval-range-info div)
1893 ;; Divisor is always positive.
1894 (let ((rem (interval-abs div)))
1895 (setf (interval-low rem) 0)
1896 (when (and (numberp (interval-high rem))
1897 (not (zerop (interval-high rem))))
1898 ;; The remainder never contains the upper bound. However,
1899 ;; watch out for the case where the high limit is zero!
1900 (setf (interval-high rem) (list (interval-high rem))))
1903 ;; Divisor is always negative
1904 (let ((rem (interval-neg (interval-abs div))))
1905 (setf (interval-high rem) 0)
1906 (when (numberp (interval-low rem))
1907 ;; The remainder never contains the lower bound.
1908 (setf (interval-low rem) (list (interval-low rem))))
1911 ;; The divisor can be positive or negative. All bets off.
1912 ;; The magnitude of remainder is the maximum value of the
1914 (let ((limit (bound-value (interval-high (interval-abs div)))))
1915 ;; The bound never reaches the limit, so make the interval open
1916 (make-interval :low (if limit
1919 :high (list limit))))))
1921 (floor-quotient-bound (make-interval :low 0.3 :high 10.3))
1922 => #S(INTERVAL :LOW 0 :HIGH 10)
1923 (floor-quotient-bound (make-interval :low 0.3 :high '(10.3)))
1924 => #S(INTERVAL :LOW 0 :HIGH 10)
1925 (floor-quotient-bound (make-interval :low 0.3 :high 10))
1926 => #S(INTERVAL :LOW 0 :HIGH 10)
1927 (floor-quotient-bound (make-interval :low 0.3 :high '(10)))
1928 => #S(INTERVAL :LOW 0 :HIGH 9)
1929 (floor-quotient-bound (make-interval :low '(0.3) :high 10.3))
1930 => #S(INTERVAL :LOW 0 :HIGH 10)
1931 (floor-quotient-bound (make-interval :low '(0.0) :high 10.3))
1932 => #S(INTERVAL :LOW 0 :HIGH 10)
1933 (floor-quotient-bound (make-interval :low '(-1.3) :high 10.3))
1934 => #S(INTERVAL :LOW -2 :HIGH 10)
1935 (floor-quotient-bound (make-interval :low '(-1.0) :high 10.3))
1936 => #S(INTERVAL :LOW -1 :HIGH 10)
1937 (floor-quotient-bound (make-interval :low -1.0 :high 10.3))
1938 => #S(INTERVAL :LOW -1 :HIGH 10)
1940 (floor-rem-bound (make-interval :low 0.3 :high 10.3))
1941 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
1942 (floor-rem-bound (make-interval :low 0.3 :high '(10.3)))
1943 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
1944 (floor-rem-bound (make-interval :low -10 :high -2.3))
1945 #S(INTERVAL :LOW (-10) :HIGH 0)
1946 (floor-rem-bound (make-interval :low 0.3 :high 10))
1947 => #S(INTERVAL :LOW 0 :HIGH '(10))
1948 (floor-rem-bound (make-interval :low '(-1.3) :high 10.3))
1949 => #S(INTERVAL :LOW '(-10.3) :HIGH '(10.3))
1950 (floor-rem-bound (make-interval :low '(-20.3) :high 10.3))
1951 => #S(INTERVAL :LOW (-20.3) :HIGH (20.3))
1954 ;;; same functions for CEILING
1955 (defun ceiling-quotient-bound (quot)
1956 ;; Take the ceiling of the quotient and then massage it into what we
1958 (let ((lo (interval-low quot))
1959 (hi (interval-high quot)))
1960 ;; Take the ceiling of the upper bound. The result is always a
1961 ;; closed upper bound.
1963 (ceiling (bound-value hi))
1965 ;; For the lower bound, we need to be careful
1968 ;; An open bound. We need to be careful here because
1969 ;; the ceiling of '(10.0) is 11, but the ceiling of
1971 (multiple-value-bind (q r) (ceiling (first lo))
1976 ;; A closed bound, so the answer is obvious.
1980 (make-interval :low lo :high hi)))
1981 (defun ceiling-rem-bound (div)
1982 ;; The remainder depends only on the divisor. Try to get the
1983 ;; correct sign for the remainder if we can.
1985 (case (interval-range-info div)
1987 ;; Divisor is always positive. The remainder is negative.
1988 (let ((rem (interval-neg (interval-abs div))))
1989 (setf (interval-high rem) 0)
1990 (when (and (numberp (interval-low rem))
1991 (not (zerop (interval-low rem))))
1992 ;; The remainder never contains the upper bound. However,
1993 ;; watch out for the case when the upper bound is zero!
1994 (setf (interval-low rem) (list (interval-low rem))))
1997 ;; Divisor is always negative. The remainder is positive
1998 (let ((rem (interval-abs div)))
1999 (setf (interval-low rem) 0)
2000 (when (numberp (interval-high rem))
2001 ;; The remainder never contains the lower bound.
2002 (setf (interval-high rem) (list (interval-high rem))))
2005 ;; The divisor can be positive or negative. All bets off.
2006 ;; The magnitude of remainder is the maximum value of the
2008 (let ((limit (bound-value (interval-high (interval-abs div)))))
2009 ;; The bound never reaches the limit, so make the interval open
2010 (make-interval :low (if limit
2013 :high (list limit))))))
2016 (ceiling-quotient-bound (make-interval :low 0.3 :high 10.3))
2017 => #S(INTERVAL :LOW 1 :HIGH 11)
2018 (ceiling-quotient-bound (make-interval :low 0.3 :high '(10.3)))
2019 => #S(INTERVAL :LOW 1 :HIGH 11)
2020 (ceiling-quotient-bound (make-interval :low 0.3 :high 10))
2021 => #S(INTERVAL :LOW 1 :HIGH 10)
2022 (ceiling-quotient-bound (make-interval :low 0.3 :high '(10)))
2023 => #S(INTERVAL :LOW 1 :HIGH 10)
2024 (ceiling-quotient-bound (make-interval :low '(0.3) :high 10.3))
2025 => #S(INTERVAL :LOW 1 :HIGH 11)
2026 (ceiling-quotient-bound (make-interval :low '(0.0) :high 10.3))
2027 => #S(INTERVAL :LOW 1 :HIGH 11)
2028 (ceiling-quotient-bound (make-interval :low '(-1.3) :high 10.3))
2029 => #S(INTERVAL :LOW -1 :HIGH 11)
2030 (ceiling-quotient-bound (make-interval :low '(-1.0) :high 10.3))
2031 => #S(INTERVAL :LOW 0 :HIGH 11)
2032 (ceiling-quotient-bound (make-interval :low -1.0 :high 10.3))
2033 => #S(INTERVAL :LOW -1 :HIGH 11)
2035 (ceiling-rem-bound (make-interval :low 0.3 :high 10.3))
2036 => #S(INTERVAL :LOW (-10.3) :HIGH 0)
2037 (ceiling-rem-bound (make-interval :low 0.3 :high '(10.3)))
2038 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
2039 (ceiling-rem-bound (make-interval :low -10 :high -2.3))
2040 => #S(INTERVAL :LOW 0 :HIGH (10))
2041 (ceiling-rem-bound (make-interval :low 0.3 :high 10))
2042 => #S(INTERVAL :LOW (-10) :HIGH 0)
2043 (ceiling-rem-bound (make-interval :low '(-1.3) :high 10.3))
2044 => #S(INTERVAL :LOW (-10.3) :HIGH (10.3))
2045 (ceiling-rem-bound (make-interval :low '(-20.3) :high 10.3))
2046 => #S(INTERVAL :LOW (-20.3) :HIGH (20.3))
2049 (defun truncate-quotient-bound (quot)
2050 ;; For positive quotients, truncate is exactly like floor. For
2051 ;; negative quotients, truncate is exactly like ceiling. Otherwise,
2052 ;; it's the union of the two pieces.
2053 (case (interval-range-info quot)
2056 (floor-quotient-bound quot))
2058 ;; Just like ceiling
2059 (ceiling-quotient-bound quot))
2061 ;; Split the interval into positive and negative pieces, compute
2062 ;; the result for each piece and put them back together.
2063 (destructuring-bind (neg pos) (interval-split 0 quot t t)
2064 (interval-merge-pair (ceiling-quotient-bound neg)
2065 (floor-quotient-bound pos))))))
2067 (defun truncate-rem-bound (num div)
2068 ;; This is significantly more complicated than floor or ceiling. We
2069 ;; need both the number and the divisor to determine the range. The
2070 ;; basic idea is to split the ranges of num and den into positive
2071 ;; and negative pieces and deal with each of the four possibilities
2073 (case (interval-range-info num)
2075 (case (interval-range-info div)
2077 (floor-rem-bound div))
2079 (ceiling-rem-bound div))
2081 (destructuring-bind (neg pos) (interval-split 0 div t t)
2082 (interval-merge-pair (truncate-rem-bound num neg)
2083 (truncate-rem-bound num pos))))))
2085 (case (interval-range-info div)
2087 (ceiling-rem-bound div))
2089 (floor-rem-bound div))
2091 (destructuring-bind (neg pos) (interval-split 0 div t t)
2092 (interval-merge-pair (truncate-rem-bound num neg)
2093 (truncate-rem-bound num pos))))))
2095 (destructuring-bind (neg pos) (interval-split 0 num t t)
2096 (interval-merge-pair (truncate-rem-bound neg div)
2097 (truncate-rem-bound pos div))))))
2100 ;;; Derive useful information about the range. Returns three values:
2101 ;;; - '+ if its positive, '- negative, or nil if it overlaps 0.
2102 ;;; - The abs of the minimal value (i.e. closest to 0) in the range.
2103 ;;; - The abs of the maximal value if there is one, or nil if it is
2105 (defun numeric-range-info (low high)
2106 (cond ((and low (not (minusp low)))
2107 (values '+ low high))
2108 ((and high (not (plusp high)))
2109 (values '- (- high) (if low (- low) nil)))
2111 (values nil 0 (and low high (max (- low) high))))))
2113 (defun integer-truncate-derive-type
2114 (number-low number-high divisor-low divisor-high)
2115 ;; The result cannot be larger in magnitude than the number, but the sign
2116 ;; might change. If we can determine the sign of either the number or
2117 ;; the divisor, we can eliminate some of the cases.
2118 (multiple-value-bind (number-sign number-min number-max)
2119 (numeric-range-info number-low number-high)
2120 (multiple-value-bind (divisor-sign divisor-min divisor-max)
2121 (numeric-range-info divisor-low divisor-high)
2122 (when (and divisor-max (zerop divisor-max))
2123 ;; We've got a problem: guaranteed division by zero.
2124 (return-from integer-truncate-derive-type t))
2125 (when (zerop divisor-min)
2126 ;; We'll assume that they aren't going to divide by zero.
2128 (cond ((and number-sign divisor-sign)
2129 ;; We know the sign of both.
2130 (if (eq number-sign divisor-sign)
2131 ;; Same sign, so the result will be positive.
2132 `(integer ,(if divisor-max
2133 (truncate number-min divisor-max)
2136 (truncate number-max divisor-min)
2138 ;; Different signs, the result will be negative.
2139 `(integer ,(if number-max
2140 (- (truncate number-max divisor-min))
2143 (- (truncate number-min divisor-max))
2145 ((eq divisor-sign '+)
2146 ;; The divisor is positive. Therefore, the number will just
2147 ;; become closer to zero.
2148 `(integer ,(if number-low
2149 (truncate number-low divisor-min)
2152 (truncate number-high divisor-min)
2154 ((eq divisor-sign '-)
2155 ;; The divisor is negative. Therefore, the absolute value of
2156 ;; the number will become closer to zero, but the sign will also
2158 `(integer ,(if number-high
2159 (- (truncate number-high divisor-min))
2162 (- (truncate number-low divisor-min))
2164 ;; The divisor could be either positive or negative.
2166 ;; The number we are dividing has a bound. Divide that by the
2167 ;; smallest posible divisor.
2168 (let ((bound (truncate number-max divisor-min)))
2169 `(integer ,(- bound) ,bound)))
2171 ;; The number we are dividing is unbounded, so we can't tell
2172 ;; anything about the result.
2175 #!-propagate-float-type
2176 (defun integer-rem-derive-type
2177 (number-low number-high divisor-low divisor-high)
2178 (if (and divisor-low divisor-high)
2179 ;; We know the range of the divisor, and the remainder must be smaller
2180 ;; than the divisor. We can tell the sign of the remainer if we know
2181 ;; the sign of the number.
2182 (let ((divisor-max (1- (max (abs divisor-low) (abs divisor-high)))))
2183 `(integer ,(if (or (null number-low)
2184 (minusp number-low))
2187 ,(if (or (null number-high)
2188 (plusp number-high))
2191 ;; The divisor is potentially either very positive or very negative.
2192 ;; Therefore, the remainer is unbounded, but we might be able to tell
2193 ;; something about the sign from the number.
2194 `(integer ,(if (and number-low (not (minusp number-low)))
2195 ;; The number we are dividing is positive. Therefore,
2196 ;; the remainder must be positive.
2199 ,(if (and number-high (not (plusp number-high)))
2200 ;; The number we are dividing is negative. Therefore,
2201 ;; the remainder must be negative.
2205 #!-propagate-float-type
2206 (defoptimizer (random derive-type) ((bound &optional state))
2207 (let ((type (continuation-type bound)))
2208 (when (numeric-type-p type)
2209 (let ((class (numeric-type-class type))
2210 (high (numeric-type-high type))
2211 (format (numeric-type-format type)))
2215 :low (coerce 0 (or format class 'real))
2216 :high (cond ((not high) nil)
2217 ((eq class 'integer) (max (1- high) 0))
2218 ((or (consp high) (zerop high)) high)
2221 #!+propagate-float-type
2222 (defun random-derive-type-aux (type)
2223 (let ((class (numeric-type-class type))
2224 (high (numeric-type-high type))
2225 (format (numeric-type-format type)))
2229 :low (coerce 0 (or format class 'real))
2230 :high (cond ((not high) nil)
2231 ((eq class 'integer) (max (1- high) 0))
2232 ((or (consp high) (zerop high)) high)
2235 #!+propagate-float-type
2236 (defoptimizer (random derive-type) ((bound &optional state))
2237 (one-arg-derive-type bound #'random-derive-type-aux nil))
2239 ;;;; logical derive-type methods
2241 ;;; Return the maximum number of bits an integer of the supplied type can take
2242 ;;; up, or NIL if it is unbounded. The second (third) value is T if the
2243 ;;; integer can be positive (negative) and NIL if not. Zero counts as
2245 (defun integer-type-length (type)
2246 (if (numeric-type-p type)
2247 (let ((min (numeric-type-low type))
2248 (max (numeric-type-high type)))
2249 (values (and min max (max (integer-length min) (integer-length max)))
2250 (or (null max) (not (minusp max)))
2251 (or (null min) (minusp min))))
2254 #!-propagate-fun-type
2256 (defoptimizer (logand derive-type) ((x y))
2257 (multiple-value-bind (x-len x-pos x-neg)
2258 (integer-type-length (continuation-type x))
2259 (declare (ignore x-pos))
2260 (multiple-value-bind (y-len y-pos y-neg)
2261 (integer-type-length (continuation-type y))
2262 (declare (ignore y-pos))
2264 ;; X must be positive.
2266 ;; The must both be positive.
2267 (cond ((or (null x-len) (null y-len))
2268 (specifier-type 'unsigned-byte))
2269 ((or (zerop x-len) (zerop y-len))
2270 (specifier-type '(integer 0 0)))
2272 (specifier-type `(unsigned-byte ,(min x-len y-len)))))
2273 ;; X is positive, but Y might be negative.
2275 (specifier-type 'unsigned-byte))
2277 (specifier-type '(integer 0 0)))
2279 (specifier-type `(unsigned-byte ,x-len)))))
2280 ;; X might be negative.
2282 ;; Y must be positive.
2284 (specifier-type 'unsigned-byte))
2286 (specifier-type '(integer 0 0)))
2289 `(unsigned-byte ,y-len))))
2290 ;; Either might be negative.
2291 (if (and x-len y-len)
2292 ;; The result is bounded.
2293 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2294 ;; We can't tell squat about the result.
2295 (specifier-type 'integer)))))))
2297 (defoptimizer (logior derive-type) ((x y))
2298 (multiple-value-bind (x-len x-pos x-neg)
2299 (integer-type-length (continuation-type x))
2300 (multiple-value-bind (y-len y-pos y-neg)
2301 (integer-type-length (continuation-type y))
2303 ((and (not x-neg) (not y-neg))
2304 ;; Both are positive.
2305 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2309 ;; X must be negative.
2311 ;; Both are negative. The result is going to be negative and be
2312 ;; the same length or shorter than the smaller.
2313 (if (and x-len y-len)
2315 (specifier-type `(integer ,(ash -1 (min x-len y-len)) -1))
2317 (specifier-type '(integer * -1)))
2318 ;; X is negative, but we don't know about Y. The result will be
2319 ;; negative, but no more negative than X.
2321 `(integer ,(or (numeric-type-low (continuation-type x)) '*)
2324 ;; X might be either positive or negative.
2326 ;; But Y is negative. The result will be negative.
2328 `(integer ,(or (numeric-type-low (continuation-type y)) '*)
2330 ;; We don't know squat about either. It won't get any bigger.
2331 (if (and x-len y-len)
2333 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2335 (specifier-type 'integer))))))))
2337 (defoptimizer (logxor derive-type) ((x y))
2338 (multiple-value-bind (x-len x-pos x-neg)
2339 (integer-type-length (continuation-type x))
2340 (multiple-value-bind (y-len y-pos y-neg)
2341 (integer-type-length (continuation-type y))
2343 ((or (and (not x-neg) (not y-neg))
2344 (and (not x-pos) (not y-pos)))
2345 ;; Either both are negative or both are positive. The result will be
2346 ;; positive, and as long as the longer.
2347 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2350 ((or (and (not x-pos) (not y-neg))
2351 (and (not y-neg) (not y-pos)))
2352 ;; Either X is negative and Y is positive of vice-verca. The result
2353 ;; will be negative.
2354 (specifier-type `(integer ,(if (and x-len y-len)
2355 (ash -1 (max x-len y-len))
2358 ;; We can't tell what the sign of the result is going to be. All we
2359 ;; know is that we don't create new bits.
2361 (specifier-type `(signed-byte ,(1+ (max x-len y-len)))))
2363 (specifier-type 'integer))))))
2367 #!+propagate-fun-type
2369 (defun logand-derive-type-aux (x y &optional same-leaf)
2370 (declare (ignore same-leaf))
2371 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2372 (declare (ignore x-pos))
2373 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2374 (declare (ignore y-pos))
2376 ;; X must be positive.
2378 ;; The must both be positive.
2379 (cond ((or (null x-len) (null y-len))
2380 (specifier-type 'unsigned-byte))
2381 ((or (zerop x-len) (zerop y-len))
2382 (specifier-type '(integer 0 0)))
2384 (specifier-type `(unsigned-byte ,(min x-len y-len)))))
2385 ;; X is positive, but Y might be negative.
2387 (specifier-type 'unsigned-byte))
2389 (specifier-type '(integer 0 0)))
2391 (specifier-type `(unsigned-byte ,x-len)))))
2392 ;; X might be negative.
2394 ;; Y must be positive.
2396 (specifier-type 'unsigned-byte))
2398 (specifier-type '(integer 0 0)))
2401 `(unsigned-byte ,y-len))))
2402 ;; Either might be negative.
2403 (if (and x-len y-len)
2404 ;; The result is bounded.
2405 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2406 ;; We can't tell squat about the result.
2407 (specifier-type 'integer)))))))
2409 (defun logior-derive-type-aux (x y &optional same-leaf)
2410 (declare (ignore same-leaf))
2411 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2412 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2414 ((and (not x-neg) (not y-neg))
2415 ;; Both are positive.
2416 (if (and x-len y-len (zerop x-len) (zerop y-len))
2417 (specifier-type '(integer 0 0))
2418 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2422 ;; X must be negative.
2424 ;; Both are negative. The result is going to be negative and be
2425 ;; the same length or shorter than the smaller.
2426 (if (and x-len y-len)
2428 (specifier-type `(integer ,(ash -1 (min x-len y-len)) -1))
2430 (specifier-type '(integer * -1)))
2431 ;; X is negative, but we don't know about Y. The result will be
2432 ;; negative, but no more negative than X.
2434 `(integer ,(or (numeric-type-low x) '*)
2437 ;; X might be either positive or negative.
2439 ;; But Y is negative. The result will be negative.
2441 `(integer ,(or (numeric-type-low y) '*)
2443 ;; We don't know squat about either. It won't get any bigger.
2444 (if (and x-len y-len)
2446 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2448 (specifier-type 'integer))))))))
2450 (defun logxor-derive-type-aux (x y &optional same-leaf)
2451 (declare (ignore same-leaf))
2452 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2453 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2455 ((or (and (not x-neg) (not y-neg))
2456 (and (not x-pos) (not y-pos)))
2457 ;; Either both are negative or both are positive. The result will be
2458 ;; positive, and as long as the longer.
2459 (if (and x-len y-len (zerop x-len) (zerop y-len))
2460 (specifier-type '(integer 0 0))
2461 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2464 ((or (and (not x-pos) (not y-neg))
2465 (and (not y-neg) (not y-pos)))
2466 ;; Either X is negative and Y is positive of vice-verca. The result
2467 ;; will be negative.
2468 (specifier-type `(integer ,(if (and x-len y-len)
2469 (ash -1 (max x-len y-len))
2472 ;; We can't tell what the sign of the result is going to be. All we
2473 ;; know is that we don't create new bits.
2475 (specifier-type `(signed-byte ,(1+ (max x-len y-len)))))
2477 (specifier-type 'integer))))))
2479 (macrolet ((frob (logfcn)
2480 (let ((fcn-aux (symbolicate logfcn "-DERIVE-TYPE-AUX")))
2481 `(defoptimizer (,logfcn derive-type) ((x y))
2482 (two-arg-derive-type x y #',fcn-aux #',logfcn)))))
2483 ;; FIXME: DEF-FROB, not just FROB
2490 ;;;; miscellaneous derive-type methods
2492 (defoptimizer (code-char derive-type) ((code))
2493 (specifier-type 'base-char))
2495 (defoptimizer (values derive-type) ((&rest values))
2496 (values-specifier-type
2497 `(values ,@(mapcar #'(lambda (x)
2498 (type-specifier (continuation-type x)))
2501 ;;;; byte operations
2503 ;;;; We try to turn byte operations into simple logical operations. First, we
2504 ;;;; convert byte specifiers into separate size and position arguments passed
2505 ;;;; to internal %FOO functions. We then attempt to transform the %FOO
2506 ;;;; functions into boolean operations when the size and position are constant
2507 ;;;; and the operands are fixnums.
2509 (macrolet (;; Evaluate body with Size-Var and Pos-Var bound to expressions that
2510 ;; evaluate to the Size and Position of the byte-specifier form
2511 ;; Spec. We may wrap a let around the result of the body to bind
2514 ;; If the spec is a Byte form, then bind the vars to the subforms.
2515 ;; otherwise, evaluate Spec and use the Byte-Size and Byte-Position.
2516 ;; The goal of this transformation is to avoid consing up byte
2517 ;; specifiers and then immediately throwing them away.
2518 (with-byte-specifier ((size-var pos-var spec) &body body)
2519 (once-only ((spec `(macroexpand ,spec))
2521 `(if (and (consp ,spec)
2522 (eq (car ,spec) 'byte)
2523 (= (length ,spec) 3))
2524 (let ((,size-var (second ,spec))
2525 (,pos-var (third ,spec)))
2527 (let ((,size-var `(byte-size ,,temp))
2528 (,pos-var `(byte-position ,,temp)))
2529 `(let ((,,temp ,,spec))
2532 (def-source-transform ldb (spec int)
2533 (with-byte-specifier (size pos spec)
2534 `(%ldb ,size ,pos ,int)))
2536 (def-source-transform dpb (newbyte spec int)
2537 (with-byte-specifier (size pos spec)
2538 `(%dpb ,newbyte ,size ,pos ,int)))
2540 (def-source-transform mask-field (spec int)
2541 (with-byte-specifier (size pos spec)
2542 `(%mask-field ,size ,pos ,int)))
2544 (def-source-transform deposit-field (newbyte spec int)
2545 (with-byte-specifier (size pos spec)
2546 `(%deposit-field ,newbyte ,size ,pos ,int))))
2548 (defoptimizer (%ldb derive-type) ((size posn num))
2549 (let ((size (continuation-type size)))
2550 (if (and (numeric-type-p size)
2551 (csubtypep size (specifier-type 'integer)))
2552 (let ((size-high (numeric-type-high size)))
2553 (if (and size-high (<= size-high sb!vm:word-bits))
2554 (specifier-type `(unsigned-byte ,size-high))
2555 (specifier-type 'unsigned-byte)))
2558 (defoptimizer (%mask-field derive-type) ((size posn num))
2559 (let ((size (continuation-type size))
2560 (posn (continuation-type posn)))
2561 (if (and (numeric-type-p size)
2562 (csubtypep size (specifier-type 'integer))
2563 (numeric-type-p posn)
2564 (csubtypep posn (specifier-type 'integer)))
2565 (let ((size-high (numeric-type-high size))
2566 (posn-high (numeric-type-high posn)))
2567 (if (and size-high posn-high
2568 (<= (+ size-high posn-high) sb!vm:word-bits))
2569 (specifier-type `(unsigned-byte ,(+ size-high posn-high)))
2570 (specifier-type 'unsigned-byte)))
2573 (defoptimizer (%dpb derive-type) ((newbyte size posn int))
2574 (let ((size (continuation-type size))
2575 (posn (continuation-type posn))
2576 (int (continuation-type int)))
2577 (if (and (numeric-type-p size)
2578 (csubtypep size (specifier-type 'integer))
2579 (numeric-type-p posn)
2580 (csubtypep posn (specifier-type 'integer))
2581 (numeric-type-p int)
2582 (csubtypep int (specifier-type 'integer)))
2583 (let ((size-high (numeric-type-high size))
2584 (posn-high (numeric-type-high posn))
2585 (high (numeric-type-high int))
2586 (low (numeric-type-low int)))
2587 (if (and size-high posn-high high low
2588 (<= (+ size-high posn-high) sb!vm:word-bits))
2590 (list (if (minusp low) 'signed-byte 'unsigned-byte)
2591 (max (integer-length high)
2592 (integer-length low)
2593 (+ size-high posn-high))))
2597 (defoptimizer (%deposit-field derive-type) ((newbyte size posn int))
2598 (let ((size (continuation-type size))
2599 (posn (continuation-type posn))
2600 (int (continuation-type int)))
2601 (if (and (numeric-type-p size)
2602 (csubtypep size (specifier-type 'integer))
2603 (numeric-type-p posn)
2604 (csubtypep posn (specifier-type 'integer))
2605 (numeric-type-p int)
2606 (csubtypep int (specifier-type 'integer)))
2607 (let ((size-high (numeric-type-high size))
2608 (posn-high (numeric-type-high posn))
2609 (high (numeric-type-high int))
2610 (low (numeric-type-low int)))
2611 (if (and size-high posn-high high low
2612 (<= (+ size-high posn-high) sb!vm:word-bits))
2614 (list (if (minusp low) 'signed-byte 'unsigned-byte)
2615 (max (integer-length high)
2616 (integer-length low)
2617 (+ size-high posn-high))))
2621 (deftransform %ldb ((size posn int)
2622 (fixnum fixnum integer)
2623 (unsigned-byte #.sb!vm:word-bits))
2624 "convert to inline logical ops"
2625 `(logand (ash int (- posn))
2626 (ash ,(1- (ash 1 sb!vm:word-bits))
2627 (- size ,sb!vm:word-bits))))
2629 (deftransform %mask-field ((size posn int)
2630 (fixnum fixnum integer)
2631 (unsigned-byte #.sb!vm:word-bits))
2632 "convert to inline logical ops"
2634 (ash (ash ,(1- (ash 1 sb!vm:word-bits))
2635 (- size ,sb!vm:word-bits))
2638 ;;; Note: for %DPB and %DEPOSIT-FIELD, we can't use
2639 ;;; (OR (SIGNED-BYTE N) (UNSIGNED-BYTE N))
2640 ;;; as the result type, as that would allow result types
2641 ;;; that cover the range -2^(n-1) .. 1-2^n, instead of allowing result types
2642 ;;; of (UNSIGNED-BYTE N) and result types of (SIGNED-BYTE N).
2644 (deftransform %dpb ((new size posn int)
2646 (unsigned-byte #.sb!vm:word-bits))
2647 "convert to inline logical ops"
2648 `(let ((mask (ldb (byte size 0) -1)))
2649 (logior (ash (logand new mask) posn)
2650 (logand int (lognot (ash mask posn))))))
2652 (deftransform %dpb ((new size posn int)
2654 (signed-byte #.sb!vm:word-bits))
2655 "convert to inline logical ops"
2656 `(let ((mask (ldb (byte size 0) -1)))
2657 (logior (ash (logand new mask) posn)
2658 (logand int (lognot (ash mask posn))))))
2660 (deftransform %deposit-field ((new size posn int)
2662 (unsigned-byte #.sb!vm:word-bits))
2663 "convert to inline logical ops"
2664 `(let ((mask (ash (ldb (byte size 0) -1) posn)))
2665 (logior (logand new mask)
2666 (logand int (lognot mask)))))
2668 (deftransform %deposit-field ((new size posn int)
2670 (signed-byte #.sb!vm:word-bits))
2671 "convert to inline logical ops"
2672 `(let ((mask (ash (ldb (byte size 0) -1) posn)))
2673 (logior (logand new mask)
2674 (logand int (lognot mask)))))
2676 ;;; miscellanous numeric transforms
2678 ;;; If a constant appears as the first arg, swap the args.
2679 (deftransform commutative-arg-swap ((x y) * * :defun-only t :node node)
2680 (if (and (constant-continuation-p x)
2681 (not (constant-continuation-p y)))
2682 `(,(continuation-function-name (basic-combination-fun node))
2684 ,(continuation-value x))
2685 (give-up-ir1-transform)))
2687 (dolist (x '(= char= + * logior logand logxor))
2688 (%deftransform x '(function * *) #'commutative-arg-swap
2689 "place constant arg last."))
2691 ;;; Handle the case of a constant BOOLE-CODE.
2692 (deftransform boole ((op x y) * * :when :both)
2693 "convert to inline logical ops"
2694 (unless (constant-continuation-p op)
2695 (give-up-ir1-transform "BOOLE code is not a constant."))
2696 (let ((control (continuation-value op)))
2702 (#.boole-c1 '(lognot x))
2703 (#.boole-c2 '(lognot y))
2704 (#.boole-and '(logand x y))
2705 (#.boole-ior '(logior x y))
2706 (#.boole-xor '(logxor x y))
2707 (#.boole-eqv '(logeqv x y))
2708 (#.boole-nand '(lognand x y))
2709 (#.boole-nor '(lognor x y))
2710 (#.boole-andc1 '(logandc1 x y))
2711 (#.boole-andc2 '(logandc2 x y))
2712 (#.boole-orc1 '(logorc1 x y))
2713 (#.boole-orc2 '(logorc2 x y))
2715 (abort-ir1-transform "~S is an illegal control arg to BOOLE."
2718 ;;;; converting special case multiply/divide to shifts
2720 ;;; If arg is a constant power of two, turn * into a shift.
2721 (deftransform * ((x y) (integer integer) * :when :both)
2722 "convert x*2^k to shift"
2723 (unless (constant-continuation-p y)
2724 (give-up-ir1-transform))
2725 (let* ((y (continuation-value y))
2727 (len (1- (integer-length y-abs))))
2728 (unless (= y-abs (ash 1 len))
2729 (give-up-ir1-transform))
2734 ;;; If both arguments and the result are (unsigned-byte 32), try to come up
2735 ;;; with a ``better'' multiplication using multiplier recoding. There are two
2736 ;;; different ways the multiplier can be recoded. The more obvious is to shift
2737 ;;; X by the correct amount for each bit set in Y and to sum the results. But
2738 ;;; if there is a string of bits that are all set, you can add X shifted by
2739 ;;; one more then the bit position of the first set bit and subtract X shifted
2740 ;;; by the bit position of the last set bit. We can't use this second method
2741 ;;; when the high order bit is bit 31 because shifting by 32 doesn't work
2743 (deftransform * ((x y)
2744 ((unsigned-byte 32) (unsigned-byte 32))
2746 "recode as shift and add"
2747 (unless (constant-continuation-p y)
2748 (give-up-ir1-transform))
2749 (let ((y (continuation-value y))
2752 (labels ((tub32 (x) `(truly-the (unsigned-byte 32) ,x))
2757 `(+ ,result ,(tub32 next-factor))
2759 (declare (inline add))
2760 (dotimes (bitpos 32)
2762 (when (not (logbitp bitpos y))
2763 (add (if (= (1+ first-one) bitpos)
2764 ;; There is only a single bit in the string.
2766 ;; There are at least two.
2767 `(- ,(tub32 `(ash x ,bitpos))
2768 ,(tub32 `(ash x ,first-one)))))
2769 (setf first-one nil))
2770 (when (logbitp bitpos y)
2771 (setf first-one bitpos))))
2773 (cond ((= first-one 31))
2777 (add `(- ,(tub32 '(ash x 31)) ,(tub32 `(ash x ,first-one))))))
2781 ;;; If arg is a constant power of two, turn FLOOR into a shift and mask.
2782 ;;; If CEILING, add in (1- (ABS Y)) and then do FLOOR.
2783 (flet ((frob (y ceil-p)
2784 (unless (constant-continuation-p y)
2785 (give-up-ir1-transform))
2786 (let* ((y (continuation-value y))
2788 (len (1- (integer-length y-abs))))
2789 (unless (= y-abs (ash 1 len))
2790 (give-up-ir1-transform))
2791 (let ((shift (- len))
2793 `(let ,(when ceil-p `((x (+ x ,(1- y-abs)))))
2795 `(values (ash (- x) ,shift)
2796 (- (logand (- x) ,mask)))
2797 `(values (ash x ,shift)
2798 (logand x ,mask))))))))
2799 (deftransform floor ((x y) (integer integer) *)
2800 "convert division by 2^k to shift"
2802 (deftransform ceiling ((x y) (integer integer) *)
2803 "convert division by 2^k to shift"
2806 ;;; Do the same for MOD.
2807 (deftransform mod ((x y) (integer integer) * :when :both)
2808 "convert remainder mod 2^k to LOGAND"
2809 (unless (constant-continuation-p y)
2810 (give-up-ir1-transform))
2811 (let* ((y (continuation-value y))
2813 (len (1- (integer-length y-abs))))
2814 (unless (= y-abs (ash 1 len))
2815 (give-up-ir1-transform))
2816 (let ((mask (1- y-abs)))
2818 `(- (logand (- x) ,mask))
2819 `(logand x ,mask)))))
2821 ;;; If arg is a constant power of two, turn TRUNCATE into a shift and mask.
2822 (deftransform truncate ((x y) (integer integer))
2823 "convert division by 2^k to shift"
2824 (unless (constant-continuation-p y)
2825 (give-up-ir1-transform))
2826 (let* ((y (continuation-value y))
2828 (len (1- (integer-length y-abs))))
2829 (unless (= y-abs (ash 1 len))
2830 (give-up-ir1-transform))
2831 (let* ((shift (- len))
2834 (values ,(if (minusp y)
2836 `(- (ash (- x) ,shift)))
2837 (- (logand (- x) ,mask)))
2838 (values ,(if (minusp y)
2839 `(- (ash (- x) ,shift))
2841 (logand x ,mask))))))
2843 ;;; And the same for REM.
2844 (deftransform rem ((x y) (integer integer) * :when :both)
2845 "convert remainder mod 2^k to LOGAND"
2846 (unless (constant-continuation-p y)
2847 (give-up-ir1-transform))
2848 (let* ((y (continuation-value y))
2850 (len (1- (integer-length y-abs))))
2851 (unless (= y-abs (ash 1 len))
2852 (give-up-ir1-transform))
2853 (let ((mask (1- y-abs)))
2855 (- (logand (- x) ,mask))
2856 (logand x ,mask)))))
2858 ;;;; arithmetic and logical identity operation elimination
2860 ;;;; Flush calls to various arith functions that convert to the identity
2861 ;;;; function or a constant.
2863 (dolist (stuff '((ash 0 x)
2868 (logxor -1 (lognot x))
2870 (destructuring-bind (name identity result) stuff
2871 (deftransform name ((x y) `(* (constant-argument (member ,identity))) '*
2872 :eval-name t :when :both)
2873 "fold identity operations"
2876 ;;; These are restricted to rationals, because (- 0 0.0) is 0.0, not -0.0, and
2877 ;;; (* 0 -4.0) is -0.0.
2878 (deftransform - ((x y) ((constant-argument (member 0)) rational) *
2880 "convert (- 0 x) to negate"
2882 (deftransform * ((x y) (rational (constant-argument (member 0))) *
2884 "convert (* x 0) to 0."
2887 ;;; Return T if in an arithmetic op including continuations X and Y, the
2888 ;;; result type is not affected by the type of X. That is, Y is at least as
2889 ;;; contagious as X.
2891 (defun not-more-contagious (x y)
2892 (declare (type continuation x y))
2893 (let ((x (continuation-type x))
2894 (y (continuation-type y)))
2895 (values (type= (numeric-contagion x y)
2896 (numeric-contagion y y)))))
2897 ;;; Patched version by Raymond Toy. dtc: Should be safer although it
2898 ;;; needs more work as valid transforms are missed; some cases are
2899 ;;; specific to particular transform functions so the use of this
2900 ;;; function may need a re-think.
2901 (defun not-more-contagious (x y)
2902 (declare (type continuation x y))
2903 (flet ((simple-numeric-type (num)
2904 (and (numeric-type-p num)
2905 ;; Return non-NIL if NUM is integer, rational, or a float
2906 ;; of some type (but not FLOAT)
2907 (case (numeric-type-class num)
2911 (numeric-type-format num))
2914 (let ((x (continuation-type x))
2915 (y (continuation-type y)))
2916 (if (and (simple-numeric-type x)
2917 (simple-numeric-type y))
2918 (values (type= (numeric-contagion x y)
2919 (numeric-contagion y y)))))))
2923 ;;; If y is not constant, not zerop, or is contagious, or a
2924 ;;; positive float +0.0 then give up.
2925 (deftransform + ((x y) (t (constant-argument t)) * :when :both)
2927 (let ((val (continuation-value y)))
2928 (unless (and (zerop val)
2929 (not (and (floatp val) (plusp (float-sign val))))
2930 (not-more-contagious y x))
2931 (give-up-ir1-transform)))
2936 ;;; If y is not constant, not zerop, or is contagious, or a
2937 ;;; negative float -0.0 then give up.
2938 (deftransform - ((x y) (t (constant-argument t)) * :when :both)
2940 (let ((val (continuation-value y)))
2941 (unless (and (zerop val)
2942 (not (and (floatp val) (minusp (float-sign val))))
2943 (not-more-contagious y x))
2944 (give-up-ir1-transform)))
2947 ;;; Fold (OP x +/-1)
2948 (dolist (stuff '((* x (%negate x))
2951 (destructuring-bind (name result minus-result) stuff
2952 (deftransform name ((x y) '(t (constant-argument real)) '* :eval-name t
2954 "fold identity operations"
2955 (let ((val (continuation-value y)))
2956 (unless (and (= (abs val) 1)
2957 (not-more-contagious y x))
2958 (give-up-ir1-transform))
2959 (if (minusp val) minus-result result)))))
2961 ;;; Fold (expt x n) into multiplications for small integral values of
2962 ;;; N; convert (expt x 1/2) to sqrt.
2963 (deftransform expt ((x y) (t (constant-argument real)) *)
2964 "recode as multiplication or sqrt"
2965 (let ((val (continuation-value y)))
2966 ;; If Y would cause the result to be promoted to the same type as
2967 ;; Y, we give up. If not, then the result will be the same type
2968 ;; as X, so we can replace the exponentiation with simple
2969 ;; multiplication and division for small integral powers.
2970 (unless (not-more-contagious y x)
2971 (give-up-ir1-transform))
2972 (cond ((zerop val) '(float 1 x))
2973 ((= val 2) '(* x x))
2974 ((= val -2) '(/ (* x x)))
2975 ((= val 3) '(* x x x))
2976 ((= val -3) '(/ (* x x x)))
2977 ((= val 1/2) '(sqrt x))
2978 ((= val -1/2) '(/ (sqrt x)))
2979 (t (give-up-ir1-transform)))))
2981 ;;; KLUDGE: Shouldn't (/ 0.0 0.0), etc. cause exceptions in these
2982 ;;; transformations?
2983 ;;; Perhaps we should have to prove that the denominator is nonzero before
2984 ;;; doing them? (Also the DOLIST over macro calls is weird. Perhaps
2985 ;;; just FROB?) -- WHN 19990917
2986 (dolist (name '(ash /))
2987 (deftransform name ((x y) '((constant-argument (integer 0 0)) integer) '*
2988 :eval-name t :when :both)
2991 (dolist (name '(truncate round floor ceiling))
2992 (deftransform name ((x y) '((constant-argument (integer 0 0)) integer) '*
2993 :eval-name t :when :both)
2997 ;;;; character operations
2999 (deftransform char-equal ((a b) (base-char base-char))
3001 '(let* ((ac (char-code a))
3003 (sum (logxor ac bc)))
3005 (when (eql sum #x20)
3006 (let ((sum (+ ac bc)))
3007 (and (> sum 161) (< sum 213)))))))
3009 (deftransform char-upcase ((x) (base-char))
3011 '(let ((n-code (char-code x)))
3012 (if (and (> n-code #o140) ; Octal 141 is #\a.
3013 (< n-code #o173)) ; Octal 172 is #\z.
3014 (code-char (logxor #x20 n-code))
3017 (deftransform char-downcase ((x) (base-char))
3019 '(let ((n-code (char-code x)))
3020 (if (and (> n-code 64) ; 65 is #\A.
3021 (< n-code 91)) ; 90 is #\Z.
3022 (code-char (logxor #x20 n-code))
3025 ;;;; equality predicate transforms
3027 ;;; Return true if X and Y are continuations whose only use is a reference
3028 ;;; to the same leaf, and the value of the leaf cannot change.
3029 (defun same-leaf-ref-p (x y)
3030 (declare (type continuation x y))
3031 (let ((x-use (continuation-use x))
3032 (y-use (continuation-use y)))
3035 (eq (ref-leaf x-use) (ref-leaf y-use))
3036 (constant-reference-p x-use))))
3038 ;;; If X and Y are the same leaf, then the result is true. Otherwise, if
3039 ;;; there is no intersection between the types of the arguments, then the
3040 ;;; result is definitely false.
3041 (deftransform simple-equality-transform ((x y) * * :defun-only t
3043 (cond ((same-leaf-ref-p x y)
3045 ((not (types-intersect (continuation-type x) (continuation-type y)))
3048 (give-up-ir1-transform))))
3050 (dolist (x '(eq char= equal))
3051 (%deftransform x '(function * *) #'simple-equality-transform))
3053 ;;; Similar to SIMPLE-EQUALITY-PREDICATE, except that we also try to convert
3054 ;;; to a type-specific predicate or EQ:
3055 ;;; -- If both args are characters, convert to CHAR=. This is better than just
3056 ;;; converting to EQ, since CHAR= may have special compilation strategies
3057 ;;; for non-standard representations, etc.
3058 ;;; -- If either arg is definitely not a number, then we can compare with EQ.
3059 ;;; -- Otherwise, we try to put the arg we know more about second. If X is
3060 ;;; constant then we put it second. If X is a subtype of Y, we put it
3061 ;;; second. These rules make it easier for the back end to match these
3062 ;;; interesting cases.
3063 ;;; -- If Y is a fixnum, then we quietly pass because the back end can handle
3064 ;;; that case, otherwise give an efficency note.
3065 (deftransform eql ((x y) * * :when :both)
3066 "convert to simpler equality predicate"
3067 (let ((x-type (continuation-type x))
3068 (y-type (continuation-type y))
3069 (char-type (specifier-type 'character))
3070 (number-type (specifier-type 'number)))
3071 (cond ((same-leaf-ref-p x y)
3073 ((not (types-intersect x-type y-type))
3075 ((and (csubtypep x-type char-type)
3076 (csubtypep y-type char-type))
3078 ((or (not (types-intersect x-type number-type))
3079 (not (types-intersect y-type number-type)))
3081 ((and (not (constant-continuation-p y))
3082 (or (constant-continuation-p x)
3083 (and (csubtypep x-type y-type)
3084 (not (csubtypep y-type x-type)))))
3087 (give-up-ir1-transform)))))
3089 ;;; Convert to EQL if both args are rational and complexp is specified
3090 ;;; and the same for both.
3091 (deftransform = ((x y) * * :when :both)
3093 (let ((x-type (continuation-type x))
3094 (y-type (continuation-type y)))
3095 (if (and (csubtypep x-type (specifier-type 'number))
3096 (csubtypep y-type (specifier-type 'number)))
3097 (cond ((or (and (csubtypep x-type (specifier-type 'float))
3098 (csubtypep y-type (specifier-type 'float)))
3099 (and (csubtypep x-type (specifier-type '(complex float)))
3100 (csubtypep y-type (specifier-type '(complex float)))))
3101 ;; They are both floats. Leave as = so that -0.0 is
3102 ;; handled correctly.
3103 (give-up-ir1-transform))
3104 ((or (and (csubtypep x-type (specifier-type 'rational))
3105 (csubtypep y-type (specifier-type 'rational)))
3106 (and (csubtypep x-type (specifier-type '(complex rational)))
3107 (csubtypep y-type (specifier-type '(complex rational)))))
3108 ;; They are both rationals and complexp is the same. Convert
3112 (give-up-ir1-transform
3113 "The operands might not be the same type.")))
3114 (give-up-ir1-transform
3115 "The operands might not be the same type."))))
3117 ;;; If Cont's type is a numeric type, then return the type, otherwise
3118 ;;; GIVE-UP-IR1-TRANSFORM.
3119 (defun numeric-type-or-lose (cont)
3120 (declare (type continuation cont))
3121 (let ((res (continuation-type cont)))
3122 (unless (numeric-type-p res) (give-up-ir1-transform))
3125 ;;; See whether we can statically determine (< X Y) using type information.
3126 ;;; If X's high bound is < Y's low, then X < Y. Similarly, if X's low is >=
3127 ;;; to Y's high, the X >= Y (so return NIL). If not, at least make sure any
3128 ;;; constant arg is second.
3130 ;;; KLUDGE: Why should constant argument be second? It would be nice to find
3131 ;;; out and explain. -- WHN 19990917
3132 #!-propagate-float-type
3133 (defun ir1-transform-< (x y first second inverse)
3134 (if (same-leaf-ref-p x y)
3136 (let* ((x-type (numeric-type-or-lose x))
3137 (x-lo (numeric-type-low x-type))
3138 (x-hi (numeric-type-high x-type))
3139 (y-type (numeric-type-or-lose y))
3140 (y-lo (numeric-type-low y-type))
3141 (y-hi (numeric-type-high y-type)))
3142 (cond ((and x-hi y-lo (< x-hi y-lo))
3144 ((and y-hi x-lo (>= x-lo y-hi))
3146 ((and (constant-continuation-p first)
3147 (not (constant-continuation-p second)))
3150 (give-up-ir1-transform))))))
3151 #!+propagate-float-type
3152 (defun ir1-transform-< (x y first second inverse)
3153 (if (same-leaf-ref-p x y)
3155 (let ((xi (numeric-type->interval (numeric-type-or-lose x)))
3156 (yi (numeric-type->interval (numeric-type-or-lose y))))
3157 (cond ((interval-< xi yi)
3159 ((interval->= xi yi)
3161 ((and (constant-continuation-p first)
3162 (not (constant-continuation-p second)))
3165 (give-up-ir1-transform))))))
3167 (deftransform < ((x y) (integer integer) * :when :both)
3168 (ir1-transform-< x y x y '>))
3170 (deftransform > ((x y) (integer integer) * :when :both)
3171 (ir1-transform-< y x x y '<))
3173 #!+propagate-float-type
3174 (deftransform < ((x y) (float float) * :when :both)
3175 (ir1-transform-< x y x y '>))
3177 #!+propagate-float-type
3178 (deftransform > ((x y) (float float) * :when :both)
3179 (ir1-transform-< y x x y '<))
3181 ;;;; converting N-arg comparisons
3183 ;;;; We convert calls to N-arg comparison functions such as < into
3184 ;;;; two-arg calls. This transformation is enabled for all such
3185 ;;;; comparisons in this file. If any of these predicates are not
3186 ;;;; open-coded, then the transformation should be removed at some
3187 ;;;; point to avoid pessimization.
3189 ;;; This function is used for source transformation of N-arg
3190 ;;; comparison functions other than inequality. We deal both with
3191 ;;; converting to two-arg calls and inverting the sense of the test,
3192 ;;; if necessary. If the call has two args, then we pass or return a
3193 ;;; negated test as appropriate. If it is a degenerate one-arg call,
3194 ;;; then we transform to code that returns true. Otherwise, we bind
3195 ;;; all the arguments and expand into a bunch of IFs.
3196 (declaim (ftype (function (symbol list boolean) *) multi-compare))
3197 (defun multi-compare (predicate args not-p)
3198 (let ((nargs (length args)))
3199 (cond ((< nargs 1) (values nil t))
3200 ((= nargs 1) `(progn ,@args t))
3203 `(if (,predicate ,(first args) ,(second args)) nil t)
3206 (do* ((i (1- nargs) (1- i))
3208 (current (gensym) (gensym))
3209 (vars (list current) (cons current vars))
3210 (result 't (if not-p
3211 `(if (,predicate ,current ,last)
3213 `(if (,predicate ,current ,last)
3216 `((lambda ,vars ,result) . ,args)))))))
3218 (def-source-transform = (&rest args) (multi-compare '= args nil))
3219 (def-source-transform < (&rest args) (multi-compare '< args nil))
3220 (def-source-transform > (&rest args) (multi-compare '> args nil))
3221 (def-source-transform <= (&rest args) (multi-compare '> args t))
3222 (def-source-transform >= (&rest args) (multi-compare '< args t))
3224 (def-source-transform char= (&rest args) (multi-compare 'char= args nil))
3225 (def-source-transform char< (&rest args) (multi-compare 'char< args nil))
3226 (def-source-transform char> (&rest args) (multi-compare 'char> args nil))
3227 (def-source-transform char<= (&rest args) (multi-compare 'char> args t))
3228 (def-source-transform char>= (&rest args) (multi-compare 'char< args t))
3230 (def-source-transform char-equal (&rest args) (multi-compare 'char-equal args nil))
3231 (def-source-transform char-lessp (&rest args) (multi-compare 'char-lessp args nil))
3232 (def-source-transform char-greaterp (&rest args) (multi-compare 'char-greaterp args nil))
3233 (def-source-transform char-not-greaterp (&rest args) (multi-compare 'char-greaterp args t))
3234 (def-source-transform char-not-lessp (&rest args) (multi-compare 'char-lessp args t))
3236 ;;; This function does source transformation of N-arg inequality
3237 ;;; functions such as /=. This is similar to Multi-Compare in the <3
3238 ;;; arg cases. If there are more than two args, then we expand into
3239 ;;; the appropriate n^2 comparisons only when speed is important.
3240 (declaim (ftype (function (symbol list) *) multi-not-equal))
3241 (defun multi-not-equal (predicate args)
3242 (let ((nargs (length args)))
3243 (cond ((< nargs 1) (values nil t))
3244 ((= nargs 1) `(progn ,@args t))
3246 `(if (,predicate ,(first args) ,(second args)) nil t))
3247 ((not (policy nil (>= speed space) (>= speed cspeed)))
3250 (let ((vars (make-gensym-list nargs)))
3251 (do ((var vars next)
3252 (next (cdr vars) (cdr next))
3255 `((lambda ,vars ,result) . ,args))
3256 (let ((v1 (first var)))
3258 (setq result `(if (,predicate ,v1 ,v2) nil ,result))))))))))
3260 (def-source-transform /= (&rest args) (multi-not-equal '= args))
3261 (def-source-transform char/= (&rest args) (multi-not-equal 'char= args))
3262 (def-source-transform char-not-equal (&rest args) (multi-not-equal 'char-equal args))
3264 ;;; Expand MAX and MIN into the obvious comparisons.
3265 (def-source-transform max (arg &rest more-args)
3266 (if (null more-args)
3268 (once-only ((arg1 arg)
3269 (arg2 `(max ,@more-args)))
3270 `(if (> ,arg1 ,arg2)
3272 (def-source-transform min (arg &rest more-args)
3273 (if (null more-args)
3275 (once-only ((arg1 arg)
3276 (arg2 `(min ,@more-args)))
3277 `(if (< ,arg1 ,arg2)
3280 ;;;; converting N-arg arithmetic functions
3282 ;;;; N-arg arithmetic and logic functions are associated into two-arg
3283 ;;;; versions, and degenerate cases are flushed.
3285 ;;; Left-associate First-Arg and More-Args using Function.
3286 (declaim (ftype (function (symbol t list) list) associate-arguments))
3287 (defun associate-arguments (function first-arg more-args)
3288 (let ((next (rest more-args))
3289 (arg (first more-args)))
3291 `(,function ,first-arg ,arg)
3292 (associate-arguments function `(,function ,first-arg ,arg) next))))
3294 ;;; Do source transformations for transitive functions such as +.
3295 ;;; One-arg cases are replaced with the arg and zero arg cases with
3296 ;;; the identity. If Leaf-Fun is true, then replace two-arg calls with
3297 ;;; a call to that function.
3298 (defun source-transform-transitive (fun args identity &optional leaf-fun)
3299 (declare (symbol fun leaf-fun) (list args))
3302 (1 `(values ,(first args)))
3304 `(,leaf-fun ,(first args) ,(second args))
3307 (associate-arguments fun (first args) (rest args)))))
3309 (def-source-transform + (&rest args) (source-transform-transitive '+ args 0))
3310 (def-source-transform * (&rest args) (source-transform-transitive '* args 1))
3311 (def-source-transform logior (&rest args) (source-transform-transitive 'logior args 0))
3312 (def-source-transform logxor (&rest args) (source-transform-transitive 'logxor args 0))
3313 (def-source-transform logand (&rest args) (source-transform-transitive 'logand args -1))
3315 (def-source-transform logeqv (&rest args)
3316 (if (evenp (length args))
3317 `(lognot (logxor ,@args))
3320 ;;; Note: we can't use SOURCE-TRANSFORM-TRANSITIVE for GCD and LCM
3321 ;;; because when they are given one argument, they return its absolute
3324 (def-source-transform gcd (&rest args)
3327 (1 `(abs (the integer ,(first args))))
3329 (t (associate-arguments 'gcd (first args) (rest args)))))
3331 (def-source-transform lcm (&rest args)
3334 (1 `(abs (the integer ,(first args))))
3336 (t (associate-arguments 'lcm (first args) (rest args)))))
3338 ;;; Do source transformations for intransitive n-arg functions such as
3339 ;;; /. With one arg, we form the inverse. With two args we pass.
3340 ;;; Otherwise we associate into two-arg calls.
3341 (declaim (ftype (function (symbol list t) list) source-transform-intransitive))
3342 (defun source-transform-intransitive (function args inverse)
3344 ((0 2) (values nil t))
3345 (1 `(,@inverse ,(first args)))
3346 (t (associate-arguments function (first args) (rest args)))))
3348 (def-source-transform - (&rest args)
3349 (source-transform-intransitive '- args '(%negate)))
3350 (def-source-transform / (&rest args)
3351 (source-transform-intransitive '/ args '(/ 1)))
3355 ;;; We convert APPLY into MULTIPLE-VALUE-CALL so that the compiler
3356 ;;; only needs to understand one kind of variable-argument call. It is
3357 ;;; more efficient to convert APPLY to MV-CALL than MV-CALL to APPLY.
3358 (def-source-transform apply (fun arg &rest more-args)
3359 (let ((args (cons arg more-args)))
3360 `(multiple-value-call ,fun
3361 ,@(mapcar #'(lambda (x)
3364 (values-list ,(car (last args))))))
3368 ;;;; If the control string is a compile-time constant, then replace it
3369 ;;;; with a use of the FORMATTER macro so that the control string is
3370 ;;;; ``compiled.'' Furthermore, if the destination is either a stream
3371 ;;;; or T and the control string is a function (i.e. FORMATTER), then
3372 ;;;; convert the call to FORMAT to just a FUNCALL of that function.
3374 (deftransform format ((dest control &rest args) (t simple-string &rest t) *
3375 :policy (> speed space))
3376 (unless (constant-continuation-p control)
3377 (give-up-ir1-transform "The control string is not a constant."))
3378 (let ((arg-names (make-gensym-list (length args))))
3379 `(lambda (dest control ,@arg-names)
3380 (declare (ignore control))
3381 (format dest (formatter ,(continuation-value control)) ,@arg-names))))
3383 (deftransform format ((stream control &rest args) (stream function &rest t) *
3384 :policy (> speed space))
3385 (let ((arg-names (make-gensym-list (length args))))
3386 `(lambda (stream control ,@arg-names)
3387 (funcall control stream ,@arg-names)
3390 (deftransform format ((tee control &rest args) ((member t) function &rest t) *
3391 :policy (> speed space))
3392 (let ((arg-names (make-gensym-list (length args))))
3393 `(lambda (tee control ,@arg-names)
3394 (declare (ignore tee))
3395 (funcall control *standard-output* ,@arg-names)