1 ;;;; This file contains floating-point-specific transforms, and may be
2 ;;;; somewhat implementation-dependent in its assumptions of what the
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.
18 (defknown %single-float (real) single-float
20 (defknown %double-float (real) double-float
23 (deftransform float ((n f) (* single-float) *)
26 (deftransform float ((n f) (* double-float) *)
29 (deftransform float ((n) *)
34 (deftransform %single-float ((n) (single-float) *)
37 (deftransform %double-float ((n) (double-float) *)
41 (macrolet ((frob (fun type)
42 `(deftransform random ((num &optional state)
43 (,type &optional *) *)
44 "Use inline float operations."
45 '(,fun num (or state *random-state*)))))
46 (frob %random-single-float single-float)
47 (frob %random-double-float double-float))
49 ;;; Mersenne Twister RNG
51 ;;; FIXME: It's unpleasant to have RANDOM functionality scattered
52 ;;; through the code this way. It would be nice to move this into the
53 ;;; same file as the other RANDOM definitions.
54 (deftransform random ((num &optional state)
55 ((integer 1 #.(expt 2 sb!vm::n-word-bits)) &optional *))
56 ;; FIXME: I almost conditionalized this as #!+sb-doc. Find some way
57 ;; of automatically finding #!+sb-doc in proximity to DEFTRANSFORM
58 ;; to let me scan for places that I made this mistake and didn't
60 "use inline (UNSIGNED-BYTE 32) operations"
61 (let ((type (lvar-type num))
62 (limit (expt 2 sb!vm::n-word-bits))
63 (random-chunk (ecase sb!vm::n-word-bits
65 (64 'sb!kernel::big-random-chunk))))
66 (if (numeric-type-p type)
67 (let ((num-high (numeric-type-high (lvar-type num))))
69 (cond ((constant-lvar-p num)
70 ;; Check the worst case sum absolute error for the
71 ;; random number expectations.
72 (let ((rem (rem limit num-high)))
73 (unless (< (/ (* 2 rem (- num-high rem))
75 (expt 2 (- sb!kernel::random-integer-extra-bits)))
76 (give-up-ir1-transform
77 "The random number expectations are inaccurate."))
78 (if (= num-high limit)
79 `(,random-chunk (or state *random-state*))
81 `(rem (,random-chunk (or state *random-state*)) num)
83 ;; Use multiplication, which is faster.
84 `(values (sb!bignum::%multiply
85 (,random-chunk (or state *random-state*))
87 ((> num-high random-fixnum-max)
88 (give-up-ir1-transform
89 "The range is too large to ensure an accurate result."))
92 `(values (sb!bignum::%multiply
93 (,random-chunk (or state *random-state*))
96 `(rem (,random-chunk (or state *random-state*)) num))))
97 ;; KLUDGE: a relatively conservative treatment, but better
98 ;; than a bug (reported by PFD sbcl-devel towards the end of
100 (give-up-ir1-transform
101 "Argument type is too complex to optimize for."))))
105 (defknown make-single-float ((signed-byte 32)) single-float
108 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
112 (deftransform make-single-float ((bits)
114 "Conditional constant folding"
115 (unless (constant-lvar-p bits)
116 (give-up-ir1-transform))
117 (let* ((bits (lvar-value bits))
118 (float (make-single-float bits)))
119 (when (float-nan-p float)
120 (give-up-ir1-transform))
124 (deftransform make-double-float ((hi lo)
125 ((signed-byte 32) (unsigned-byte 32)))
126 "Conditional constant folding"
127 (unless (and (constant-lvar-p hi)
128 (constant-lvar-p lo))
129 (give-up-ir1-transform))
130 (let* ((hi (lvar-value hi))
132 (float (make-double-float hi lo)))
133 (when (float-nan-p float)
134 (give-up-ir1-transform))
137 (defknown single-float-bits (single-float) (signed-byte 32)
138 (movable foldable flushable))
140 (defknown double-float-high-bits (double-float) (signed-byte 32)
141 (movable foldable flushable))
143 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
144 (movable foldable flushable))
146 (deftransform float-sign ((float &optional float2)
147 (single-float &optional single-float) *)
149 (let ((temp (gensym)))
150 `(let ((,temp (abs float2)))
151 (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
152 '(if (minusp (single-float-bits float)) -1f0 1f0)))
154 (deftransform float-sign ((float &optional float2)
155 (double-float &optional double-float) *)
157 (let ((temp (gensym)))
158 `(let ((,temp (abs float2)))
159 (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
160 '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
162 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
164 (defknown decode-single-float (single-float)
165 (values single-float single-float-exponent (single-float -1f0 1f0))
166 (movable foldable flushable))
168 (defknown decode-double-float (double-float)
169 (values double-float double-float-exponent (double-float -1d0 1d0))
170 (movable foldable flushable))
172 (defknown integer-decode-single-float (single-float)
173 (values single-float-significand single-float-int-exponent (integer -1 1))
174 (movable foldable flushable))
176 (defknown integer-decode-double-float (double-float)
177 (values double-float-significand double-float-int-exponent (integer -1 1))
178 (movable foldable flushable))
180 (defknown scale-single-float (single-float integer) single-float
181 (movable foldable flushable))
183 (defknown scale-double-float (double-float integer) double-float
184 (movable foldable flushable))
186 (deftransform decode-float ((x) (single-float) *)
187 '(decode-single-float x))
189 (deftransform decode-float ((x) (double-float) *)
190 '(decode-double-float x))
192 (deftransform integer-decode-float ((x) (single-float) *)
193 '(integer-decode-single-float x))
195 (deftransform integer-decode-float ((x) (double-float) *)
196 '(integer-decode-double-float x))
198 (deftransform scale-float ((f ex) (single-float *) *)
199 (if (and #!+x86 t #!-x86 nil
200 (csubtypep (lvar-type ex)
201 (specifier-type '(signed-byte 32))))
202 '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
203 '(scale-single-float f ex)))
205 (deftransform scale-float ((f ex) (double-float *) *)
206 (if (and #!+x86 t #!-x86 nil
207 (csubtypep (lvar-type ex)
208 (specifier-type '(signed-byte 32))))
210 '(scale-double-float f ex)))
212 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
214 ;;; SBCL's own implementation of floating point supports floating
215 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
216 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
217 ;;; floating point support. Thus, we have to avoid running it on the
218 ;;; cross-compilation host, since we're not guaranteed that the
219 ;;; cross-compilation host will support floating point infinities.
221 ;;; If we wanted to live dangerously, we could conditionalize the code
222 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
223 ;;; host happened to be SBCL, we'd be able to run the infinity-using
225 ;;; * SBCL itself gets built with more complete optimization.
227 ;;; * You get a different SBCL depending on what your cross-compilation
229 ;;; So far the pros and cons seem seem to be mostly academic, since
230 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
231 ;;; actually important in compiling SBCL itself. If this changes, then
232 ;;; we have to decide:
233 ;;; * Go for simplicity, leaving things as they are.
234 ;;; * Go for performance at the expense of conceptual clarity,
235 ;;; using #+(OR SBCL SB-XC) and otherwise leaving the build
237 ;;; * Go for performance at the expense of build time, using
238 ;;; #+(OR SBCL SB-XC) and also making SBCL do not just
239 ;;; make-host-1.sh and make-host-2.sh, but a third step
240 ;;; make-host-3.sh where it builds itself under itself. (Such a
241 ;;; 3-step build process could also help with other things, e.g.
242 ;;; using specialized arrays to represent debug information.)
243 ;;; * Rewrite the code so that it doesn't depend on unportable
244 ;;; floating point infinities.
246 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
247 ;;; are computed for the result, if possible.
248 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
251 (defun scale-float-derive-type-aux (f ex same-arg)
252 (declare (ignore same-arg))
253 (flet ((scale-bound (x n)
254 ;; We need to be a bit careful here and catch any overflows
255 ;; that might occur. We can ignore underflows which become
259 (scale-float (type-bound-number x) n)
260 (floating-point-overflow ()
263 (when (and (numeric-type-p f) (numeric-type-p ex))
264 (let ((f-lo (numeric-type-low f))
265 (f-hi (numeric-type-high f))
266 (ex-lo (numeric-type-low ex))
267 (ex-hi (numeric-type-high ex))
271 (if (< (float-sign (type-bound-number f-hi)) 0.0)
273 (setf new-hi (scale-bound f-hi ex-lo)))
275 (setf new-hi (scale-bound f-hi ex-hi)))))
277 (if (< (float-sign (type-bound-number f-lo)) 0.0)
279 (setf new-lo (scale-bound f-lo ex-hi)))
281 (setf new-lo (scale-bound f-lo ex-lo)))))
282 (make-numeric-type :class (numeric-type-class f)
283 :format (numeric-type-format f)
287 (defoptimizer (scale-single-float derive-type) ((f ex))
288 (two-arg-derive-type f ex #'scale-float-derive-type-aux
289 #'scale-single-float t))
290 (defoptimizer (scale-double-float derive-type) ((f ex))
291 (two-arg-derive-type f ex #'scale-float-derive-type-aux
292 #'scale-double-float t))
294 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
295 ;;; FLOAT function return the correct ranges if the input has some
296 ;;; defined range. Quite useful if we want to convert some type of
297 ;;; bounded integer into a float.
299 ((frob (fun type most-negative most-positive)
300 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
302 (defun ,aux-name (num)
303 ;; When converting a number to a float, the limits are
305 (let* ((lo (bound-func (lambda (x)
306 (if (< x ,most-negative)
309 (numeric-type-low num)))
310 (hi (bound-func (lambda (x)
311 (if (< ,most-positive x )
314 (numeric-type-high num))))
315 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
317 (defoptimizer (,fun derive-type) ((num))
319 (one-arg-derive-type num #',aux-name #',fun)
322 (frob %single-float single-float
323 most-negative-single-float most-positive-single-float)
324 (frob %double-float double-float
325 most-negative-double-float most-positive-double-float))
330 (defun safe-ctype-for-single-coercion-p (x)
331 ;; See comment in SAFE-SINGLE-COERCION-P -- this deals with the same
332 ;; problem, but in the context of evaluated and compiled (+ <int> <single>)
333 ;; giving different result if we fail to check for this.
334 (or (not (csubtypep x (specifier-type 'integer)))
336 (csubtypep x (specifier-type `(integer ,most-negative-exactly-single-float-fixnum
337 ,most-positive-exactly-single-float-fixnum)))
339 (csubtypep x (specifier-type 'fixnum))))
341 ;;; Do some stuff to recognize when the loser is doing mixed float and
342 ;;; rational arithmetic, or different float types, and fix it up. If
343 ;;; we don't, he won't even get so much as an efficiency note.
344 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
345 (if (or (not (types-equal-or-intersect (lvar-type y) (specifier-type 'single-float)))
346 (safe-ctype-for-single-coercion-p (lvar-type x)))
347 `(,(lvar-fun-name (basic-combination-fun node))
349 (give-up-ir1-transform)))
350 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
351 (if (or (not (types-equal-or-intersect (lvar-type x) (specifier-type 'single-float)))
352 (safe-ctype-for-single-coercion-p (lvar-type y)))
353 `(,(lvar-fun-name (basic-combination-fun node))
355 (give-up-ir1-transform)))
357 (dolist (x '(+ * / -))
358 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
359 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
361 (dolist (x '(= < > + * / -))
362 (%deftransform x '(function (single-float double-float) *)
363 #'float-contagion-arg1)
364 (%deftransform x '(function (double-float single-float) *)
365 #'float-contagion-arg2))
367 (macrolet ((def (type &rest args)
368 `(deftransform * ((x y) (,type (constant-arg (member ,@args))) *
370 :policy (zerop float-accuracy))
371 "optimize multiplication by one"
372 (let ((y (lvar-value y)))
376 (def single-float 1.0 -1.0)
377 (def double-float 1.0d0 -1.0d0))
379 ;;; Return the reciprocal of X if it can be represented exactly, NIL otherwise.
380 (defun maybe-exact-reciprocal (x)
383 (multiple-value-bind (significand exponent sign)
384 (integer-decode-float x)
385 ;; only powers of 2 can be inverted exactly
386 (unless (zerop (logand significand (1- significand)))
387 (return-from maybe-exact-reciprocal nil))
388 (let ((expected (/ sign significand (expt 2 exponent)))
390 (multiple-value-bind (significand exponent sign)
391 (integer-decode-float reciprocal)
392 ;; Denorms can't be inverted safely.
393 (and (eql expected (* sign significand (expt 2 exponent)))
395 (error () (return-from maybe-exact-reciprocal nil)))))
397 ;;; Replace constant division by multiplication with exact reciprocal,
399 (macrolet ((def (type)
400 `(deftransform / ((x y) (,type (constant-arg ,type)) *
402 "convert to multiplication by reciprocal"
403 (let ((n (lvar-value y)))
404 (if (policy node (zerop float-accuracy))
406 (let ((r (maybe-exact-reciprocal n)))
409 (give-up-ir1-transform
410 "~S does not have an exact reciprocal"
415 ;;; Optimize addition and subtraction of zero
416 (macrolet ((def (op type &rest args)
417 `(deftransform ,op ((x y) (,type (constant-arg (member ,@args))) *
419 :policy (zerop float-accuracy))
421 ;; No signed zeros, thanks.
422 (def + single-float 0 0.0)
423 (def - single-float 0 0.0)
424 (def + double-float 0 0.0 0.0d0)
425 (def - double-float 0 0.0 0.0d0))
427 ;;; On most platforms (+ x x) is faster than (* x 2)
428 (macrolet ((def (type &rest args)
429 `(deftransform * ((x y) (,type (constant-arg (member ,@args))))
431 (def single-float 2 2.0)
432 (def double-float 2 2.0 2.0d0))
434 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
435 ;;; general float rational args to comparison, since Common Lisp
436 ;;; semantics says we are supposed to compare as rationals, but we can
437 ;;; do it for any rational that has a precise representation as a
438 ;;; float (such as 0).
439 (macrolet ((frob (op)
440 `(deftransform ,op ((x y) (float rational) *)
441 "open-code FLOAT to RATIONAL comparison"
442 (unless (constant-lvar-p y)
443 (give-up-ir1-transform
444 "The RATIONAL value isn't known at compile time."))
445 (let ((val (lvar-value y)))
446 (unless (eql (rational (float val)) val)
447 (give-up-ir1-transform
448 "~S doesn't have a precise float representation."
450 `(,',op x (float y x)))))
455 ;;;; irrational derive-type methods
457 ;;; Derive the result to be float for argument types in the
458 ;;; appropriate domain.
459 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
460 (dolist (stuff '((asin (real -1.0 1.0))
461 (acos (real -1.0 1.0))
463 (atanh (real -1.0 1.0))
465 (destructuring-bind (name type) stuff
466 (let ((type (specifier-type type)))
467 (setf (fun-info-derive-type (fun-info-or-lose name))
469 (declare (type combination call))
470 (when (csubtypep (lvar-type
471 (first (combination-args call)))
473 (specifier-type 'float)))))))
475 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
476 (defoptimizer (log derive-type) ((x &optional y))
477 (when (and (csubtypep (lvar-type x)
478 (specifier-type '(real 0.0)))
480 (csubtypep (lvar-type y)
481 (specifier-type '(real 0.0)))))
482 (specifier-type 'float)))
484 ;;;; irrational transforms
486 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
487 (double-float) double-float
488 (movable foldable flushable))
490 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
491 (double-float) (double-float -1.0d0 1.0d0)
492 (movable foldable flushable))
494 (defknown (%asin %atan)
496 (double-float #.(coerce (- (/ pi 2)) 'double-float)
497 #.(coerce (/ pi 2) 'double-float))
498 (movable foldable flushable))
501 (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
502 (movable foldable flushable))
505 (double-float) (double-float 1.0d0)
506 (movable foldable flushable))
508 (defknown (%acosh %exp %sqrt)
509 (double-float) (double-float 0.0d0)
510 (movable foldable flushable))
513 (double-float) (double-float -1d0)
514 (movable foldable flushable))
517 (double-float double-float) (double-float 0d0)
518 (movable foldable flushable))
521 (double-float double-float) double-float
522 (movable foldable flushable))
525 (double-float double-float)
526 (double-float #.(coerce (- pi) 'double-float)
527 #.(coerce pi 'double-float))
528 (movable foldable flushable))
531 (double-float double-float) double-float
532 (movable foldable flushable))
535 (double-float (signed-byte 32)) double-float
536 (movable foldable flushable))
539 (double-float) double-float
540 (movable foldable flushable))
542 (macrolet ((def (name prim rtype)
544 (deftransform ,name ((x) (single-float) ,rtype)
545 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
546 (deftransform ,name ((x) (double-float) ,rtype)
550 (def sqrt %sqrt float)
551 (def asin %asin float)
552 (def acos %acos float)
558 (def acosh %acosh float)
559 (def atanh %atanh float))
561 ;;; The argument range is limited on the x86 FP trig. functions. A
562 ;;; post-test can detect a failure (and load a suitable result), but
563 ;;; this test is avoided if possible.
564 (macrolet ((def (name prim prim-quick)
565 (declare (ignorable prim-quick))
567 (deftransform ,name ((x) (single-float) *)
568 #!+x86 (cond ((csubtypep (lvar-type x)
569 (specifier-type '(single-float
570 (#.(- (expt 2f0 63)))
572 `(coerce (,',prim-quick (coerce x 'double-float))
576 "unable to avoid inline argument range check~@
577 because the argument range (~S) was not within 2^63"
578 (type-specifier (lvar-type x)))
579 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
580 #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
581 (deftransform ,name ((x) (double-float) *)
582 #!+x86 (cond ((csubtypep (lvar-type x)
583 (specifier-type '(double-float
584 (#.(- (expt 2d0 63)))
589 "unable to avoid inline argument range check~@
590 because the argument range (~S) was not within 2^63"
591 (type-specifier (lvar-type x)))
593 #!-x86 `(,',prim x)))))
594 (def sin %sin %sin-quick)
595 (def cos %cos %cos-quick)
596 (def tan %tan %tan-quick))
598 (deftransform atan ((x y) (single-float single-float) *)
599 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
601 (deftransform atan ((x y) (double-float double-float) *)
604 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
605 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
607 (deftransform expt ((x y) ((double-float 0d0) double-float) *)
609 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
610 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
612 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) *)
613 `(%pow x (coerce y 'double-float)))
615 ;;; ANSI says log with base zero returns zero.
616 (deftransform log ((x y) (float float) float)
617 '(if (zerop y) y (/ (log x) (log y))))
619 ;;; Handle some simple transformations.
621 (deftransform abs ((x) ((complex double-float)) double-float)
622 '(%hypot (realpart x) (imagpart x)))
624 (deftransform abs ((x) ((complex single-float)) single-float)
625 '(coerce (%hypot (coerce (realpart x) 'double-float)
626 (coerce (imagpart x) 'double-float))
629 (deftransform phase ((x) ((complex double-float)) double-float)
630 '(%atan2 (imagpart x) (realpart x)))
632 (deftransform phase ((x) ((complex single-float)) single-float)
633 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
634 (coerce (realpart x) 'double-float))
637 (deftransform phase ((x) ((float)) float)
638 '(if (minusp (float-sign x))
642 ;;; The number is of type REAL.
643 (defun numeric-type-real-p (type)
644 (and (numeric-type-p type)
645 (eq (numeric-type-complexp type) :real)))
647 ;;; Coerce a numeric type bound to the given type while handling
648 ;;; exclusive bounds.
649 (defun coerce-numeric-bound (bound type)
652 (list (coerce (car bound) type))
653 (coerce bound type))))
655 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
658 ;;;; optimizers for elementary functions
660 ;;;; These optimizers compute the output range of the elementary
661 ;;;; function, based on the domain of the input.
663 ;;; Generate a specifier for a complex type specialized to the same
664 ;;; type as the argument.
665 (defun complex-float-type (arg)
666 (declare (type numeric-type arg))
667 (let* ((format (case (numeric-type-class arg)
668 ((integer rational) 'single-float)
669 (t (numeric-type-format arg))))
670 (float-type (or format 'float)))
671 (specifier-type `(complex ,float-type))))
673 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
674 ;;; should be the right kind of float. Allow bounds for the float
676 (defun float-or-complex-float-type (arg &optional lo hi)
677 (declare (type numeric-type arg))
678 (let* ((format (case (numeric-type-class arg)
679 ((integer rational) 'single-float)
680 (t (numeric-type-format arg))))
681 (float-type (or format 'float))
682 (lo (coerce-numeric-bound lo float-type))
683 (hi (coerce-numeric-bound hi float-type)))
684 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
685 (complex ,float-type)))))
689 (eval-when (:compile-toplevel :execute)
690 ;; So the problem with this hack is that it's actually broken. If
691 ;; the host does not have long floats, then setting *R-D-F-F* to
692 ;; LONG-FLOAT doesn't actually buy us anything. FIXME.
693 (setf *read-default-float-format*
694 #!+long-float 'long-float #!-long-float 'double-float))
695 ;;; Test whether the numeric-type ARG is within the domain specified by
696 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
698 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
699 (defun domain-subtypep (arg domain-low domain-high)
700 (declare (type numeric-type arg)
701 (type (or real null) domain-low domain-high))
702 (let* ((arg-lo (numeric-type-low arg))
703 (arg-lo-val (type-bound-number arg-lo))
704 (arg-hi (numeric-type-high arg))
705 (arg-hi-val (type-bound-number arg-hi)))
706 ;; Check that the ARG bounds are correctly canonicalized.
707 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
708 (minusp (float-sign arg-lo-val)))
709 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-lo)
710 (setq arg-lo 0e0 arg-lo-val arg-lo))
711 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
712 (plusp (float-sign arg-hi-val)))
713 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-hi)
714 (setq arg-hi (ecase *read-default-float-format*
715 (double-float (load-time-value (make-unportable-float :double-float-negative-zero)))
717 (long-float (load-time-value (make-unportable-float :long-float-negative-zero))))
719 (flet ((fp-neg-zero-p (f) ; Is F -0.0?
720 (and (floatp f) (zerop f) (minusp (float-sign f))))
721 (fp-pos-zero-p (f) ; Is F +0.0?
722 (and (floatp f) (zerop f) (plusp (float-sign f)))))
723 (and (or (null domain-low)
724 (and arg-lo (>= arg-lo-val domain-low)
725 (not (and (fp-pos-zero-p domain-low)
726 (fp-neg-zero-p arg-lo)))))
727 (or (null domain-high)
728 (and arg-hi (<= arg-hi-val domain-high)
729 (not (and (fp-neg-zero-p domain-high)
730 (fp-pos-zero-p arg-hi)))))))))
731 (eval-when (:compile-toplevel :execute)
732 (setf *read-default-float-format* 'single-float))
734 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
737 ;;; Handle monotonic functions of a single variable whose domain is
738 ;;; possibly part of the real line. ARG is the variable, FUN is the
739 ;;; function, and DOMAIN is a specifier that gives the (real) domain
740 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
741 ;;; bounds directly. Otherwise, we compute the bounds for the
742 ;;; intersection between ARG and DOMAIN, and then append a complex
743 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
745 ;;; Negative and positive zero are considered distinct within
746 ;;; DOMAIN-LOW and DOMAIN-HIGH.
748 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
749 ;;; can't compute the bounds using FUN.
750 (defun elfun-derive-type-simple (arg fun domain-low domain-high
751 default-low default-high
752 &optional (increasingp t))
753 (declare (type (or null real) domain-low domain-high))
756 (cond ((eq (numeric-type-complexp arg) :complex)
757 (complex-float-type arg))
758 ((numeric-type-real-p arg)
759 ;; The argument is real, so let's find the intersection
760 ;; between the argument and the domain of the function.
761 ;; We compute the bounds on the intersection, and for
762 ;; everything else, we return a complex number of the
764 (multiple-value-bind (intersection difference)
765 (interval-intersection/difference (numeric-type->interval arg)
771 ;; Process the intersection.
772 (let* ((low (interval-low intersection))
773 (high (interval-high intersection))
774 (res-lo (or (bound-func fun (if increasingp low high))
776 (res-hi (or (bound-func fun (if increasingp high low))
778 (format (case (numeric-type-class arg)
779 ((integer rational) 'single-float)
780 (t (numeric-type-format arg))))
781 (bound-type (or format 'float))
786 :low (coerce-numeric-bound res-lo bound-type)
787 :high (coerce-numeric-bound res-hi bound-type))))
788 ;; If the ARG is a subset of the domain, we don't
789 ;; have to worry about the difference, because that
791 (if (or (null difference)
792 ;; Check whether the arg is within the domain.
793 (domain-subtypep arg domain-low domain-high))
796 (specifier-type `(complex ,bound-type))))))
798 ;; No intersection so the result must be purely complex.
799 (complex-float-type arg)))))
801 (float-or-complex-float-type arg default-low default-high))))))
804 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
805 &key (increasingp t))
806 (let ((num (gensym)))
807 `(defoptimizer (,name derive-type) ((,num))
811 (elfun-derive-type-simple arg #',name
812 ,domain-low ,domain-high
813 ,def-low-bnd ,def-high-bnd
816 ;; These functions are easy because they are defined for the whole
818 (frob exp nil nil 0 nil)
819 (frob sinh nil nil nil nil)
820 (frob tanh nil nil -1 1)
821 (frob asinh nil nil nil nil)
823 ;; These functions are only defined for part of the real line. The
824 ;; condition selects the desired part of the line.
825 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
826 ;; Acos is monotonic decreasing, so we need to swap the function
827 ;; values at the lower and upper bounds of the input domain.
828 (frob acos -1d0 1d0 0 pi :increasingp nil)
829 (frob acosh 1d0 nil nil nil)
830 (frob atanh -1d0 1d0 -1 1)
831 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
833 (frob sqrt (load-time-value (make-unportable-float :double-float-negative-zero)) nil 0 nil))
835 ;;; Compute bounds for (expt x y). This should be easy since (expt x
836 ;;; y) = (exp (* y (log x))). However, computations done this way
837 ;;; have too much roundoff. Thus we have to do it the hard way.
838 (defun safe-expt (x y)
840 (when (< (abs y) 10000)
845 ;;; Handle the case when x >= 1.
846 (defun interval-expt-> (x y)
847 (case (sb!c::interval-range-info y 0d0)
849 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
850 ;; obviously non-negative. We just have to be careful for
851 ;; infinite bounds (given by nil).
852 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
853 (type-bound-number (sb!c::interval-low y))))
854 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
855 (type-bound-number (sb!c::interval-high y)))))
856 (list (sb!c::make-interval :low (or lo 1) :high hi))))
858 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
859 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
861 (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
862 (type-bound-number (sb!c::interval-low y))))
863 (hi (safe-expt (type-bound-number (sb!c::interval-low x))
864 (type-bound-number (sb!c::interval-high y)))))
865 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
867 ;; Split the interval in half.
868 (destructuring-bind (y- y+)
869 (sb!c::interval-split 0 y t)
870 (list (interval-expt-> x y-)
871 (interval-expt-> x y+))))))
873 ;;; Handle the case when x <= 1
874 (defun interval-expt-< (x y)
875 (case (sb!c::interval-range-info x 0d0)
877 ;; The case of 0 <= x <= 1 is easy
878 (case (sb!c::interval-range-info y)
880 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
881 ;; obviously [0, 1]. We just have to be careful for infinite bounds
883 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
884 (type-bound-number (sb!c::interval-high y))))
885 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
886 (type-bound-number (sb!c::interval-low y)))))
887 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
889 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
890 ;; obviously [1, inf].
891 (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
892 (type-bound-number (sb!c::interval-low y))))
893 (lo (safe-expt (type-bound-number (sb!c::interval-high x))
894 (type-bound-number (sb!c::interval-high y)))))
895 (list (sb!c::make-interval :low (or lo 1) :high hi))))
897 ;; Split the interval in half
898 (destructuring-bind (y- y+)
899 (sb!c::interval-split 0 y t)
900 (list (interval-expt-< x y-)
901 (interval-expt-< x y+))))))
903 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
904 ;; The calling function must insure this! For now we'll just
905 ;; return the appropriate unbounded float type.
906 (list (sb!c::make-interval :low nil :high nil)))
908 (destructuring-bind (neg pos)
909 (interval-split 0 x t t)
910 (list (interval-expt-< neg y)
911 (interval-expt-< pos y))))))
913 ;;; Compute bounds for (expt x y).
914 (defun interval-expt (x y)
915 (case (interval-range-info x 1)
918 (interval-expt-> x y))
921 (interval-expt-< x y))
923 (destructuring-bind (left right)
924 (interval-split 1 x t t)
925 (list (interval-expt left y)
926 (interval-expt right y))))))
928 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
929 (declare (ignore x-int))
930 ;; Figure out what the return type should be, given the argument
931 ;; types and bounds and the result type and bounds.
932 (cond ((csubtypep x-type (specifier-type 'integer))
933 ;; an integer to some power
934 (case (numeric-type-class y-type)
936 ;; Positive integer to an integer power is either an
937 ;; integer or a rational.
938 (let ((lo (or (interval-low bnd) '*))
939 (hi (or (interval-high bnd) '*)))
940 (if (and (interval-low y-int)
941 (>= (type-bound-number (interval-low y-int)) 0))
942 (specifier-type `(integer ,lo ,hi))
943 (specifier-type `(rational ,lo ,hi)))))
945 ;; Positive integer to rational power is either a rational
946 ;; or a single-float.
947 (let* ((lo (interval-low bnd))
948 (hi (interval-high bnd))
950 (floor (type-bound-number lo))
953 (ceiling (type-bound-number hi))
955 (f-lo (or (bound-func #'float lo)
957 (f-hi (or (bound-func #'float hi)
959 (specifier-type `(or (rational ,int-lo ,int-hi)
960 (single-float ,f-lo, f-hi)))))
962 ;; A positive integer to a float power is a float.
963 (modified-numeric-type y-type
964 :low (interval-low bnd)
965 :high (interval-high bnd)))
967 ;; A positive integer to a number is a number (for now).
968 (specifier-type 'number))))
969 ((csubtypep x-type (specifier-type 'rational))
970 ;; a rational to some power
971 (case (numeric-type-class y-type)
973 ;; A positive rational to an integer power is always a rational.
974 (specifier-type `(rational ,(or (interval-low bnd) '*)
975 ,(or (interval-high bnd) '*))))
977 ;; A positive rational to rational power is either a rational
978 ;; or a single-float.
979 (let* ((lo (interval-low bnd))
980 (hi (interval-high bnd))
982 (floor (type-bound-number lo))
985 (ceiling (type-bound-number hi))
987 (f-lo (or (bound-func #'float lo)
989 (f-hi (or (bound-func #'float hi)
991 (specifier-type `(or (rational ,int-lo ,int-hi)
992 (single-float ,f-lo, f-hi)))))
994 ;; A positive rational to a float power is a float.
995 (modified-numeric-type y-type
996 :low (interval-low bnd)
997 :high (interval-high bnd)))
999 ;; A positive rational to a number is a number (for now).
1000 (specifier-type 'number))))
1001 ((csubtypep x-type (specifier-type 'float))
1002 ;; a float to some power
1003 (case (numeric-type-class y-type)
1004 ((or integer rational)
1005 ;; A positive float to an integer or rational power is
1009 :format (numeric-type-format x-type)
1010 :low (interval-low bnd)
1011 :high (interval-high bnd)))
1013 ;; A positive float to a float power is a float of the
1017 :format (float-format-max (numeric-type-format x-type)
1018 (numeric-type-format y-type))
1019 :low (interval-low bnd)
1020 :high (interval-high bnd)))
1022 ;; A positive float to a number is a number (for now)
1023 (specifier-type 'number))))
1025 ;; A number to some power is a number.
1026 (specifier-type 'number))))
1028 (defun merged-interval-expt (x y)
1029 (let* ((x-int (numeric-type->interval x))
1030 (y-int (numeric-type->interval y)))
1031 (mapcar (lambda (type)
1032 (fixup-interval-expt type x-int y-int x y))
1033 (flatten-list (interval-expt x-int y-int)))))
1035 (defun expt-derive-type-aux (x y same-arg)
1036 (declare (ignore same-arg))
1037 (cond ((or (not (numeric-type-real-p x))
1038 (not (numeric-type-real-p y)))
1039 ;; Use numeric contagion if either is not real.
1040 (numeric-contagion x y))
1041 ((csubtypep y (specifier-type 'integer))
1042 ;; A real raised to an integer power is well-defined.
1043 (merged-interval-expt x y))
1044 ;; A real raised to a non-integral power can be a float or a
1046 ((or (csubtypep x (specifier-type '(rational 0)))
1047 (csubtypep x (specifier-type '(float (0d0)))))
1048 ;; But a positive real to any power is well-defined.
1049 (merged-interval-expt x y))
1050 ((and (csubtypep x (specifier-type 'rational))
1051 (csubtypep y (specifier-type 'rational)))
1052 ;; A rational to the power of a rational could be a rational
1053 ;; or a possibly-complex single float
1054 (specifier-type '(or rational single-float (complex single-float))))
1056 ;; a real to some power. The result could be a real or a
1058 (float-or-complex-float-type (numeric-contagion x y)))))
1060 (defoptimizer (expt derive-type) ((x y))
1061 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
1063 ;;; Note we must assume that a type including 0.0 may also include
1064 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
1065 (defun log-derive-type-aux-1 (x)
1066 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
1068 (defun log-derive-type-aux-2 (x y same-arg)
1069 (let ((log-x (log-derive-type-aux-1 x))
1070 (log-y (log-derive-type-aux-1 y))
1071 (accumulated-list nil))
1072 ;; LOG-X or LOG-Y might be union types. We need to run through
1073 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
1074 (dolist (x-type (prepare-arg-for-derive-type log-x))
1075 (dolist (y-type (prepare-arg-for-derive-type log-y))
1076 (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
1077 (apply #'type-union (flatten-list accumulated-list))))
1079 (defoptimizer (log derive-type) ((x &optional y))
1081 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
1082 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
1084 (defun atan-derive-type-aux-1 (y)
1085 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
1087 (defun atan-derive-type-aux-2 (y x same-arg)
1088 (declare (ignore same-arg))
1089 ;; The hard case with two args. We just return the max bounds.
1090 (let ((result-type (numeric-contagion y x)))
1091 (cond ((and (numeric-type-real-p x)
1092 (numeric-type-real-p y))
1093 (let* (;; FIXME: This expression for FORMAT seems to
1094 ;; appear multiple times, and should be factored out.
1095 (format (case (numeric-type-class result-type)
1096 ((integer rational) 'single-float)
1097 (t (numeric-type-format result-type))))
1098 (bound-format (or format 'float)))
1099 (make-numeric-type :class 'float
1102 :low (coerce (- pi) bound-format)
1103 :high (coerce pi bound-format))))
1105 ;; The result is a float or a complex number
1106 (float-or-complex-float-type result-type)))))
1108 (defoptimizer (atan derive-type) ((y &optional x))
1110 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
1111 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
1113 (defun cosh-derive-type-aux (x)
1114 ;; We note that cosh x = cosh |x| for all real x.
1115 (elfun-derive-type-simple
1116 (if (numeric-type-real-p x)
1117 (abs-derive-type-aux x)
1119 #'cosh nil nil 0 nil))
1121 (defoptimizer (cosh derive-type) ((num))
1122 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
1124 (defun phase-derive-type-aux (arg)
1125 (let* ((format (case (numeric-type-class arg)
1126 ((integer rational) 'single-float)
1127 (t (numeric-type-format arg))))
1128 (bound-type (or format 'float)))
1129 (cond ((numeric-type-real-p arg)
1130 (case (interval-range-info (numeric-type->interval arg) 0.0)
1132 ;; The number is positive, so the phase is 0.
1133 (make-numeric-type :class 'float
1136 :low (coerce 0 bound-type)
1137 :high (coerce 0 bound-type)))
1139 ;; The number is always negative, so the phase is pi.
1140 (make-numeric-type :class 'float
1143 :low (coerce pi bound-type)
1144 :high (coerce pi bound-type)))
1146 ;; We can't tell. The result is 0 or pi. Use a union
1149 (make-numeric-type :class 'float
1152 :low (coerce 0 bound-type)
1153 :high (coerce 0 bound-type))
1154 (make-numeric-type :class 'float
1157 :low (coerce pi bound-type)
1158 :high (coerce pi bound-type))))))
1160 ;; We have a complex number. The answer is the range -pi
1161 ;; to pi. (-pi is included because we have -0.)
1162 (make-numeric-type :class 'float
1165 :low (coerce (- pi) bound-type)
1166 :high (coerce pi bound-type))))))
1168 (defoptimizer (phase derive-type) ((num))
1169 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1173 (deftransform realpart ((x) ((complex rational)) *)
1174 '(sb!kernel:%realpart x))
1175 (deftransform imagpart ((x) ((complex rational)) *)
1176 '(sb!kernel:%imagpart x))
1178 ;;; Make REALPART and IMAGPART return the appropriate types. This
1179 ;;; should help a lot in optimized code.
1180 (defun realpart-derive-type-aux (type)
1181 (let ((class (numeric-type-class type))
1182 (format (numeric-type-format type)))
1183 (cond ((numeric-type-real-p type)
1184 ;; The realpart of a real has the same type and range as
1186 (make-numeric-type :class class
1189 :low (numeric-type-low type)
1190 :high (numeric-type-high type)))
1192 ;; We have a complex number. The result has the same type
1193 ;; as the real part, except that it's real, not complex,
1195 (make-numeric-type :class class
1198 :low (numeric-type-low type)
1199 :high (numeric-type-high type))))))
1200 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1201 (defoptimizer (realpart derive-type) ((num))
1202 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1203 (defun imagpart-derive-type-aux (type)
1204 (let ((class (numeric-type-class type))
1205 (format (numeric-type-format type)))
1206 (cond ((numeric-type-real-p type)
1207 ;; The imagpart of a real has the same type as the input,
1208 ;; except that it's zero.
1209 (let ((bound-format (or format class 'real)))
1210 (make-numeric-type :class class
1213 :low (coerce 0 bound-format)
1214 :high (coerce 0 bound-format))))
1216 ;; We have a complex number. The result has the same type as
1217 ;; the imaginary part, except that it's real, not complex,
1219 (make-numeric-type :class class
1222 :low (numeric-type-low type)
1223 :high (numeric-type-high type))))))
1224 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1225 (defoptimizer (imagpart derive-type) ((num))
1226 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1228 (defun complex-derive-type-aux-1 (re-type)
1229 (if (numeric-type-p re-type)
1230 (make-numeric-type :class (numeric-type-class re-type)
1231 :format (numeric-type-format re-type)
1232 :complexp (if (csubtypep re-type
1233 (specifier-type 'rational))
1236 :low (numeric-type-low re-type)
1237 :high (numeric-type-high re-type))
1238 (specifier-type 'complex)))
1240 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1241 (declare (ignore same-arg))
1242 (if (and (numeric-type-p re-type)
1243 (numeric-type-p im-type))
1244 ;; Need to check to make sure numeric-contagion returns the
1245 ;; right type for what we want here.
1247 ;; Also, what about rational canonicalization, like (complex 5 0)
1248 ;; is 5? So, if the result must be complex, we make it so.
1249 ;; If the result might be complex, which happens only if the
1250 ;; arguments are rational, we make it a union type of (or
1251 ;; rational (complex rational)).
1252 (let* ((element-type (numeric-contagion re-type im-type))
1253 (rat-result-p (csubtypep element-type
1254 (specifier-type 'rational))))
1256 (type-union element-type
1258 `(complex ,(numeric-type-class element-type))))
1259 (make-numeric-type :class (numeric-type-class element-type)
1260 :format (numeric-type-format element-type)
1261 :complexp (if rat-result-p
1264 (specifier-type 'complex)))
1266 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1267 (defoptimizer (complex derive-type) ((re &optional im))
1269 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1270 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1272 ;;; Define some transforms for complex operations. We do this in lieu
1273 ;;; of complex operation VOPs.
1274 (macrolet ((frob (type)
1276 (deftransform complex ((r) (,type))
1277 '(complex r ,(coerce 0 type)))
1278 (deftransform complex ((r i) (,type (and real (not ,type))))
1279 '(complex r (truly-the ,type (coerce i ',type))))
1280 (deftransform complex ((r i) ((and real (not ,type)) ,type))
1281 '(complex (truly-the ,type (coerce r ',type)) i))
1283 #!-complex-float-vops
1284 (deftransform %negate ((z) ((complex ,type)) *)
1285 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1286 ;; complex addition and subtraction
1287 #!-complex-float-vops
1288 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1289 '(complex (+ (realpart w) (realpart z))
1290 (+ (imagpart w) (imagpart z))))
1291 #!-complex-float-vops
1292 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1293 '(complex (- (realpart w) (realpart z))
1294 (- (imagpart w) (imagpart z))))
1295 ;; Add and subtract a complex and a real.
1296 #!-complex-float-vops
1297 (deftransform + ((w z) ((complex ,type) real) *)
1298 `(complex (+ (realpart w) z)
1299 (+ (imagpart w) ,(coerce 0 ',type))))
1300 #!-complex-float-vops
1301 (deftransform + ((z w) (real (complex ,type)) *)
1302 `(complex (+ (realpart w) z)
1303 (+ (imagpart w) ,(coerce 0 ',type))))
1304 ;; Add and subtract a real and a complex number.
1305 #!-complex-float-vops
1306 (deftransform - ((w z) ((complex ,type) real) *)
1307 `(complex (- (realpart w) z)
1308 (- (imagpart w) ,(coerce 0 ',type))))
1309 #!-complex-float-vops
1310 (deftransform - ((z w) (real (complex ,type)) *)
1311 `(complex (- z (realpart w))
1312 (- ,(coerce 0 ',type) (imagpart w))))
1313 ;; Multiply and divide two complex numbers.
1314 #!-complex-float-vops
1315 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1316 '(let* ((rx (realpart x))
1320 (complex (- (* rx ry) (* ix iy))
1321 (+ (* rx iy) (* ix ry)))))
1322 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1323 #!-complex-float-vops
1324 '(let* ((rx (realpart x))
1328 (if (> (abs ry) (abs iy))
1329 (let* ((r (/ iy ry))
1330 (dn (+ ry (* r iy))))
1331 (complex (/ (+ rx (* ix r)) dn)
1332 (/ (- ix (* rx r)) dn)))
1333 (let* ((r (/ ry iy))
1334 (dn (+ iy (* r ry))))
1335 (complex (/ (+ (* rx r) ix) dn)
1336 (/ (- (* ix r) rx) dn)))))
1337 #!+complex-float-vops
1338 `(let* ((cs (conjugate (sb!vm::swap-complex x)))
1341 (if (> (abs ry) (abs iy))
1342 (let* ((r (/ iy ry))
1343 (dn (+ ry (* r iy))))
1344 (/ (+ x (* cs r)) dn))
1345 (let* ((r (/ ry iy))
1346 (dn (+ iy (* r ry))))
1347 (/ (+ (* x r) cs) dn)))))
1348 ;; Multiply a complex by a real or vice versa.
1349 #!-complex-float-vops
1350 (deftransform * ((w z) ((complex ,type) real) *)
1351 '(complex (* (realpart w) z) (* (imagpart w) z)))
1352 #!-complex-float-vops
1353 (deftransform * ((z w) (real (complex ,type)) *)
1354 '(complex (* (realpart w) z) (* (imagpart w) z)))
1355 ;; Divide a complex by a real or vice versa.
1356 #!-complex-float-vops
1357 (deftransform / ((w z) ((complex ,type) real) *)
1358 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1359 (deftransform / ((x y) (,type (complex ,type)) *)
1360 #!-complex-float-vops
1361 '(let* ((ry (realpart y))
1363 (if (> (abs ry) (abs iy))
1364 (let* ((r (/ iy ry))
1365 (dn (+ ry (* r iy))))
1367 (/ (- (* x r)) dn)))
1368 (let* ((r (/ ry iy))
1369 (dn (+ iy (* r ry))))
1370 (complex (/ (* x r) dn)
1372 #!+complex-float-vops
1373 '(let* ((ry (realpart y))
1375 (if (> (abs ry) (abs iy))
1376 (let* ((r (/ iy ry))
1377 (dn (+ ry (* r iy))))
1378 (/ (complex x (- (* x r))) dn))
1379 (let* ((r (/ ry iy))
1380 (dn (+ iy (* r ry))))
1381 (/ (complex (* x r) (- x)) dn)))))
1382 ;; conjugate of complex number
1383 #!-complex-float-vops
1384 (deftransform conjugate ((z) ((complex ,type)) *)
1385 '(complex (realpart z) (- (imagpart z))))
1387 (deftransform cis ((z) ((,type)) *)
1388 '(complex (cos z) (sin z)))
1390 #!-complex-float-vops
1391 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1392 '(and (= (realpart w) (realpart z))
1393 (= (imagpart w) (imagpart z))))
1394 #!-complex-float-vops
1395 (deftransform = ((w z) ((complex ,type) real) *)
1396 '(and (= (realpart w) z) (zerop (imagpart w))))
1397 #!-complex-float-vops
1398 (deftransform = ((w z) (real (complex ,type)) *)
1399 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1402 (frob double-float))
1404 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1405 ;;; produce a minimal range for the result; the result is the widest
1406 ;;; possible answer. This gets around the problem of doing range
1407 ;;; reduction correctly but still provides useful results when the
1408 ;;; inputs are union types.
1409 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1411 (defun trig-derive-type-aux (arg domain fun
1412 &optional def-lo def-hi (increasingp t))
1415 (cond ((eq (numeric-type-complexp arg) :complex)
1416 (make-numeric-type :class (numeric-type-class arg)
1417 :format (numeric-type-format arg)
1418 :complexp :complex))
1419 ((numeric-type-real-p arg)
1420 (let* ((format (case (numeric-type-class arg)
1421 ((integer rational) 'single-float)
1422 (t (numeric-type-format arg))))
1423 (bound-type (or format 'float)))
1424 ;; If the argument is a subset of the "principal" domain
1425 ;; of the function, we can compute the bounds because
1426 ;; the function is monotonic. We can't do this in
1427 ;; general for these periodic functions because we can't
1428 ;; (and don't want to) do the argument reduction in
1429 ;; exactly the same way as the functions themselves do
1431 (if (csubtypep arg domain)
1432 (let ((res-lo (bound-func fun (numeric-type-low arg)))
1433 (res-hi (bound-func fun (numeric-type-high arg))))
1435 (rotatef res-lo res-hi))
1439 :low (coerce-numeric-bound res-lo bound-type)
1440 :high (coerce-numeric-bound res-hi bound-type)))
1444 :low (and def-lo (coerce def-lo bound-type))
1445 :high (and def-hi (coerce def-hi bound-type))))))
1447 (float-or-complex-float-type arg def-lo def-hi))))))
1449 (defoptimizer (sin derive-type) ((num))
1450 (one-arg-derive-type
1453 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1454 (trig-derive-type-aux
1456 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1461 (defoptimizer (cos derive-type) ((num))
1462 (one-arg-derive-type
1465 ;; Derive the bounds if the arg is in [0, pi].
1466 (trig-derive-type-aux arg
1467 (specifier-type `(float 0d0 ,pi))
1473 (defoptimizer (tan derive-type) ((num))
1474 (one-arg-derive-type
1477 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1478 (trig-derive-type-aux arg
1479 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1484 (defoptimizer (conjugate derive-type) ((num))
1485 (one-arg-derive-type num
1487 (flet ((most-negative-bound (l h)
1489 (if (< (type-bound-number l) (- (type-bound-number h)))
1491 (set-bound (- (type-bound-number h)) (consp h)))))
1492 (most-positive-bound (l h)
1494 (if (> (type-bound-number h) (- (type-bound-number l)))
1496 (set-bound (- (type-bound-number l)) (consp l))))))
1497 (if (numeric-type-real-p arg)
1499 (let ((low (numeric-type-low arg))
1500 (high (numeric-type-high arg)))
1501 (let ((new-low (most-negative-bound low high))
1502 (new-high (most-positive-bound low high)))
1503 (modified-numeric-type arg :low new-low :high new-high))))))
1506 (defoptimizer (cis derive-type) ((num))
1507 (one-arg-derive-type num
1509 (sb!c::specifier-type
1510 `(complex ,(or (numeric-type-format arg) 'float))))
1515 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1517 (macrolet ((define-frobs (fun ufun)
1519 (defknown ,ufun (real) integer (movable foldable flushable))
1520 (deftransform ,fun ((x &optional by)
1522 (constant-arg (member 1))))
1523 '(let ((res (,ufun x)))
1524 (values res (- x res)))))))
1525 (define-frobs truncate %unary-truncate)
1526 (define-frobs round %unary-round))
1528 (deftransform %unary-truncate ((x) (single-float))
1529 `(%unary-truncate/single-float x))
1530 (deftransform %unary-truncate ((x) (double-float))
1531 `(%unary-truncate/double-float x))
1533 ;;; Convert (TRUNCATE x y) to the obvious implementation.
1535 ;;; ...plus hair: Insert explicit coercions to appropriate float types: Python
1536 ;;; is reluctant it generate explicit integer->float coercions due to
1537 ;;; precision issues (see SAFE-SINGLE-COERCION-P &co), but this is not an
1538 ;;; issue here as there is no DERIVE-TYPE optimizer on specialized versions of
1539 ;;; %UNARY-TRUNCATE, so the derived type of TRUNCATE remains the best we can
1540 ;;; do here -- which is fine. Also take care not to add unnecassary division
1541 ;;; or multiplication by 1, since we are not able to always eliminate them,
1542 ;;; depending on FLOAT-ACCURACY. Finally, leave out the secondary value when
1543 ;;; we know it is unused: COERCE is not flushable.
1544 (macrolet ((def (type other-float-arg-types)
1545 (let ((unary (symbolicate "%UNARY-TRUNCATE/" type))
1546 (coerce (symbolicate "%" type)))
1547 `(deftransform truncate ((x &optional y)
1549 &optional (or ,type ,@other-float-arg-types integer))
1551 (let* ((result-type (and result
1552 (lvar-derived-type result)))
1553 (compute-all (and (values-type-p result-type)
1554 (not (type-single-value-p result-type)))))
1556 (and (constant-lvar-p y) (= 1 (lvar-value y))))
1558 `(let ((res (,',unary x)))
1559 (values res (- x (,',coerce res))))
1560 `(let ((res (,',unary x)))
1561 ;; Dummy secondary value!
1564 `(let* ((f (,',coerce y))
1565 (res (,',unary (/ x f))))
1566 (values res (- x (* f (,',coerce res)))))
1567 `(let* ((f (,',coerce y))
1568 (res (,',unary (/ x f))))
1569 ;; Dummy secondary value!
1570 (values res x)))))))))
1571 (def single-float ())
1572 (def double-float (single-float)))
1574 (deftransform floor ((number &optional divisor)
1575 (float &optional (or integer float)))
1576 (let ((defaulted-divisor (if divisor 'divisor 1)))
1577 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1578 (if (and (not (zerop rem))
1579 (if (minusp ,defaulted-divisor)
1582 (values (1- tru) (+ rem ,defaulted-divisor))
1583 (values tru rem)))))
1585 (deftransform ceiling ((number &optional divisor)
1586 (float &optional (or integer float)))
1587 (let ((defaulted-divisor (if divisor 'divisor 1)))
1588 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1589 (if (and (not (zerop rem))
1590 (if (minusp ,defaulted-divisor)
1593 (values (1+ tru) (- rem ,defaulted-divisor))
1594 (values tru rem)))))
1596 (defknown %unary-ftruncate (real) float (movable foldable flushable))
1597 (defknown %unary-ftruncate/single (single-float) single-float
1598 (movable foldable flushable))
1599 (defknown %unary-ftruncate/double (double-float) double-float
1600 (movable foldable flushable))
1602 (defun %unary-ftruncate/single (x)
1603 (declare (type single-float x))
1604 (declare (optimize speed (safety 0)))
1605 (let* ((bits (single-float-bits x))
1606 (exp (ldb sb!vm:single-float-exponent-byte bits))
1607 (biased (the single-float-exponent
1608 (- exp sb!vm:single-float-bias))))
1609 (declare (type (signed-byte 32) bits))
1611 ((= exp sb!vm:single-float-normal-exponent-max) x)
1612 ((<= biased 0) (* x 0f0))
1613 ((>= biased (float-digits x)) x)
1615 (let ((frac-bits (- (float-digits x) biased)))
1616 (setf bits (logandc2 bits (- (ash 1 frac-bits) 1)))
1617 (make-single-float bits))))))
1619 (defun %unary-ftruncate/double (x)
1620 (declare (type double-float x))
1621 (declare (optimize speed (safety 0)))
1622 (let* ((high (double-float-high-bits x))
1623 (low (double-float-low-bits x))
1624 (exp (ldb sb!vm:double-float-exponent-byte high))
1625 (biased (the double-float-exponent
1626 (- exp sb!vm:double-float-bias))))
1627 (declare (type (signed-byte 32) high)
1628 (type (unsigned-byte 32) low))
1630 ((= exp sb!vm:double-float-normal-exponent-max) x)
1631 ((<= biased 0) (* x 0d0))
1632 ((>= biased (float-digits x)) x)
1634 (let ((frac-bits (- (float-digits x) biased)))
1635 (cond ((< frac-bits 32)
1636 (setf low (logandc2 low (- (ash 1 frac-bits) 1))))
1639 (setf high (logandc2 high (- (ash 1 (- frac-bits 32)) 1)))))
1640 (make-double-float high low))))))
1643 ((def (float-type fun)
1644 `(deftransform %unary-ftruncate ((x) (,float-type))
1646 (def single-float %unary-ftruncate/single)
1647 (def double-float %unary-ftruncate/double))