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 #!+propagate-float-type
189 (defun scale-float-derive-type-aux (f ex same-arg)
190 (declare (ignore same-arg))
191 (flet ((scale-bound (x n)
192 ;; We need to be a bit careful here and catch any overflows
193 ;; that might occur. We can ignore underflows which become
197 (scale-float (bound-value x) n)
198 (floating-point-overflow ()
201 (when (and (numeric-type-p f) (numeric-type-p ex))
202 (let ((f-lo (numeric-type-low f))
203 (f-hi (numeric-type-high f))
204 (ex-lo (numeric-type-low ex))
205 (ex-hi (numeric-type-high ex))
208 (when (and f-hi ex-hi)
209 (setf new-hi (scale-bound f-hi ex-hi)))
210 (when (and f-lo ex-lo)
211 (setf new-lo (scale-bound f-lo ex-lo)))
212 (make-numeric-type :class (numeric-type-class f)
213 :format (numeric-type-format f)
217 (defoptimizer (scale-single-float derive-type) ((f ex))
218 (two-arg-derive-type f ex #'scale-float-derive-type-aux
219 #'scale-single-float t))
220 (defoptimizer (scale-double-float derive-type) ((f ex))
221 (two-arg-derive-type f ex #'scale-float-derive-type-aux
222 #'scale-double-float t))
224 ;;; toy@rtp.ericsson.se:
226 ;;; Defoptimizers for %single-float and %double-float. This makes the
227 ;;; FLOAT function return the correct ranges if the input has some
228 ;;; defined range. Quite useful if we want to convert some type of
229 ;;; bounded integer into a float.
233 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
235 (defun ,aux-name (num)
236 ;; When converting a number to a float, the limits are
238 (let* ((lo (bound-func #'(lambda (x)
240 (numeric-type-low num)))
241 (hi (bound-func #'(lambda (x)
243 (numeric-type-high num))))
244 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
246 (defoptimizer (,fun derive-type) ((num))
247 (one-arg-derive-type num #',aux-name #',fun))))))
248 (frob %single-float single-float)
249 (frob %double-float double-float))
254 ;;; Do some stuff to recognize when the loser is doing mixed float and
255 ;;; rational arithmetic, or different float types, and fix it up. If
256 ;;; we don't, he won't even get so much as an efficency note.
257 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
258 `(,(continuation-function-name (basic-combination-fun node))
260 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
261 `(,(continuation-function-name (basic-combination-fun node))
264 (dolist (x '(+ * / -))
265 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
266 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
268 (dolist (x '(= < > + * / -))
269 (%deftransform x '(function (single-float double-float) *)
270 #'float-contagion-arg1)
271 (%deftransform x '(function (double-float single-float) *)
272 #'float-contagion-arg2))
274 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
275 ;;; general float rational args to comparison, since Common Lisp
276 ;;; semantics says we are supposed to compare as rationals, but we can
277 ;;; do it for any rational that has a precise representation as a
278 ;;; float (such as 0).
279 (macrolet ((frob (op)
280 `(deftransform ,op ((x y) (float rational) * :when :both)
281 (unless (constant-continuation-p y)
282 (give-up-ir1-transform
283 "can't open-code float to rational comparison"))
284 (let ((val (continuation-value y)))
285 (unless (eql (rational (float val)) val)
286 (give-up-ir1-transform
287 "~S doesn't have a precise float representation."
289 `(,',op x (float y x)))))
294 ;;;; irrational derive-type methods
296 ;;; Derive the result to be float for argument types in the
297 ;;; appropriate domain.
298 #!-propagate-fun-type
299 (dolist (stuff '((asin (real -1.0 1.0))
300 (acos (real -1.0 1.0))
302 (atanh (real -1.0 1.0))
304 (destructuring-bind (name type) stuff
305 (let ((type (specifier-type type)))
306 (setf (function-info-derive-type (function-info-or-lose name))
308 (declare (type combination call))
309 (when (csubtypep (continuation-type
310 (first (combination-args call)))
312 (specifier-type 'float)))))))
314 #!-propagate-fun-type
315 (defoptimizer (log derive-type) ((x &optional y))
316 (when (and (csubtypep (continuation-type x)
317 (specifier-type '(real 0.0)))
319 (csubtypep (continuation-type y)
320 (specifier-type '(real 0.0)))))
321 (specifier-type 'float)))
323 ;;;; irrational transforms
325 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
326 (double-float) double-float
327 (movable foldable flushable))
329 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
330 (double-float) (double-float -1.0d0 1.0d0)
331 (movable foldable flushable))
333 (defknown (%asin %atan)
334 (double-float) (double-float #.(- (/ pi 2)) #.(/ pi 2))
335 (movable foldable flushable))
338 (double-float) (double-float 0.0d0 #.pi)
339 (movable foldable flushable))
342 (double-float) (double-float 1.0d0)
343 (movable foldable flushable))
345 (defknown (%acosh %exp %sqrt)
346 (double-float) (double-float 0.0d0)
347 (movable foldable flushable))
350 (double-float) (double-float -1d0)
351 (movable foldable flushable))
354 (double-float double-float) (double-float 0d0)
355 (movable foldable flushable))
358 (double-float double-float) double-float
359 (movable foldable flushable))
362 (double-float double-float) (double-float #.(- pi) #.pi)
363 (movable foldable flushable))
366 (double-float double-float) double-float
367 (movable foldable flushable))
370 (double-float (signed-byte 32)) double-float
371 (movable foldable flushable))
374 (double-float) double-float
375 (movable foldable flushable))
377 (dolist (stuff '((exp %exp *)
388 (atanh %atanh float)))
389 (destructuring-bind (name prim rtype) stuff
390 (deftransform name ((x) '(single-float) rtype :eval-name t)
391 `(coerce (,prim (coerce x 'double-float)) 'single-float))
392 (deftransform name ((x) '(double-float) rtype :eval-name t :when :both)
395 ;;; The argument range is limited on the x86 FP trig. functions. A
396 ;;; post-test can detect a failure (and load a suitable result), but
397 ;;; this test is avoided if possible.
398 (dolist (stuff '((sin %sin %sin-quick)
399 (cos %cos %cos-quick)
400 (tan %tan %tan-quick)))
401 (destructuring-bind (name prim prim-quick) stuff
402 (deftransform name ((x) '(single-float) '* :eval-name t)
403 #!+x86 (cond ((csubtypep (continuation-type x)
404 (specifier-type '(single-float
405 (#.(- (expt 2f0 64)))
407 `(coerce (,prim-quick (coerce x 'double-float))
411 "unable to avoid inline argument range check~@
412 because the argument range (~S) was not within 2^64"
413 (type-specifier (continuation-type x)))
414 `(coerce (,prim (coerce x 'double-float)) 'single-float)))
415 #!-x86 `(coerce (,prim (coerce x 'double-float)) 'single-float))
416 (deftransform name ((x) '(double-float) '* :eval-name t :when :both)
417 #!+x86 (cond ((csubtypep (continuation-type x)
418 (specifier-type '(double-float
419 (#.(- (expt 2d0 64)))
424 "unable to avoid inline argument range check~@
425 because the argument range (~S) was not within 2^64"
426 (type-specifier (continuation-type x)))
430 (deftransform atan ((x y) (single-float single-float) *)
431 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
433 (deftransform atan ((x y) (double-float double-float) * :when :both)
436 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
437 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
439 (deftransform expt ((x y) ((double-float 0d0) double-float) * :when :both)
441 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
442 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
444 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) * :when :both)
445 `(%pow x (coerce y 'double-float)))
447 ;;; ANSI says log with base zero returns zero.
448 (deftransform log ((x y) (float float) float)
449 '(if (zerop y) y (/ (log x) (log y))))
451 ;;; Handle some simple transformations.
453 (deftransform abs ((x) ((complex double-float)) double-float :when :both)
454 '(%hypot (realpart x) (imagpart x)))
456 (deftransform abs ((x) ((complex single-float)) single-float)
457 '(coerce (%hypot (coerce (realpart x) 'double-float)
458 (coerce (imagpart x) 'double-float))
461 (deftransform phase ((x) ((complex double-float)) double-float :when :both)
462 '(%atan2 (imagpart x) (realpart x)))
464 (deftransform phase ((x) ((complex single-float)) single-float)
465 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
466 (coerce (realpart x) 'double-float))
469 (deftransform phase ((x) ((float)) float :when :both)
470 '(if (minusp (float-sign x))
474 #!+(or propagate-float-type propagate-fun-type)
477 ;;; The number is of type REAL.
478 #!-sb-fluid (declaim (inline numeric-type-real-p))
479 (defun numeric-type-real-p (type)
480 (and (numeric-type-p type)
481 (eq (numeric-type-complexp type) :real)))
483 ;;; Coerce a numeric type bound to the given type while handling
484 ;;; exclusive bounds.
485 (defun coerce-numeric-bound (bound type)
488 (list (coerce (car bound) type))
489 (coerce bound type))))
493 #!+propagate-fun-type
496 ;;;; optimizers for elementary functions
498 ;;;; These optimizers compute the output range of the elementary
499 ;;;; function, based on the domain of the input.
501 ;;; Generate a specifier for a complex type specialized to the same
502 ;;; type as the argument.
503 (defun complex-float-type (arg)
504 (declare (type numeric-type arg))
505 (let* ((format (case (numeric-type-class arg)
506 ((integer rational) 'single-float)
507 (t (numeric-type-format arg))))
508 (float-type (or format 'float)))
509 (specifier-type `(complex ,float-type))))
511 ;;; Compute a specifier like '(or float (complex float)), except float
512 ;;; should be the right kind of float. Allow bounds for the float
514 (defun float-or-complex-float-type (arg &optional lo hi)
515 (declare (type numeric-type arg))
516 (let* ((format (case (numeric-type-class arg)
517 ((integer rational) 'single-float)
518 (t (numeric-type-format arg))))
519 (float-type (or format 'float))
520 (lo (coerce-numeric-bound lo float-type))
521 (hi (coerce-numeric-bound hi float-type)))
522 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
523 (complex ,float-type)))))
525 ;;; Test whether the numeric-type ARG is within in domain specified by
526 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
527 ;;; be distinct as for the :negative-zero-is-not-zero feature. With
528 ;;; the :negative-zero-is-not-zero feature this could be handled by
529 ;;; the numeric subtype code in type.lisp.
530 (defun domain-subtypep (arg domain-low domain-high)
531 (declare (type numeric-type arg)
532 (type (or real null) domain-low domain-high))
533 (let* ((arg-lo (numeric-type-low arg))
534 (arg-lo-val (bound-value arg-lo))
535 (arg-hi (numeric-type-high arg))
536 (arg-hi-val (bound-value arg-hi)))
537 ;; Check that the ARG bounds are correctly canonicalized.
538 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
539 (minusp (float-sign arg-lo-val)))
540 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-lo)
541 (setq arg-lo '(0l0) arg-lo-val 0l0))
542 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
543 (plusp (float-sign arg-hi-val)))
544 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-hi)
545 (setq arg-hi '(-0l0) arg-hi-val -0l0))
546 (and (or (null domain-low)
547 (and arg-lo (>= arg-lo-val domain-low)
548 (not (and (zerop domain-low) (floatp domain-low)
549 (plusp (float-sign domain-low))
550 (zerop arg-lo-val) (floatp arg-lo-val)
552 (plusp (float-sign arg-lo-val))
553 (minusp (float-sign arg-lo-val)))))))
554 (or (null domain-high)
555 (and arg-hi (<= arg-hi-val domain-high)
556 (not (and (zerop domain-high) (floatp domain-high)
557 (minusp (float-sign domain-high))
558 (zerop arg-hi-val) (floatp arg-hi-val)
560 (minusp (float-sign arg-hi-val))
561 (plusp (float-sign arg-hi-val))))))))))
563 ;;; Handle monotonic functions of a single variable whose domain is
564 ;;; possibly part of the real line. ARG is the variable, FCN is the
565 ;;; function, and DOMAIN is a specifier that gives the (real) domain
566 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
567 ;;; bounds directly. Otherwise, we compute the bounds for the
568 ;;; intersection between ARG and DOMAIN, and then append a complex
569 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
571 ;;; Negative and positive zero are considered distinct within
572 ;;; DOMAIN-LOW and DOMAIN-HIGH, as for the :negative-zero-is-not-zero
575 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
576 ;;; can't compute the bounds using FCN.
577 (defun elfun-derive-type-simple (arg fcn domain-low domain-high
578 default-low default-high
579 &optional (increasingp t))
580 (declare (type (or null real) domain-low domain-high))
583 (cond ((eq (numeric-type-complexp arg) :complex)
584 (make-numeric-type :class (numeric-type-class arg)
585 :format (numeric-type-format arg)
587 ((numeric-type-real-p arg)
588 ;; The argument is real, so let's find the intersection
589 ;; between the argument and the domain of the function.
590 ;; We compute the bounds on the intersection, and for
591 ;; everything else, we return a complex number of the
593 (multiple-value-bind (intersection difference)
594 (interval-intersection/difference (numeric-type->interval arg)
600 ;; Process the intersection.
601 (let* ((low (interval-low intersection))
602 (high (interval-high intersection))
603 (res-lo (or (bound-func fcn (if increasingp low high))
605 (res-hi (or (bound-func fcn (if increasingp high low))
607 ;; Result specifier type.
608 (format (case (numeric-type-class arg)
609 ((integer rational) 'single-float)
610 (t (numeric-type-format arg))))
611 (bound-type (or format 'float))
616 :low (coerce-numeric-bound res-lo bound-type)
617 :high (coerce-numeric-bound res-hi bound-type))))
618 ;; If the ARG is a subset of the domain, we don't
619 ;; have to worry about the difference, because that
621 (if (or (null difference)
622 ;; Check whether the arg is within the domain.
623 (domain-subtypep arg domain-low domain-high))
626 (specifier-type `(complex ,bound-type))))))
628 ;; No intersection so the result must be purely complex.
629 (complex-float-type arg)))))
631 (float-or-complex-float-type arg default-low default-high))))))
634 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
635 &key (increasingp t))
636 (let ((num (gensym)))
637 `(defoptimizer (,name derive-type) ((,num))
641 (elfun-derive-type-simple arg #',name
642 ,domain-low ,domain-high
643 ,def-low-bnd ,def-high-bnd
646 ;; These functions are easy because they are defined for the whole
648 (frob exp nil nil 0 nil)
649 (frob sinh nil nil nil nil)
650 (frob tanh nil nil -1 1)
651 (frob asinh nil nil nil nil)
653 ;; These functions are only defined for part of the real line. The
654 ;; condition selects the desired part of the line.
655 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
656 ;; Acos is monotonic decreasing, so we need to swap the function
657 ;; values at the lower and upper bounds of the input domain.
658 (frob acos -1d0 1d0 0 pi :increasingp nil)
659 (frob acosh 1d0 nil nil nil)
660 (frob atanh -1d0 1d0 -1 1)
661 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
663 (frob sqrt -0d0 nil 0 nil))
665 ;;; Compute bounds for (expt x y). This should be easy since (expt x
666 ;;; y) = (exp (* y (log x))). However, computations done this way
667 ;;; have too much roundoff. Thus we have to do it the hard way.
668 (defun safe-expt (x y)
674 ;;; Handle the case when x >= 1.
675 (defun interval-expt-> (x y)
676 (case (sb!c::interval-range-info y 0d0)
678 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
679 ;; obviously non-negative. We just have to be careful for
680 ;; infinite bounds (given by nil).
681 (let ((lo (safe-expt (sb!c::bound-value (sb!c::interval-low x))
682 (sb!c::bound-value (sb!c::interval-low y))))
683 (hi (safe-expt (sb!c::bound-value (sb!c::interval-high x))
684 (sb!c::bound-value (sb!c::interval-high y)))))
685 (list (sb!c::make-interval :low (or lo 1) :high hi))))
687 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
688 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
690 (let ((lo (safe-expt (sb!c::bound-value (sb!c::interval-high x))
691 (sb!c::bound-value (sb!c::interval-low y))))
692 (hi (safe-expt (sb!c::bound-value (sb!c::interval-low x))
693 (sb!c::bound-value (sb!c::interval-high y)))))
694 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
696 ;; Split the interval in half.
697 (destructuring-bind (y- y+)
698 (sb!c::interval-split 0 y t)
699 (list (interval-expt-> x y-)
700 (interval-expt-> x y+))))))
702 ;;; Handle the case when x <= 1
703 (defun interval-expt-< (x y)
704 (case (sb!c::interval-range-info x 0d0)
706 ;; The case of 0 <= x <= 1 is easy
707 (case (sb!c::interval-range-info y)
709 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
710 ;; obviously [0, 1]. We just have to be careful for infinite bounds
712 (let ((lo (safe-expt (sb!c::bound-value (sb!c::interval-low x))
713 (sb!c::bound-value (sb!c::interval-high y))))
714 (hi (safe-expt (sb!c::bound-value (sb!c::interval-high x))
715 (sb!c::bound-value (sb!c::interval-low y)))))
716 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
718 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
719 ;; obviously [1, inf].
720 (let ((hi (safe-expt (sb!c::bound-value (sb!c::interval-low x))
721 (sb!c::bound-value (sb!c::interval-low y))))
722 (lo (safe-expt (sb!c::bound-value (sb!c::interval-high x))
723 (sb!c::bound-value (sb!c::interval-high y)))))
724 (list (sb!c::make-interval :low (or lo 1) :high hi))))
726 ;; Split the interval in half
727 (destructuring-bind (y- y+)
728 (sb!c::interval-split 0 y t)
729 (list (interval-expt-< x y-)
730 (interval-expt-< x y+))))))
732 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
733 ;; The calling function must insure this! For now we'll just
734 ;; return the appropriate unbounded float type.
735 (list (sb!c::make-interval :low nil :high nil)))
737 (destructuring-bind (neg pos)
738 (interval-split 0 x t t)
739 (list (interval-expt-< neg y)
740 (interval-expt-< pos y))))))
742 ;;; Compute bounds for (expt x y).
744 (defun interval-expt (x y)
745 (case (interval-range-info x 1)
748 (interval-expt-> x y))
751 (interval-expt-< x y))
753 (destructuring-bind (left right)
754 (interval-split 1 x t t)
755 (list (interval-expt left y)
756 (interval-expt right y))))))
758 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
759 (declare (ignore x-int))
760 ;; Figure out what the return type should be, given the argument
761 ;; types and bounds and the result type and bounds.
762 (cond ((csubtypep x-type (specifier-type 'integer))
763 ;; An integer to some power. Cases to consider:
764 (case (numeric-type-class y-type)
766 ;; Positive integer to an integer power is either an
767 ;; integer or a rational.
768 (let ((lo (or (interval-low bnd) '*))
769 (hi (or (interval-high bnd) '*)))
770 (if (and (interval-low y-int)
771 (>= (bound-value (interval-low y-int)) 0))
772 (specifier-type `(integer ,lo ,hi))
773 (specifier-type `(rational ,lo ,hi)))))
775 ;; Positive integer to rational power is either a rational
776 ;; or a single-float.
777 (let* ((lo (interval-low bnd))
778 (hi (interval-high bnd))
780 (floor (bound-value lo))
783 (ceiling (bound-value hi))
786 (bound-func #'float lo)
789 (bound-func #'float hi)
791 (specifier-type `(or (rational ,int-lo ,int-hi)
792 (single-float ,f-lo, f-hi)))))
794 ;; Positive integer to a float power is a float.
795 (let ((res (copy-numeric-type y-type)))
796 (setf (numeric-type-low res) (interval-low bnd))
797 (setf (numeric-type-high res) (interval-high bnd))
800 ;; Positive integer to a number is a number (for now).
801 (specifier-type 'number)))
803 ((csubtypep x-type (specifier-type 'rational))
804 ;; a rational to some power
805 (case (numeric-type-class y-type)
807 ;; Positive rational to an integer power is always a rational.
808 (specifier-type `(rational ,(or (interval-low bnd) '*)
809 ,(or (interval-high bnd) '*))))
811 ;; Positive rational to rational power is either a rational
812 ;; or a single-float.
813 (let* ((lo (interval-low bnd))
814 (hi (interval-high bnd))
816 (floor (bound-value lo))
819 (ceiling (bound-value hi))
822 (bound-func #'float lo)
825 (bound-func #'float hi)
827 (specifier-type `(or (rational ,int-lo ,int-hi)
828 (single-float ,f-lo, f-hi)))))
830 ;; Positive rational to a float power is a float.
831 (let ((res (copy-numeric-type y-type)))
832 (setf (numeric-type-low res) (interval-low bnd))
833 (setf (numeric-type-high res) (interval-high bnd))
836 ;; Positive rational to a number is a number (for now).
837 (specifier-type 'number)))
839 ((csubtypep x-type (specifier-type 'float))
840 ;; a float to some power
841 (case (numeric-type-class y-type)
842 ((or integer rational)
843 ;; Positive float to an integer or rational power is
847 :format (numeric-type-format x-type)
848 :low (interval-low bnd)
849 :high (interval-high bnd)))
851 ;; Positive float to a float power is a float of the higher type.
854 :format (float-format-max (numeric-type-format x-type)
855 (numeric-type-format y-type))
856 :low (interval-low bnd)
857 :high (interval-high bnd)))
859 ;; Positive float to a number is a number (for now)
860 (specifier-type 'number))))
862 ;; A number to some power is a number.
863 (specifier-type 'number))))
865 (defun merged-interval-expt (x y)
866 (let* ((x-int (numeric-type->interval x))
867 (y-int (numeric-type->interval y)))
868 (mapcar #'(lambda (type)
869 (fixup-interval-expt type x-int y-int x y))
870 (flatten-list (interval-expt x-int y-int)))))
872 (defun expt-derive-type-aux (x y same-arg)
873 (declare (ignore same-arg))
874 (cond ((or (not (numeric-type-real-p x))
875 (not (numeric-type-real-p y)))
876 ;; Use numeric contagion if either is not real.
877 (numeric-contagion x y))
878 ((csubtypep y (specifier-type 'integer))
879 ;; A real raised to an integer power is well-defined.
880 (merged-interval-expt x y))
882 ;; A real raised to a non-integral power can be a float or a
884 (cond ((or (csubtypep x (specifier-type '(rational 0)))
885 (csubtypep x (specifier-type '(float (0d0)))))
886 ;; But a positive real to any power is well-defined.
887 (merged-interval-expt x y))
889 ;; A real to some power. The result could be a real
891 (float-or-complex-float-type (numeric-contagion x y)))))))
893 (defoptimizer (expt derive-type) ((x y))
894 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
896 ;;; Note we must assume that a type including 0.0 may also include
897 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
898 (defun log-derive-type-aux-1 (x)
899 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
901 (defun log-derive-type-aux-2 (x y same-arg)
902 (let ((log-x (log-derive-type-aux-1 x))
903 (log-y (log-derive-type-aux-1 y))
905 ;; log-x or log-y might be union types. We need to run through
906 ;; the union types ourselves because /-derive-type-aux doesn't.
907 (dolist (x-type (prepare-arg-for-derive-type log-x))
908 (dolist (y-type (prepare-arg-for-derive-type log-y))
909 (push (/-derive-type-aux x-type y-type same-arg) result)))
910 (setf result (flatten-list result))
912 (make-union-type-or-something result)
915 (defoptimizer (log derive-type) ((x &optional y))
917 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
918 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
920 (defun atan-derive-type-aux-1 (y)
921 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
923 (defun atan-derive-type-aux-2 (y x same-arg)
924 (declare (ignore same-arg))
925 ;; The hard case with two args. We just return the max bounds.
926 (let ((result-type (numeric-contagion y x)))
927 (cond ((and (numeric-type-real-p x)
928 (numeric-type-real-p y))
929 (let* ((format (case (numeric-type-class result-type)
930 ((integer rational) 'single-float)
931 (t (numeric-type-format result-type))))
932 (bound-format (or format 'float)))
933 (make-numeric-type :class 'float
936 :low (coerce (- pi) bound-format)
937 :high (coerce pi bound-format))))
939 ;; The result is a float or a complex number
940 (float-or-complex-float-type result-type)))))
942 (defoptimizer (atan derive-type) ((y &optional x))
944 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
945 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
947 (defun cosh-derive-type-aux (x)
948 ;; We note that cosh x = cosh |x| for all real x.
949 (elfun-derive-type-simple
950 (if (numeric-type-real-p x)
951 (abs-derive-type-aux x)
953 #'cosh nil nil 0 nil))
955 (defoptimizer (cosh derive-type) ((num))
956 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
958 (defun phase-derive-type-aux (arg)
959 (let* ((format (case (numeric-type-class arg)
960 ((integer rational) 'single-float)
961 (t (numeric-type-format arg))))
962 (bound-type (or format 'float)))
963 (cond ((numeric-type-real-p arg)
964 (case (interval-range-info (numeric-type->interval arg) 0.0)
966 ;; The number is positive, so the phase is 0.
967 (make-numeric-type :class 'float
970 :low (coerce 0 bound-type)
971 :high (coerce 0 bound-type)))
973 ;; The number is always negative, so the phase is pi.
974 (make-numeric-type :class 'float
977 :low (coerce pi bound-type)
978 :high (coerce pi bound-type)))
980 ;; We can't tell. The result is 0 or pi. Use a union
983 (make-numeric-type :class 'float
986 :low (coerce 0 bound-type)
987 :high (coerce 0 bound-type))
988 (make-numeric-type :class 'float
991 :low (coerce pi bound-type)
992 :high (coerce pi bound-type))))))
994 ;; We have a complex number. The answer is the range -pi
995 ;; to pi. (-pi is included because we have -0.)
996 (make-numeric-type :class 'float
999 :low (coerce (- pi) bound-type)
1000 :high (coerce pi bound-type))))))
1002 (defoptimizer (phase derive-type) ((num))
1003 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1007 (deftransform realpart ((x) ((complex rational)) *)
1008 '(sb!kernel:%realpart x))
1009 (deftransform imagpart ((x) ((complex rational)) *)
1010 '(sb!kernel:%imagpart x))
1012 ;;; Make REALPART and IMAGPART return the appropriate types. This
1013 ;;; should help a lot in optimized code.
1014 (defun realpart-derive-type-aux (type)
1015 (let ((class (numeric-type-class type))
1016 (format (numeric-type-format type)))
1017 (cond ((numeric-type-real-p type)
1018 ;; The realpart of a real has the same type and range as
1020 (make-numeric-type :class class
1023 :low (numeric-type-low type)
1024 :high (numeric-type-high type)))
1026 ;; We have a complex number. The result has the same type
1027 ;; as the real part, except that it's real, not complex,
1029 (make-numeric-type :class class
1032 :low (numeric-type-low type)
1033 :high (numeric-type-high type))))))
1034 #!+(or propagate-fun-type propagate-float-type)
1035 (defoptimizer (realpart derive-type) ((num))
1036 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1037 (defun imagpart-derive-type-aux (type)
1038 (let ((class (numeric-type-class type))
1039 (format (numeric-type-format type)))
1040 (cond ((numeric-type-real-p type)
1041 ;; The imagpart of a real has the same type as the input,
1042 ;; except that it's zero.
1043 (let ((bound-format (or format class 'real)))
1044 (make-numeric-type :class class
1047 :low (coerce 0 bound-format)
1048 :high (coerce 0 bound-format))))
1050 ;; We have a complex number. The result has the same type as
1051 ;; the imaginary part, except that it's real, not complex,
1053 (make-numeric-type :class class
1056 :low (numeric-type-low type)
1057 :high (numeric-type-high type))))))
1058 #!+(or propagate-fun-type propagate-float-type)
1059 (defoptimizer (imagpart derive-type) ((num))
1060 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1062 (defun complex-derive-type-aux-1 (re-type)
1063 (if (numeric-type-p re-type)
1064 (make-numeric-type :class (numeric-type-class re-type)
1065 :format (numeric-type-format re-type)
1066 :complexp (if (csubtypep re-type
1067 (specifier-type 'rational))
1070 :low (numeric-type-low re-type)
1071 :high (numeric-type-high re-type))
1072 (specifier-type 'complex)))
1074 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1075 (declare (ignore same-arg))
1076 (if (and (numeric-type-p re-type)
1077 (numeric-type-p im-type))
1078 ;; Need to check to make sure numeric-contagion returns the
1079 ;; right type for what we want here.
1081 ;; Also, what about rational canonicalization, like (complex 5 0)
1082 ;; is 5? So, if the result must be complex, we make it so.
1083 ;; If the result might be complex, which happens only if the
1084 ;; arguments are rational, we make it a union type of (or
1085 ;; rational (complex rational)).
1086 (let* ((element-type (numeric-contagion re-type im-type))
1087 (rat-result-p (csubtypep element-type
1088 (specifier-type 'rational))))
1090 (make-union-type-or-something
1093 `(complex ,(numeric-type-class element-type)))))
1094 (make-numeric-type :class (numeric-type-class element-type)
1095 :format (numeric-type-format element-type)
1096 :complexp (if rat-result-p
1099 (specifier-type 'complex)))
1101 #!+(or propagate-fun-type propagate-float-type)
1102 (defoptimizer (complex derive-type) ((re &optional im))
1104 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1105 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1107 ;;; Define some transforms for complex operations. We do this in lieu
1108 ;;; of complex operation VOPs.
1109 (macrolet ((frob (type)
1112 (deftransform %negate ((z) ((complex ,type)) *)
1113 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1114 ;; complex addition and subtraction
1115 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1116 '(complex (+ (realpart w) (realpart z))
1117 (+ (imagpart w) (imagpart z))))
1118 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1119 '(complex (- (realpart w) (realpart z))
1120 (- (imagpart w) (imagpart z))))
1121 ;; Add and subtract a complex and a real.
1122 (deftransform + ((w z) ((complex ,type) real) *)
1123 '(complex (+ (realpart w) z) (imagpart w)))
1124 (deftransform + ((z w) (real (complex ,type)) *)
1125 '(complex (+ (realpart w) z) (imagpart w)))
1126 ;; Add and subtract a real and a complex number.
1127 (deftransform - ((w z) ((complex ,type) real) *)
1128 '(complex (- (realpart w) z) (imagpart w)))
1129 (deftransform - ((z w) (real (complex ,type)) *)
1130 '(complex (- z (realpart w)) (- (imagpart w))))
1131 ;; Multiply and divide two complex numbers.
1132 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1133 '(let* ((rx (realpart x))
1137 (complex (- (* rx ry) (* ix iy))
1138 (+ (* rx iy) (* ix ry)))))
1139 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1140 '(let* ((rx (realpart x))
1144 (if (> (abs ry) (abs iy))
1145 (let* ((r (/ iy ry))
1146 (dn (* ry (+ 1 (* r r)))))
1147 (complex (/ (+ rx (* ix r)) dn)
1148 (/ (- ix (* rx r)) dn)))
1149 (let* ((r (/ ry iy))
1150 (dn (* iy (+ 1 (* r r)))))
1151 (complex (/ (+ (* rx r) ix) dn)
1152 (/ (- (* ix r) rx) dn))))))
1153 ;; Multiply a complex by a real or vice versa.
1154 (deftransform * ((w z) ((complex ,type) real) *)
1155 '(complex (* (realpart w) z) (* (imagpart w) z)))
1156 (deftransform * ((z w) (real (complex ,type)) *)
1157 '(complex (* (realpart w) z) (* (imagpart w) z)))
1158 ;; Divide a complex by a real.
1159 (deftransform / ((w z) ((complex ,type) real) *)
1160 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1161 ;; conjugate of complex number
1162 (deftransform conjugate ((z) ((complex ,type)) *)
1163 '(complex (realpart z) (- (imagpart z))))
1165 (deftransform cis ((z) ((,type)) *)
1166 '(complex (cos z) (sin z)))
1168 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1169 '(and (= (realpart w) (realpart z))
1170 (= (imagpart w) (imagpart z))))
1171 (deftransform = ((w z) ((complex ,type) real) *)
1172 '(and (= (realpart w) z) (zerop (imagpart w))))
1173 (deftransform = ((w z) (real (complex ,type)) *)
1174 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1177 (frob double-float))
1179 ;;; Here are simple optimizers for sin, cos, and tan. They do not
1180 ;;; produce a minimal range for the result; the result is the widest
1181 ;;; possible answer. This gets around the problem of doing range
1182 ;;; reduction correctly but still provides useful results when the
1183 ;;; inputs are union types.
1185 #!+propagate-fun-type
1187 (defun trig-derive-type-aux (arg domain fcn
1188 &optional def-lo def-hi (increasingp t))
1191 (cond ((eq (numeric-type-complexp arg) :complex)
1192 (make-numeric-type :class (numeric-type-class arg)
1193 :format (numeric-type-format arg)
1194 :complexp :complex))
1195 ((numeric-type-real-p arg)
1196 (let* ((format (case (numeric-type-class arg)
1197 ((integer rational) 'single-float)
1198 (t (numeric-type-format arg))))
1199 (bound-type (or format 'float)))
1200 ;; If the argument is a subset of the "principal" domain
1201 ;; of the function, we can compute the bounds because
1202 ;; the function is monotonic. We can't do this in
1203 ;; general for these periodic functions because we can't
1204 ;; (and don't want to) do the argument reduction in
1205 ;; exactly the same way as the functions themselves do
1207 (if (csubtypep arg domain)
1208 (let ((res-lo (bound-func fcn (numeric-type-low arg)))
1209 (res-hi (bound-func fcn (numeric-type-high arg))))
1211 (rotatef res-lo res-hi))
1215 :low (coerce-numeric-bound res-lo bound-type)
1216 :high (coerce-numeric-bound res-hi bound-type)))
1220 :low (and def-lo (coerce def-lo bound-type))
1221 :high (and def-hi (coerce def-hi bound-type))))))
1223 (float-or-complex-float-type arg def-lo def-hi))))))
1225 (defoptimizer (sin derive-type) ((num))
1226 (one-arg-derive-type
1229 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1230 (trig-derive-type-aux
1232 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1237 (defoptimizer (cos derive-type) ((num))
1238 (one-arg-derive-type
1241 ;; Derive the bounds if the arg is in [0, pi].
1242 (trig-derive-type-aux arg
1243 (specifier-type `(float 0d0 ,pi))
1249 (defoptimizer (tan derive-type) ((num))
1250 (one-arg-derive-type
1253 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1254 (trig-derive-type-aux arg
1255 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1260 ;;; CONJUGATE always returns the same type as the input type.
1261 (defoptimizer (conjugate derive-type) ((num))
1262 (continuation-type num))
1264 (defoptimizer (cis derive-type) ((num))
1265 (one-arg-derive-type num
1267 (sb!c::specifier-type
1268 `(complex ,(or (numeric-type-format arg) 'float))))