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 '(rem (random-chunk (or state *random-state*)) num))))
104 (defknown make-single-float ((signed-byte 32)) single-float
105 (movable foldable flushable))
107 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
108 (movable foldable flushable))
110 (defknown single-float-bits (single-float) (signed-byte 32)
111 (movable foldable flushable))
113 (defknown double-float-high-bits (double-float) (signed-byte 32)
114 (movable foldable flushable))
116 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
117 (movable foldable flushable))
119 (deftransform float-sign ((float &optional float2)
120 (single-float &optional single-float) *)
122 (let ((temp (gensym)))
123 `(let ((,temp (abs float2)))
124 (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
125 '(if (minusp (single-float-bits float)) -1f0 1f0)))
127 (deftransform float-sign ((float &optional float2)
128 (double-float &optional double-float) *)
130 (let ((temp (gensym)))
131 `(let ((,temp (abs float2)))
132 (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
133 '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
135 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
137 (defknown decode-single-float (single-float)
138 (values single-float single-float-exponent (single-float -1f0 1f0))
139 (movable foldable flushable))
141 (defknown decode-double-float (double-float)
142 (values double-float double-float-exponent (double-float -1d0 1d0))
143 (movable foldable flushable))
145 (defknown integer-decode-single-float (single-float)
146 (values single-float-significand single-float-int-exponent (integer -1 1))
147 (movable foldable flushable))
149 (defknown integer-decode-double-float (double-float)
150 (values double-float-significand double-float-int-exponent (integer -1 1))
151 (movable foldable flushable))
153 (defknown scale-single-float (single-float integer) single-float
154 (movable foldable flushable))
156 (defknown scale-double-float (double-float integer) double-float
157 (movable foldable flushable))
159 (deftransform decode-float ((x) (single-float) *)
160 '(decode-single-float x))
162 (deftransform decode-float ((x) (double-float) *)
163 '(decode-double-float x))
165 (deftransform integer-decode-float ((x) (single-float) *)
166 '(integer-decode-single-float x))
168 (deftransform integer-decode-float ((x) (double-float) *)
169 '(integer-decode-double-float x))
171 (deftransform scale-float ((f ex) (single-float *) *)
172 (if (and #!+x86 t #!-x86 nil
173 (csubtypep (lvar-type ex)
174 (specifier-type '(signed-byte 32))))
175 '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
176 '(scale-single-float f ex)))
178 (deftransform scale-float ((f ex) (double-float *) *)
179 (if (and #!+x86 t #!-x86 nil
180 (csubtypep (lvar-type ex)
181 (specifier-type '(signed-byte 32))))
183 '(scale-double-float f ex)))
185 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
187 ;;; SBCL's own implementation of floating point supports floating
188 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
189 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
190 ;;; floating point support. Thus, we have to avoid running it on the
191 ;;; cross-compilation host, since we're not guaranteed that the
192 ;;; cross-compilation host will support floating point infinities.
194 ;;; If we wanted to live dangerously, we could conditionalize the code
195 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
196 ;;; host happened to be SBCL, we'd be able to run the infinity-using
198 ;;; * SBCL itself gets built with more complete optimization.
200 ;;; * You get a different SBCL depending on what your cross-compilation
202 ;;; So far the pros and cons seem seem to be mostly academic, since
203 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
204 ;;; actually important in compiling SBCL itself. If this changes, then
205 ;;; we have to decide:
206 ;;; * Go for simplicity, leaving things as they are.
207 ;;; * Go for performance at the expense of conceptual clarity,
208 ;;; using #+(OR SBCL SB-XC) and otherwise leaving the build
210 ;;; * Go for performance at the expense of build time, using
211 ;;; #+(OR SBCL SB-XC) and also making SBCL do not just
212 ;;; make-host-1.sh and make-host-2.sh, but a third step
213 ;;; make-host-3.sh where it builds itself under itself. (Such a
214 ;;; 3-step build process could also help with other things, e.g.
215 ;;; using specialized arrays to represent debug information.)
216 ;;; * Rewrite the code so that it doesn't depend on unportable
217 ;;; floating point infinities.
219 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
220 ;;; are computed for the result, if possible.
221 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
224 (defun scale-float-derive-type-aux (f ex same-arg)
225 (declare (ignore same-arg))
226 (flet ((scale-bound (x n)
227 ;; We need to be a bit careful here and catch any overflows
228 ;; that might occur. We can ignore underflows which become
232 (scale-float (type-bound-number x) n)
233 (floating-point-overflow ()
236 (when (and (numeric-type-p f) (numeric-type-p ex))
237 (let ((f-lo (numeric-type-low f))
238 (f-hi (numeric-type-high f))
239 (ex-lo (numeric-type-low ex))
240 (ex-hi (numeric-type-high ex))
244 (if (< (float-sign (type-bound-number f-hi)) 0.0)
246 (setf new-hi (scale-bound f-hi ex-lo)))
248 (setf new-hi (scale-bound f-hi ex-hi)))))
250 (if (< (float-sign (type-bound-number f-lo)) 0.0)
252 (setf new-lo (scale-bound f-lo ex-hi)))
254 (setf new-lo (scale-bound f-lo ex-lo)))))
255 (make-numeric-type :class (numeric-type-class f)
256 :format (numeric-type-format f)
260 (defoptimizer (scale-single-float derive-type) ((f ex))
261 (two-arg-derive-type f ex #'scale-float-derive-type-aux
262 #'scale-single-float t))
263 (defoptimizer (scale-double-float derive-type) ((f ex))
264 (two-arg-derive-type f ex #'scale-float-derive-type-aux
265 #'scale-double-float t))
267 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
268 ;;; FLOAT function return the correct ranges if the input has some
269 ;;; defined range. Quite useful if we want to convert some type of
270 ;;; bounded integer into a float.
272 ((frob (fun type most-negative most-positive)
273 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
275 (defun ,aux-name (num)
276 ;; When converting a number to a float, the limits are
278 (let* ((lo (bound-func (lambda (x)
279 (if (< x ,most-negative)
282 (numeric-type-low num)))
283 (hi (bound-func (lambda (x)
284 (if (< ,most-positive x )
287 (numeric-type-high num))))
288 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
290 (defoptimizer (,fun derive-type) ((num))
292 (one-arg-derive-type num #',aux-name #',fun)
295 (frob %single-float single-float
296 most-negative-single-float most-positive-single-float)
297 (frob %double-float double-float
298 most-negative-double-float most-positive-double-float))
303 (defun safe-ctype-for-single-coercion-p (x)
304 ;; See comment in SAFE-SINGLE-COERCION-P -- this deals with the same
305 ;; problem, but in the context of evaluated and compiled (+ <int> <single>)
306 ;; giving different result if we fail to check for this.
307 (or (not (csubtypep x (specifier-type 'integer)))
308 (csubtypep x (specifier-type `(integer ,most-negative-exactly-single-float-fixnum
309 ,most-positive-exactly-single-float-fixnum)))))
311 ;;; Do some stuff to recognize when the loser is doing mixed float and
312 ;;; rational arithmetic, or different float types, and fix it up. If
313 ;;; we don't, he won't even get so much as an efficiency note.
314 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
315 (if (or (not (types-equal-or-intersect (lvar-type y) (specifier-type 'single-float)))
316 (safe-ctype-for-single-coercion-p (lvar-type x)))
317 `(,(lvar-fun-name (basic-combination-fun node))
319 (give-up-ir1-transform)))
320 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
321 (if (or (not (types-equal-or-intersect (lvar-type x) (specifier-type 'single-float)))
322 (safe-ctype-for-single-coercion-p (lvar-type y)))
323 `(,(lvar-fun-name (basic-combination-fun node))
325 (give-up-ir1-transform)))
327 (dolist (x '(+ * / -))
328 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
329 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
331 (dolist (x '(= < > + * / -))
332 (%deftransform x '(function (single-float double-float) *)
333 #'float-contagion-arg1)
334 (%deftransform x '(function (double-float single-float) *)
335 #'float-contagion-arg2))
337 (macrolet ((def (type &rest args)
338 `(deftransform * ((x y) (,type (constant-arg (member ,@args))) *
340 :policy (zerop float-accuracy))
341 "optimize multiplication by one"
342 (let ((y (lvar-value y)))
346 (def single-float 1.0 -1.0)
347 (def double-float 1.0d0 -1.0d0))
349 ;;; Return the reciprocal of X if it can be represented exactly, NIL otherwise.
350 (defun maybe-exact-reciprocal (x)
353 (multiple-value-bind (significand exponent sign)
354 (integer-decode-float x)
355 ;; only powers of 2 can be inverted exactly
356 (unless (zerop (logand significand (1- significand)))
357 (return-from maybe-exact-reciprocal nil))
358 (let ((expected (/ sign significand (expt 2 exponent)))
360 (multiple-value-bind (significand exponent sign)
361 (integer-decode-float reciprocal)
362 ;; Denorms can't be inverted safely.
363 (and (eql expected (* sign significand (expt 2 exponent)))
365 (error () (return-from maybe-exact-reciprocal nil)))))
367 ;;; Replace constant division by multiplication with exact reciprocal,
369 (macrolet ((def (type)
370 `(deftransform / ((x y) (,type (constant-arg ,type)) *
372 "convert to multiplication by reciprocal"
373 (let ((n (lvar-value y)))
374 (if (policy node (zerop float-accuracy))
376 (let ((r (maybe-exact-reciprocal n)))
379 (give-up-ir1-transform
380 "~S does not have an exact reciprocal"
385 ;;; Optimize addition and subtraction of zero
386 (macrolet ((def (op type &rest args)
387 `(deftransform ,op ((x y) (,type (constant-arg (member ,@args))) *
389 :policy (zerop float-accuracy))
391 ;; No signed zeros, thanks.
392 (def + single-float 0 0.0)
393 (def - single-float 0 0.0)
394 (def + double-float 0 0.0 0.0d0)
395 (def - double-float 0 0.0 0.0d0))
397 ;;; On most platforms (+ x x) is faster than (* x 2)
398 (macrolet ((def (type &rest args)
399 `(deftransform * ((x y) (,type (constant-arg (member ,@args))))
401 (def single-float 2 2.0)
402 (def double-float 2 2.0 2.0d0))
404 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
405 ;;; general float rational args to comparison, since Common Lisp
406 ;;; semantics says we are supposed to compare as rationals, but we can
407 ;;; do it for any rational that has a precise representation as a
408 ;;; float (such as 0).
409 (macrolet ((frob (op)
410 `(deftransform ,op ((x y) (float rational) *)
411 "open-code FLOAT to RATIONAL comparison"
412 (unless (constant-lvar-p y)
413 (give-up-ir1-transform
414 "The RATIONAL value isn't known at compile time."))
415 (let ((val (lvar-value y)))
416 (unless (eql (rational (float val)) val)
417 (give-up-ir1-transform
418 "~S doesn't have a precise float representation."
420 `(,',op x (float y x)))))
425 ;;;; irrational derive-type methods
427 ;;; Derive the result to be float for argument types in the
428 ;;; appropriate domain.
429 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
430 (dolist (stuff '((asin (real -1.0 1.0))
431 (acos (real -1.0 1.0))
433 (atanh (real -1.0 1.0))
435 (destructuring-bind (name type) stuff
436 (let ((type (specifier-type type)))
437 (setf (fun-info-derive-type (fun-info-or-lose name))
439 (declare (type combination call))
440 (when (csubtypep (lvar-type
441 (first (combination-args call)))
443 (specifier-type 'float)))))))
445 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
446 (defoptimizer (log derive-type) ((x &optional y))
447 (when (and (csubtypep (lvar-type x)
448 (specifier-type '(real 0.0)))
450 (csubtypep (lvar-type y)
451 (specifier-type '(real 0.0)))))
452 (specifier-type 'float)))
454 ;;;; irrational transforms
456 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
457 (double-float) double-float
458 (movable foldable flushable))
460 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
461 (double-float) (double-float -1.0d0 1.0d0)
462 (movable foldable flushable))
464 (defknown (%asin %atan)
466 (double-float #.(coerce (- (/ pi 2)) 'double-float)
467 #.(coerce (/ pi 2) 'double-float))
468 (movable foldable flushable))
471 (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
472 (movable foldable flushable))
475 (double-float) (double-float 1.0d0)
476 (movable foldable flushable))
478 (defknown (%acosh %exp %sqrt)
479 (double-float) (double-float 0.0d0)
480 (movable foldable flushable))
483 (double-float) (double-float -1d0)
484 (movable foldable flushable))
487 (double-float double-float) (double-float 0d0)
488 (movable foldable flushable))
491 (double-float double-float) double-float
492 (movable foldable flushable))
495 (double-float double-float)
496 (double-float #.(coerce (- pi) 'double-float)
497 #.(coerce pi 'double-float))
498 (movable foldable flushable))
501 (double-float double-float) double-float
502 (movable foldable flushable))
505 (double-float (signed-byte 32)) double-float
506 (movable foldable flushable))
509 (double-float) double-float
510 (movable foldable flushable))
512 (macrolet ((def (name prim rtype)
514 (deftransform ,name ((x) (single-float) ,rtype)
515 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
516 (deftransform ,name ((x) (double-float) ,rtype)
520 (def sqrt %sqrt float)
521 (def asin %asin float)
522 (def acos %acos float)
528 (def acosh %acosh float)
529 (def atanh %atanh float))
531 ;;; The argument range is limited on the x86 FP trig. functions. A
532 ;;; post-test can detect a failure (and load a suitable result), but
533 ;;; this test is avoided if possible.
534 (macrolet ((def (name prim prim-quick)
535 (declare (ignorable prim-quick))
537 (deftransform ,name ((x) (single-float) *)
538 #!+x86 (cond ((csubtypep (lvar-type x)
539 (specifier-type '(single-float
540 (#.(- (expt 2f0 63)))
542 `(coerce (,',prim-quick (coerce x 'double-float))
546 "unable to avoid inline argument range check~@
547 because the argument range (~S) was not within 2^63"
548 (type-specifier (lvar-type x)))
549 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
550 #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
551 (deftransform ,name ((x) (double-float) *)
552 #!+x86 (cond ((csubtypep (lvar-type x)
553 (specifier-type '(double-float
554 (#.(- (expt 2d0 63)))
559 "unable to avoid inline argument range check~@
560 because the argument range (~S) was not within 2^63"
561 (type-specifier (lvar-type x)))
563 #!-x86 `(,',prim x)))))
564 (def sin %sin %sin-quick)
565 (def cos %cos %cos-quick)
566 (def tan %tan %tan-quick))
568 (deftransform atan ((x y) (single-float single-float) *)
569 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
571 (deftransform atan ((x y) (double-float double-float) *)
574 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
575 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
577 (deftransform expt ((x y) ((double-float 0d0) double-float) *)
579 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
580 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
582 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) *)
583 `(%pow x (coerce y 'double-float)))
585 ;;; ANSI says log with base zero returns zero.
586 (deftransform log ((x y) (float float) float)
587 '(if (zerop y) y (/ (log x) (log y))))
589 ;;; Handle some simple transformations.
591 (deftransform abs ((x) ((complex double-float)) double-float)
592 '(%hypot (realpart x) (imagpart x)))
594 (deftransform abs ((x) ((complex single-float)) single-float)
595 '(coerce (%hypot (coerce (realpart x) 'double-float)
596 (coerce (imagpart x) 'double-float))
599 (deftransform phase ((x) ((complex double-float)) double-float)
600 '(%atan2 (imagpart x) (realpart x)))
602 (deftransform phase ((x) ((complex single-float)) single-float)
603 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
604 (coerce (realpart x) 'double-float))
607 (deftransform phase ((x) ((float)) float)
608 '(if (minusp (float-sign x))
612 ;;; The number is of type REAL.
613 (defun numeric-type-real-p (type)
614 (and (numeric-type-p type)
615 (eq (numeric-type-complexp type) :real)))
617 ;;; Coerce a numeric type bound to the given type while handling
618 ;;; exclusive bounds.
619 (defun coerce-numeric-bound (bound type)
622 (list (coerce (car bound) type))
623 (coerce bound type))))
625 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
628 ;;;; optimizers for elementary functions
630 ;;;; These optimizers compute the output range of the elementary
631 ;;;; function, based on the domain of the input.
633 ;;; Generate a specifier for a complex type specialized to the same
634 ;;; type as the argument.
635 (defun complex-float-type (arg)
636 (declare (type numeric-type arg))
637 (let* ((format (case (numeric-type-class arg)
638 ((integer rational) 'single-float)
639 (t (numeric-type-format arg))))
640 (float-type (or format 'float)))
641 (specifier-type `(complex ,float-type))))
643 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
644 ;;; should be the right kind of float. Allow bounds for the float
646 (defun float-or-complex-float-type (arg &optional lo hi)
647 (declare (type numeric-type arg))
648 (let* ((format (case (numeric-type-class arg)
649 ((integer rational) 'single-float)
650 (t (numeric-type-format arg))))
651 (float-type (or format 'float))
652 (lo (coerce-numeric-bound lo float-type))
653 (hi (coerce-numeric-bound hi float-type)))
654 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
655 (complex ,float-type)))))
659 (eval-when (:compile-toplevel :execute)
660 ;; So the problem with this hack is that it's actually broken. If
661 ;; the host does not have long floats, then setting *R-D-F-F* to
662 ;; LONG-FLOAT doesn't actually buy us anything. FIXME.
663 (setf *read-default-float-format*
664 #!+long-float 'long-float #!-long-float 'double-float))
665 ;;; Test whether the numeric-type ARG is within in domain specified by
666 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
668 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
669 (defun domain-subtypep (arg domain-low domain-high)
670 (declare (type numeric-type arg)
671 (type (or real null) domain-low domain-high))
672 (let* ((arg-lo (numeric-type-low arg))
673 (arg-lo-val (type-bound-number arg-lo))
674 (arg-hi (numeric-type-high arg))
675 (arg-hi-val (type-bound-number arg-hi)))
676 ;; Check that the ARG bounds are correctly canonicalized.
677 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
678 (minusp (float-sign arg-lo-val)))
679 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-lo)
680 (setq arg-lo 0e0 arg-lo-val arg-lo))
681 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
682 (plusp (float-sign arg-hi-val)))
683 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-hi)
684 (setq arg-hi (ecase *read-default-float-format*
685 (double-float (load-time-value (make-unportable-float :double-float-negative-zero)))
687 (long-float (load-time-value (make-unportable-float :long-float-negative-zero))))
689 (flet ((fp-neg-zero-p (f) ; Is F -0.0?
690 (and (floatp f) (zerop f) (minusp (float-sign f))))
691 (fp-pos-zero-p (f) ; Is F +0.0?
692 (and (floatp f) (zerop f) (plusp (float-sign f)))))
693 (and (or (null domain-low)
694 (and arg-lo (>= arg-lo-val domain-low)
695 (not (and (fp-pos-zero-p domain-low)
696 (fp-neg-zero-p arg-lo)))))
697 (or (null domain-high)
698 (and arg-hi (<= arg-hi-val domain-high)
699 (not (and (fp-neg-zero-p domain-high)
700 (fp-pos-zero-p arg-hi)))))))))
701 (eval-when (:compile-toplevel :execute)
702 (setf *read-default-float-format* 'single-float))
704 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
707 ;;; Handle monotonic functions of a single variable whose domain is
708 ;;; possibly part of the real line. ARG is the variable, FUN is the
709 ;;; function, and DOMAIN is a specifier that gives the (real) domain
710 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
711 ;;; bounds directly. Otherwise, we compute the bounds for the
712 ;;; intersection between ARG and DOMAIN, and then append a complex
713 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
715 ;;; Negative and positive zero are considered distinct within
716 ;;; DOMAIN-LOW and DOMAIN-HIGH.
718 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
719 ;;; can't compute the bounds using FUN.
720 (defun elfun-derive-type-simple (arg fun domain-low domain-high
721 default-low default-high
722 &optional (increasingp t))
723 (declare (type (or null real) domain-low domain-high))
726 (cond ((eq (numeric-type-complexp arg) :complex)
727 (complex-float-type arg))
728 ((numeric-type-real-p arg)
729 ;; The argument is real, so let's find the intersection
730 ;; between the argument and the domain of the function.
731 ;; We compute the bounds on the intersection, and for
732 ;; everything else, we return a complex number of the
734 (multiple-value-bind (intersection difference)
735 (interval-intersection/difference (numeric-type->interval arg)
741 ;; Process the intersection.
742 (let* ((low (interval-low intersection))
743 (high (interval-high intersection))
744 (res-lo (or (bound-func fun (if increasingp low high))
746 (res-hi (or (bound-func fun (if increasingp high low))
748 (format (case (numeric-type-class arg)
749 ((integer rational) 'single-float)
750 (t (numeric-type-format arg))))
751 (bound-type (or format 'float))
756 :low (coerce-numeric-bound res-lo bound-type)
757 :high (coerce-numeric-bound res-hi bound-type))))
758 ;; If the ARG is a subset of the domain, we don't
759 ;; have to worry about the difference, because that
761 (if (or (null difference)
762 ;; Check whether the arg is within the domain.
763 (domain-subtypep arg domain-low domain-high))
766 (specifier-type `(complex ,bound-type))))))
768 ;; No intersection so the result must be purely complex.
769 (complex-float-type arg)))))
771 (float-or-complex-float-type arg default-low default-high))))))
774 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
775 &key (increasingp t))
776 (let ((num (gensym)))
777 `(defoptimizer (,name derive-type) ((,num))
781 (elfun-derive-type-simple arg #',name
782 ,domain-low ,domain-high
783 ,def-low-bnd ,def-high-bnd
786 ;; These functions are easy because they are defined for the whole
788 (frob exp nil nil 0 nil)
789 (frob sinh nil nil nil nil)
790 (frob tanh nil nil -1 1)
791 (frob asinh nil nil nil nil)
793 ;; These functions are only defined for part of the real line. The
794 ;; condition selects the desired part of the line.
795 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
796 ;; Acos is monotonic decreasing, so we need to swap the function
797 ;; values at the lower and upper bounds of the input domain.
798 (frob acos -1d0 1d0 0 pi :increasingp nil)
799 (frob acosh 1d0 nil nil nil)
800 (frob atanh -1d0 1d0 -1 1)
801 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
803 (frob sqrt (load-time-value (make-unportable-float :double-float-negative-zero)) nil 0 nil))
805 ;;; Compute bounds for (expt x y). This should be easy since (expt x
806 ;;; y) = (exp (* y (log x))). However, computations done this way
807 ;;; have too much roundoff. Thus we have to do it the hard way.
808 (defun safe-expt (x y)
810 (when (< (abs y) 10000)
815 ;;; Handle the case when x >= 1.
816 (defun interval-expt-> (x y)
817 (case (sb!c::interval-range-info y 0d0)
819 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
820 ;; obviously non-negative. We just have to be careful for
821 ;; infinite bounds (given by nil).
822 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
823 (type-bound-number (sb!c::interval-low y))))
824 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
825 (type-bound-number (sb!c::interval-high y)))))
826 (list (sb!c::make-interval :low (or lo 1) :high hi))))
828 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
829 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
831 (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
832 (type-bound-number (sb!c::interval-low y))))
833 (hi (safe-expt (type-bound-number (sb!c::interval-low x))
834 (type-bound-number (sb!c::interval-high y)))))
835 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
837 ;; Split the interval in half.
838 (destructuring-bind (y- y+)
839 (sb!c::interval-split 0 y t)
840 (list (interval-expt-> x y-)
841 (interval-expt-> x y+))))))
843 ;;; Handle the case when x <= 1
844 (defun interval-expt-< (x y)
845 (case (sb!c::interval-range-info x 0d0)
847 ;; The case of 0 <= x <= 1 is easy
848 (case (sb!c::interval-range-info y)
850 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
851 ;; obviously [0, 1]. We just have to be careful for infinite bounds
853 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
854 (type-bound-number (sb!c::interval-high y))))
855 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
856 (type-bound-number (sb!c::interval-low y)))))
857 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
859 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
860 ;; obviously [1, inf].
861 (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
862 (type-bound-number (sb!c::interval-low y))))
863 (lo (safe-expt (type-bound-number (sb!c::interval-high x))
864 (type-bound-number (sb!c::interval-high y)))))
865 (list (sb!c::make-interval :low (or lo 1) :high hi))))
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 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
874 ;; The calling function must insure this! For now we'll just
875 ;; return the appropriate unbounded float type.
876 (list (sb!c::make-interval :low nil :high nil)))
878 (destructuring-bind (neg pos)
879 (interval-split 0 x t t)
880 (list (interval-expt-< neg y)
881 (interval-expt-< pos y))))))
883 ;;; Compute bounds for (expt x y).
884 (defun interval-expt (x y)
885 (case (interval-range-info x 1)
888 (interval-expt-> x y))
891 (interval-expt-< x y))
893 (destructuring-bind (left right)
894 (interval-split 1 x t t)
895 (list (interval-expt left y)
896 (interval-expt right y))))))
898 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
899 (declare (ignore x-int))
900 ;; Figure out what the return type should be, given the argument
901 ;; types and bounds and the result type and bounds.
902 (cond ((csubtypep x-type (specifier-type 'integer))
903 ;; an integer to some power
904 (case (numeric-type-class y-type)
906 ;; Positive integer to an integer power is either an
907 ;; integer or a rational.
908 (let ((lo (or (interval-low bnd) '*))
909 (hi (or (interval-high bnd) '*)))
910 (if (and (interval-low y-int)
911 (>= (type-bound-number (interval-low y-int)) 0))
912 (specifier-type `(integer ,lo ,hi))
913 (specifier-type `(rational ,lo ,hi)))))
915 ;; Positive integer to rational power is either a rational
916 ;; or a single-float.
917 (let* ((lo (interval-low bnd))
918 (hi (interval-high bnd))
920 (floor (type-bound-number lo))
923 (ceiling (type-bound-number hi))
926 (bound-func #'float lo)
929 (bound-func #'float hi)
931 (specifier-type `(or (rational ,int-lo ,int-hi)
932 (single-float ,f-lo, f-hi)))))
934 ;; A positive integer to a float power is a float.
935 (modified-numeric-type y-type
936 :low (interval-low bnd)
937 :high (interval-high bnd)))
939 ;; A positive integer to a number is a number (for now).
940 (specifier-type 'number))))
941 ((csubtypep x-type (specifier-type 'rational))
942 ;; a rational to some power
943 (case (numeric-type-class y-type)
945 ;; A positive rational to an integer power is always a rational.
946 (specifier-type `(rational ,(or (interval-low bnd) '*)
947 ,(or (interval-high bnd) '*))))
949 ;; A positive rational to rational power is either a rational
950 ;; or a single-float.
951 (let* ((lo (interval-low bnd))
952 (hi (interval-high bnd))
954 (floor (type-bound-number lo))
957 (ceiling (type-bound-number hi))
960 (bound-func #'float lo)
963 (bound-func #'float hi)
965 (specifier-type `(or (rational ,int-lo ,int-hi)
966 (single-float ,f-lo, f-hi)))))
968 ;; A positive rational to a float power is a float.
969 (modified-numeric-type y-type
970 :low (interval-low bnd)
971 :high (interval-high bnd)))
973 ;; A positive rational to a number is a number (for now).
974 (specifier-type 'number))))
975 ((csubtypep x-type (specifier-type 'float))
976 ;; a float to some power
977 (case (numeric-type-class y-type)
978 ((or integer rational)
979 ;; A positive float to an integer or rational power is
983 :format (numeric-type-format x-type)
984 :low (interval-low bnd)
985 :high (interval-high bnd)))
987 ;; A positive float to a float power is a float of the
991 :format (float-format-max (numeric-type-format x-type)
992 (numeric-type-format y-type))
993 :low (interval-low bnd)
994 :high (interval-high bnd)))
996 ;; A positive float to a number is a number (for now)
997 (specifier-type 'number))))
999 ;; A number to some power is a number.
1000 (specifier-type 'number))))
1002 (defun merged-interval-expt (x y)
1003 (let* ((x-int (numeric-type->interval x))
1004 (y-int (numeric-type->interval y)))
1005 (mapcar (lambda (type)
1006 (fixup-interval-expt type x-int y-int x y))
1007 (flatten-list (interval-expt x-int y-int)))))
1009 (defun expt-derive-type-aux (x y same-arg)
1010 (declare (ignore same-arg))
1011 (cond ((or (not (numeric-type-real-p x))
1012 (not (numeric-type-real-p y)))
1013 ;; Use numeric contagion if either is not real.
1014 (numeric-contagion x y))
1015 ((csubtypep y (specifier-type 'integer))
1016 ;; A real raised to an integer power is well-defined.
1017 (merged-interval-expt x y))
1018 ;; A real raised to a non-integral power can be a float or a
1020 ((or (csubtypep x (specifier-type '(rational 0)))
1021 (csubtypep x (specifier-type '(float (0d0)))))
1022 ;; But a positive real to any power is well-defined.
1023 (merged-interval-expt x y))
1024 ((and (csubtypep x (specifier-type 'rational))
1025 (csubtypep y (specifier-type 'rational)))
1026 ;; A rational to the power of a rational could be a rational
1027 ;; or a possibly-complex single float
1028 (specifier-type '(or rational single-float (complex single-float))))
1030 ;; a real to some power. The result could be a real or a
1032 (float-or-complex-float-type (numeric-contagion x y)))))
1034 (defoptimizer (expt derive-type) ((x y))
1035 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
1037 ;;; Note we must assume that a type including 0.0 may also include
1038 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
1039 (defun log-derive-type-aux-1 (x)
1040 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
1042 (defun log-derive-type-aux-2 (x y same-arg)
1043 (let ((log-x (log-derive-type-aux-1 x))
1044 (log-y (log-derive-type-aux-1 y))
1045 (accumulated-list nil))
1046 ;; LOG-X or LOG-Y might be union types. We need to run through
1047 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
1048 (dolist (x-type (prepare-arg-for-derive-type log-x))
1049 (dolist (y-type (prepare-arg-for-derive-type log-y))
1050 (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
1051 (apply #'type-union (flatten-list accumulated-list))))
1053 (defoptimizer (log derive-type) ((x &optional y))
1055 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
1056 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
1058 (defun atan-derive-type-aux-1 (y)
1059 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
1061 (defun atan-derive-type-aux-2 (y x same-arg)
1062 (declare (ignore same-arg))
1063 ;; The hard case with two args. We just return the max bounds.
1064 (let ((result-type (numeric-contagion y x)))
1065 (cond ((and (numeric-type-real-p x)
1066 (numeric-type-real-p y))
1067 (let* (;; FIXME: This expression for FORMAT seems to
1068 ;; appear multiple times, and should be factored out.
1069 (format (case (numeric-type-class result-type)
1070 ((integer rational) 'single-float)
1071 (t (numeric-type-format result-type))))
1072 (bound-format (or format 'float)))
1073 (make-numeric-type :class 'float
1076 :low (coerce (- pi) bound-format)
1077 :high (coerce pi bound-format))))
1079 ;; The result is a float or a complex number
1080 (float-or-complex-float-type result-type)))))
1082 (defoptimizer (atan derive-type) ((y &optional x))
1084 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
1085 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
1087 (defun cosh-derive-type-aux (x)
1088 ;; We note that cosh x = cosh |x| for all real x.
1089 (elfun-derive-type-simple
1090 (if (numeric-type-real-p x)
1091 (abs-derive-type-aux x)
1093 #'cosh nil nil 0 nil))
1095 (defoptimizer (cosh derive-type) ((num))
1096 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
1098 (defun phase-derive-type-aux (arg)
1099 (let* ((format (case (numeric-type-class arg)
1100 ((integer rational) 'single-float)
1101 (t (numeric-type-format arg))))
1102 (bound-type (or format 'float)))
1103 (cond ((numeric-type-real-p arg)
1104 (case (interval-range-info (numeric-type->interval arg) 0.0)
1106 ;; The number is positive, so the phase is 0.
1107 (make-numeric-type :class 'float
1110 :low (coerce 0 bound-type)
1111 :high (coerce 0 bound-type)))
1113 ;; The number is always negative, so the phase is pi.
1114 (make-numeric-type :class 'float
1117 :low (coerce pi bound-type)
1118 :high (coerce pi bound-type)))
1120 ;; We can't tell. The result is 0 or pi. Use a union
1123 (make-numeric-type :class 'float
1126 :low (coerce 0 bound-type)
1127 :high (coerce 0 bound-type))
1128 (make-numeric-type :class 'float
1131 :low (coerce pi bound-type)
1132 :high (coerce pi bound-type))))))
1134 ;; We have a complex number. The answer is the range -pi
1135 ;; to pi. (-pi is included because we have -0.)
1136 (make-numeric-type :class 'float
1139 :low (coerce (- pi) bound-type)
1140 :high (coerce pi bound-type))))))
1142 (defoptimizer (phase derive-type) ((num))
1143 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1147 (deftransform realpart ((x) ((complex rational)) *)
1148 '(sb!kernel:%realpart x))
1149 (deftransform imagpart ((x) ((complex rational)) *)
1150 '(sb!kernel:%imagpart x))
1152 ;;; Make REALPART and IMAGPART return the appropriate types. This
1153 ;;; should help a lot in optimized code.
1154 (defun realpart-derive-type-aux (type)
1155 (let ((class (numeric-type-class type))
1156 (format (numeric-type-format type)))
1157 (cond ((numeric-type-real-p type)
1158 ;; The realpart of a real has the same type and range as
1160 (make-numeric-type :class class
1163 :low (numeric-type-low type)
1164 :high (numeric-type-high type)))
1166 ;; We have a complex number. The result has the same type
1167 ;; as the real part, except that it's real, not complex,
1169 (make-numeric-type :class class
1172 :low (numeric-type-low type)
1173 :high (numeric-type-high type))))))
1174 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1175 (defoptimizer (realpart derive-type) ((num))
1176 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1177 (defun imagpart-derive-type-aux (type)
1178 (let ((class (numeric-type-class type))
1179 (format (numeric-type-format type)))
1180 (cond ((numeric-type-real-p type)
1181 ;; The imagpart of a real has the same type as the input,
1182 ;; except that it's zero.
1183 (let ((bound-format (or format class 'real)))
1184 (make-numeric-type :class class
1187 :low (coerce 0 bound-format)
1188 :high (coerce 0 bound-format))))
1190 ;; We have a complex number. The result has the same type as
1191 ;; the imaginary part, except that it's real, not complex,
1193 (make-numeric-type :class class
1196 :low (numeric-type-low type)
1197 :high (numeric-type-high type))))))
1198 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1199 (defoptimizer (imagpart derive-type) ((num))
1200 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1202 (defun complex-derive-type-aux-1 (re-type)
1203 (if (numeric-type-p re-type)
1204 (make-numeric-type :class (numeric-type-class re-type)
1205 :format (numeric-type-format re-type)
1206 :complexp (if (csubtypep re-type
1207 (specifier-type 'rational))
1210 :low (numeric-type-low re-type)
1211 :high (numeric-type-high re-type))
1212 (specifier-type 'complex)))
1214 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1215 (declare (ignore same-arg))
1216 (if (and (numeric-type-p re-type)
1217 (numeric-type-p im-type))
1218 ;; Need to check to make sure numeric-contagion returns the
1219 ;; right type for what we want here.
1221 ;; Also, what about rational canonicalization, like (complex 5 0)
1222 ;; is 5? So, if the result must be complex, we make it so.
1223 ;; If the result might be complex, which happens only if the
1224 ;; arguments are rational, we make it a union type of (or
1225 ;; rational (complex rational)).
1226 (let* ((element-type (numeric-contagion re-type im-type))
1227 (rat-result-p (csubtypep element-type
1228 (specifier-type 'rational))))
1230 (type-union element-type
1232 `(complex ,(numeric-type-class element-type))))
1233 (make-numeric-type :class (numeric-type-class element-type)
1234 :format (numeric-type-format element-type)
1235 :complexp (if rat-result-p
1238 (specifier-type 'complex)))
1240 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1241 (defoptimizer (complex derive-type) ((re &optional im))
1243 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1244 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1246 ;;; Define some transforms for complex operations. We do this in lieu
1247 ;;; of complex operation VOPs.
1248 (macrolet ((frob (type)
1250 (deftransform complex ((r) (,type))
1251 '(complex r ,(coerce 0 type)))
1252 (deftransform complex ((r i) (,type (and real (not ,type))))
1253 '(complex r (truly-the ,type (coerce i ',type))))
1254 (deftransform complex ((r i) ((and real (not ,type)) ,type))
1255 '(complex (truly-the ,type (coerce r ',type)) i))
1257 #!-complex-float-vops
1258 (deftransform %negate ((z) ((complex ,type)) *)
1259 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1260 ;; complex addition and subtraction
1261 #!-complex-float-vops
1262 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1263 '(complex (+ (realpart w) (realpart z))
1264 (+ (imagpart w) (imagpart z))))
1265 #!-complex-float-vops
1266 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1267 '(complex (- (realpart w) (realpart z))
1268 (- (imagpart w) (imagpart z))))
1269 ;; Add and subtract a complex and a real.
1270 #!-complex-float-vops
1271 (deftransform + ((w z) ((complex ,type) real) *)
1272 `(complex (+ (realpart w) z)
1273 (+ (imagpart w) ,(coerce 0 ',type))))
1274 #!-complex-float-vops
1275 (deftransform + ((z w) (real (complex ,type)) *)
1276 `(complex (+ (realpart w) z)
1277 (+ (imagpart w) ,(coerce 0 ',type))))
1278 ;; Add and subtract a real and a complex number.
1279 #!-complex-float-vops
1280 (deftransform - ((w z) ((complex ,type) real) *)
1281 `(complex (- (realpart w) z)
1282 (- (imagpart w) ,(coerce 0 ',type))))
1283 #!-complex-float-vops
1284 (deftransform - ((z w) (real (complex ,type)) *)
1285 `(complex (- z (realpart w))
1286 (- ,(coerce 0 ',type) (imagpart w))))
1287 ;; Multiply and divide two complex numbers.
1288 #!-complex-float-vops
1289 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1290 '(let* ((rx (realpart x))
1294 (complex (- (* rx ry) (* ix iy))
1295 (+ (* rx iy) (* ix ry)))))
1296 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1297 #!-complex-float-vops
1298 '(let* ((rx (realpart x))
1302 (if (> (abs ry) (abs iy))
1303 (let* ((r (/ iy ry))
1304 (dn (+ ry (* r iy))))
1305 (complex (/ (+ rx (* ix r)) dn)
1306 (/ (- ix (* rx r)) dn)))
1307 (let* ((r (/ ry iy))
1308 (dn (+ iy (* r ry))))
1309 (complex (/ (+ (* rx r) ix) dn)
1310 (/ (- (* ix r) rx) dn)))))
1311 #!+complex-float-vops
1312 `(let* ((cs (conjugate (sb!vm::swap-complex x)))
1315 (if (> (abs ry) (abs iy))
1316 (let* ((r (/ iy ry))
1317 (dn (+ ry (* r iy))))
1318 (/ (+ x (* cs r)) dn))
1319 (let* ((r (/ ry iy))
1320 (dn (+ iy (* r ry))))
1321 (/ (+ (* x r) cs) dn)))))
1322 ;; Multiply a complex by a real or vice versa.
1323 #!-complex-float-vops
1324 (deftransform * ((w z) ((complex ,type) real) *)
1325 '(complex (* (realpart w) z) (* (imagpart w) z)))
1326 #!-complex-float-vops
1327 (deftransform * ((z w) (real (complex ,type)) *)
1328 '(complex (* (realpart w) z) (* (imagpart w) z)))
1329 ;; Divide a complex by a real or vice versa.
1330 #!-complex-float-vops
1331 (deftransform / ((w z) ((complex ,type) real) *)
1332 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1333 (deftransform / ((x y) (,type (complex ,type)) *)
1334 #!-complex-float-vops
1335 '(let* ((ry (realpart y))
1337 (if (> (abs ry) (abs iy))
1338 (let* ((r (/ iy ry))
1339 (dn (+ ry (* r iy))))
1341 (/ (- (* x r)) dn)))
1342 (let* ((r (/ ry iy))
1343 (dn (+ iy (* r ry))))
1344 (complex (/ (* x r) dn)
1346 #!+complex-float-vops
1347 '(let* ((ry (realpart y))
1349 (if (> (abs ry) (abs iy))
1350 (let* ((r (/ iy ry))
1351 (dn (+ ry (* r iy))))
1352 (/ (complex x (- (* x r))) dn))
1353 (let* ((r (/ ry iy))
1354 (dn (+ iy (* r ry))))
1355 (/ (complex (* x r) (- x)) dn)))))
1356 ;; conjugate of complex number
1357 #!-complex-float-vops
1358 (deftransform conjugate ((z) ((complex ,type)) *)
1359 '(complex (realpart z) (- (imagpart z))))
1361 (deftransform cis ((z) ((,type)) *)
1362 '(complex (cos z) (sin z)))
1364 #!-complex-float-vops
1365 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1366 '(and (= (realpart w) (realpart z))
1367 (= (imagpart w) (imagpart z))))
1368 #!-complex-float-vops
1369 (deftransform = ((w z) ((complex ,type) real) *)
1370 '(and (= (realpart w) z) (zerop (imagpart w))))
1371 #!-complex-float-vops
1372 (deftransform = ((w z) (real (complex ,type)) *)
1373 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1376 (frob double-float))
1378 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1379 ;;; produce a minimal range for the result; the result is the widest
1380 ;;; possible answer. This gets around the problem of doing range
1381 ;;; reduction correctly but still provides useful results when the
1382 ;;; inputs are union types.
1383 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1385 (defun trig-derive-type-aux (arg domain fun
1386 &optional def-lo def-hi (increasingp t))
1389 (cond ((eq (numeric-type-complexp arg) :complex)
1390 (make-numeric-type :class (numeric-type-class arg)
1391 :format (numeric-type-format arg)
1392 :complexp :complex))
1393 ((numeric-type-real-p arg)
1394 (let* ((format (case (numeric-type-class arg)
1395 ((integer rational) 'single-float)
1396 (t (numeric-type-format arg))))
1397 (bound-type (or format 'float)))
1398 ;; If the argument is a subset of the "principal" domain
1399 ;; of the function, we can compute the bounds because
1400 ;; the function is monotonic. We can't do this in
1401 ;; general for these periodic functions because we can't
1402 ;; (and don't want to) do the argument reduction in
1403 ;; exactly the same way as the functions themselves do
1405 (if (csubtypep arg domain)
1406 (let ((res-lo (bound-func fun (numeric-type-low arg)))
1407 (res-hi (bound-func fun (numeric-type-high arg))))
1409 (rotatef res-lo res-hi))
1413 :low (coerce-numeric-bound res-lo bound-type)
1414 :high (coerce-numeric-bound res-hi bound-type)))
1418 :low (and def-lo (coerce def-lo bound-type))
1419 :high (and def-hi (coerce def-hi bound-type))))))
1421 (float-or-complex-float-type arg def-lo def-hi))))))
1423 (defoptimizer (sin derive-type) ((num))
1424 (one-arg-derive-type
1427 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1428 (trig-derive-type-aux
1430 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1435 (defoptimizer (cos derive-type) ((num))
1436 (one-arg-derive-type
1439 ;; Derive the bounds if the arg is in [0, pi].
1440 (trig-derive-type-aux arg
1441 (specifier-type `(float 0d0 ,pi))
1447 (defoptimizer (tan derive-type) ((num))
1448 (one-arg-derive-type
1451 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1452 (trig-derive-type-aux arg
1453 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1458 (defoptimizer (conjugate derive-type) ((num))
1459 (one-arg-derive-type num
1461 (flet ((most-negative-bound (l h)
1463 (if (< (type-bound-number l) (- (type-bound-number h)))
1465 (set-bound (- (type-bound-number h)) (consp h)))))
1466 (most-positive-bound (l h)
1468 (if (> (type-bound-number h) (- (type-bound-number l)))
1470 (set-bound (- (type-bound-number l)) (consp l))))))
1471 (if (numeric-type-real-p arg)
1473 (let ((low (numeric-type-low arg))
1474 (high (numeric-type-high arg)))
1475 (let ((new-low (most-negative-bound low high))
1476 (new-high (most-positive-bound low high)))
1477 (modified-numeric-type arg :low new-low :high new-high))))))
1480 (defoptimizer (cis derive-type) ((num))
1481 (one-arg-derive-type num
1483 (sb!c::specifier-type
1484 `(complex ,(or (numeric-type-format arg) 'float))))
1489 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1491 (macrolet ((define-frobs (fun ufun)
1493 (defknown ,ufun (real) integer (movable foldable flushable))
1494 (deftransform ,fun ((x &optional by)
1496 (constant-arg (member 1))))
1497 '(let ((res (,ufun x)))
1498 (values res (- x res)))))))
1499 (define-frobs truncate %unary-truncate)
1500 (define-frobs round %unary-round))
1502 (deftransform %unary-truncate ((x) (single-float))
1503 `(%unary-truncate/single-float x))
1504 (deftransform %unary-truncate ((x) (double-float))
1505 `(%unary-truncate/double-float x))
1507 ;;; Convert (TRUNCATE x y) to the obvious implementation.
1509 ;;; ...plus hair: Insert explicit coercions to appropriate float types: Python
1510 ;;; is reluctant it generate explicit integer->float coercions due to
1511 ;;; precision issues (see SAFE-SINGLE-COERCION-P &co), but this is not an
1512 ;;; issue here as there is no DERIVE-TYPE optimizer on specialized versions of
1513 ;;; %UNARY-TRUNCATE, so the derived type of TRUNCATE remains the best we can
1514 ;;; do here -- which is fine. Also take care not to add unnecassary division
1515 ;;; or multiplication by 1, since we are not able to always eliminate them,
1516 ;;; depending on FLOAT-ACCURACY. Finally, leave out the secondary value when
1517 ;;; we know it is unused: COERCE is not flushable.
1518 (macrolet ((def (type other-float-arg-types)
1519 (let ((unary (symbolicate "%UNARY-TRUNCATE/" type))
1520 (coerce (symbolicate "%" type)))
1521 `(deftransform truncate ((x &optional y)
1523 &optional (or ,type ,@other-float-arg-types integer))
1525 (let* ((result-type (and result
1526 (lvar-derived-type result)))
1527 (compute-all (and (values-type-p result-type)
1528 (not (type-single-value-p result-type)))))
1530 (and (constant-lvar-p y) (= 1 (lvar-value y))))
1532 `(let ((res (,',unary x)))
1533 (values res (- x (,',coerce res))))
1534 `(let ((res (,',unary x)))
1535 ;; Dummy secondary value!
1538 `(let* ((f (,',coerce y))
1539 (res (,',unary (/ x f))))
1540 (values res (- x (* f (,',coerce res)))))
1541 `(let* ((f (,',coerce y))
1542 (res (,',unary (/ x f))))
1543 ;; Dummy secondary value!
1544 (values res x)))))))))
1545 (def single-float ())
1546 (def double-float (single-float)))
1548 (deftransform floor ((number &optional divisor)
1549 (float &optional (or integer float)))
1550 (let ((defaulted-divisor (if divisor 'divisor 1)))
1551 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1552 (if (and (not (zerop rem))
1553 (if (minusp ,defaulted-divisor)
1556 (values (1- tru) (+ rem ,defaulted-divisor))
1557 (values tru rem)))))
1559 (deftransform ceiling ((number &optional divisor)
1560 (float &optional (or integer float)))
1561 (let ((defaulted-divisor (if divisor 'divisor 1)))
1562 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1563 (if (and (not (zerop rem))
1564 (if (minusp ,defaulted-divisor)
1567 (values (1+ tru) (- rem ,defaulted-divisor))
1568 (values tru rem)))))
1570 (defknown %unary-ftruncate (real) float (movable foldable flushable))
1571 (defknown %unary-ftruncate/single (single-float) single-float
1572 (movable foldable flushable))
1573 (defknown %unary-ftruncate/double (double-float) double-float
1574 (movable foldable flushable))
1576 (defun %unary-ftruncate/single (x)
1577 (declare (type single-float x))
1578 (declare (optimize speed (safety 0)))
1579 (let* ((bits (single-float-bits x))
1580 (exp (ldb sb!vm:single-float-exponent-byte bits))
1581 (biased (the single-float-exponent
1582 (- exp sb!vm:single-float-bias))))
1583 (declare (type (signed-byte 32) bits))
1585 ((= exp sb!vm:single-float-normal-exponent-max) x)
1586 ((<= biased 0) (* x 0f0))
1587 ((>= biased (float-digits x)) x)
1589 (let ((frac-bits (- (float-digits x) biased)))
1590 (setf bits (logandc2 bits (- (ash 1 frac-bits) 1)))
1591 (make-single-float bits))))))
1593 (defun %unary-ftruncate/double (x)
1594 (declare (type double-float x))
1595 (declare (optimize speed (safety 0)))
1596 (let* ((high (double-float-high-bits x))
1597 (low (double-float-low-bits x))
1598 (exp (ldb sb!vm:double-float-exponent-byte high))
1599 (biased (the double-float-exponent
1600 (- exp sb!vm:double-float-bias))))
1601 (declare (type (signed-byte 32) high)
1602 (type (unsigned-byte 32) low))
1604 ((= exp sb!vm:double-float-normal-exponent-max) x)
1605 ((<= biased 0) (* x 0d0))
1606 ((>= biased (float-digits x)) x)
1608 (let ((frac-bits (- (float-digits x) biased)))
1609 (cond ((< frac-bits 32)
1610 (setf low (logandc2 low (- (ash 1 frac-bits) 1))))
1613 (setf high (logandc2 high (- (ash 1 (- frac-bits 32)) 1)))))
1614 (make-double-float high low))))))
1617 ((def (float-type fun)
1618 `(deftransform %unary-ftruncate ((x) (,float-type))
1620 (def single-float %unary-ftruncate/single)
1621 (def double-float %unary-ftruncate/double))