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 (movable foldable flushable))
19 (defknown %double-float (real) double-float (movable foldable flushable))
21 (deftransform float ((n &optional f) (* &optional single-float) * :when :both)
24 (deftransform float ((n f) (* double-float) * :when :both)
27 (deftransform %single-float ((n) (single-float) * :when :both)
30 (deftransform %double-float ((n) (double-float) * :when :both)
33 ;;; not strictly float functions, but primarily useful on floats:
34 (macrolet ((frob (fun ufun)
36 (defknown ,ufun (real) integer (movable foldable flushable))
37 (deftransform ,fun ((x &optional by)
39 (constant-argument (member 1))))
40 '(let ((res (,ufun x)))
41 (values res (- x res)))))))
42 (frob truncate %unary-truncate)
43 (frob round %unary-round))
46 (macrolet ((frob (fun type)
47 `(deftransform random ((num &optional state)
50 "Use inline float operations."
51 '(,fun num (or state *random-state*)))))
52 (frob %random-single-float single-float)
53 (frob %random-double-float double-float))
55 ;;; Mersenne Twister RNG
57 ;;; FIXME: It's unpleasant to have RANDOM functionality scattered
58 ;;; through the code this way. It would be nice to move this into the
59 ;;; same file as the other RANDOM definitions.
60 (deftransform random ((num &optional state)
61 ((integer 1 #.(expt 2 32)) &optional *))
62 ;; FIXME: I almost conditionalized this as #!+sb-doc. Find some way
63 ;; of automatically finding #!+sb-doc in proximity to DEFTRANSFORM
64 ;; to let me scan for places that I made this mistake and didn't
66 "use inline (unsigned-byte 32) operations"
67 (let ((num-high (numeric-type-high (continuation-type num))))
69 (give-up-ir1-transform))
70 (cond ((constant-continuation-p num)
71 ;; Check the worst case sum absolute error for the random number
73 (let ((rem (rem (expt 2 32) num-high)))
74 (unless (< (/ (* 2 rem (- num-high rem)) num-high (expt 2 32))
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 (expt 2 32))
79 '(random-chunk (or state *random-state*))
80 #!-x86 '(rem (random-chunk (or state *random-state*)) num)
82 ;; Use multiplication, which is faster.
83 '(values (sb!bignum::%multiply
84 (random-chunk (or state *random-state*))
86 ((> num-high random-fixnum-max)
87 (give-up-ir1-transform
88 "The range is too large to ensure an accurate result."))
90 ((< num-high (expt 2 32))
91 '(values (sb!bignum::%multiply (random-chunk (or state
95 '(rem (random-chunk (or state *random-state*)) num)))))
99 (defknown make-single-float ((signed-byte 32)) single-float
100 (movable foldable flushable))
102 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
103 (movable foldable flushable))
105 (defknown single-float-bits (single-float) (signed-byte 32)
106 (movable foldable flushable))
108 (defknown double-float-high-bits (double-float) (signed-byte 32)
109 (movable foldable flushable))
111 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
112 (movable foldable flushable))
114 (deftransform float-sign ((float &optional float2)
115 (single-float &optional single-float) *)
117 (let ((temp (gensym)))
118 `(let ((,temp (abs float2)))
119 (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
120 '(if (minusp (single-float-bits float)) -1f0 1f0)))
122 (deftransform float-sign ((float &optional float2)
123 (double-float &optional double-float) *)
125 (let ((temp (gensym)))
126 `(let ((,temp (abs float2)))
127 (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
128 '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
130 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
132 (defknown decode-single-float (single-float)
133 (values single-float single-float-exponent (single-float -1f0 1f0))
134 (movable foldable flushable))
136 (defknown decode-double-float (double-float)
137 (values double-float double-float-exponent (double-float -1d0 1d0))
138 (movable foldable flushable))
140 (defknown integer-decode-single-float (single-float)
141 (values single-float-significand single-float-int-exponent (integer -1 1))
142 (movable foldable flushable))
144 (defknown integer-decode-double-float (double-float)
145 (values double-float-significand double-float-int-exponent (integer -1 1))
146 (movable foldable flushable))
148 (defknown scale-single-float (single-float fixnum) single-float
149 (movable foldable flushable))
151 (defknown scale-double-float (double-float fixnum) double-float
152 (movable foldable flushable))
154 (deftransform decode-float ((x) (single-float) * :when :both)
155 '(decode-single-float x))
157 (deftransform decode-float ((x) (double-float) * :when :both)
158 '(decode-double-float x))
160 (deftransform integer-decode-float ((x) (single-float) * :when :both)
161 '(integer-decode-single-float x))
163 (deftransform integer-decode-float ((x) (double-float) * :when :both)
164 '(integer-decode-double-float x))
166 (deftransform scale-float ((f ex) (single-float *) * :when :both)
167 (if (and #!+x86 t #!-x86 nil
168 (csubtypep (continuation-type ex)
169 (specifier-type '(signed-byte 32)))
170 (not (byte-compiling)))
171 '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
172 '(scale-single-float f ex)))
174 (deftransform scale-float ((f ex) (double-float *) * :when :both)
175 (if (and #!+x86 t #!-x86 nil
176 (csubtypep (continuation-type ex)
177 (specifier-type '(signed-byte 32))))
179 '(scale-double-float f ex)))
181 ;;; toy@rtp.ericsson.se:
183 ;;; Optimizers for scale-float. If the float has bounds, new bounds
184 ;;; are computed for the result, if possible.
186 #-sb-xc-host ;(CROSS-FLOAT-INFINITY-KLUDGE, see base-target-features.lisp-expr)
188 #!+propagate-float-type
191 (defun scale-float-derive-type-aux (f ex same-arg)
192 (declare (ignore same-arg))
193 (flet ((scale-bound (x n)
194 ;; We need to be a bit careful here and catch any overflows
195 ;; that might occur. We can ignore underflows which become
199 (scale-float (bound-value x) n)
200 (floating-point-overflow ()
203 (when (and (numeric-type-p f) (numeric-type-p ex))
204 (let ((f-lo (numeric-type-low f))
205 (f-hi (numeric-type-high f))
206 (ex-lo (numeric-type-low ex))
207 (ex-hi (numeric-type-high ex))
210 (when (and f-hi ex-hi)
211 (setf new-hi (scale-bound f-hi ex-hi)))
212 (when (and f-lo ex-lo)
213 (setf new-lo (scale-bound f-lo ex-lo)))
214 (make-numeric-type :class (numeric-type-class f)
215 :format (numeric-type-format f)
219 (defoptimizer (scale-single-float derive-type) ((f ex))
220 (two-arg-derive-type f ex #'scale-float-derive-type-aux
221 #'scale-single-float t))
222 (defoptimizer (scale-double-float derive-type) ((f ex))
223 (two-arg-derive-type f ex #'scale-float-derive-type-aux
224 #'scale-double-float t))
226 ;;; toy@rtp.ericsson.se:
228 ;;; Defoptimizers for %single-float and %double-float. This makes the
229 ;;; FLOAT function return the correct ranges if the input has some
230 ;;; defined range. Quite useful if we want to convert some type of
231 ;;; bounded integer into a float.
235 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
237 (defun ,aux-name (num)
238 ;; When converting a number to a float, the limits are
240 (let* ((lo (bound-func #'(lambda (x)
242 (numeric-type-low num)))
243 (hi (bound-func #'(lambda (x)
245 (numeric-type-high num))))
246 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
248 (defoptimizer (,fun derive-type) ((num))
249 (one-arg-derive-type num #',aux-name #',fun))))))
250 (frob %single-float single-float)
251 (frob %double-float double-float))
256 ;;; Do some stuff to recognize when the loser is doing mixed float and
257 ;;; rational arithmetic, or different float types, and fix it up. If
258 ;;; we don't, he won't even get so much as an efficency note.
259 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
260 `(,(continuation-function-name (basic-combination-fun node))
262 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
263 `(,(continuation-function-name (basic-combination-fun node))
266 (dolist (x '(+ * / -))
267 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
268 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
270 (dolist (x '(= < > + * / -))
271 (%deftransform x '(function (single-float double-float) *)
272 #'float-contagion-arg1)
273 (%deftransform x '(function (double-float single-float) *)
274 #'float-contagion-arg2))
276 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
277 ;;; general float rational args to comparison, since Common Lisp
278 ;;; semantics says we are supposed to compare as rationals, but we can
279 ;;; do it for any rational that has a precise representation as a
280 ;;; float (such as 0).
281 (macrolet ((frob (op)
282 `(deftransform ,op ((x y) (float rational) * :when :both)
283 (unless (constant-continuation-p y)
284 (give-up-ir1-transform
285 "can't open-code float to rational comparison"))
286 (let ((val (continuation-value y)))
287 (unless (eql (rational (float val)) val)
288 (give-up-ir1-transform
289 "~S doesn't have a precise float representation."
291 `(,',op x (float y x)))))
296 ;;;; irrational derive-type methods
298 ;;; Derive the result to be float for argument types in the
299 ;;; appropriate domain.
300 #!-propagate-fun-type
301 (dolist (stuff '((asin (real -1.0 1.0))
302 (acos (real -1.0 1.0))
304 (atanh (real -1.0 1.0))
306 (destructuring-bind (name type) stuff
307 (let ((type (specifier-type type)))
308 (setf (function-info-derive-type (function-info-or-lose name))
310 (declare (type combination call))
311 (when (csubtypep (continuation-type
312 (first (combination-args call)))
314 (specifier-type 'float)))))))
316 #!-propagate-fun-type
317 (defoptimizer (log derive-type) ((x &optional y))
318 (when (and (csubtypep (continuation-type x)
319 (specifier-type '(real 0.0)))
321 (csubtypep (continuation-type y)
322 (specifier-type '(real 0.0)))))
323 (specifier-type 'float)))
325 ;;;; irrational transforms
327 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
328 (double-float) double-float
329 (movable foldable flushable))
331 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
332 (double-float) (double-float -1.0d0 1.0d0)
333 (movable foldable flushable))
335 (defknown (%asin %atan)
336 (double-float) (double-float #.(- (/ pi 2)) #.(/ pi 2))
337 (movable foldable flushable))
340 (double-float) (double-float 0.0d0 #.pi)
341 (movable foldable flushable))
344 (double-float) (double-float 1.0d0)
345 (movable foldable flushable))
347 (defknown (%acosh %exp %sqrt)
348 (double-float) (double-float 0.0d0)
349 (movable foldable flushable))
352 (double-float) (double-float -1d0)
353 (movable foldable flushable))
356 (double-float double-float) (double-float 0d0)
357 (movable foldable flushable))
360 (double-float double-float) double-float
361 (movable foldable flushable))
364 (double-float double-float) (double-float #.(- pi) #.pi)
365 (movable foldable flushable))
368 (double-float double-float) double-float
369 (movable foldable flushable))
372 (double-float (signed-byte 32)) double-float
373 (movable foldable flushable))
376 (double-float) double-float
377 (movable foldable flushable))
379 (dolist (stuff '((exp %exp *)
390 (atanh %atanh float)))
391 (destructuring-bind (name prim rtype) stuff
392 (deftransform name ((x) '(single-float) rtype :eval-name t)
393 `(coerce (,prim (coerce x 'double-float)) 'single-float))
394 (deftransform name ((x) '(double-float) rtype :eval-name t :when :both)
397 ;;; The argument range is limited on the x86 FP trig. functions. A
398 ;;; post-test can detect a failure (and load a suitable result), but
399 ;;; this test is avoided if possible.
400 (dolist (stuff '((sin %sin %sin-quick)
401 (cos %cos %cos-quick)
402 (tan %tan %tan-quick)))
403 (destructuring-bind (name prim prim-quick) stuff
404 (deftransform name ((x) '(single-float) '* :eval-name t)
405 #!+x86 (cond ((csubtypep (continuation-type x)
406 (specifier-type '(single-float
407 (#.(- (expt 2f0 64)))
409 `(coerce (,prim-quick (coerce x 'double-float))
413 "unable to avoid inline argument range check~@
414 because the argument range (~S) was not within 2^64"
415 (type-specifier (continuation-type x)))
416 `(coerce (,prim (coerce x 'double-float)) 'single-float)))
417 #!-x86 `(coerce (,prim (coerce x 'double-float)) 'single-float))
418 (deftransform name ((x) '(double-float) '* :eval-name t :when :both)
419 #!+x86 (cond ((csubtypep (continuation-type x)
420 (specifier-type '(double-float
421 (#.(- (expt 2d0 64)))
426 "unable to avoid inline argument range check~@
427 because the argument range (~S) was not within 2^64"
428 (type-specifier (continuation-type x)))
432 (deftransform atan ((x y) (single-float single-float) *)
433 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
435 (deftransform atan ((x y) (double-float double-float) * :when :both)
438 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
439 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
441 (deftransform expt ((x y) ((double-float 0d0) double-float) * :when :both)
443 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
444 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
446 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) * :when :both)
447 `(%pow x (coerce y 'double-float)))
449 ;;; ANSI says log with base zero returns zero.
450 (deftransform log ((x y) (float float) float)
451 '(if (zerop y) y (/ (log x) (log y))))
453 ;;; Handle some simple transformations.
455 (deftransform abs ((x) ((complex double-float)) double-float :when :both)
456 '(%hypot (realpart x) (imagpart x)))
458 (deftransform abs ((x) ((complex single-float)) single-float)
459 '(coerce (%hypot (coerce (realpart x) 'double-float)
460 (coerce (imagpart x) 'double-float))
463 (deftransform phase ((x) ((complex double-float)) double-float :when :both)
464 '(%atan2 (imagpart x) (realpart x)))
466 (deftransform phase ((x) ((complex single-float)) single-float)
467 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
468 (coerce (realpart x) 'double-float))
471 (deftransform phase ((x) ((float)) float :when :both)
472 '(if (minusp (float-sign x))
476 #!+(or propagate-float-type propagate-fun-type)
479 ;;; The number is of type REAL.
480 #!-sb-fluid (declaim (inline numeric-type-real-p))
481 (defun numeric-type-real-p (type)
482 (and (numeric-type-p type)
483 (eq (numeric-type-complexp type) :real)))
485 ;;; Coerce a numeric type bound to the given type while handling
486 ;;; exclusive bounds.
487 (defun coerce-numeric-bound (bound type)
490 (list (coerce (car bound) type))
491 (coerce bound type))))
495 #!+propagate-fun-type
498 ;;;; optimizers for elementary functions
500 ;;;; These optimizers compute the output range of the elementary
501 ;;;; function, based on the domain of the input.
503 ;;; Generate a specifier for a complex type specialized to the same
504 ;;; type as the argument.
505 (defun complex-float-type (arg)
506 (declare (type numeric-type arg))
507 (let* ((format (case (numeric-type-class arg)
508 ((integer rational) 'single-float)
509 (t (numeric-type-format arg))))
510 (float-type (or format 'float)))
511 (specifier-type `(complex ,float-type))))
513 ;;; Compute a specifier like '(or float (complex float)), except float
514 ;;; should be the right kind of float. Allow bounds for the float
516 (defun float-or-complex-float-type (arg &optional lo hi)
517 (declare (type numeric-type arg))
518 (let* ((format (case (numeric-type-class arg)
519 ((integer rational) 'single-float)
520 (t (numeric-type-format arg))))
521 (float-type (or format 'float))
522 (lo (coerce-numeric-bound lo float-type))
523 (hi (coerce-numeric-bound hi float-type)))
524 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
525 (complex ,float-type)))))
527 ;;; Test whether the numeric-type ARG is within in domain specified by
528 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
529 ;;; be distinct as for the :negative-zero-is-not-zero feature. With
530 ;;; the :negative-zero-is-not-zero feature this could be handled by
531 ;;; the numeric subtype code in type.lisp.
532 (defun domain-subtypep (arg domain-low domain-high)
533 (declare (type numeric-type arg)
534 (type (or real null) domain-low domain-high))
535 (let* ((arg-lo (numeric-type-low arg))
536 (arg-lo-val (bound-value arg-lo))
537 (arg-hi (numeric-type-high arg))
538 (arg-hi-val (bound-value arg-hi)))
539 ;; Check that the ARG bounds are correctly canonicalized.
540 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
541 (minusp (float-sign arg-lo-val)))
542 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-lo)
543 (setq arg-lo '(0l0) arg-lo-val 0l0))
544 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
545 (plusp (float-sign arg-hi-val)))
546 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-hi)
547 (setq arg-hi '(-0l0) arg-hi-val -0l0))
548 (and (or (null domain-low)
549 (and arg-lo (>= arg-lo-val domain-low)
550 (not (and (zerop domain-low) (floatp domain-low)
551 (plusp (float-sign domain-low))
552 (zerop arg-lo-val) (floatp arg-lo-val)
554 (plusp (float-sign arg-lo-val))
555 (minusp (float-sign arg-lo-val)))))))
556 (or (null domain-high)
557 (and arg-hi (<= arg-hi-val domain-high)
558 (not (and (zerop domain-high) (floatp domain-high)
559 (minusp (float-sign domain-high))
560 (zerop arg-hi-val) (floatp arg-hi-val)
562 (minusp (float-sign arg-hi-val))
563 (plusp (float-sign arg-hi-val))))))))))
565 ;;; Handle monotonic functions of a single variable whose domain is
566 ;;; possibly part of the real line. ARG is the variable, FCN is the
567 ;;; function, and DOMAIN is a specifier that gives the (real) domain
568 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
569 ;;; bounds directly. Otherwise, we compute the bounds for the
570 ;;; intersection between ARG and DOMAIN, and then append a complex
571 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
573 ;;; Negative and positive zero are considered distinct within
574 ;;; DOMAIN-LOW and DOMAIN-HIGH, as for the :negative-zero-is-not-zero
577 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
578 ;;; can't compute the bounds using FCN.
579 (defun elfun-derive-type-simple (arg fcn domain-low domain-high
580 default-low default-high
581 &optional (increasingp t))
582 (declare (type (or null real) domain-low domain-high))
585 (cond ((eq (numeric-type-complexp arg) :complex)
586 (make-numeric-type :class (numeric-type-class arg)
587 :format (numeric-type-format arg)
589 ((numeric-type-real-p arg)
590 ;; The argument is real, so let's find the intersection
591 ;; between the argument and the domain of the function.
592 ;; We compute the bounds on the intersection, and for
593 ;; everything else, we return a complex number of the
595 (multiple-value-bind (intersection difference)
596 (interval-intersection/difference (numeric-type->interval arg)
602 ;; Process the intersection.
603 (let* ((low (interval-low intersection))
604 (high (interval-high intersection))
605 (res-lo (or (bound-func fcn (if increasingp low high))
607 (res-hi (or (bound-func fcn (if increasingp high low))
609 ;; Result specifier type.
610 (format (case (numeric-type-class arg)
611 ((integer rational) 'single-float)
612 (t (numeric-type-format arg))))
613 (bound-type (or format 'float))
618 :low (coerce-numeric-bound res-lo bound-type)
619 :high (coerce-numeric-bound res-hi bound-type))))
620 ;; If the ARG is a subset of the domain, we don't
621 ;; have to worry about the difference, because that
623 (if (or (null difference)
624 ;; Check whether the arg is within the domain.
625 (domain-subtypep arg domain-low domain-high))
628 (specifier-type `(complex ,bound-type))))))
630 ;; No intersection so the result must be purely complex.
631 (complex-float-type arg)))))
633 (float-or-complex-float-type arg default-low default-high))))))
636 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
637 &key (increasingp t))
638 (let ((num (gensym)))
639 `(defoptimizer (,name derive-type) ((,num))
643 (elfun-derive-type-simple arg #',name
644 ,domain-low ,domain-high
645 ,def-low-bnd ,def-high-bnd
648 ;; These functions are easy because they are defined for the whole
650 (frob exp nil nil 0 nil)
651 (frob sinh nil nil nil nil)
652 (frob tanh nil nil -1 1)
653 (frob asinh nil nil nil nil)
655 ;; These functions are only defined for part of the real line. The
656 ;; condition selects the desired part of the line.
657 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
658 ;; Acos is monotonic decreasing, so we need to swap the function
659 ;; values at the lower and upper bounds of the input domain.
660 (frob acos -1d0 1d0 0 pi :increasingp nil)
661 (frob acosh 1d0 nil nil nil)
662 (frob atanh -1d0 1d0 -1 1)
663 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
665 (frob sqrt -0d0 nil 0 nil))
667 ;;; Compute bounds for (expt x y). This should be easy since (expt x
668 ;;; y) = (exp (* y (log x))). However, computations done this way
669 ;;; have too much roundoff. Thus we have to do it the hard way.
670 (defun safe-expt (x y)
676 ;;; Handle the case when x >= 1.
677 (defun interval-expt-> (x y)
678 (case (sb!c::interval-range-info y 0d0)
680 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
681 ;; obviously non-negative. We just have to be careful for
682 ;; infinite bounds (given by nil).
683 (let ((lo (safe-expt (sb!c::bound-value (sb!c::interval-low x))
684 (sb!c::bound-value (sb!c::interval-low y))))
685 (hi (safe-expt (sb!c::bound-value (sb!c::interval-high x))
686 (sb!c::bound-value (sb!c::interval-high y)))))
687 (list (sb!c::make-interval :low (or lo 1) :high hi))))
689 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
690 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
692 (let ((lo (safe-expt (sb!c::bound-value (sb!c::interval-high x))
693 (sb!c::bound-value (sb!c::interval-low y))))
694 (hi (safe-expt (sb!c::bound-value (sb!c::interval-low x))
695 (sb!c::bound-value (sb!c::interval-high y)))))
696 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
698 ;; Split the interval in half.
699 (destructuring-bind (y- y+)
700 (sb!c::interval-split 0 y t)
701 (list (interval-expt-> x y-)
702 (interval-expt-> x y+))))))
704 ;;; Handle the case when x <= 1
705 (defun interval-expt-< (x y)
706 (case (sb!c::interval-range-info x 0d0)
708 ;; The case of 0 <= x <= 1 is easy
709 (case (sb!c::interval-range-info y)
711 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
712 ;; obviously [0, 1]. We just have to be careful for infinite bounds
714 (let ((lo (safe-expt (sb!c::bound-value (sb!c::interval-low x))
715 (sb!c::bound-value (sb!c::interval-high y))))
716 (hi (safe-expt (sb!c::bound-value (sb!c::interval-high x))
717 (sb!c::bound-value (sb!c::interval-low y)))))
718 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
720 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
721 ;; obviously [1, inf].
722 (let ((hi (safe-expt (sb!c::bound-value (sb!c::interval-low x))
723 (sb!c::bound-value (sb!c::interval-low y))))
724 (lo (safe-expt (sb!c::bound-value (sb!c::interval-high x))
725 (sb!c::bound-value (sb!c::interval-high y)))))
726 (list (sb!c::make-interval :low (or lo 1) :high hi))))
728 ;; Split the interval in half
729 (destructuring-bind (y- y+)
730 (sb!c::interval-split 0 y t)
731 (list (interval-expt-< x y-)
732 (interval-expt-< x y+))))))
734 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
735 ;; The calling function must insure this! For now we'll just
736 ;; return the appropriate unbounded float type.
737 (list (sb!c::make-interval :low nil :high nil)))
739 (destructuring-bind (neg pos)
740 (interval-split 0 x t t)
741 (list (interval-expt-< neg y)
742 (interval-expt-< pos y))))))
744 ;;; Compute bounds for (expt x y).
746 (defun interval-expt (x y)
747 (case (interval-range-info x 1)
750 (interval-expt-> x y))
753 (interval-expt-< x y))
755 (destructuring-bind (left right)
756 (interval-split 1 x t t)
757 (list (interval-expt left y)
758 (interval-expt right y))))))
760 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
761 (declare (ignore x-int))
762 ;; Figure out what the return type should be, given the argument
763 ;; types and bounds and the result type and bounds.
764 (cond ((csubtypep x-type (specifier-type 'integer))
765 ;; An integer to some power. Cases to consider:
766 (case (numeric-type-class y-type)
768 ;; Positive integer to an integer power is either an
769 ;; integer or a rational.
770 (let ((lo (or (interval-low bnd) '*))
771 (hi (or (interval-high bnd) '*)))
772 (if (and (interval-low y-int)
773 (>= (bound-value (interval-low y-int)) 0))
774 (specifier-type `(integer ,lo ,hi))
775 (specifier-type `(rational ,lo ,hi)))))
777 ;; Positive integer to rational power is either a rational
778 ;; or a single-float.
779 (let* ((lo (interval-low bnd))
780 (hi (interval-high bnd))
782 (floor (bound-value lo))
785 (ceiling (bound-value hi))
788 (bound-func #'float lo)
791 (bound-func #'float hi)
793 (specifier-type `(or (rational ,int-lo ,int-hi)
794 (single-float ,f-lo, f-hi)))))
796 ;; Positive integer to a float power is a float.
797 (let ((res (copy-numeric-type y-type)))
798 (setf (numeric-type-low res) (interval-low bnd))
799 (setf (numeric-type-high res) (interval-high bnd))
802 ;; Positive integer to a number is a number (for now).
803 (specifier-type 'number)))
805 ((csubtypep x-type (specifier-type 'rational))
806 ;; a rational to some power
807 (case (numeric-type-class y-type)
809 ;; Positive rational to an integer power is always a rational.
810 (specifier-type `(rational ,(or (interval-low bnd) '*)
811 ,(or (interval-high bnd) '*))))
813 ;; Positive rational to rational power is either a rational
814 ;; or a single-float.
815 (let* ((lo (interval-low bnd))
816 (hi (interval-high bnd))
818 (floor (bound-value lo))
821 (ceiling (bound-value hi))
824 (bound-func #'float lo)
827 (bound-func #'float hi)
829 (specifier-type `(or (rational ,int-lo ,int-hi)
830 (single-float ,f-lo, f-hi)))))
832 ;; Positive rational to a float power is a float.
833 (let ((res (copy-numeric-type y-type)))
834 (setf (numeric-type-low res) (interval-low bnd))
835 (setf (numeric-type-high res) (interval-high bnd))
838 ;; Positive rational to a number is a number (for now).
839 (specifier-type 'number)))
841 ((csubtypep x-type (specifier-type 'float))
842 ;; a float to some power
843 (case (numeric-type-class y-type)
844 ((or integer rational)
845 ;; Positive float to an integer or rational power is
849 :format (numeric-type-format x-type)
850 :low (interval-low bnd)
851 :high (interval-high bnd)))
853 ;; Positive float to a float power is a float of the higher type.
856 :format (float-format-max (numeric-type-format x-type)
857 (numeric-type-format y-type))
858 :low (interval-low bnd)
859 :high (interval-high bnd)))
861 ;; Positive float to a number is a number (for now)
862 (specifier-type 'number))))
864 ;; A number to some power is a number.
865 (specifier-type 'number))))
867 (defun merged-interval-expt (x y)
868 (let* ((x-int (numeric-type->interval x))
869 (y-int (numeric-type->interval y)))
870 (mapcar #'(lambda (type)
871 (fixup-interval-expt type x-int y-int x y))
872 (flatten-list (interval-expt x-int y-int)))))
874 (defun expt-derive-type-aux (x y same-arg)
875 (declare (ignore same-arg))
876 (cond ((or (not (numeric-type-real-p x))
877 (not (numeric-type-real-p y)))
878 ;; Use numeric contagion if either is not real.
879 (numeric-contagion x y))
880 ((csubtypep y (specifier-type 'integer))
881 ;; A real raised to an integer power is well-defined.
882 (merged-interval-expt x y))
884 ;; A real raised to a non-integral power can be a float or a
886 (cond ((or (csubtypep x (specifier-type '(rational 0)))
887 (csubtypep x (specifier-type '(float (0d0)))))
888 ;; But a positive real to any power is well-defined.
889 (merged-interval-expt x y))
891 ;; A real to some power. The result could be a real
893 (float-or-complex-float-type (numeric-contagion x y)))))))
895 (defoptimizer (expt derive-type) ((x y))
896 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
898 ;;; Note we must assume that a type including 0.0 may also include
899 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
900 (defun log-derive-type-aux-1 (x)
901 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
903 (defun log-derive-type-aux-2 (x y same-arg)
904 (let ((log-x (log-derive-type-aux-1 x))
905 (log-y (log-derive-type-aux-1 y))
907 ;; log-x or log-y might be union types. We need to run through
908 ;; the union types ourselves because /-derive-type-aux doesn't.
909 (dolist (x-type (prepare-arg-for-derive-type log-x))
910 (dolist (y-type (prepare-arg-for-derive-type log-y))
911 (push (/-derive-type-aux x-type y-type same-arg) result)))
912 (setf result (flatten-list result))
914 (make-union-type-or-something result)
917 (defoptimizer (log derive-type) ((x &optional y))
919 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
920 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
922 (defun atan-derive-type-aux-1 (y)
923 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
925 (defun atan-derive-type-aux-2 (y x same-arg)
926 (declare (ignore same-arg))
927 ;; The hard case with two args. We just return the max bounds.
928 (let ((result-type (numeric-contagion y x)))
929 (cond ((and (numeric-type-real-p x)
930 (numeric-type-real-p y))
931 (let* ((format (case (numeric-type-class result-type)
932 ((integer rational) 'single-float)
933 (t (numeric-type-format result-type))))
934 (bound-format (or format 'float)))
935 (make-numeric-type :class 'float
938 :low (coerce (- pi) bound-format)
939 :high (coerce pi bound-format))))
941 ;; The result is a float or a complex number
942 (float-or-complex-float-type result-type)))))
944 (defoptimizer (atan derive-type) ((y &optional x))
946 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
947 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
949 (defun cosh-derive-type-aux (x)
950 ;; We note that cosh x = cosh |x| for all real x.
951 (elfun-derive-type-simple
952 (if (numeric-type-real-p x)
953 (abs-derive-type-aux x)
955 #'cosh nil nil 0 nil))
957 (defoptimizer (cosh derive-type) ((num))
958 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
960 (defun phase-derive-type-aux (arg)
961 (let* ((format (case (numeric-type-class arg)
962 ((integer rational) 'single-float)
963 (t (numeric-type-format arg))))
964 (bound-type (or format 'float)))
965 (cond ((numeric-type-real-p arg)
966 (case (interval-range-info (numeric-type->interval arg) 0.0)
968 ;; The number is positive, so the phase is 0.
969 (make-numeric-type :class 'float
972 :low (coerce 0 bound-type)
973 :high (coerce 0 bound-type)))
975 ;; The number is always negative, so the phase is pi.
976 (make-numeric-type :class 'float
979 :low (coerce pi bound-type)
980 :high (coerce pi bound-type)))
982 ;; We can't tell. The result is 0 or pi. Use a union
985 (make-numeric-type :class 'float
988 :low (coerce 0 bound-type)
989 :high (coerce 0 bound-type))
990 (make-numeric-type :class 'float
993 :low (coerce pi bound-type)
994 :high (coerce pi bound-type))))))
996 ;; We have a complex number. The answer is the range -pi
997 ;; to pi. (-pi is included because we have -0.)
998 (make-numeric-type :class 'float
1001 :low (coerce (- pi) bound-type)
1002 :high (coerce pi bound-type))))))
1004 (defoptimizer (phase derive-type) ((num))
1005 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1009 (deftransform realpart ((x) ((complex rational)) *)
1010 '(sb!kernel:%realpart x))
1011 (deftransform imagpart ((x) ((complex rational)) *)
1012 '(sb!kernel:%imagpart x))
1014 ;;; Make REALPART and IMAGPART return the appropriate types. This
1015 ;;; should help a lot in optimized code.
1016 (defun realpart-derive-type-aux (type)
1017 (let ((class (numeric-type-class type))
1018 (format (numeric-type-format type)))
1019 (cond ((numeric-type-real-p type)
1020 ;; The realpart of a real has the same type and range as
1022 (make-numeric-type :class class
1025 :low (numeric-type-low type)
1026 :high (numeric-type-high type)))
1028 ;; We have a complex number. The result has the same type
1029 ;; as the real part, except that it's real, not complex,
1031 (make-numeric-type :class class
1034 :low (numeric-type-low type)
1035 :high (numeric-type-high type))))))
1036 #!+(or propagate-fun-type propagate-float-type)
1037 (defoptimizer (realpart derive-type) ((num))
1038 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1039 (defun imagpart-derive-type-aux (type)
1040 (let ((class (numeric-type-class type))
1041 (format (numeric-type-format type)))
1042 (cond ((numeric-type-real-p type)
1043 ;; The imagpart of a real has the same type as the input,
1044 ;; except that it's zero.
1045 (let ((bound-format (or format class 'real)))
1046 (make-numeric-type :class class
1049 :low (coerce 0 bound-format)
1050 :high (coerce 0 bound-format))))
1052 ;; We have a complex number. The result has the same type as
1053 ;; the imaginary part, except that it's real, not complex,
1055 (make-numeric-type :class class
1058 :low (numeric-type-low type)
1059 :high (numeric-type-high type))))))
1060 #!+(or propagate-fun-type propagate-float-type)
1061 (defoptimizer (imagpart derive-type) ((num))
1062 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1064 (defun complex-derive-type-aux-1 (re-type)
1065 (if (numeric-type-p re-type)
1066 (make-numeric-type :class (numeric-type-class re-type)
1067 :format (numeric-type-format re-type)
1068 :complexp (if (csubtypep re-type
1069 (specifier-type 'rational))
1072 :low (numeric-type-low re-type)
1073 :high (numeric-type-high re-type))
1074 (specifier-type 'complex)))
1076 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1077 (declare (ignore same-arg))
1078 (if (and (numeric-type-p re-type)
1079 (numeric-type-p im-type))
1080 ;; Need to check to make sure numeric-contagion returns the
1081 ;; right type for what we want here.
1083 ;; Also, what about rational canonicalization, like (complex 5 0)
1084 ;; is 5? So, if the result must be complex, we make it so.
1085 ;; If the result might be complex, which happens only if the
1086 ;; arguments are rational, we make it a union type of (or
1087 ;; rational (complex rational)).
1088 (let* ((element-type (numeric-contagion re-type im-type))
1089 (rat-result-p (csubtypep element-type
1090 (specifier-type 'rational))))
1092 (make-union-type-or-something
1095 `(complex ,(numeric-type-class element-type)))))
1096 (make-numeric-type :class (numeric-type-class element-type)
1097 :format (numeric-type-format element-type)
1098 :complexp (if rat-result-p
1101 (specifier-type 'complex)))
1103 #!+(or propagate-fun-type propagate-float-type)
1104 (defoptimizer (complex derive-type) ((re &optional im))
1106 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1107 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1109 ;;; Define some transforms for complex operations. We do this in lieu
1110 ;;; of complex operation VOPs.
1111 (macrolet ((frob (type)
1114 (deftransform %negate ((z) ((complex ,type)) *)
1115 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1116 ;; complex addition and subtraction
1117 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1118 '(complex (+ (realpart w) (realpart z))
1119 (+ (imagpart w) (imagpart z))))
1120 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1121 '(complex (- (realpart w) (realpart z))
1122 (- (imagpart w) (imagpart z))))
1123 ;; Add and subtract a complex and a real.
1124 (deftransform + ((w z) ((complex ,type) real) *)
1125 '(complex (+ (realpart w) z) (imagpart w)))
1126 (deftransform + ((z w) (real (complex ,type)) *)
1127 '(complex (+ (realpart w) z) (imagpart w)))
1128 ;; Add and subtract a real and a complex number.
1129 (deftransform - ((w z) ((complex ,type) real) *)
1130 '(complex (- (realpart w) z) (imagpart w)))
1131 (deftransform - ((z w) (real (complex ,type)) *)
1132 '(complex (- z (realpart w)) (- (imagpart w))))
1133 ;; Multiply and divide two complex numbers.
1134 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1135 '(let* ((rx (realpart x))
1139 (complex (- (* rx ry) (* ix iy))
1140 (+ (* rx iy) (* ix ry)))))
1141 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1142 '(let* ((rx (realpart x))
1146 (if (> (abs ry) (abs iy))
1147 (let* ((r (/ iy ry))
1148 (dn (* ry (+ 1 (* r r)))))
1149 (complex (/ (+ rx (* ix r)) dn)
1150 (/ (- ix (* rx r)) dn)))
1151 (let* ((r (/ ry iy))
1152 (dn (* iy (+ 1 (* r r)))))
1153 (complex (/ (+ (* rx r) ix) dn)
1154 (/ (- (* ix r) rx) dn))))))
1155 ;; Multiply a complex by a real or vice versa.
1156 (deftransform * ((w z) ((complex ,type) real) *)
1157 '(complex (* (realpart w) z) (* (imagpart w) z)))
1158 (deftransform * ((z w) (real (complex ,type)) *)
1159 '(complex (* (realpart w) z) (* (imagpart w) z)))
1160 ;; Divide a complex by a real.
1161 (deftransform / ((w z) ((complex ,type) real) *)
1162 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1163 ;; conjugate of complex number
1164 (deftransform conjugate ((z) ((complex ,type)) *)
1165 '(complex (realpart z) (- (imagpart z))))
1167 (deftransform cis ((z) ((,type)) *)
1168 '(complex (cos z) (sin z)))
1170 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1171 '(and (= (realpart w) (realpart z))
1172 (= (imagpart w) (imagpart z))))
1173 (deftransform = ((w z) ((complex ,type) real) *)
1174 '(and (= (realpart w) z) (zerop (imagpart w))))
1175 (deftransform = ((w z) (real (complex ,type)) *)
1176 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1179 (frob double-float))
1181 ;;; Here are simple optimizers for sin, cos, and tan. They do not
1182 ;;; produce a minimal range for the result; the result is the widest
1183 ;;; possible answer. This gets around the problem of doing range
1184 ;;; reduction correctly but still provides useful results when the
1185 ;;; inputs are union types.
1187 #!+propagate-fun-type
1189 (defun trig-derive-type-aux (arg domain fcn
1190 &optional def-lo def-hi (increasingp t))
1193 (cond ((eq (numeric-type-complexp arg) :complex)
1194 (make-numeric-type :class (numeric-type-class arg)
1195 :format (numeric-type-format arg)
1196 :complexp :complex))
1197 ((numeric-type-real-p arg)
1198 (let* ((format (case (numeric-type-class arg)
1199 ((integer rational) 'single-float)
1200 (t (numeric-type-format arg))))
1201 (bound-type (or format 'float)))
1202 ;; If the argument is a subset of the "principal" domain
1203 ;; of the function, we can compute the bounds because
1204 ;; the function is monotonic. We can't do this in
1205 ;; general for these periodic functions because we can't
1206 ;; (and don't want to) do the argument reduction in
1207 ;; exactly the same way as the functions themselves do
1209 (if (csubtypep arg domain)
1210 (let ((res-lo (bound-func fcn (numeric-type-low arg)))
1211 (res-hi (bound-func fcn (numeric-type-high arg))))
1213 (rotatef res-lo res-hi))
1217 :low (coerce-numeric-bound res-lo bound-type)
1218 :high (coerce-numeric-bound res-hi bound-type)))
1222 :low (and def-lo (coerce def-lo bound-type))
1223 :high (and def-hi (coerce def-hi bound-type))))))
1225 (float-or-complex-float-type arg def-lo def-hi))))))
1227 (defoptimizer (sin derive-type) ((num))
1228 (one-arg-derive-type
1231 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1232 (trig-derive-type-aux
1234 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1239 (defoptimizer (cos derive-type) ((num))
1240 (one-arg-derive-type
1243 ;; Derive the bounds if the arg is in [0, pi].
1244 (trig-derive-type-aux arg
1245 (specifier-type `(float 0d0 ,pi))
1251 (defoptimizer (tan derive-type) ((num))
1252 (one-arg-derive-type
1255 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1256 (trig-derive-type-aux arg
1257 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1262 ;;; CONJUGATE always returns the same type as the input type.
1263 (defoptimizer (conjugate derive-type) ((num))
1264 (continuation-type num))
1266 (defoptimizer (cis derive-type) ((num))
1267 (one-arg-derive-type num
1269 (sb!c::specifier-type
1270 `(complex ,(or (numeric-type-format arg) 'float))))