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.
16 ;;; Convert into an IF so that IF optimizations will eliminate redundant
18 (def-source-transform not (x) `(if ,x nil t))
19 (def-source-transform null (x) `(if ,x nil t))
21 ;;; ENDP is just NULL with a LIST assertion. The assertion will be
22 ;;; optimized away when SAFETY optimization is low; hopefully that
23 ;;; is consistent with ANSI's "should return an error".
24 (def-source-transform endp (x) `(null (the list ,x)))
26 ;;; We turn IDENTITY into PROG1 so that it is obvious that it just
27 ;;; returns the first value of its argument. Ditto for VALUES with one
29 (def-source-transform identity (x) `(prog1 ,x))
30 (def-source-transform values (x) `(prog1 ,x))
32 ;;; Bind the values and make a closure that returns them.
33 (def-source-transform constantly (value)
34 (let ((rest (gensym "CONSTANTLY-REST-")))
35 `(lambda (&rest ,rest)
36 (declare (ignore ,rest))
39 ;;; If the function has a known number of arguments, then return a
40 ;;; lambda with the appropriate fixed number of args. If the
41 ;;; destination is a FUNCALL, then do the &REST APPLY thing, and let
42 ;;; MV optimization figure things out.
43 (deftransform complement ((fun) * * :node node :when :both)
45 (multiple-value-bind (min max)
46 (function-type-nargs (continuation-type fun))
48 ((and min (eql min max))
49 (let ((dums (make-gensym-list min)))
50 `#'(lambda ,dums (not (funcall fun ,@dums)))))
51 ((let* ((cont (node-cont node))
52 (dest (continuation-dest cont)))
53 (and (combination-p dest)
54 (eq (combination-fun dest) cont)))
55 '#'(lambda (&rest args)
56 (not (apply fun args))))
58 (give-up-ir1-transform
59 "The function doesn't have a fixed argument count.")))))
63 ;;; Translate CxxR into CAR/CDR combos.
65 (defun source-transform-cxr (form)
66 (if (or (byte-compiling) (/= (length form) 2))
68 (let ((name (symbol-name (car form))))
69 (do ((i (- (length name) 2) (1- i))
71 `(,(ecase (char name i)
78 (b '(1 0) (cons i b)))
80 (dotimes (j (ash 1 i))
81 (setf (info :function :source-transform
82 (intern (format nil "C~{~:[A~;D~]~}R"
83 (mapcar #'(lambda (x) (logbitp x j)) b))))
84 #'source-transform-cxr)))
86 ;;; Turn FIRST..FOURTH and REST into the obvious synonym, assuming
87 ;;; whatever is right for them is right for us. FIFTH..TENTH turn into
88 ;;; Nth, which can be expanded into a CAR/CDR later on if policy
90 (def-source-transform first (x) `(car ,x))
91 (def-source-transform rest (x) `(cdr ,x))
92 (def-source-transform second (x) `(cadr ,x))
93 (def-source-transform third (x) `(caddr ,x))
94 (def-source-transform fourth (x) `(cadddr ,x))
95 (def-source-transform fifth (x) `(nth 4 ,x))
96 (def-source-transform sixth (x) `(nth 5 ,x))
97 (def-source-transform seventh (x) `(nth 6 ,x))
98 (def-source-transform eighth (x) `(nth 7 ,x))
99 (def-source-transform ninth (x) `(nth 8 ,x))
100 (def-source-transform tenth (x) `(nth 9 ,x))
102 ;;; Translate RPLACx to LET and SETF.
103 (def-source-transform rplaca (x y)
108 (def-source-transform rplacd (x y)
114 (def-source-transform nth (n l) `(car (nthcdr ,n ,l)))
116 (defvar *default-nthcdr-open-code-limit* 6)
117 (defvar *extreme-nthcdr-open-code-limit* 20)
119 (deftransform nthcdr ((n l) (unsigned-byte t) * :node node)
120 "convert NTHCDR to CAxxR"
121 (unless (constant-continuation-p n)
122 (give-up-ir1-transform))
123 (let ((n (continuation-value n)))
125 (if (policy node (and (= speed 3) (= space 0)))
126 *extreme-nthcdr-open-code-limit*
127 *default-nthcdr-open-code-limit*))
128 (give-up-ir1-transform))
133 `(cdr ,(frob (1- n))))))
136 ;;;; arithmetic and numerology
138 (def-source-transform plusp (x) `(> ,x 0))
139 (def-source-transform minusp (x) `(< ,x 0))
140 (def-source-transform zerop (x) `(= ,x 0))
142 (def-source-transform 1+ (x) `(+ ,x 1))
143 (def-source-transform 1- (x) `(- ,x 1))
145 (def-source-transform oddp (x) `(not (zerop (logand ,x 1))))
146 (def-source-transform evenp (x) `(zerop (logand ,x 1)))
148 ;;; Note that all the integer division functions are available for
149 ;;; inline expansion.
151 ;;; FIXME: DEF-FROB instead of FROB
152 (macrolet ((frob (fun)
153 `(def-source-transform ,fun (x &optional (y nil y-p))
160 #!+propagate-float-type
162 #!+propagate-float-type
165 (def-source-transform lognand (x y) `(lognot (logand ,x ,y)))
166 (def-source-transform lognor (x y) `(lognot (logior ,x ,y)))
167 (def-source-transform logandc1 (x y) `(logand (lognot ,x) ,y))
168 (def-source-transform logandc2 (x y) `(logand ,x (lognot ,y)))
169 (def-source-transform logorc1 (x y) `(logior (lognot ,x) ,y))
170 (def-source-transform logorc2 (x y) `(logior ,x (lognot ,y)))
171 (def-source-transform logtest (x y) `(not (zerop (logand ,x ,y))))
172 (def-source-transform logbitp (index integer)
173 `(not (zerop (logand (ash 1 ,index) ,integer))))
174 (def-source-transform byte (size position) `(cons ,size ,position))
175 (def-source-transform byte-size (spec) `(car ,spec))
176 (def-source-transform byte-position (spec) `(cdr ,spec))
177 (def-source-transform ldb-test (bytespec integer)
178 `(not (zerop (mask-field ,bytespec ,integer))))
180 ;;; With the ratio and complex accessors, we pick off the "identity"
181 ;;; case, and use a primitive to handle the cell access case.
182 (def-source-transform numerator (num)
183 (once-only ((n-num `(the rational ,num)))
187 (def-source-transform denominator (num)
188 (once-only ((n-num `(the rational ,num)))
190 (%denominator ,n-num)
193 ;;;; Interval arithmetic for computing bounds
194 ;;;; (toy@rtp.ericsson.se)
196 ;;;; This is a set of routines for operating on intervals. It
197 ;;;; implements a simple interval arithmetic package. Although SBCL
198 ;;;; has an interval type in numeric-type, we choose to use our own
199 ;;;; for two reasons:
201 ;;;; 1. This package is simpler than numeric-type
203 ;;;; 2. It makes debugging much easier because you can just strip
204 ;;;; out these routines and test them independently of SBCL. (a
207 ;;;; One disadvantage is a probable increase in consing because we
208 ;;;; have to create these new interval structures even though
209 ;;;; numeric-type has everything we want to know. Reason 2 wins for
212 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
214 #!+propagate-float-type
217 ;;; The basic interval type. It can handle open and closed intervals.
218 ;;; A bound is open if it is a list containing a number, just like
219 ;;; Lisp says. NIL means unbounded.
220 (defstruct (interval (:constructor %make-interval)
224 (defun make-interval (&key low high)
225 (labels ((normalize-bound (val)
226 (cond ((and (floatp val)
227 (float-infinity-p val))
232 ;; Handle any closed bounds
235 ;; We have an open bound. Normalize the numeric
236 ;; bound. If the normalized bound is still a number
237 ;; (not nil), keep the bound open. Otherwise, the
238 ;; bound is really unbounded, so drop the openness.
239 (let ((new-val (normalize-bound (first val))))
241 ;; Bound exists, so keep it open still
244 (error "Unknown bound type in make-interval!")))))
245 (%make-interval :low (normalize-bound low)
246 :high (normalize-bound high))))
248 #!-sb-fluid (declaim (inline bound-value set-bound))
250 ;;; Extract the numeric value of a bound. Return NIL, if X is NIL.
251 (defun bound-value (x)
252 (if (consp x) (car x) x))
254 ;;; Given a number X, create a form suitable as a bound for an
255 ;;; interval. Make the bound open if OPEN-P is T. NIL remains NIL.
256 (defun set-bound (x open-p)
257 (if (and x open-p) (list x) x))
259 ;;; Apply the function F to a bound X. If X is an open bound, then
260 ;;; the result will be open. IF X is NIL, the result is NIL.
261 (defun bound-func (f x)
263 (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)
264 ;; With these traps masked, we might get things like infinity
265 ;; or negative infinity returned. Check for this and return
266 ;; NIL to indicate unbounded.
267 (let ((y (funcall f (bound-value x))))
269 (float-infinity-p y))
271 (set-bound (funcall f (bound-value x)) (consp x)))))))
273 ;;; Apply a binary operator OP to two bounds X and Y. The result is
274 ;;; NIL if either is NIL. Otherwise bound is computed and the result
275 ;;; is open if either X or Y is open.
277 ;;; FIXME: only used in this file, not needed in target runtime
278 (defmacro bound-binop (op x y)
280 (with-float-traps-masked (:underflow :overflow :inexact :divide-by-zero)
281 (set-bound (,op (bound-value ,x)
283 (or (consp ,x) (consp ,y))))))
285 ;;; NUMERIC-TYPE->INTERVAL
287 ;;; Convert a numeric-type object to an interval object.
289 (defun numeric-type->interval (x)
290 (declare (type numeric-type x))
291 (make-interval :low (numeric-type-low x)
292 :high (numeric-type-high x)))
294 (defun copy-interval-limit (limit)
299 (defun copy-interval (x)
300 (declare (type interval x))
301 (make-interval :low (copy-interval-limit (interval-low x))
302 :high (copy-interval-limit (interval-high x))))
306 ;;; Given a point P contained in the interval X, split X into two
307 ;;; interval at the point P. If CLOSE-LOWER is T, then the left
308 ;;; interval contains P. If CLOSE-UPPER is T, the right interval
309 ;;; contains P. You can specify both to be T or NIL.
310 (defun interval-split (p x &optional close-lower close-upper)
311 (declare (type number p)
313 (list (make-interval :low (copy-interval-limit (interval-low x))
314 :high (if close-lower p (list p)))
315 (make-interval :low (if close-upper (list p) p)
316 :high (copy-interval-limit (interval-high x)))))
320 ;;; Return the closure of the interval. That is, convert open bounds
321 ;;; to closed bounds.
322 (defun interval-closure (x)
323 (declare (type interval x))
324 (make-interval :low (bound-value (interval-low x))
325 :high (bound-value (interval-high x))))
327 (defun signed-zero->= (x y)
331 (>= (float-sign (float x))
332 (float-sign (float y))))))
334 ;;; INTERVAL-RANGE-INFO
336 ;;; For an interval X, if X >= POINT, return '+. If X <= POINT, return
337 ;;; '-. Otherwise return NIL.
339 (defun interval-range-info (x &optional (point 0))
340 (declare (type interval x))
341 (let ((lo (interval-low x))
342 (hi (interval-high x)))
343 (cond ((and lo (signed-zero->= (bound-value lo) point))
345 ((and hi (signed-zero->= point (bound-value hi)))
349 (defun interval-range-info (x &optional (point 0))
350 (declare (type interval x))
351 (labels ((signed->= (x y)
352 (if (and (zerop x) (zerop y) (floatp x) (floatp y))
353 (>= (float-sign x) (float-sign y))
355 (let ((lo (interval-low x))
356 (hi (interval-high x)))
357 (cond ((and lo (signed->= (bound-value lo) point))
359 ((and hi (signed->= point (bound-value hi)))
364 ;;; INTERVAL-BOUNDED-P
366 ;;; Test to see whether the interval X is bounded. HOW determines the
367 ;;; test, and should be either ABOVE, BELOW, or BOTH.
368 (defun interval-bounded-p (x how)
369 (declare (type interval x))
376 (and (interval-low x) (interval-high x)))))
378 ;;; Signed zero comparison functions. Use these functions if we need
379 ;;; to distinguish between signed zeroes.
381 (defun signed-zero-< (x y)
385 (< (float-sign (float x))
386 (float-sign (float y))))))
387 (defun signed-zero-> (x y)
391 (> (float-sign (float x))
392 (float-sign (float y))))))
394 (defun signed-zero-= (x y)
397 (= (float-sign (float x))
398 (float-sign (float y)))))
400 (defun signed-zero-<= (x y)
404 (<= (float-sign (float x))
405 (float-sign (float y))))))
407 ;;; INTERVAL-CONTAINS-P
409 ;;; See whether the interval X contains the number P, taking into account
410 ;;; that the interval might not be closed.
411 (defun interval-contains-p (p x)
412 (declare (type number p)
414 ;; Does the interval X contain the number P? This would be a lot
415 ;; easier if all intervals were closed!
416 (let ((lo (interval-low x))
417 (hi (interval-high x)))
419 ;; The interval is bounded
420 (if (and (signed-zero-<= (bound-value lo) p)
421 (signed-zero-<= p (bound-value hi)))
422 ;; P is definitely in the closure of the interval.
423 ;; We just need to check the end points now.
424 (cond ((signed-zero-= p (bound-value lo))
426 ((signed-zero-= p (bound-value hi))
431 ;; Interval with upper bound
432 (if (signed-zero-< p (bound-value hi))
434 (and (numberp hi) (signed-zero-= p hi))))
436 ;; Interval with lower bound
437 (if (signed-zero-> p (bound-value lo))
439 (and (numberp lo) (signed-zero-= p lo))))
441 ;; Interval with no bounds
444 ;;; INTERVAL-INTERSECT-P
446 ;;; Determine if two intervals X and Y intersect. Return T if so. If
447 ;;; CLOSED-INTERVALS-P is T, the treat the intervals as if they were
448 ;;; closed. Otherwise the intervals are treated as they are.
450 ;;; Thus if X = [0, 1) and Y = (1, 2), then they do not intersect
451 ;;; because no element in X is in Y. However, if CLOSED-INTERVALS-P
452 ;;; is T, then they do intersect because we use the closure of X = [0,
453 ;;; 1] and Y = [1, 2] to determine intersection.
454 (defun interval-intersect-p (x y &optional closed-intervals-p)
455 (declare (type interval x y))
456 (multiple-value-bind (intersect diff)
457 (interval-intersection/difference (if closed-intervals-p
460 (if closed-intervals-p
463 (declare (ignore diff))
466 ;;; Are the two intervals adjacent? That is, is there a number
467 ;;; between the two intervals that is not an element of either
468 ;;; interval? If so, they are not adjacent. For example [0, 1) and
469 ;;; [1, 2] are adjacent but [0, 1) and (1, 2] are not because 1 lies
470 ;;; between both intervals.
471 (defun interval-adjacent-p (x y)
472 (declare (type interval x y))
473 (flet ((adjacent (lo hi)
474 ;; Check to see whether lo and hi are adjacent. If either is
475 ;; nil, they can't be adjacent.
476 (when (and lo hi (= (bound-value lo) (bound-value hi)))
477 ;; The bounds are equal. They are adjacent if one of
478 ;; them is closed (a number). If both are open (consp),
479 ;; then there is a number that lies between them.
480 (or (numberp lo) (numberp hi)))))
481 (or (adjacent (interval-low y) (interval-high x))
482 (adjacent (interval-low x) (interval-high y)))))
484 ;;; INTERVAL-INTERSECTION/DIFFERENCE
486 ;;; Compute the intersection and difference between two intervals.
487 ;;; Two values are returned: the intersection and the difference.
489 ;;; Let the two intervals be X and Y, and let I and D be the two
490 ;;; values returned by this function. Then I = X intersect Y. If I
491 ;;; is NIL (the empty set), then D is X union Y, represented as the
492 ;;; list of X and Y. If I is not the empty set, then D is (X union Y)
493 ;;; - I, which is a list of two intervals.
495 ;;; For example, let X = [1,5] and Y = [-1,3). Then I = [1,3) and D =
496 ;;; [-1,1) union [3,5], which is returned as a list of two intervals.
497 (defun interval-intersection/difference (x y)
498 (declare (type interval x y))
499 (let ((x-lo (interval-low x))
500 (x-hi (interval-high x))
501 (y-lo (interval-low y))
502 (y-hi (interval-high y)))
505 ;; If p is an open bound, make it closed. If p is a closed
506 ;; bound, make it open.
511 ;; Test whether P is in the interval.
512 (when (interval-contains-p (bound-value p)
513 (interval-closure int))
514 (let ((lo (interval-low int))
515 (hi (interval-high int)))
516 ;; Check for endpoints
517 (cond ((and lo (= (bound-value p) (bound-value lo)))
518 (not (and (consp p) (numberp lo))))
519 ((and hi (= (bound-value p) (bound-value hi)))
520 (not (and (numberp p) (consp hi))))
522 (test-lower-bound (p int)
523 ;; P is a lower bound of an interval.
526 (not (interval-bounded-p int 'below))))
527 (test-upper-bound (p int)
528 ;; P is an upper bound of an interval
531 (not (interval-bounded-p int 'above)))))
532 (let ((x-lo-in-y (test-lower-bound x-lo y))
533 (x-hi-in-y (test-upper-bound x-hi y))
534 (y-lo-in-x (test-lower-bound y-lo x))
535 (y-hi-in-x (test-upper-bound y-hi x)))
536 (cond ((or x-lo-in-y x-hi-in-y y-lo-in-x y-hi-in-x)
537 ;; Intervals intersect. Let's compute the intersection
538 ;; and the difference.
539 (multiple-value-bind (lo left-lo left-hi)
540 (cond (x-lo-in-y (values x-lo y-lo (opposite-bound x-lo)))
541 (y-lo-in-x (values y-lo x-lo (opposite-bound y-lo))))
542 (multiple-value-bind (hi right-lo right-hi)
544 (values x-hi (opposite-bound x-hi) y-hi))
546 (values y-hi (opposite-bound y-hi) x-hi)))
547 (values (make-interval :low lo :high hi)
548 (list (make-interval :low left-lo :high left-hi)
549 (make-interval :low right-lo :high right-hi))))))
551 (values nil (list x y))))))))
553 ;;; INTERVAL-MERGE-PAIR
555 ;;; If intervals X and Y intersect, return a new interval that is the
556 ;;; union of the two. If they do not intersect, return NIL.
557 (defun interval-merge-pair (x y)
558 (declare (type interval x y))
559 ;; If x and y intersect or are adjacent, create the union.
560 ;; Otherwise return nil
561 (when (or (interval-intersect-p x y)
562 (interval-adjacent-p x y))
563 (flet ((select-bound (x1 x2 min-op max-op)
564 (let ((x1-val (bound-value x1))
565 (x2-val (bound-value x2)))
567 ;; Both bounds are finite. Select the right one.
568 (cond ((funcall min-op x1-val x2-val)
569 ;; x1 definitely better
571 ((funcall max-op x1-val x2-val)
572 ;; x2 definitely better
575 ;; Bounds are equal. Select either
576 ;; value and make it open only if
578 (set-bound x1-val (and (consp x1) (consp x2))))))
580 ;; At least one bound is not finite. The
581 ;; non-finite bound always wins.
583 (let* ((x-lo (copy-interval-limit (interval-low x)))
584 (x-hi (copy-interval-limit (interval-high x)))
585 (y-lo (copy-interval-limit (interval-low y)))
586 (y-hi (copy-interval-limit (interval-high y))))
587 (make-interval :low (select-bound x-lo y-lo #'< #'>)
588 :high (select-bound x-hi y-hi #'> #'<))))))
590 ;;; Basic arithmetic operations on intervals. We probably should do
591 ;;; true interval arithmetic here, but it's complicated because we
592 ;;; have float and integer types and bounds can be open or closed.
596 ;;; The negative of an interval
597 (defun interval-neg (x)
598 (declare (type interval x))
599 (make-interval :low (bound-func #'- (interval-high x))
600 :high (bound-func #'- (interval-low x))))
604 ;;; Add two intervals
605 (defun interval-add (x y)
606 (declare (type interval x y))
607 (make-interval :low (bound-binop + (interval-low x) (interval-low y))
608 :high (bound-binop + (interval-high x) (interval-high y))))
612 ;;; Subtract two intervals
613 (defun interval-sub (x y)
614 (declare (type interval x y))
615 (make-interval :low (bound-binop - (interval-low x) (interval-high y))
616 :high (bound-binop - (interval-high x) (interval-low y))))
620 ;;; Multiply two intervals
621 (defun interval-mul (x y)
622 (declare (type interval x y))
623 (flet ((bound-mul (x y)
624 (cond ((or (null x) (null y))
625 ;; Multiply by infinity is infinity
627 ((or (and (numberp x) (zerop x))
628 (and (numberp y) (zerop y)))
629 ;; Multiply by closed zero is special. The result
630 ;; is always a closed bound. But don't replace this
631 ;; with zero; we want the multiplication to produce
632 ;; the correct signed zero, if needed.
633 (* (bound-value x) (bound-value y)))
634 ((or (and (floatp x) (float-infinity-p x))
635 (and (floatp y) (float-infinity-p y)))
636 ;; Infinity times anything is infinity
639 ;; General multiply. The result is open if either is open.
640 (bound-binop * x y)))))
641 (let ((x-range (interval-range-info x))
642 (y-range (interval-range-info y)))
643 (cond ((null x-range)
644 ;; Split x into two and multiply each separately
645 (destructuring-bind (x- x+) (interval-split 0 x t t)
646 (interval-merge-pair (interval-mul x- y)
647 (interval-mul x+ y))))
649 ;; Split y into two and multiply each separately
650 (destructuring-bind (y- y+) (interval-split 0 y t t)
651 (interval-merge-pair (interval-mul x y-)
652 (interval-mul x y+))))
654 (interval-neg (interval-mul (interval-neg x) y)))
656 (interval-neg (interval-mul x (interval-neg y))))
657 ((and (eq x-range '+) (eq y-range '+))
658 ;; If we are here, X and Y are both positive
659 (make-interval :low (bound-mul (interval-low x) (interval-low y))
660 :high (bound-mul (interval-high x) (interval-high y))))
662 (error "This shouldn't happen!"))))))
666 ;;; Divide two intervals.
667 (defun interval-div (top bot)
668 (declare (type interval top bot))
669 (flet ((bound-div (x y y-low-p)
672 ;; Divide by infinity means result is 0. However,
673 ;; we need to watch out for the sign of the result,
674 ;; to correctly handle signed zeros. We also need
675 ;; to watch out for positive or negative infinity.
676 (if (floatp (bound-value x))
678 (- (float-sign (bound-value x) 0.0))
679 (float-sign (bound-value x) 0.0))
681 ((zerop (bound-value y))
682 ;; Divide by zero means result is infinity
684 ((and (numberp x) (zerop x))
685 ;; Zero divided by anything is zero.
688 (bound-binop / x y)))))
689 (let ((top-range (interval-range-info top))
690 (bot-range (interval-range-info bot)))
691 (cond ((null bot-range)
692 ;; The denominator contains zero, so anything goes!
693 (make-interval :low nil :high nil))
695 ;; Denominator is negative so flip the sign, compute the
696 ;; result, and flip it back.
697 (interval-neg (interval-div top (interval-neg bot))))
699 ;; Split top into two positive and negative parts, and
700 ;; divide each separately
701 (destructuring-bind (top- top+) (interval-split 0 top t t)
702 (interval-merge-pair (interval-div top- bot)
703 (interval-div top+ bot))))
705 ;; Top is negative so flip the sign, divide, and flip the
706 ;; sign of the result.
707 (interval-neg (interval-div (interval-neg top) bot)))
708 ((and (eq top-range '+) (eq bot-range '+))
710 (make-interval :low (bound-div (interval-low top) (interval-high bot) t)
711 :high (bound-div (interval-high top) (interval-low bot) nil)))
713 (error "This shouldn't happen!"))))))
717 ;;; Apply the function F to the interval X. If X = [a, b], then the
718 ;;; result is [f(a), f(b)]. It is up to the user to make sure the
719 ;;; result makes sense. It will if F is monotonic increasing (or
721 (defun interval-func (f x)
722 (declare (type interval x))
723 (let ((lo (bound-func f (interval-low x)))
724 (hi (bound-func f (interval-high x))))
725 (make-interval :low lo :high hi)))
729 ;;; Return T if X < Y. That is every number in the interval X is
730 ;;; always less than any number in the interval Y.
731 (defun interval-< (x y)
732 (declare (type interval x y))
733 ;; X < Y only if X is bounded above, Y is bounded below, and they
735 (when (and (interval-bounded-p x 'above)
736 (interval-bounded-p y 'below))
737 ;; Intervals are bounded in the appropriate way. Make sure they
739 (let ((left (interval-high x))
740 (right (interval-low y)))
741 (cond ((> (bound-value left)
743 ;; Definitely overlap so result is NIL
745 ((< (bound-value left)
747 ;; Definitely don't touch, so result is T
750 ;; Limits are equal. Check for open or closed bounds.
751 ;; Don't overlap if one or the other are open.
752 (or (consp left) (consp right)))))))
756 ;;; Return T if X >= Y. That is, every number in the interval X is
757 ;;; always greater than any number in the interval Y.
758 (defun interval->= (x y)
759 (declare (type interval x y))
760 ;; X >= Y if lower bound of X >= upper bound of Y
761 (when (and (interval-bounded-p x 'below)
762 (interval-bounded-p y 'above))
763 (>= (bound-value (interval-low x)) (bound-value (interval-high y)))))
767 ;;; Return an interval that is the absolute value of X. Thus, if X =
768 ;;; [-1 10], the result is [0, 10].
769 (defun interval-abs (x)
770 (declare (type interval x))
771 (case (interval-range-info x)
777 (destructuring-bind (x- x+) (interval-split 0 x t t)
778 (interval-merge-pair (interval-neg x-) x+)))))
782 ;;; Compute the square of an interval.
783 (defun interval-sqr (x)
784 (declare (type interval x))
785 (interval-func #'(lambda (x) (* x x))
789 ;;;; numeric derive-type methods
791 ;;; Utility for defining derive-type methods of integer operations. If the
792 ;;; types of both X and Y are integer types, then we compute a new integer type
793 ;;; with bounds determined Fun when applied to X and Y. Otherwise, we use
794 ;;; Numeric-Contagion.
795 (defun derive-integer-type (x y fun)
796 (declare (type continuation x y) (type function fun))
797 (let ((x (continuation-type x))
798 (y (continuation-type y)))
799 (if (and (numeric-type-p x) (numeric-type-p y)
800 (eq (numeric-type-class x) 'integer)
801 (eq (numeric-type-class y) 'integer)
802 (eq (numeric-type-complexp x) :real)
803 (eq (numeric-type-complexp y) :real))
804 (multiple-value-bind (low high) (funcall fun x y)
805 (make-numeric-type :class 'integer
809 (numeric-contagion x y))))
811 #!+(or propagate-float-type propagate-fun-type)
814 ;; Simple utility to flatten a list
815 (defun flatten-list (x)
816 (labels ((flatten-helper (x r);; 'r' is the stuff to the 'right'.
820 (t (flatten-helper (car x)
821 (flatten-helper (cdr x) r))))))
822 (flatten-helper x nil)))
824 ;;; Take some type of continuation and massage it so that we get a
825 ;;; list of the constituent types. If ARG is *EMPTY-TYPE*, return NIL
826 ;;; to indicate failure.
827 (defun prepare-arg-for-derive-type (arg)
828 (flet ((listify (arg)
833 (union-type-types arg))
836 (unless (eq arg *empty-type*)
837 ;; Make sure all args are some type of numeric-type. For member
838 ;; types, convert the list of members into a union of equivalent
839 ;; single-element member-type's.
840 (let ((new-args nil))
841 (dolist (arg (listify arg))
842 (if (member-type-p arg)
843 ;; Run down the list of members and convert to a list of
845 (dolist (member (member-type-members arg))
846 (push (if (numberp member)
847 (make-member-type :members (list member))
850 (push arg new-args)))
851 (unless (member *empty-type* new-args)
854 ;;; Convert from the standard type convention for which -0.0 and 0.0
855 ;;; and equal to an intermediate convention for which they are
856 ;;; considered different which is more natural for some of the
858 #!-negative-zero-is-not-zero
859 (defun convert-numeric-type (type)
860 (declare (type numeric-type type))
861 ;;; Only convert real float interval delimiters types.
862 (if (eq (numeric-type-complexp type) :real)
863 (let* ((lo (numeric-type-low type))
864 (lo-val (bound-value lo))
865 (lo-float-zero-p (and lo (floatp lo-val) (= lo-val 0.0)))
866 (hi (numeric-type-high type))
867 (hi-val (bound-value hi))
868 (hi-float-zero-p (and hi (floatp hi-val) (= hi-val 0.0))))
869 (if (or lo-float-zero-p hi-float-zero-p)
871 :class (numeric-type-class type)
872 :format (numeric-type-format type)
874 :low (if lo-float-zero-p
876 (list (float 0.0 lo-val))
879 :high (if hi-float-zero-p
881 (list (float -0.0 hi-val))
888 ;;; Convert back from the intermediate convention for which -0.0 and
889 ;;; 0.0 are considered different to the standard type convention for
891 #!-negative-zero-is-not-zero
892 (defun convert-back-numeric-type (type)
893 (declare (type numeric-type type))
894 ;;; Only convert real float interval delimiters types.
895 (if (eq (numeric-type-complexp type) :real)
896 (let* ((lo (numeric-type-low type))
897 (lo-val (bound-value lo))
899 (and lo (floatp lo-val) (= lo-val 0.0)
900 (float-sign lo-val)))
901 (hi (numeric-type-high type))
902 (hi-val (bound-value hi))
904 (and hi (floatp hi-val) (= hi-val 0.0)
905 (float-sign hi-val))))
907 ;; (float +0.0 +0.0) => (member 0.0)
908 ;; (float -0.0 -0.0) => (member -0.0)
909 ((and lo-float-zero-p hi-float-zero-p)
910 ;; Shouldn't have exclusive bounds here.
911 (assert (and (not (consp lo)) (not (consp hi))))
912 (if (= lo-float-zero-p hi-float-zero-p)
913 ;; (float +0.0 +0.0) => (member 0.0)
914 ;; (float -0.0 -0.0) => (member -0.0)
915 (specifier-type `(member ,lo-val))
916 ;; (float -0.0 +0.0) => (float 0.0 0.0)
917 ;; (float +0.0 -0.0) => (float 0.0 0.0)
918 (make-numeric-type :class (numeric-type-class type)
919 :format (numeric-type-format type)
925 ;; (float -0.0 x) => (float 0.0 x)
926 ((and (not (consp lo)) (minusp lo-float-zero-p))
927 (make-numeric-type :class (numeric-type-class type)
928 :format (numeric-type-format type)
930 :low (float 0.0 lo-val)
932 ;; (float (+0.0) x) => (float (0.0) x)
933 ((and (consp lo) (plusp lo-float-zero-p))
934 (make-numeric-type :class (numeric-type-class type)
935 :format (numeric-type-format type)
937 :low (list (float 0.0 lo-val))
940 ;; (float +0.0 x) => (or (member 0.0) (float (0.0) x))
941 ;; (float (-0.0) x) => (or (member 0.0) (float (0.0) x))
942 (list (make-member-type :members (list (float 0.0 lo-val)))
943 (make-numeric-type :class (numeric-type-class type)
944 :format (numeric-type-format type)
946 :low (list (float 0.0 lo-val))
950 ;; (float x +0.0) => (float x 0.0)
951 ((and (not (consp hi)) (plusp hi-float-zero-p))
952 (make-numeric-type :class (numeric-type-class type)
953 :format (numeric-type-format type)
956 :high (float 0.0 hi-val)))
957 ;; (float x (-0.0)) => (float x (0.0))
958 ((and (consp hi) (minusp hi-float-zero-p))
959 (make-numeric-type :class (numeric-type-class type)
960 :format (numeric-type-format type)
963 :high (list (float 0.0 hi-val))))
965 ;; (float x (+0.0)) => (or (member -0.0) (float x (0.0)))
966 ;; (float x -0.0) => (or (member -0.0) (float x (0.0)))
967 (list (make-member-type :members (list (float -0.0 hi-val)))
968 (make-numeric-type :class (numeric-type-class type)
969 :format (numeric-type-format type)
972 :high (list (float 0.0 hi-val)))))))
978 ;;; Convert back a possible list of numeric types.
979 #!-negative-zero-is-not-zero
980 (defun convert-back-numeric-type-list (type-list)
984 (dolist (type type-list)
985 (if (numeric-type-p type)
986 (let ((result (convert-back-numeric-type type)))
988 (setf results (append results result))
989 (push result results)))
990 (push type results)))
993 (convert-back-numeric-type type-list))
995 (convert-back-numeric-type-list (union-type-types type-list)))
999 ;;; Take a list of types and return a canonical type specifier,
1000 ;;; combining any MEMBER types together. If both positive and
1001 ;;; negative MEMBER types are present they are converted to a float
1002 ;;; type. XXX This would be far simpler if the type-union methods could
1003 ;;; handle member/number unions.
1004 (defun make-canonical-union-type (type-list)
1007 (dolist (type type-list)
1008 (if (member-type-p type)
1009 (setf members (union members (member-type-members type)))
1010 (push type misc-types)))
1012 (when (null (set-difference '(-0l0 0l0) members))
1013 #!-negative-zero-is-not-zero
1014 (push (specifier-type '(long-float 0l0 0l0)) misc-types)
1015 #!+negative-zero-is-not-zero
1016 (push (specifier-type '(long-float -0l0 0l0)) misc-types)
1017 (setf members (set-difference members '(-0l0 0l0))))
1018 (when (null (set-difference '(-0d0 0d0) members))
1019 #!-negative-zero-is-not-zero
1020 (push (specifier-type '(double-float 0d0 0d0)) misc-types)
1021 #!+negative-zero-is-not-zero
1022 (push (specifier-type '(double-float -0d0 0d0)) misc-types)
1023 (setf members (set-difference members '(-0d0 0d0))))
1024 (when (null (set-difference '(-0f0 0f0) members))
1025 #!-negative-zero-is-not-zero
1026 (push (specifier-type '(single-float 0f0 0f0)) misc-types)
1027 #!+negative-zero-is-not-zero
1028 (push (specifier-type '(single-float -0f0 0f0)) misc-types)
1029 (setf members (set-difference members '(-0f0 0f0))))
1030 (cond ((null members)
1031 (let ((res (first misc-types)))
1032 (dolist (type (rest misc-types))
1033 (setq res (type-union res type)))
1036 (make-member-type :members members))
1038 (let ((res (first misc-types)))
1039 (dolist (type (rest misc-types))
1040 (setq res (type-union res type)))
1041 (dolist (type members)
1042 (setq res (type-union
1043 res (make-member-type :members (list type)))))
1046 ;;; Convert-Member-Type
1048 ;;; Convert a member type with a single member to a numeric type.
1049 (defun convert-member-type (arg)
1050 (let* ((members (member-type-members arg))
1051 (member (first members))
1052 (member-type (type-of member)))
1053 (assert (not (rest members)))
1054 (specifier-type `(,(if (subtypep member-type 'integer)
1059 ;;; ONE-ARG-DERIVE-TYPE
1061 ;;; This is used in defoptimizers for computing the resulting type of
1064 ;;; Given the continuation ARG, derive the resulting type using the
1065 ;;; DERIVE-FCN. DERIVE-FCN takes exactly one argument which is some
1066 ;;; "atomic" continuation type like numeric-type or member-type
1067 ;;; (containing just one element). It should return the resulting
1068 ;;; type, which can be a list of types.
1070 ;;; For the case of member types, if a member-fcn is given it is
1071 ;;; called to compute the result otherwise the member type is first
1072 ;;; converted to a numeric type and the derive-fcn is call.
1073 (defun one-arg-derive-type (arg derive-fcn member-fcn
1074 &optional (convert-type t))
1075 (declare (type function derive-fcn)
1076 (type (or null function) member-fcn)
1077 #!+negative-zero-is-not-zero (ignore convert-type))
1078 (let ((arg-list (prepare-arg-for-derive-type (continuation-type arg))))
1084 (with-float-traps-masked
1085 (:underflow :overflow :divide-by-zero)
1089 (first (member-type-members x))))))
1090 ;; Otherwise convert to a numeric type.
1091 (let ((result-type-list
1092 (funcall derive-fcn (convert-member-type x))))
1093 #!-negative-zero-is-not-zero
1095 (convert-back-numeric-type-list result-type-list)
1097 #!+negative-zero-is-not-zero
1100 #!-negative-zero-is-not-zero
1102 (convert-back-numeric-type-list
1103 (funcall derive-fcn (convert-numeric-type x)))
1104 (funcall derive-fcn x))
1105 #!+negative-zero-is-not-zero
1106 (funcall derive-fcn x))
1108 *universal-type*))))
1109 ;; Run down the list of args and derive the type of each one,
1110 ;; saving all of the results in a list.
1111 (let ((results nil))
1112 (dolist (arg arg-list)
1113 (let ((result (deriver arg)))
1115 (setf results (append results result))
1116 (push result results))))
1118 (make-canonical-union-type results)
1119 (first results)))))))
1121 ;;; TWO-ARG-DERIVE-TYPE
1123 ;;; Same as ONE-ARG-DERIVE-TYPE, except we assume the function takes
1124 ;;; two arguments. DERIVE-FCN takes 3 args in this case: the two
1125 ;;; original args and a third which is T to indicate if the two args
1126 ;;; really represent the same continuation. This is useful for
1127 ;;; deriving the type of things like (* x x), which should always be
1128 ;;; positive. If we didn't do this, we wouldn't be able to tell.
1129 (defun two-arg-derive-type (arg1 arg2 derive-fcn fcn
1130 &optional (convert-type t))
1131 #!+negative-zero-is-not-zero
1132 (declare (ignore convert-type))
1133 (flet (#!-negative-zero-is-not-zero
1134 (deriver (x y same-arg)
1135 (cond ((and (member-type-p x) (member-type-p y))
1136 (let* ((x (first (member-type-members x)))
1137 (y (first (member-type-members y)))
1138 (result (with-float-traps-masked
1139 (:underflow :overflow :divide-by-zero
1141 (funcall fcn x y))))
1142 (cond ((null result))
1143 ((and (floatp result) (float-nan-p result))
1146 :format (type-of result)
1149 (make-member-type :members (list result))))))
1150 ((and (member-type-p x) (numeric-type-p y))
1151 (let* ((x (convert-member-type x))
1152 (y (if convert-type (convert-numeric-type y) y))
1153 (result (funcall derive-fcn x y same-arg)))
1155 (convert-back-numeric-type-list result)
1157 ((and (numeric-type-p x) (member-type-p y))
1158 (let* ((x (if convert-type (convert-numeric-type x) x))
1159 (y (convert-member-type y))
1160 (result (funcall derive-fcn x y same-arg)))
1162 (convert-back-numeric-type-list result)
1164 ((and (numeric-type-p x) (numeric-type-p y))
1165 (let* ((x (if convert-type (convert-numeric-type x) x))
1166 (y (if convert-type (convert-numeric-type y) y))
1167 (result (funcall derive-fcn x y same-arg)))
1169 (convert-back-numeric-type-list result)
1173 #!+negative-zero-is-not-zero
1174 (deriver (x y same-arg)
1175 (cond ((and (member-type-p x) (member-type-p y))
1176 (let* ((x (first (member-type-members x)))
1177 (y (first (member-type-members y)))
1178 (result (with-float-traps-masked
1179 (:underflow :overflow :divide-by-zero)
1180 (funcall fcn x y))))
1182 (make-member-type :members (list result)))))
1183 ((and (member-type-p x) (numeric-type-p y))
1184 (let ((x (convert-member-type x)))
1185 (funcall derive-fcn x y same-arg)))
1186 ((and (numeric-type-p x) (member-type-p y))
1187 (let ((y (convert-member-type y)))
1188 (funcall derive-fcn x y same-arg)))
1189 ((and (numeric-type-p x) (numeric-type-p y))
1190 (funcall derive-fcn x y same-arg))
1192 *universal-type*))))
1193 (let ((same-arg (same-leaf-ref-p arg1 arg2))
1194 (a1 (prepare-arg-for-derive-type (continuation-type arg1)))
1195 (a2 (prepare-arg-for-derive-type (continuation-type arg2))))
1197 (let ((results nil))
1199 ;; Since the args are the same continuation, just run
1202 (let ((result (deriver x x same-arg)))
1204 (setf results (append results result))
1205 (push result results))))
1206 ;; Try all pairwise combinations.
1209 (let ((result (or (deriver x y same-arg)
1210 (numeric-contagion x y))))
1212 (setf results (append results result))
1213 (push result results))))))
1215 (make-canonical-union-type results)
1216 (first results)))))))
1220 #!-propagate-float-type
1222 (defoptimizer (+ derive-type) ((x y))
1223 (derive-integer-type
1230 (values (frob (numeric-type-low x) (numeric-type-low y))
1231 (frob (numeric-type-high x) (numeric-type-high y)))))))
1233 (defoptimizer (- derive-type) ((x y))
1234 (derive-integer-type
1241 (values (frob (numeric-type-low x) (numeric-type-high y))
1242 (frob (numeric-type-high x) (numeric-type-low y)))))))
1244 (defoptimizer (* derive-type) ((x y))
1245 (derive-integer-type
1248 (let ((x-low (numeric-type-low x))
1249 (x-high (numeric-type-high x))
1250 (y-low (numeric-type-low y))
1251 (y-high (numeric-type-high y)))
1252 (cond ((not (and x-low y-low))
1254 ((or (minusp x-low) (minusp y-low))
1255 (if (and x-high y-high)
1256 (let ((max (* (max (abs x-low) (abs x-high))
1257 (max (abs y-low) (abs y-high)))))
1258 (values (- max) max))
1261 (values (* x-low y-low)
1262 (if (and x-high y-high)
1266 (defoptimizer (/ derive-type) ((x y))
1267 (numeric-contagion (continuation-type x) (continuation-type y)))
1271 #!+propagate-float-type
1273 (defun +-derive-type-aux (x y same-arg)
1274 (if (and (numeric-type-real-p x)
1275 (numeric-type-real-p y))
1278 (let ((x-int (numeric-type->interval x)))
1279 (interval-add x-int x-int))
1280 (interval-add (numeric-type->interval x)
1281 (numeric-type->interval y))))
1282 (result-type (numeric-contagion x y)))
1283 ;; If the result type is a float, we need to be sure to coerce
1284 ;; the bounds into the correct type.
1285 (when (eq (numeric-type-class result-type) 'float)
1286 (setf result (interval-func
1288 (coerce x (or (numeric-type-format result-type)
1292 :class (if (and (eq (numeric-type-class x) 'integer)
1293 (eq (numeric-type-class y) 'integer))
1294 ;; The sum of integers is always an integer
1296 (numeric-type-class result-type))
1297 :format (numeric-type-format result-type)
1298 :low (interval-low result)
1299 :high (interval-high result)))
1300 ;; General contagion
1301 (numeric-contagion x y)))
1303 (defoptimizer (+ derive-type) ((x y))
1304 (two-arg-derive-type x y #'+-derive-type-aux #'+))
1306 (defun --derive-type-aux (x y same-arg)
1307 (if (and (numeric-type-real-p x)
1308 (numeric-type-real-p y))
1310 ;; (- x x) is always 0.
1312 (make-interval :low 0 :high 0)
1313 (interval-sub (numeric-type->interval x)
1314 (numeric-type->interval y))))
1315 (result-type (numeric-contagion x y)))
1316 ;; If the result type is a float, we need to be sure to coerce
1317 ;; the bounds into the correct type.
1318 (when (eq (numeric-type-class result-type) 'float)
1319 (setf result (interval-func
1321 (coerce x (or (numeric-type-format result-type)
1325 :class (if (and (eq (numeric-type-class x) 'integer)
1326 (eq (numeric-type-class y) 'integer))
1327 ;; The difference of integers is always an integer
1329 (numeric-type-class result-type))
1330 :format (numeric-type-format result-type)
1331 :low (interval-low result)
1332 :high (interval-high result)))
1333 ;; General contagion
1334 (numeric-contagion x y)))
1336 (defoptimizer (- derive-type) ((x y))
1337 (two-arg-derive-type x y #'--derive-type-aux #'-))
1339 (defun *-derive-type-aux (x y same-arg)
1340 (if (and (numeric-type-real-p x)
1341 (numeric-type-real-p y))
1343 ;; (* x x) is always positive, so take care to do it
1346 (interval-sqr (numeric-type->interval x))
1347 (interval-mul (numeric-type->interval x)
1348 (numeric-type->interval y))))
1349 (result-type (numeric-contagion x y)))
1350 ;; If the result type is a float, we need to be sure to coerce
1351 ;; the bounds into the correct type.
1352 (when (eq (numeric-type-class result-type) 'float)
1353 (setf result (interval-func
1355 (coerce x (or (numeric-type-format result-type)
1359 :class (if (and (eq (numeric-type-class x) 'integer)
1360 (eq (numeric-type-class y) 'integer))
1361 ;; The product of integers is always an integer
1363 (numeric-type-class result-type))
1364 :format (numeric-type-format result-type)
1365 :low (interval-low result)
1366 :high (interval-high result)))
1367 (numeric-contagion x y)))
1369 (defoptimizer (* derive-type) ((x y))
1370 (two-arg-derive-type x y #'*-derive-type-aux #'*))
1372 (defun /-derive-type-aux (x y same-arg)
1373 (if (and (numeric-type-real-p x)
1374 (numeric-type-real-p y))
1376 ;; (/ x x) is always 1, except if x can contain 0. In
1377 ;; that case, we shouldn't optimize the division away
1378 ;; because we want 0/0 to signal an error.
1380 (not (interval-contains-p
1381 0 (interval-closure (numeric-type->interval y)))))
1382 (make-interval :low 1 :high 1)
1383 (interval-div (numeric-type->interval x)
1384 (numeric-type->interval y))))
1385 (result-type (numeric-contagion x y)))
1386 ;; If the result type is a float, we need to be sure to coerce
1387 ;; the bounds into the correct type.
1388 (when (eq (numeric-type-class result-type) 'float)
1389 (setf result (interval-func
1391 (coerce x (or (numeric-type-format result-type)
1394 (make-numeric-type :class (numeric-type-class result-type)
1395 :format (numeric-type-format result-type)
1396 :low (interval-low result)
1397 :high (interval-high result)))
1398 (numeric-contagion x y)))
1400 (defoptimizer (/ derive-type) ((x y))
1401 (two-arg-derive-type x y #'/-derive-type-aux #'/))
1406 ;;; ASH derive type optimizer
1408 ;;; Large resulting bounds are easy to generate but are not
1409 ;;; particularly useful, so an open outer bound is returned for a
1410 ;;; shift greater than 64 - the largest word size of any of the ports.
1411 ;;; Large negative shifts are also problematic as the ASH
1412 ;;; implementation only accepts shifts greater than
1413 ;;; MOST-NEGATIVE-FIXNUM. These issues are handled by two local
1415 ;;; ASH-OUTER: Perform the shift when within an acceptable range,
1416 ;;; otherwise return an open bound.
1417 ;;; ASH-INNER: Perform the shift when within range, limited to a
1418 ;;; maximum of 64, otherwise returns the inner limit.
1420 ;;; KLUDGE: All this ASH optimization is suppressed under CMU CL
1421 ;;; because as of version 2.4.6 for Debian, CMU CL blows up on (ASH
1422 ;;; 1000000000 -100000000000) (i.e. ASH of two bignums yielding zero)
1423 ;;; and it's hard to avoid that calculation in here.
1424 #-(and cmu sb-xc-host)
1426 #!-propagate-fun-type
1427 (defoptimizer (ash derive-type) ((n shift))
1428 (flet ((ash-outer (n s)
1429 (when (and (target-fixnump s)
1431 (> s sb!vm:*target-most-negative-fixnum*))
1434 (if (and (target-fixnump s)
1435 (> s sb!vm:*target-most-negative-fixnum*))
1437 (if (minusp n) -1 0))))
1438 (or (let ((n-type (continuation-type n)))
1439 (when (numeric-type-p n-type)
1440 (let ((n-low (numeric-type-low n-type))
1441 (n-high (numeric-type-high n-type)))
1442 (if (constant-continuation-p shift)
1443 (let ((shift (continuation-value shift)))
1444 (make-numeric-type :class 'integer
1446 :low (when n-low (ash n-low shift))
1447 :high (when n-high (ash n-high shift))))
1448 (let ((s-type (continuation-type shift)))
1449 (when (numeric-type-p s-type)
1450 (let* ((s-low (numeric-type-low s-type))
1451 (s-high (numeric-type-high s-type))
1452 (low-slot (when n-low
1454 (ash-outer n-low s-high)
1455 (ash-inner n-low s-low))))
1456 (high-slot (when n-high
1458 (ash-inner n-high s-low)
1459 (ash-outer n-high s-high)))))
1460 (make-numeric-type :class 'integer
1463 :high high-slot))))))))
1465 (or (let ((n-type (continuation-type n)))
1466 (when (numeric-type-p n-type)
1467 (let ((n-low (numeric-type-low n-type))
1468 (n-high (numeric-type-high n-type)))
1469 (if (constant-continuation-p shift)
1470 (let ((shift (continuation-value shift)))
1471 (make-numeric-type :class 'integer
1473 :low (when n-low (ash n-low shift))
1474 :high (when n-high (ash n-high shift))))
1475 (let ((s-type (continuation-type shift)))
1476 (when (numeric-type-p s-type)
1477 (let ((s-low (numeric-type-low s-type))
1478 (s-high (numeric-type-high s-type)))
1479 (if (and s-low s-high (<= s-low 64) (<= s-high 64))
1480 (make-numeric-type :class 'integer
1483 (min (ash n-low s-high)
1486 (max (ash n-high s-high)
1487 (ash n-high s-low))))
1488 (make-numeric-type :class 'integer
1489 :complexp :real)))))))))
1492 #!+propagate-fun-type
1493 (defun ash-derive-type-aux (n-type shift same-arg)
1494 (declare (ignore same-arg))
1495 (flet ((ash-outer (n s)
1496 (when (and (target-fixnump s)
1498 (> s sb!vm:*target-most-negative-fixnum*))
1500 ;; KLUDGE: The bare 64's here should be related to
1501 ;; symbolic machine word size values somehow.
1504 (if (and (target-fixnump s)
1505 (> s sb!vm:*target-most-negative-fixnum*))
1507 (if (minusp n) -1 0))))
1508 (or (and (csubtypep n-type (specifier-type 'integer))
1509 (csubtypep shift (specifier-type 'integer))
1510 (let ((n-low (numeric-type-low n-type))
1511 (n-high (numeric-type-high n-type))
1512 (s-low (numeric-type-low shift))
1513 (s-high (numeric-type-high shift)))
1514 (make-numeric-type :class 'integer :complexp :real
1517 (ash-outer n-low s-high)
1518 (ash-inner n-low s-low)))
1521 (ash-inner n-high s-low)
1522 (ash-outer n-high s-high))))))
1525 #!+propagate-fun-type
1526 (defoptimizer (ash derive-type) ((n shift))
1527 (two-arg-derive-type n shift #'ash-derive-type-aux #'ash))
1530 #!-propagate-float-type
1531 (macrolet ((frob (fun)
1532 `#'(lambda (type type2)
1533 (declare (ignore type2))
1534 (let ((lo (numeric-type-low type))
1535 (hi (numeric-type-high type)))
1536 (values (if hi (,fun hi) nil) (if lo (,fun lo) nil))))))
1538 (defoptimizer (%negate derive-type) ((num))
1539 (derive-integer-type num num (frob -)))
1541 (defoptimizer (lognot derive-type) ((int))
1542 (derive-integer-type int int (frob lognot))))
1544 #!+propagate-float-type
1545 (defoptimizer (lognot derive-type) ((int))
1546 (derive-integer-type int int
1547 #'(lambda (type type2)
1548 (declare (ignore type2))
1549 (let ((lo (numeric-type-low type))
1550 (hi (numeric-type-high type)))
1551 (values (if hi (lognot hi) nil)
1552 (if lo (lognot lo) nil)
1553 (numeric-type-class type)
1554 (numeric-type-format type))))))
1556 #!+propagate-float-type
1557 (defoptimizer (%negate derive-type) ((num))
1558 (flet ((negate-bound (b)
1559 (set-bound (- (bound-value b)) (consp b))))
1560 (one-arg-derive-type num
1562 (let ((lo (numeric-type-low type))
1563 (hi (numeric-type-high type))
1564 (result (copy-numeric-type type)))
1565 (setf (numeric-type-low result)
1566 (if hi (negate-bound hi) nil))
1567 (setf (numeric-type-high result)
1568 (if lo (negate-bound lo) nil))
1572 #!-propagate-float-type
1573 (defoptimizer (abs derive-type) ((num))
1574 (let ((type (continuation-type num)))
1575 (if (and (numeric-type-p type)
1576 (eq (numeric-type-class type) 'integer)
1577 (eq (numeric-type-complexp type) :real))
1578 (let ((lo (numeric-type-low type))
1579 (hi (numeric-type-high type)))
1580 (make-numeric-type :class 'integer :complexp :real
1581 :low (cond ((and hi (minusp hi))
1587 :high (if (and hi lo)
1588 (max (abs hi) (abs lo))
1590 (numeric-contagion type type))))
1592 #!+propagate-float-type
1593 (defun abs-derive-type-aux (type)
1594 (cond ((eq (numeric-type-complexp type) :complex)
1595 ;; The absolute value of a complex number is always a
1596 ;; non-negative float.
1597 (let* ((format (case (numeric-type-class type)
1598 ((integer rational) 'single-float)
1599 (t (numeric-type-format type))))
1600 (bound-format (or format 'float)))
1601 (make-numeric-type :class 'float
1604 :low (coerce 0 bound-format)
1607 ;; The absolute value of a real number is a non-negative real
1608 ;; of the same type.
1609 (let* ((abs-bnd (interval-abs (numeric-type->interval type)))
1610 (class (numeric-type-class type))
1611 (format (numeric-type-format type))
1612 (bound-type (or format class 'real)))
1617 :low (coerce-numeric-bound (interval-low abs-bnd) bound-type)
1618 :high (coerce-numeric-bound
1619 (interval-high abs-bnd) bound-type))))))
1621 #!+propagate-float-type
1622 (defoptimizer (abs derive-type) ((num))
1623 (one-arg-derive-type num #'abs-derive-type-aux #'abs))
1625 #!-propagate-float-type
1626 (defoptimizer (truncate derive-type) ((number divisor))
1627 (let ((number-type (continuation-type number))
1628 (divisor-type (continuation-type divisor))
1629 (integer-type (specifier-type 'integer)))
1630 (if (and (numeric-type-p number-type)
1631 (csubtypep number-type integer-type)
1632 (numeric-type-p divisor-type)
1633 (csubtypep divisor-type integer-type))
1634 (let ((number-low (numeric-type-low number-type))
1635 (number-high (numeric-type-high number-type))
1636 (divisor-low (numeric-type-low divisor-type))
1637 (divisor-high (numeric-type-high divisor-type)))
1638 (values-specifier-type
1639 `(values ,(integer-truncate-derive-type number-low number-high
1640 divisor-low divisor-high)
1641 ,(integer-rem-derive-type number-low number-high
1642 divisor-low divisor-high))))
1645 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
1647 #!+propagate-float-type
1650 (defun rem-result-type (number-type divisor-type)
1651 ;; Figure out what the remainder type is. The remainder is an
1652 ;; integer if both args are integers; a rational if both args are
1653 ;; rational; and a float otherwise.
1654 (cond ((and (csubtypep number-type (specifier-type 'integer))
1655 (csubtypep divisor-type (specifier-type 'integer)))
1657 ((and (csubtypep number-type (specifier-type 'rational))
1658 (csubtypep divisor-type (specifier-type 'rational)))
1660 ((and (csubtypep number-type (specifier-type 'float))
1661 (csubtypep divisor-type (specifier-type 'float)))
1662 ;; Both are floats so the result is also a float, of
1663 ;; the largest type.
1664 (or (float-format-max (numeric-type-format number-type)
1665 (numeric-type-format divisor-type))
1667 ((and (csubtypep number-type (specifier-type 'float))
1668 (csubtypep divisor-type (specifier-type 'rational)))
1669 ;; One of the arguments is a float and the other is a
1670 ;; rational. The remainder is a float of the same
1672 (or (numeric-type-format number-type) 'float))
1673 ((and (csubtypep divisor-type (specifier-type 'float))
1674 (csubtypep number-type (specifier-type 'rational)))
1675 ;; One of the arguments is a float and the other is a
1676 ;; rational. The remainder is a float of the same
1678 (or (numeric-type-format divisor-type) 'float))
1680 ;; Some unhandled combination. This usually means both args
1681 ;; are REAL so the result is a REAL.
1684 (defun truncate-derive-type-quot (number-type divisor-type)
1685 (let* ((rem-type (rem-result-type number-type divisor-type))
1686 (number-interval (numeric-type->interval number-type))
1687 (divisor-interval (numeric-type->interval divisor-type)))
1688 ;;(declare (type (member '(integer rational float)) rem-type))
1689 ;; We have real numbers now.
1690 (cond ((eq rem-type 'integer)
1691 ;; Since the remainder type is INTEGER, both args are
1693 (let* ((res (integer-truncate-derive-type
1694 (interval-low number-interval)
1695 (interval-high number-interval)
1696 (interval-low divisor-interval)
1697 (interval-high divisor-interval))))
1698 (specifier-type (if (listp res) res 'integer))))
1700 (let ((quot (truncate-quotient-bound
1701 (interval-div number-interval
1702 divisor-interval))))
1703 (specifier-type `(integer ,(or (interval-low quot) '*)
1704 ,(or (interval-high quot) '*))))))))
1706 (defun truncate-derive-type-rem (number-type divisor-type)
1707 (let* ((rem-type (rem-result-type number-type divisor-type))
1708 (number-interval (numeric-type->interval number-type))
1709 (divisor-interval (numeric-type->interval divisor-type))
1710 (rem (truncate-rem-bound number-interval divisor-interval)))
1711 ;;(declare (type (member '(integer rational float)) rem-type))
1712 ;; We have real numbers now.
1713 (cond ((eq rem-type 'integer)
1714 ;; Since the remainder type is INTEGER, both args are
1716 (specifier-type `(,rem-type ,(or (interval-low rem) '*)
1717 ,(or (interval-high rem) '*))))
1719 (multiple-value-bind (class format)
1722 (values 'integer nil))
1724 (values 'rational nil))
1725 ((or single-float double-float #!+long-float long-float)
1726 (values 'float rem-type))
1728 (values 'float nil))
1731 (when (member rem-type '(float single-float double-float
1732 #!+long-float long-float))
1733 (setf rem (interval-func #'(lambda (x)
1734 (coerce x rem-type))
1736 (make-numeric-type :class class
1738 :low (interval-low rem)
1739 :high (interval-high rem)))))))
1741 (defun truncate-derive-type-quot-aux (num div same-arg)
1742 (declare (ignore same-arg))
1743 (if (and (numeric-type-real-p num)
1744 (numeric-type-real-p div))
1745 (truncate-derive-type-quot num div)
1748 (defun truncate-derive-type-rem-aux (num div same-arg)
1749 (declare (ignore same-arg))
1750 (if (and (numeric-type-real-p num)
1751 (numeric-type-real-p div))
1752 (truncate-derive-type-rem num div)
1755 (defoptimizer (truncate derive-type) ((number divisor))
1756 (let ((quot (two-arg-derive-type number divisor
1757 #'truncate-derive-type-quot-aux #'truncate))
1758 (rem (two-arg-derive-type number divisor
1759 #'truncate-derive-type-rem-aux #'rem)))
1760 (when (and quot rem)
1761 (make-values-type :required (list quot rem)))))
1763 (defun ftruncate-derive-type-quot (number-type divisor-type)
1764 ;; The bounds are the same as for truncate. However, the first
1765 ;; result is a float of some type. We need to determine what that
1766 ;; type is. Basically it's the more contagious of the two types.
1767 (let ((q-type (truncate-derive-type-quot number-type divisor-type))
1768 (res-type (numeric-contagion number-type divisor-type)))
1769 (make-numeric-type :class 'float
1770 :format (numeric-type-format res-type)
1771 :low (numeric-type-low q-type)
1772 :high (numeric-type-high q-type))))
1774 (defun ftruncate-derive-type-quot-aux (n d same-arg)
1775 (declare (ignore same-arg))
1776 (if (and (numeric-type-real-p n)
1777 (numeric-type-real-p d))
1778 (ftruncate-derive-type-quot n d)
1781 (defoptimizer (ftruncate derive-type) ((number divisor))
1783 (two-arg-derive-type number divisor
1784 #'ftruncate-derive-type-quot-aux #'ftruncate))
1785 (rem (two-arg-derive-type number divisor
1786 #'truncate-derive-type-rem-aux #'rem)))
1787 (when (and quot rem)
1788 (make-values-type :required (list quot rem)))))
1790 (defun %unary-truncate-derive-type-aux (number)
1791 (truncate-derive-type-quot number (specifier-type '(integer 1 1))))
1793 (defoptimizer (%unary-truncate derive-type) ((number))
1794 (one-arg-derive-type number
1795 #'%unary-truncate-derive-type-aux
1798 ;;; Define optimizers for FLOOR and CEILING.
1800 ((frob-opt (name q-name r-name)
1801 (let ((q-aux (symbolicate q-name "-AUX"))
1802 (r-aux (symbolicate r-name "-AUX")))
1804 ;; Compute type of quotient (first) result
1805 (defun ,q-aux (number-type divisor-type)
1806 (let* ((number-interval
1807 (numeric-type->interval number-type))
1809 (numeric-type->interval divisor-type))
1810 (quot (,q-name (interval-div number-interval
1811 divisor-interval))))
1812 (specifier-type `(integer ,(or (interval-low quot) '*)
1813 ,(or (interval-high quot) '*)))))
1814 ;; Compute type of remainder
1815 (defun ,r-aux (number-type divisor-type)
1816 (let* ((divisor-interval
1817 (numeric-type->interval divisor-type))
1818 (rem (,r-name divisor-interval))
1819 (result-type (rem-result-type number-type divisor-type)))
1820 (multiple-value-bind (class format)
1823 (values 'integer nil))
1825 (values 'rational nil))
1826 ((or single-float double-float #!+long-float long-float)
1827 (values 'float result-type))
1829 (values 'float nil))
1832 (when (member result-type '(float single-float double-float
1833 #!+long-float long-float))
1834 ;; Make sure the limits on the interval have
1836 (setf rem (interval-func #'(lambda (x)
1837 (coerce x result-type))
1839 (make-numeric-type :class class
1841 :low (interval-low rem)
1842 :high (interval-high rem)))))
1843 ;; The optimizer itself
1844 (defoptimizer (,name derive-type) ((number divisor))
1845 (flet ((derive-q (n d same-arg)
1846 (declare (ignore same-arg))
1847 (if (and (numeric-type-real-p n)
1848 (numeric-type-real-p d))
1851 (derive-r (n d same-arg)
1852 (declare (ignore same-arg))
1853 (if (and (numeric-type-real-p n)
1854 (numeric-type-real-p d))
1857 (let ((quot (two-arg-derive-type
1858 number divisor #'derive-q #',name))
1859 (rem (two-arg-derive-type
1860 number divisor #'derive-r #'mod)))
1861 (when (and quot rem)
1862 (make-values-type :required (list quot rem))))))
1865 ;; FIXME: DEF-FROB-OPT, not just FROB-OPT
1866 (frob-opt floor floor-quotient-bound floor-rem-bound)
1867 (frob-opt ceiling ceiling-quotient-bound ceiling-rem-bound))
1869 ;;; Define optimizers for FFLOOR and FCEILING
1871 ((frob-opt (name q-name r-name)
1872 (let ((q-aux (symbolicate "F" q-name "-AUX"))
1873 (r-aux (symbolicate r-name "-AUX")))
1875 ;; Compute type of quotient (first) result
1876 (defun ,q-aux (number-type divisor-type)
1877 (let* ((number-interval
1878 (numeric-type->interval number-type))
1880 (numeric-type->interval divisor-type))
1881 (quot (,q-name (interval-div number-interval
1883 (res-type (numeric-contagion number-type divisor-type)))
1885 :class (numeric-type-class res-type)
1886 :format (numeric-type-format res-type)
1887 :low (interval-low quot)
1888 :high (interval-high quot))))
1890 (defoptimizer (,name derive-type) ((number divisor))
1891 (flet ((derive-q (n d same-arg)
1892 (declare (ignore same-arg))
1893 (if (and (numeric-type-real-p n)
1894 (numeric-type-real-p d))
1897 (derive-r (n d same-arg)
1898 (declare (ignore same-arg))
1899 (if (and (numeric-type-real-p n)
1900 (numeric-type-real-p d))
1903 (let ((quot (two-arg-derive-type
1904 number divisor #'derive-q #',name))
1905 (rem (two-arg-derive-type
1906 number divisor #'derive-r #'mod)))
1907 (when (and quot rem)
1908 (make-values-type :required (list quot rem))))))))))
1910 ;; FIXME: DEF-FROB-OPT, not just FROB-OPT
1911 (frob-opt ffloor floor-quotient-bound floor-rem-bound)
1912 (frob-opt fceiling ceiling-quotient-bound ceiling-rem-bound))
1914 ;;; Functions to compute the bounds on the quotient and remainder for
1915 ;;; the FLOOR function.
1916 (defun floor-quotient-bound (quot)
1917 ;; Take the floor of the quotient and then massage it into what we
1919 (let ((lo (interval-low quot))
1920 (hi (interval-high quot)))
1921 ;; Take the floor of the lower bound. The result is always a
1922 ;; closed lower bound.
1924 (floor (bound-value lo))
1926 ;; For the upper bound, we need to be careful
1929 ;; An open bound. We need to be careful here because
1930 ;; the floor of '(10.0) is 9, but the floor of
1932 (multiple-value-bind (q r) (floor (first hi))
1937 ;; A closed bound, so the answer is obvious.
1941 (make-interval :low lo :high hi)))
1942 (defun floor-rem-bound (div)
1943 ;; The remainder depends only on the divisor. Try to get the
1944 ;; correct sign for the remainder if we can.
1945 (case (interval-range-info div)
1947 ;; Divisor is always positive.
1948 (let ((rem (interval-abs div)))
1949 (setf (interval-low rem) 0)
1950 (when (and (numberp (interval-high rem))
1951 (not (zerop (interval-high rem))))
1952 ;; The remainder never contains the upper bound. However,
1953 ;; watch out for the case where the high limit is zero!
1954 (setf (interval-high rem) (list (interval-high rem))))
1957 ;; Divisor is always negative
1958 (let ((rem (interval-neg (interval-abs div))))
1959 (setf (interval-high rem) 0)
1960 (when (numberp (interval-low rem))
1961 ;; The remainder never contains the lower bound.
1962 (setf (interval-low rem) (list (interval-low rem))))
1965 ;; The divisor can be positive or negative. All bets off.
1966 ;; The magnitude of remainder is the maximum value of the
1968 (let ((limit (bound-value (interval-high (interval-abs div)))))
1969 ;; The bound never reaches the limit, so make the interval open
1970 (make-interval :low (if limit
1973 :high (list limit))))))
1975 (floor-quotient-bound (make-interval :low 0.3 :high 10.3))
1976 => #S(INTERVAL :LOW 0 :HIGH 10)
1977 (floor-quotient-bound (make-interval :low 0.3 :high '(10.3)))
1978 => #S(INTERVAL :LOW 0 :HIGH 10)
1979 (floor-quotient-bound (make-interval :low 0.3 :high 10))
1980 => #S(INTERVAL :LOW 0 :HIGH 10)
1981 (floor-quotient-bound (make-interval :low 0.3 :high '(10)))
1982 => #S(INTERVAL :LOW 0 :HIGH 9)
1983 (floor-quotient-bound (make-interval :low '(0.3) :high 10.3))
1984 => #S(INTERVAL :LOW 0 :HIGH 10)
1985 (floor-quotient-bound (make-interval :low '(0.0) :high 10.3))
1986 => #S(INTERVAL :LOW 0 :HIGH 10)
1987 (floor-quotient-bound (make-interval :low '(-1.3) :high 10.3))
1988 => #S(INTERVAL :LOW -2 :HIGH 10)
1989 (floor-quotient-bound (make-interval :low '(-1.0) :high 10.3))
1990 => #S(INTERVAL :LOW -1 :HIGH 10)
1991 (floor-quotient-bound (make-interval :low -1.0 :high 10.3))
1992 => #S(INTERVAL :LOW -1 :HIGH 10)
1994 (floor-rem-bound (make-interval :low 0.3 :high 10.3))
1995 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
1996 (floor-rem-bound (make-interval :low 0.3 :high '(10.3)))
1997 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
1998 (floor-rem-bound (make-interval :low -10 :high -2.3))
1999 #S(INTERVAL :LOW (-10) :HIGH 0)
2000 (floor-rem-bound (make-interval :low 0.3 :high 10))
2001 => #S(INTERVAL :LOW 0 :HIGH '(10))
2002 (floor-rem-bound (make-interval :low '(-1.3) :high 10.3))
2003 => #S(INTERVAL :LOW '(-10.3) :HIGH '(10.3))
2004 (floor-rem-bound (make-interval :low '(-20.3) :high 10.3))
2005 => #S(INTERVAL :LOW (-20.3) :HIGH (20.3))
2008 ;;; same functions for CEILING
2009 (defun ceiling-quotient-bound (quot)
2010 ;; Take the ceiling of the quotient and then massage it into what we
2012 (let ((lo (interval-low quot))
2013 (hi (interval-high quot)))
2014 ;; Take the ceiling of the upper bound. The result is always a
2015 ;; closed upper bound.
2017 (ceiling (bound-value hi))
2019 ;; For the lower bound, we need to be careful
2022 ;; An open bound. We need to be careful here because
2023 ;; the ceiling of '(10.0) is 11, but the ceiling of
2025 (multiple-value-bind (q r) (ceiling (first lo))
2030 ;; A closed bound, so the answer is obvious.
2034 (make-interval :low lo :high hi)))
2035 (defun ceiling-rem-bound (div)
2036 ;; The remainder depends only on the divisor. Try to get the
2037 ;; correct sign for the remainder if we can.
2039 (case (interval-range-info div)
2041 ;; Divisor is always positive. The remainder is negative.
2042 (let ((rem (interval-neg (interval-abs div))))
2043 (setf (interval-high rem) 0)
2044 (when (and (numberp (interval-low rem))
2045 (not (zerop (interval-low rem))))
2046 ;; The remainder never contains the upper bound. However,
2047 ;; watch out for the case when the upper bound is zero!
2048 (setf (interval-low rem) (list (interval-low rem))))
2051 ;; Divisor is always negative. The remainder is positive
2052 (let ((rem (interval-abs div)))
2053 (setf (interval-low rem) 0)
2054 (when (numberp (interval-high rem))
2055 ;; The remainder never contains the lower bound.
2056 (setf (interval-high rem) (list (interval-high rem))))
2059 ;; The divisor can be positive or negative. All bets off.
2060 ;; The magnitude of remainder is the maximum value of the
2062 (let ((limit (bound-value (interval-high (interval-abs div)))))
2063 ;; The bound never reaches the limit, so make the interval open
2064 (make-interval :low (if limit
2067 :high (list limit))))))
2070 (ceiling-quotient-bound (make-interval :low 0.3 :high 10.3))
2071 => #S(INTERVAL :LOW 1 :HIGH 11)
2072 (ceiling-quotient-bound (make-interval :low 0.3 :high '(10.3)))
2073 => #S(INTERVAL :LOW 1 :HIGH 11)
2074 (ceiling-quotient-bound (make-interval :low 0.3 :high 10))
2075 => #S(INTERVAL :LOW 1 :HIGH 10)
2076 (ceiling-quotient-bound (make-interval :low 0.3 :high '(10)))
2077 => #S(INTERVAL :LOW 1 :HIGH 10)
2078 (ceiling-quotient-bound (make-interval :low '(0.3) :high 10.3))
2079 => #S(INTERVAL :LOW 1 :HIGH 11)
2080 (ceiling-quotient-bound (make-interval :low '(0.0) :high 10.3))
2081 => #S(INTERVAL :LOW 1 :HIGH 11)
2082 (ceiling-quotient-bound (make-interval :low '(-1.3) :high 10.3))
2083 => #S(INTERVAL :LOW -1 :HIGH 11)
2084 (ceiling-quotient-bound (make-interval :low '(-1.0) :high 10.3))
2085 => #S(INTERVAL :LOW 0 :HIGH 11)
2086 (ceiling-quotient-bound (make-interval :low -1.0 :high 10.3))
2087 => #S(INTERVAL :LOW -1 :HIGH 11)
2089 (ceiling-rem-bound (make-interval :low 0.3 :high 10.3))
2090 => #S(INTERVAL :LOW (-10.3) :HIGH 0)
2091 (ceiling-rem-bound (make-interval :low 0.3 :high '(10.3)))
2092 => #S(INTERVAL :LOW 0 :HIGH '(10.3))
2093 (ceiling-rem-bound (make-interval :low -10 :high -2.3))
2094 => #S(INTERVAL :LOW 0 :HIGH (10))
2095 (ceiling-rem-bound (make-interval :low 0.3 :high 10))
2096 => #S(INTERVAL :LOW (-10) :HIGH 0)
2097 (ceiling-rem-bound (make-interval :low '(-1.3) :high 10.3))
2098 => #S(INTERVAL :LOW (-10.3) :HIGH (10.3))
2099 (ceiling-rem-bound (make-interval :low '(-20.3) :high 10.3))
2100 => #S(INTERVAL :LOW (-20.3) :HIGH (20.3))
2103 (defun truncate-quotient-bound (quot)
2104 ;; For positive quotients, truncate is exactly like floor. For
2105 ;; negative quotients, truncate is exactly like ceiling. Otherwise,
2106 ;; it's the union of the two pieces.
2107 (case (interval-range-info quot)
2110 (floor-quotient-bound quot))
2112 ;; Just like ceiling
2113 (ceiling-quotient-bound quot))
2115 ;; Split the interval into positive and negative pieces, compute
2116 ;; the result for each piece and put them back together.
2117 (destructuring-bind (neg pos) (interval-split 0 quot t t)
2118 (interval-merge-pair (ceiling-quotient-bound neg)
2119 (floor-quotient-bound pos))))))
2121 (defun truncate-rem-bound (num div)
2122 ;; This is significantly more complicated than floor or ceiling. We
2123 ;; need both the number and the divisor to determine the range. The
2124 ;; basic idea is to split the ranges of num and den into positive
2125 ;; and negative pieces and deal with each of the four possibilities
2127 (case (interval-range-info num)
2129 (case (interval-range-info div)
2131 (floor-rem-bound div))
2133 (ceiling-rem-bound div))
2135 (destructuring-bind (neg pos) (interval-split 0 div t t)
2136 (interval-merge-pair (truncate-rem-bound num neg)
2137 (truncate-rem-bound num pos))))))
2139 (case (interval-range-info div)
2141 (ceiling-rem-bound div))
2143 (floor-rem-bound div))
2145 (destructuring-bind (neg pos) (interval-split 0 div t t)
2146 (interval-merge-pair (truncate-rem-bound num neg)
2147 (truncate-rem-bound num pos))))))
2149 (destructuring-bind (neg pos) (interval-split 0 num t t)
2150 (interval-merge-pair (truncate-rem-bound neg div)
2151 (truncate-rem-bound pos div))))))
2154 ;;; Derive useful information about the range. Returns three values:
2155 ;;; - '+ if its positive, '- negative, or nil if it overlaps 0.
2156 ;;; - The abs of the minimal value (i.e. closest to 0) in the range.
2157 ;;; - The abs of the maximal value if there is one, or nil if it is
2159 (defun numeric-range-info (low high)
2160 (cond ((and low (not (minusp low)))
2161 (values '+ low high))
2162 ((and high (not (plusp high)))
2163 (values '- (- high) (if low (- low) nil)))
2165 (values nil 0 (and low high (max (- low) high))))))
2167 (defun integer-truncate-derive-type
2168 (number-low number-high divisor-low divisor-high)
2169 ;; The result cannot be larger in magnitude than the number, but the sign
2170 ;; might change. If we can determine the sign of either the number or
2171 ;; the divisor, we can eliminate some of the cases.
2172 (multiple-value-bind (number-sign number-min number-max)
2173 (numeric-range-info number-low number-high)
2174 (multiple-value-bind (divisor-sign divisor-min divisor-max)
2175 (numeric-range-info divisor-low divisor-high)
2176 (when (and divisor-max (zerop divisor-max))
2177 ;; We've got a problem: guaranteed division by zero.
2178 (return-from integer-truncate-derive-type t))
2179 (when (zerop divisor-min)
2180 ;; We'll assume that they aren't going to divide by zero.
2182 (cond ((and number-sign divisor-sign)
2183 ;; We know the sign of both.
2184 (if (eq number-sign divisor-sign)
2185 ;; Same sign, so the result will be positive.
2186 `(integer ,(if divisor-max
2187 (truncate number-min divisor-max)
2190 (truncate number-max divisor-min)
2192 ;; Different signs, the result will be negative.
2193 `(integer ,(if number-max
2194 (- (truncate number-max divisor-min))
2197 (- (truncate number-min divisor-max))
2199 ((eq divisor-sign '+)
2200 ;; The divisor is positive. Therefore, the number will just
2201 ;; become closer to zero.
2202 `(integer ,(if number-low
2203 (truncate number-low divisor-min)
2206 (truncate number-high divisor-min)
2208 ((eq divisor-sign '-)
2209 ;; The divisor is negative. Therefore, the absolute value of
2210 ;; the number will become closer to zero, but the sign will also
2212 `(integer ,(if number-high
2213 (- (truncate number-high divisor-min))
2216 (- (truncate number-low divisor-min))
2218 ;; The divisor could be either positive or negative.
2220 ;; The number we are dividing has a bound. Divide that by the
2221 ;; smallest posible divisor.
2222 (let ((bound (truncate number-max divisor-min)))
2223 `(integer ,(- bound) ,bound)))
2225 ;; The number we are dividing is unbounded, so we can't tell
2226 ;; anything about the result.
2229 #!-propagate-float-type
2230 (defun integer-rem-derive-type
2231 (number-low number-high divisor-low divisor-high)
2232 (if (and divisor-low divisor-high)
2233 ;; We know the range of the divisor, and the remainder must be smaller
2234 ;; than the divisor. We can tell the sign of the remainer if we know
2235 ;; the sign of the number.
2236 (let ((divisor-max (1- (max (abs divisor-low) (abs divisor-high)))))
2237 `(integer ,(if (or (null number-low)
2238 (minusp number-low))
2241 ,(if (or (null number-high)
2242 (plusp number-high))
2245 ;; The divisor is potentially either very positive or very negative.
2246 ;; Therefore, the remainer is unbounded, but we might be able to tell
2247 ;; something about the sign from the number.
2248 `(integer ,(if (and number-low (not (minusp number-low)))
2249 ;; The number we are dividing is positive. Therefore,
2250 ;; the remainder must be positive.
2253 ,(if (and number-high (not (plusp number-high)))
2254 ;; The number we are dividing is negative. Therefore,
2255 ;; the remainder must be negative.
2259 #!-propagate-float-type
2260 (defoptimizer (random derive-type) ((bound &optional state))
2261 (let ((type (continuation-type bound)))
2262 (when (numeric-type-p type)
2263 (let ((class (numeric-type-class type))
2264 (high (numeric-type-high type))
2265 (format (numeric-type-format type)))
2269 :low (coerce 0 (or format class 'real))
2270 :high (cond ((not high) nil)
2271 ((eq class 'integer) (max (1- high) 0))
2272 ((or (consp high) (zerop high)) high)
2275 #!+propagate-float-type
2276 (defun random-derive-type-aux (type)
2277 (let ((class (numeric-type-class type))
2278 (high (numeric-type-high type))
2279 (format (numeric-type-format type)))
2283 :low (coerce 0 (or format class 'real))
2284 :high (cond ((not high) nil)
2285 ((eq class 'integer) (max (1- high) 0))
2286 ((or (consp high) (zerop high)) high)
2289 #!+propagate-float-type
2290 (defoptimizer (random derive-type) ((bound &optional state))
2291 (one-arg-derive-type bound #'random-derive-type-aux nil))
2293 ;;;; logical derive-type methods
2295 ;;; Return the maximum number of bits an integer of the supplied type can take
2296 ;;; up, or NIL if it is unbounded. The second (third) value is T if the
2297 ;;; integer can be positive (negative) and NIL if not. Zero counts as
2299 (defun integer-type-length (type)
2300 (if (numeric-type-p type)
2301 (let ((min (numeric-type-low type))
2302 (max (numeric-type-high type)))
2303 (values (and min max (max (integer-length min) (integer-length max)))
2304 (or (null max) (not (minusp max)))
2305 (or (null min) (minusp min))))
2308 #!-propagate-fun-type
2310 (defoptimizer (logand derive-type) ((x y))
2311 (multiple-value-bind (x-len x-pos x-neg)
2312 (integer-type-length (continuation-type x))
2313 (declare (ignore x-pos))
2314 (multiple-value-bind (y-len y-pos y-neg)
2315 (integer-type-length (continuation-type y))
2316 (declare (ignore y-pos))
2318 ;; X must be positive.
2320 ;; The must both be positive.
2321 (cond ((or (null x-len) (null y-len))
2322 (specifier-type 'unsigned-byte))
2323 ((or (zerop x-len) (zerop y-len))
2324 (specifier-type '(integer 0 0)))
2326 (specifier-type `(unsigned-byte ,(min x-len y-len)))))
2327 ;; X is positive, but Y might be negative.
2329 (specifier-type 'unsigned-byte))
2331 (specifier-type '(integer 0 0)))
2333 (specifier-type `(unsigned-byte ,x-len)))))
2334 ;; X might be negative.
2336 ;; Y must be positive.
2338 (specifier-type 'unsigned-byte))
2340 (specifier-type '(integer 0 0)))
2343 `(unsigned-byte ,y-len))))
2344 ;; Either might be negative.
2345 (if (and x-len y-len)
2346 ;; The result is bounded.
2347 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2348 ;; We can't tell squat about the result.
2349 (specifier-type 'integer)))))))
2351 (defoptimizer (logior derive-type) ((x y))
2352 (multiple-value-bind (x-len x-pos x-neg)
2353 (integer-type-length (continuation-type x))
2354 (multiple-value-bind (y-len y-pos y-neg)
2355 (integer-type-length (continuation-type y))
2357 ((and (not x-neg) (not y-neg))
2358 ;; Both are positive.
2359 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2363 ;; X must be negative.
2365 ;; Both are negative. The result is going to be negative and be
2366 ;; the same length or shorter than the smaller.
2367 (if (and x-len y-len)
2369 (specifier-type `(integer ,(ash -1 (min x-len y-len)) -1))
2371 (specifier-type '(integer * -1)))
2372 ;; X is negative, but we don't know about Y. The result will be
2373 ;; negative, but no more negative than X.
2375 `(integer ,(or (numeric-type-low (continuation-type x)) '*)
2378 ;; X might be either positive or negative.
2380 ;; But Y is negative. The result will be negative.
2382 `(integer ,(or (numeric-type-low (continuation-type y)) '*)
2384 ;; We don't know squat about either. It won't get any bigger.
2385 (if (and x-len y-len)
2387 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2389 (specifier-type 'integer))))))))
2391 (defoptimizer (logxor derive-type) ((x y))
2392 (multiple-value-bind (x-len x-pos x-neg)
2393 (integer-type-length (continuation-type x))
2394 (multiple-value-bind (y-len y-pos y-neg)
2395 (integer-type-length (continuation-type y))
2397 ((or (and (not x-neg) (not y-neg))
2398 (and (not x-pos) (not y-pos)))
2399 ;; Either both are negative or both are positive. The result will be
2400 ;; positive, and as long as the longer.
2401 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2404 ((or (and (not x-pos) (not y-neg))
2405 (and (not y-neg) (not y-pos)))
2406 ;; Either X is negative and Y is positive of vice-verca. The result
2407 ;; will be negative.
2408 (specifier-type `(integer ,(if (and x-len y-len)
2409 (ash -1 (max x-len y-len))
2412 ;; We can't tell what the sign of the result is going to be. All we
2413 ;; know is that we don't create new bits.
2415 (specifier-type `(signed-byte ,(1+ (max x-len y-len)))))
2417 (specifier-type 'integer))))))
2421 #!+propagate-fun-type
2423 (defun logand-derive-type-aux (x y &optional same-leaf)
2424 (declare (ignore same-leaf))
2425 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2426 (declare (ignore x-pos))
2427 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2428 (declare (ignore y-pos))
2430 ;; X must be positive.
2432 ;; The must both be positive.
2433 (cond ((or (null x-len) (null y-len))
2434 (specifier-type 'unsigned-byte))
2435 ((or (zerop x-len) (zerop y-len))
2436 (specifier-type '(integer 0 0)))
2438 (specifier-type `(unsigned-byte ,(min x-len y-len)))))
2439 ;; X is positive, but Y might be negative.
2441 (specifier-type 'unsigned-byte))
2443 (specifier-type '(integer 0 0)))
2445 (specifier-type `(unsigned-byte ,x-len)))))
2446 ;; X might be negative.
2448 ;; Y must be positive.
2450 (specifier-type 'unsigned-byte))
2452 (specifier-type '(integer 0 0)))
2455 `(unsigned-byte ,y-len))))
2456 ;; Either might be negative.
2457 (if (and x-len y-len)
2458 ;; The result is bounded.
2459 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2460 ;; We can't tell squat about the result.
2461 (specifier-type 'integer)))))))
2463 (defun logior-derive-type-aux (x y &optional same-leaf)
2464 (declare (ignore same-leaf))
2465 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2466 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2468 ((and (not x-neg) (not y-neg))
2469 ;; Both are positive.
2470 (if (and x-len y-len (zerop x-len) (zerop y-len))
2471 (specifier-type '(integer 0 0))
2472 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2476 ;; X must be negative.
2478 ;; Both are negative. The result is going to be negative and be
2479 ;; the same length or shorter than the smaller.
2480 (if (and x-len y-len)
2482 (specifier-type `(integer ,(ash -1 (min x-len y-len)) -1))
2484 (specifier-type '(integer * -1)))
2485 ;; X is negative, but we don't know about Y. The result will be
2486 ;; negative, but no more negative than X.
2488 `(integer ,(or (numeric-type-low x) '*)
2491 ;; X might be either positive or negative.
2493 ;; But Y is negative. The result will be negative.
2495 `(integer ,(or (numeric-type-low y) '*)
2497 ;; We don't know squat about either. It won't get any bigger.
2498 (if (and x-len y-len)
2500 (specifier-type `(signed-byte ,(1+ (max x-len y-len))))
2502 (specifier-type 'integer))))))))
2504 (defun logxor-derive-type-aux (x y &optional same-leaf)
2505 (declare (ignore same-leaf))
2506 (multiple-value-bind (x-len x-pos x-neg) (integer-type-length x)
2507 (multiple-value-bind (y-len y-pos y-neg) (integer-type-length y)
2509 ((or (and (not x-neg) (not y-neg))
2510 (and (not x-pos) (not y-pos)))
2511 ;; Either both are negative or both are positive. The result will be
2512 ;; positive, and as long as the longer.
2513 (if (and x-len y-len (zerop x-len) (zerop y-len))
2514 (specifier-type '(integer 0 0))
2515 (specifier-type `(unsigned-byte ,(if (and x-len y-len)
2518 ((or (and (not x-pos) (not y-neg))
2519 (and (not y-neg) (not y-pos)))
2520 ;; Either X is negative and Y is positive of vice-verca. The result
2521 ;; will be negative.
2522 (specifier-type `(integer ,(if (and x-len y-len)
2523 (ash -1 (max x-len y-len))
2526 ;; We can't tell what the sign of the result is going to be. All we
2527 ;; know is that we don't create new bits.
2529 (specifier-type `(signed-byte ,(1+ (max x-len y-len)))))
2531 (specifier-type 'integer))))))
2533 (macrolet ((frob (logfcn)
2534 (let ((fcn-aux (symbolicate logfcn "-DERIVE-TYPE-AUX")))
2535 `(defoptimizer (,logfcn derive-type) ((x y))
2536 (two-arg-derive-type x y #',fcn-aux #',logfcn)))))
2537 ;; FIXME: DEF-FROB, not just FROB
2542 (defoptimizer (integer-length derive-type) ((x))
2543 (let ((x-type (continuation-type x)))
2544 (when (and (numeric-type-p x-type)
2545 (csubtypep x-type (specifier-type 'integer)))
2546 ;; If the X is of type (INTEGER LO HI), then the integer-length
2547 ;; of X is (INTEGER (min lo hi) (max lo hi), basically. Be
2548 ;; careful about LO or HI being NIL, though. Also, if 0 is
2549 ;; contained in X, the lower bound is obviously 0.
2550 (flet ((null-or-min (a b)
2551 (and a b (min (integer-length a)
2552 (integer-length b))))
2554 (and a b (max (integer-length a)
2555 (integer-length b)))))
2556 (let* ((min (numeric-type-low x-type))
2557 (max (numeric-type-high x-type))
2558 (min-len (null-or-min min max))
2559 (max-len (null-or-max min max)))
2560 (when (ctypep 0 x-type)
2562 (specifier-type `(integer ,(or min-len '*) ,(or max-len '*))))))))
2565 ;;;; miscellaneous derive-type methods
2567 (defoptimizer (code-char derive-type) ((code))
2568 (specifier-type 'base-char))
2570 (defoptimizer (values derive-type) ((&rest values))
2571 (values-specifier-type
2572 `(values ,@(mapcar #'(lambda (x)
2573 (type-specifier (continuation-type x)))
2576 ;;;; byte operations
2578 ;;;; We try to turn byte operations into simple logical operations. First, we
2579 ;;;; convert byte specifiers into separate size and position arguments passed
2580 ;;;; to internal %FOO functions. We then attempt to transform the %FOO
2581 ;;;; functions into boolean operations when the size and position are constant
2582 ;;;; and the operands are fixnums.
2584 (macrolet (;; Evaluate body with SIZE-VAR and POS-VAR bound to expressions that
2585 ;; evaluate to the SIZE and POSITION of the byte-specifier form
2586 ;; SPEC. We may wrap a let around the result of the body to bind
2589 ;; If the spec is a BYTE form, then bind the vars to the subforms.
2590 ;; otherwise, evaluate SPEC and use the BYTE-SIZE and BYTE-POSITION.
2591 ;; The goal of this transformation is to avoid consing up byte
2592 ;; specifiers and then immediately throwing them away.
2593 (with-byte-specifier ((size-var pos-var spec) &body body)
2594 (once-only ((spec `(macroexpand ,spec))
2596 `(if (and (consp ,spec)
2597 (eq (car ,spec) 'byte)
2598 (= (length ,spec) 3))
2599 (let ((,size-var (second ,spec))
2600 (,pos-var (third ,spec)))
2602 (let ((,size-var `(byte-size ,,temp))
2603 (,pos-var `(byte-position ,,temp)))
2604 `(let ((,,temp ,,spec))
2607 (def-source-transform ldb (spec int)
2608 (with-byte-specifier (size pos spec)
2609 `(%ldb ,size ,pos ,int)))
2611 (def-source-transform dpb (newbyte spec int)
2612 (with-byte-specifier (size pos spec)
2613 `(%dpb ,newbyte ,size ,pos ,int)))
2615 (def-source-transform mask-field (spec int)
2616 (with-byte-specifier (size pos spec)
2617 `(%mask-field ,size ,pos ,int)))
2619 (def-source-transform deposit-field (newbyte spec int)
2620 (with-byte-specifier (size pos spec)
2621 `(%deposit-field ,newbyte ,size ,pos ,int))))
2623 (defoptimizer (%ldb derive-type) ((size posn num))
2624 (let ((size (continuation-type size)))
2625 (if (and (numeric-type-p size)
2626 (csubtypep size (specifier-type 'integer)))
2627 (let ((size-high (numeric-type-high size)))
2628 (if (and size-high (<= size-high sb!vm:word-bits))
2629 (specifier-type `(unsigned-byte ,size-high))
2630 (specifier-type 'unsigned-byte)))
2633 (defoptimizer (%mask-field derive-type) ((size posn num))
2634 (let ((size (continuation-type size))
2635 (posn (continuation-type posn)))
2636 (if (and (numeric-type-p size)
2637 (csubtypep size (specifier-type 'integer))
2638 (numeric-type-p posn)
2639 (csubtypep posn (specifier-type 'integer)))
2640 (let ((size-high (numeric-type-high size))
2641 (posn-high (numeric-type-high posn)))
2642 (if (and size-high posn-high
2643 (<= (+ size-high posn-high) sb!vm:word-bits))
2644 (specifier-type `(unsigned-byte ,(+ size-high posn-high)))
2645 (specifier-type 'unsigned-byte)))
2648 (defoptimizer (%dpb derive-type) ((newbyte size posn int))
2649 (let ((size (continuation-type size))
2650 (posn (continuation-type posn))
2651 (int (continuation-type int)))
2652 (if (and (numeric-type-p size)
2653 (csubtypep size (specifier-type 'integer))
2654 (numeric-type-p posn)
2655 (csubtypep posn (specifier-type 'integer))
2656 (numeric-type-p int)
2657 (csubtypep int (specifier-type 'integer)))
2658 (let ((size-high (numeric-type-high size))
2659 (posn-high (numeric-type-high posn))
2660 (high (numeric-type-high int))
2661 (low (numeric-type-low int)))
2662 (if (and size-high posn-high high low
2663 (<= (+ size-high posn-high) sb!vm:word-bits))
2665 (list (if (minusp low) 'signed-byte 'unsigned-byte)
2666 (max (integer-length high)
2667 (integer-length low)
2668 (+ size-high posn-high))))
2672 (defoptimizer (%deposit-field derive-type) ((newbyte size posn int))
2673 (let ((size (continuation-type size))
2674 (posn (continuation-type posn))
2675 (int (continuation-type int)))
2676 (if (and (numeric-type-p size)
2677 (csubtypep size (specifier-type 'integer))
2678 (numeric-type-p posn)
2679 (csubtypep posn (specifier-type 'integer))
2680 (numeric-type-p int)
2681 (csubtypep int (specifier-type 'integer)))
2682 (let ((size-high (numeric-type-high size))
2683 (posn-high (numeric-type-high posn))
2684 (high (numeric-type-high int))
2685 (low (numeric-type-low int)))
2686 (if (and size-high posn-high high low
2687 (<= (+ size-high posn-high) sb!vm:word-bits))
2689 (list (if (minusp low) 'signed-byte 'unsigned-byte)
2690 (max (integer-length high)
2691 (integer-length low)
2692 (+ size-high posn-high))))
2696 (deftransform %ldb ((size posn int)
2697 (fixnum fixnum integer)
2698 (unsigned-byte #.sb!vm:word-bits))
2699 "convert to inline logical operations"
2700 `(logand (ash int (- posn))
2701 (ash ,(1- (ash 1 sb!vm:word-bits))
2702 (- size ,sb!vm:word-bits))))
2704 (deftransform %mask-field ((size posn int)
2705 (fixnum fixnum integer)
2706 (unsigned-byte #.sb!vm:word-bits))
2707 "convert to inline logical operations"
2709 (ash (ash ,(1- (ash 1 sb!vm:word-bits))
2710 (- size ,sb!vm:word-bits))
2713 ;;; Note: for %DPB and %DEPOSIT-FIELD, we can't use
2714 ;;; (OR (SIGNED-BYTE N) (UNSIGNED-BYTE N))
2715 ;;; as the result type, as that would allow result types
2716 ;;; that cover the range -2^(n-1) .. 1-2^n, instead of allowing result types
2717 ;;; of (UNSIGNED-BYTE N) and result types of (SIGNED-BYTE N).
2719 (deftransform %dpb ((new size posn int)
2721 (unsigned-byte #.sb!vm:word-bits))
2722 "convert to inline logical operations"
2723 `(let ((mask (ldb (byte size 0) -1)))
2724 (logior (ash (logand new mask) posn)
2725 (logand int (lognot (ash mask posn))))))
2727 (deftransform %dpb ((new size posn int)
2729 (signed-byte #.sb!vm:word-bits))
2730 "convert to inline logical operations"
2731 `(let ((mask (ldb (byte size 0) -1)))
2732 (logior (ash (logand new mask) posn)
2733 (logand int (lognot (ash mask posn))))))
2735 (deftransform %deposit-field ((new size posn int)
2737 (unsigned-byte #.sb!vm:word-bits))
2738 "convert to inline logical operations"
2739 `(let ((mask (ash (ldb (byte size 0) -1) posn)))
2740 (logior (logand new mask)
2741 (logand int (lognot mask)))))
2743 (deftransform %deposit-field ((new size posn int)
2745 (signed-byte #.sb!vm:word-bits))
2746 "convert to inline logical operations"
2747 `(let ((mask (ash (ldb (byte size 0) -1) posn)))
2748 (logior (logand new mask)
2749 (logand int (lognot mask)))))
2751 ;;; miscellanous numeric transforms
2753 ;;; If a constant appears as the first arg, swap the args.
2754 (deftransform commutative-arg-swap ((x y) * * :defun-only t :node node)
2755 (if (and (constant-continuation-p x)
2756 (not (constant-continuation-p y)))
2757 `(,(continuation-function-name (basic-combination-fun node))
2759 ,(continuation-value x))
2760 (give-up-ir1-transform)))
2762 (dolist (x '(= char= + * logior logand logxor))
2763 (%deftransform x '(function * *) #'commutative-arg-swap
2764 "place constant arg last."))
2766 ;;; Handle the case of a constant BOOLE-CODE.
2767 (deftransform boole ((op x y) * * :when :both)
2768 "convert to inline logical operations"
2769 (unless (constant-continuation-p op)
2770 (give-up-ir1-transform "BOOLE code is not a constant."))
2771 (let ((control (continuation-value op)))
2777 (#.boole-c1 '(lognot x))
2778 (#.boole-c2 '(lognot y))
2779 (#.boole-and '(logand x y))
2780 (#.boole-ior '(logior x y))
2781 (#.boole-xor '(logxor x y))
2782 (#.boole-eqv '(logeqv x y))
2783 (#.boole-nand '(lognand x y))
2784 (#.boole-nor '(lognor x y))
2785 (#.boole-andc1 '(logandc1 x y))
2786 (#.boole-andc2 '(logandc2 x y))
2787 (#.boole-orc1 '(logorc1 x y))
2788 (#.boole-orc2 '(logorc2 x y))
2790 (abort-ir1-transform "~S is an illegal control arg to BOOLE."
2793 ;;;; converting special case multiply/divide to shifts
2795 ;;; If arg is a constant power of two, turn * into a shift.
2796 (deftransform * ((x y) (integer integer) * :when :both)
2797 "convert x*2^k to shift"
2798 (unless (constant-continuation-p y)
2799 (give-up-ir1-transform))
2800 (let* ((y (continuation-value y))
2802 (len (1- (integer-length y-abs))))
2803 (unless (= y-abs (ash 1 len))
2804 (give-up-ir1-transform))
2809 ;;; If both arguments and the result are (unsigned-byte 32), try to come up
2810 ;;; with a ``better'' multiplication using multiplier recoding. There are two
2811 ;;; different ways the multiplier can be recoded. The more obvious is to shift
2812 ;;; X by the correct amount for each bit set in Y and to sum the results. But
2813 ;;; if there is a string of bits that are all set, you can add X shifted by
2814 ;;; one more then the bit position of the first set bit and subtract X shifted
2815 ;;; by the bit position of the last set bit. We can't use this second method
2816 ;;; when the high order bit is bit 31 because shifting by 32 doesn't work
2818 (deftransform * ((x y)
2819 ((unsigned-byte 32) (unsigned-byte 32))
2821 "recode as shift and add"
2822 (unless (constant-continuation-p y)
2823 (give-up-ir1-transform))
2824 (let ((y (continuation-value y))
2827 (labels ((tub32 (x) `(truly-the (unsigned-byte 32) ,x))
2832 `(+ ,result ,(tub32 next-factor))
2834 (declare (inline add))
2835 (dotimes (bitpos 32)
2837 (when (not (logbitp bitpos y))
2838 (add (if (= (1+ first-one) bitpos)
2839 ;; There is only a single bit in the string.
2841 ;; There are at least two.
2842 `(- ,(tub32 `(ash x ,bitpos))
2843 ,(tub32 `(ash x ,first-one)))))
2844 (setf first-one nil))
2845 (when (logbitp bitpos y)
2846 (setf first-one bitpos))))
2848 (cond ((= first-one 31))
2852 (add `(- ,(tub32 '(ash x 31)) ,(tub32 `(ash x ,first-one))))))
2856 ;;; If arg is a constant power of two, turn FLOOR into a shift and mask.
2857 ;;; If CEILING, add in (1- (ABS Y)) and then do FLOOR.
2858 (flet ((frob (y ceil-p)
2859 (unless (constant-continuation-p y)
2860 (give-up-ir1-transform))
2861 (let* ((y (continuation-value y))
2863 (len (1- (integer-length y-abs))))
2864 (unless (= y-abs (ash 1 len))
2865 (give-up-ir1-transform))
2866 (let ((shift (- len))
2868 `(let ,(when ceil-p `((x (+ x ,(1- y-abs)))))
2870 `(values (ash (- x) ,shift)
2871 (- (logand (- x) ,mask)))
2872 `(values (ash x ,shift)
2873 (logand x ,mask))))))))
2874 (deftransform floor ((x y) (integer integer) *)
2875 "convert division by 2^k to shift"
2877 (deftransform ceiling ((x y) (integer integer) *)
2878 "convert division by 2^k to shift"
2881 ;;; Do the same for MOD.
2882 (deftransform mod ((x y) (integer integer) * :when :both)
2883 "convert remainder mod 2^k to LOGAND"
2884 (unless (constant-continuation-p y)
2885 (give-up-ir1-transform))
2886 (let* ((y (continuation-value y))
2888 (len (1- (integer-length y-abs))))
2889 (unless (= y-abs (ash 1 len))
2890 (give-up-ir1-transform))
2891 (let ((mask (1- y-abs)))
2893 `(- (logand (- x) ,mask))
2894 `(logand x ,mask)))))
2896 ;;; If arg is a constant power of two, turn TRUNCATE into a shift and mask.
2897 (deftransform truncate ((x y) (integer integer))
2898 "convert division by 2^k to shift"
2899 (unless (constant-continuation-p y)
2900 (give-up-ir1-transform))
2901 (let* ((y (continuation-value y))
2903 (len (1- (integer-length y-abs))))
2904 (unless (= y-abs (ash 1 len))
2905 (give-up-ir1-transform))
2906 (let* ((shift (- len))
2909 (values ,(if (minusp y)
2911 `(- (ash (- x) ,shift)))
2912 (- (logand (- x) ,mask)))
2913 (values ,(if (minusp y)
2914 `(- (ash (- x) ,shift))
2916 (logand x ,mask))))))
2918 ;;; And the same for REM.
2919 (deftransform rem ((x y) (integer integer) * :when :both)
2920 "convert remainder mod 2^k to LOGAND"
2921 (unless (constant-continuation-p y)
2922 (give-up-ir1-transform))
2923 (let* ((y (continuation-value y))
2925 (len (1- (integer-length y-abs))))
2926 (unless (= y-abs (ash 1 len))
2927 (give-up-ir1-transform))
2928 (let ((mask (1- y-abs)))
2930 (- (logand (- x) ,mask))
2931 (logand x ,mask)))))
2933 ;;;; arithmetic and logical identity operation elimination
2935 ;;;; Flush calls to various arith functions that convert to the identity
2936 ;;;; function or a constant.
2938 (dolist (stuff '((ash 0 x)
2943 (logxor -1 (lognot x))
2945 (destructuring-bind (name identity result) stuff
2946 (deftransform name ((x y) `(* (constant-argument (member ,identity))) '*
2947 :eval-name t :when :both)
2948 "fold identity operations"
2951 ;;; These are restricted to rationals, because (- 0 0.0) is 0.0, not -0.0, and
2952 ;;; (* 0 -4.0) is -0.0.
2953 (deftransform - ((x y) ((constant-argument (member 0)) rational) *
2955 "convert (- 0 x) to negate"
2957 (deftransform * ((x y) (rational (constant-argument (member 0))) *
2959 "convert (* x 0) to 0."
2962 ;;; Return T if in an arithmetic op including continuations X and Y, the
2963 ;;; result type is not affected by the type of X. That is, Y is at least as
2964 ;;; contagious as X.
2966 (defun not-more-contagious (x y)
2967 (declare (type continuation x y))
2968 (let ((x (continuation-type x))
2969 (y (continuation-type y)))
2970 (values (type= (numeric-contagion x y)
2971 (numeric-contagion y y)))))
2972 ;;; Patched version by Raymond Toy. dtc: Should be safer although it
2973 ;;; needs more work as valid transforms are missed; some cases are
2974 ;;; specific to particular transform functions so the use of this
2975 ;;; function may need a re-think.
2976 (defun not-more-contagious (x y)
2977 (declare (type continuation x y))
2978 (flet ((simple-numeric-type (num)
2979 (and (numeric-type-p num)
2980 ;; Return non-NIL if NUM is integer, rational, or a float
2981 ;; of some type (but not FLOAT)
2982 (case (numeric-type-class num)
2986 (numeric-type-format num))
2989 (let ((x (continuation-type x))
2990 (y (continuation-type y)))
2991 (if (and (simple-numeric-type x)
2992 (simple-numeric-type y))
2993 (values (type= (numeric-contagion x y)
2994 (numeric-contagion y y)))))))
2998 ;;; If y is not constant, not zerop, or is contagious, or a
2999 ;;; positive float +0.0 then give up.
3000 (deftransform + ((x y) (t (constant-argument t)) * :when :both)
3002 (let ((val (continuation-value y)))
3003 (unless (and (zerop val)
3004 (not (and (floatp val) (plusp (float-sign val))))
3005 (not-more-contagious y x))
3006 (give-up-ir1-transform)))
3011 ;;; If y is not constant, not zerop, or is contagious, or a
3012 ;;; negative float -0.0 then give up.
3013 (deftransform - ((x y) (t (constant-argument t)) * :when :both)
3015 (let ((val (continuation-value y)))
3016 (unless (and (zerop val)
3017 (not (and (floatp val) (minusp (float-sign val))))
3018 (not-more-contagious y x))
3019 (give-up-ir1-transform)))
3022 ;;; Fold (OP x +/-1)
3023 (dolist (stuff '((* x (%negate x))
3026 (destructuring-bind (name result minus-result) stuff
3027 (deftransform name ((x y) '(t (constant-argument real)) '* :eval-name t
3029 "fold identity operations"
3030 (let ((val (continuation-value y)))
3031 (unless (and (= (abs val) 1)
3032 (not-more-contagious y x))
3033 (give-up-ir1-transform))
3034 (if (minusp val) minus-result result)))))
3036 ;;; Fold (expt x n) into multiplications for small integral values of
3037 ;;; N; convert (expt x 1/2) to sqrt.
3038 (deftransform expt ((x y) (t (constant-argument real)) *)
3039 "recode as multiplication or sqrt"
3040 (let ((val (continuation-value y)))
3041 ;; If Y would cause the result to be promoted to the same type as
3042 ;; Y, we give up. If not, then the result will be the same type
3043 ;; as X, so we can replace the exponentiation with simple
3044 ;; multiplication and division for small integral powers.
3045 (unless (not-more-contagious y x)
3046 (give-up-ir1-transform))
3047 (cond ((zerop val) '(float 1 x))
3048 ((= val 2) '(* x x))
3049 ((= val -2) '(/ (* x x)))
3050 ((= val 3) '(* x x x))
3051 ((= val -3) '(/ (* x x x)))
3052 ((= val 1/2) '(sqrt x))
3053 ((= val -1/2) '(/ (sqrt x)))
3054 (t (give-up-ir1-transform)))))
3056 ;;; KLUDGE: Shouldn't (/ 0.0 0.0), etc. cause exceptions in these
3057 ;;; transformations?
3058 ;;; Perhaps we should have to prove that the denominator is nonzero before
3059 ;;; doing them? (Also the DOLIST over macro calls is weird. Perhaps
3060 ;;; just FROB?) -- WHN 19990917
3062 ;;; FIXME: What gives with the single quotes in the argument lists
3063 ;;; for DEFTRANSFORMs here? Does that work? Is it needed? Why?
3064 (dolist (name '(ash /))
3065 (deftransform name ((x y) '((constant-argument (integer 0 0)) integer) '*
3066 :eval-name t :when :both)
3069 (dolist (name '(truncate round floor ceiling))
3070 (deftransform name ((x y) '((constant-argument (integer 0 0)) integer) '*
3071 :eval-name t :when :both)
3075 ;;;; character operations
3077 (deftransform char-equal ((a b) (base-char base-char))
3079 '(let* ((ac (char-code a))
3081 (sum (logxor ac bc)))
3083 (when (eql sum #x20)
3084 (let ((sum (+ ac bc)))
3085 (and (> sum 161) (< sum 213)))))))
3087 (deftransform char-upcase ((x) (base-char))
3089 '(let ((n-code (char-code x)))
3090 (if (and (> n-code #o140) ; Octal 141 is #\a.
3091 (< n-code #o173)) ; Octal 172 is #\z.
3092 (code-char (logxor #x20 n-code))
3095 (deftransform char-downcase ((x) (base-char))
3097 '(let ((n-code (char-code x)))
3098 (if (and (> n-code 64) ; 65 is #\A.
3099 (< n-code 91)) ; 90 is #\Z.
3100 (code-char (logxor #x20 n-code))
3103 ;;;; equality predicate transforms
3105 ;;; Return true if X and Y are continuations whose only use is a reference
3106 ;;; to the same leaf, and the value of the leaf cannot change.
3107 (defun same-leaf-ref-p (x y)
3108 (declare (type continuation x y))
3109 (let ((x-use (continuation-use x))
3110 (y-use (continuation-use y)))
3113 (eq (ref-leaf x-use) (ref-leaf y-use))
3114 (constant-reference-p x-use))))
3116 ;;; If X and Y are the same leaf, then the result is true. Otherwise, if
3117 ;;; there is no intersection between the types of the arguments, then the
3118 ;;; result is definitely false.
3119 (deftransform simple-equality-transform ((x y) * *
3122 (cond ((same-leaf-ref-p x y)
3124 ((not (types-intersect (continuation-type x) (continuation-type y)))
3127 (give-up-ir1-transform))))
3129 (dolist (x '(eq char= equal))
3130 (%deftransform x '(function * *) #'simple-equality-transform))
3132 ;;; Similar to SIMPLE-EQUALITY-PREDICATE, except that we also try to
3133 ;;; convert to a type-specific predicate or EQ:
3134 ;;; -- If both args are characters, convert to CHAR=. This is better than
3135 ;;; just converting to EQ, since CHAR= may have special compilation
3136 ;;; strategies for non-standard representations, etc.
3137 ;;; -- If either arg is definitely not a number, then we can compare
3139 ;;; -- Otherwise, we try to put the arg we know more about second. If X
3140 ;;; is constant then we put it second. If X is a subtype of Y, we put
3141 ;;; it second. These rules make it easier for the back end to match
3142 ;;; these interesting cases.
3143 ;;; -- If Y is a fixnum, then we quietly pass because the back end can
3144 ;;; handle that case, otherwise give an efficency note.
3145 (deftransform eql ((x y) * * :when :both)
3146 "convert to simpler equality predicate"
3147 (let ((x-type (continuation-type x))
3148 (y-type (continuation-type y))
3149 (char-type (specifier-type 'character))
3150 (number-type (specifier-type 'number)))
3151 (cond ((same-leaf-ref-p x y)
3153 ((not (types-intersect x-type y-type))
3155 ((and (csubtypep x-type char-type)
3156 (csubtypep y-type char-type))
3158 ((or (not (types-intersect x-type number-type))
3159 (not (types-intersect y-type number-type)))
3161 ((and (not (constant-continuation-p y))
3162 (or (constant-continuation-p x)
3163 (and (csubtypep x-type y-type)
3164 (not (csubtypep y-type x-type)))))
3167 (give-up-ir1-transform)))))
3169 ;;; Convert to EQL if both args are rational and complexp is specified
3170 ;;; and the same for both.
3171 (deftransform = ((x y) * * :when :both)
3173 (let ((x-type (continuation-type x))
3174 (y-type (continuation-type y)))
3175 (if (and (csubtypep x-type (specifier-type 'number))
3176 (csubtypep y-type (specifier-type 'number)))
3177 (cond ((or (and (csubtypep x-type (specifier-type 'float))
3178 (csubtypep y-type (specifier-type 'float)))
3179 (and (csubtypep x-type (specifier-type '(complex float)))
3180 (csubtypep y-type (specifier-type '(complex float)))))
3181 ;; They are both floats. Leave as = so that -0.0 is
3182 ;; handled correctly.
3183 (give-up-ir1-transform))
3184 ((or (and (csubtypep x-type (specifier-type 'rational))
3185 (csubtypep y-type (specifier-type 'rational)))
3186 (and (csubtypep x-type (specifier-type '(complex rational)))
3187 (csubtypep y-type (specifier-type '(complex rational)))))
3188 ;; They are both rationals and complexp is the same. Convert
3192 (give-up-ir1-transform
3193 "The operands might not be the same type.")))
3194 (give-up-ir1-transform
3195 "The operands might not be the same type."))))
3197 ;;; If Cont's type is a numeric type, then return the type, otherwise
3198 ;;; GIVE-UP-IR1-TRANSFORM.
3199 (defun numeric-type-or-lose (cont)
3200 (declare (type continuation cont))
3201 (let ((res (continuation-type cont)))
3202 (unless (numeric-type-p res) (give-up-ir1-transform))
3205 ;;; See whether we can statically determine (< X Y) using type information.
3206 ;;; If X's high bound is < Y's low, then X < Y. Similarly, if X's low is >=
3207 ;;; to Y's high, the X >= Y (so return NIL). If not, at least make sure any
3208 ;;; constant arg is second.
3210 ;;; KLUDGE: Why should constant argument be second? It would be nice to find
3211 ;;; out and explain. -- WHN 19990917
3212 #!-propagate-float-type
3213 (defun ir1-transform-< (x y first second inverse)
3214 (if (same-leaf-ref-p x y)
3216 (let* ((x-type (numeric-type-or-lose x))
3217 (x-lo (numeric-type-low x-type))
3218 (x-hi (numeric-type-high x-type))
3219 (y-type (numeric-type-or-lose y))
3220 (y-lo (numeric-type-low y-type))
3221 (y-hi (numeric-type-high y-type)))
3222 (cond ((and x-hi y-lo (< x-hi y-lo))
3224 ((and y-hi x-lo (>= x-lo y-hi))
3226 ((and (constant-continuation-p first)
3227 (not (constant-continuation-p second)))
3230 (give-up-ir1-transform))))))
3231 #!+propagate-float-type
3232 (defun ir1-transform-< (x y first second inverse)
3233 (if (same-leaf-ref-p x y)
3235 (let ((xi (numeric-type->interval (numeric-type-or-lose x)))
3236 (yi (numeric-type->interval (numeric-type-or-lose y))))
3237 (cond ((interval-< xi yi)
3239 ((interval->= xi yi)
3241 ((and (constant-continuation-p first)
3242 (not (constant-continuation-p second)))
3245 (give-up-ir1-transform))))))
3247 (deftransform < ((x y) (integer integer) * :when :both)
3248 (ir1-transform-< x y x y '>))
3250 (deftransform > ((x y) (integer integer) * :when :both)
3251 (ir1-transform-< y x x y '<))
3253 #!+propagate-float-type
3254 (deftransform < ((x y) (float float) * :when :both)
3255 (ir1-transform-< x y x y '>))
3257 #!+propagate-float-type
3258 (deftransform > ((x y) (float float) * :when :both)
3259 (ir1-transform-< y x x y '<))
3261 ;;;; converting N-arg comparisons
3263 ;;;; We convert calls to N-arg comparison functions such as < into
3264 ;;;; two-arg calls. This transformation is enabled for all such
3265 ;;;; comparisons in this file. If any of these predicates are not
3266 ;;;; open-coded, then the transformation should be removed at some
3267 ;;;; point to avoid pessimization.
3269 ;;; This function is used for source transformation of N-arg
3270 ;;; comparison functions other than inequality. We deal both with
3271 ;;; converting to two-arg calls and inverting the sense of the test,
3272 ;;; if necessary. If the call has two args, then we pass or return a
3273 ;;; negated test as appropriate. If it is a degenerate one-arg call,
3274 ;;; then we transform to code that returns true. Otherwise, we bind
3275 ;;; all the arguments and expand into a bunch of IFs.
3276 (declaim (ftype (function (symbol list boolean) *) multi-compare))
3277 (defun multi-compare (predicate args not-p)
3278 (let ((nargs (length args)))
3279 (cond ((< nargs 1) (values nil t))
3280 ((= nargs 1) `(progn ,@args t))
3283 `(if (,predicate ,(first args) ,(second args)) nil t)
3286 (do* ((i (1- nargs) (1- i))
3288 (current (gensym) (gensym))
3289 (vars (list current) (cons current vars))
3290 (result 't (if not-p
3291 `(if (,predicate ,current ,last)
3293 `(if (,predicate ,current ,last)
3296 `((lambda ,vars ,result) . ,args)))))))
3298 (def-source-transform = (&rest args) (multi-compare '= args nil))
3299 (def-source-transform < (&rest args) (multi-compare '< args nil))
3300 (def-source-transform > (&rest args) (multi-compare '> args nil))
3301 (def-source-transform <= (&rest args) (multi-compare '> args t))
3302 (def-source-transform >= (&rest args) (multi-compare '< args t))
3304 (def-source-transform char= (&rest args) (multi-compare 'char= args nil))
3305 (def-source-transform char< (&rest args) (multi-compare 'char< args nil))
3306 (def-source-transform char> (&rest args) (multi-compare 'char> args nil))
3307 (def-source-transform char<= (&rest args) (multi-compare 'char> args t))
3308 (def-source-transform char>= (&rest args) (multi-compare 'char< args t))
3310 (def-source-transform char-equal (&rest args) (multi-compare 'char-equal args nil))
3311 (def-source-transform char-lessp (&rest args) (multi-compare 'char-lessp args nil))
3312 (def-source-transform char-greaterp (&rest args)
3313 (multi-compare 'char-greaterp args nil))
3314 (def-source-transform char-not-greaterp (&rest args)
3315 (multi-compare 'char-greaterp args t))
3316 (def-source-transform char-not-lessp (&rest args) (multi-compare 'char-lessp args t))
3318 ;;; This function does source transformation of N-arg inequality
3319 ;;; functions such as /=. This is similar to Multi-Compare in the <3
3320 ;;; arg cases. If there are more than two args, then we expand into
3321 ;;; the appropriate n^2 comparisons only when speed is important.
3322 (declaim (ftype (function (symbol list) *) multi-not-equal))
3323 (defun multi-not-equal (predicate args)
3324 (let ((nargs (length args)))
3325 (cond ((< nargs 1) (values nil t))
3326 ((= nargs 1) `(progn ,@args t))
3328 `(if (,predicate ,(first args) ,(second args)) nil t))
3329 ((not (policy nil (and (>= speed space)
3330 (>= speed compilation-speed))))
3333 (let ((vars (make-gensym-list nargs)))
3334 (do ((var vars next)
3335 (next (cdr vars) (cdr next))
3338 `((lambda ,vars ,result) . ,args))
3339 (let ((v1 (first var)))
3341 (setq result `(if (,predicate ,v1 ,v2) nil ,result))))))))))
3343 (def-source-transform /= (&rest args) (multi-not-equal '= args))
3344 (def-source-transform char/= (&rest args) (multi-not-equal 'char= args))
3345 (def-source-transform char-not-equal (&rest args) (multi-not-equal 'char-equal args))
3347 ;;; Expand MAX and MIN into the obvious comparisons.
3348 (def-source-transform max (arg &rest more-args)
3349 (if (null more-args)
3351 (once-only ((arg1 arg)
3352 (arg2 `(max ,@more-args)))
3353 `(if (> ,arg1 ,arg2)
3355 (def-source-transform min (arg &rest more-args)
3356 (if (null more-args)
3358 (once-only ((arg1 arg)
3359 (arg2 `(min ,@more-args)))
3360 `(if (< ,arg1 ,arg2)
3363 ;;;; converting N-arg arithmetic functions
3365 ;;;; N-arg arithmetic and logic functions are associated into two-arg
3366 ;;;; versions, and degenerate cases are flushed.
3368 ;;; Left-associate First-Arg and More-Args using Function.
3369 (declaim (ftype (function (symbol t list) list) associate-arguments))
3370 (defun associate-arguments (function first-arg more-args)
3371 (let ((next (rest more-args))
3372 (arg (first more-args)))
3374 `(,function ,first-arg ,arg)
3375 (associate-arguments function `(,function ,first-arg ,arg) next))))
3377 ;;; Do source transformations for transitive functions such as +.
3378 ;;; One-arg cases are replaced with the arg and zero arg cases with
3379 ;;; the identity. If LEAF-FUN is true, then replace two-arg calls with
3380 ;;; a call to that function.
3381 (defun source-transform-transitive (fun args identity &optional leaf-fun)
3382 (declare (symbol fun leaf-fun) (list args))
3385 (1 `(values ,(first args)))
3387 `(,leaf-fun ,(first args) ,(second args))
3390 (associate-arguments fun (first args) (rest args)))))
3392 (def-source-transform + (&rest args) (source-transform-transitive '+ args 0))
3393 (def-source-transform * (&rest args) (source-transform-transitive '* args 1))
3394 (def-source-transform logior (&rest args)
3395 (source-transform-transitive 'logior args 0))
3396 (def-source-transform logxor (&rest args)
3397 (source-transform-transitive 'logxor args 0))
3398 (def-source-transform logand (&rest args)
3399 (source-transform-transitive 'logand args -1))
3401 (def-source-transform logeqv (&rest args)
3402 (if (evenp (length args))
3403 `(lognot (logxor ,@args))
3406 ;;; Note: we can't use SOURCE-TRANSFORM-TRANSITIVE for GCD and LCM
3407 ;;; because when they are given one argument, they return its absolute
3410 (def-source-transform gcd (&rest args)
3413 (1 `(abs (the integer ,(first args))))
3415 (t (associate-arguments 'gcd (first args) (rest args)))))
3417 (def-source-transform lcm (&rest args)
3420 (1 `(abs (the integer ,(first args))))
3422 (t (associate-arguments 'lcm (first args) (rest args)))))
3424 ;;; Do source transformations for intransitive n-arg functions such as
3425 ;;; /. With one arg, we form the inverse. With two args we pass.
3426 ;;; Otherwise we associate into two-arg calls.
3427 (declaim (ftype (function (symbol list t) list) source-transform-intransitive))
3428 (defun source-transform-intransitive (function args inverse)
3430 ((0 2) (values nil t))
3431 (1 `(,@inverse ,(first args)))
3432 (t (associate-arguments function (first args) (rest args)))))
3434 (def-source-transform - (&rest args)
3435 (source-transform-intransitive '- args '(%negate)))
3436 (def-source-transform / (&rest args)
3437 (source-transform-intransitive '/ args '(/ 1)))
3441 ;;; We convert APPLY into MULTIPLE-VALUE-CALL so that the compiler
3442 ;;; only needs to understand one kind of variable-argument call. It is
3443 ;;; more efficient to convert APPLY to MV-CALL than MV-CALL to APPLY.
3444 (def-source-transform apply (fun arg &rest more-args)
3445 (let ((args (cons arg more-args)))
3446 `(multiple-value-call ,fun
3447 ,@(mapcar #'(lambda (x)
3450 (values-list ,(car (last args))))))
3454 ;;;; If the control string is a compile-time constant, then replace it
3455 ;;;; with a use of the FORMATTER macro so that the control string is
3456 ;;;; ``compiled.'' Furthermore, if the destination is either a stream
3457 ;;;; or T and the control string is a function (i.e. FORMATTER), then
3458 ;;;; convert the call to FORMAT to just a FUNCALL of that function.
3460 (deftransform format ((dest control &rest args) (t simple-string &rest t) *
3461 :policy (> speed space))
3462 (unless (constant-continuation-p control)
3463 (give-up-ir1-transform "The control string is not a constant."))
3464 (let ((arg-names (make-gensym-list (length args))))
3465 `(lambda (dest control ,@arg-names)
3466 (declare (ignore control))
3467 (format dest (formatter ,(continuation-value control)) ,@arg-names))))
3469 (deftransform format ((stream control &rest args) (stream function &rest t) *
3470 :policy (> speed space))
3471 (let ((arg-names (make-gensym-list (length args))))
3472 `(lambda (stream control ,@arg-names)
3473 (funcall control stream ,@arg-names)
3476 (deftransform format ((tee control &rest args) ((member t) function &rest t) *
3477 :policy (> speed space))
3478 (let ((arg-names (make-gensym-list (length args))))
3479 `(lambda (tee control ,@arg-names)
3480 (declare (ignore tee))
3481 (funcall control *standard-output* ,@arg-names)