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 ;;; Return an expression to generate an integer of N-BITS many random
50 ;;; bits, using the minimal number of random chunks possible.
51 (defun generate-random-expr-for-power-of-2 (n-bits state
)
52 (declare (type (integer 1 #.sb
!vm
:n-word-bits
) n-bits
))
53 (multiple-value-bind (n-chunk-bits chunk-expr
)
54 (cond ((<= n-bits n-random-chunk-bits
)
55 (values n-random-chunk-bits
`(random-chunk ,state
)))
56 ((<= n-bits
(* 2 n-random-chunk-bits
))
57 (values (* 2 n-random-chunk-bits
) `(big-random-chunk ,state
)))
59 (error "Unexpectedly small N-RANDOM-CHUNK-BITS")))
60 (if (< n-bits n-chunk-bits
)
61 `(logand ,(1- (ash 1 n-bits
)) ,chunk-expr
)
64 ;;; This transform for compile-time constant word-sized integers
65 ;;; generates an accept-reject loop to achieve equidistribution of the
66 ;;; returned values. Several optimizations are done: If NUM is a power
67 ;;; of two no loop is needed. If the random chunk size is half the word
68 ;;; size only one chunk is used where sufficient. For values of NUM
69 ;;; where it is possible and results in faster code, the rejection
70 ;;; probability is reduced by accepting all values below the largest
71 ;;; multiple of the limit that fits into one or two chunks and and doing
72 ;;; a division to get the random value into the desired range.
73 (deftransform random
((num &optional state
)
74 ((constant-arg (integer 1 #.
(expt 2 sb
!vm
:n-word-bits
)))
77 :policy
(and (> speed compilation-speed
)
79 "optimize to inlined RANDOM-CHUNK operations"
80 (let ((num (lvar-value num
)))
83 (flet ((chunk-n-bits-and-expr (n-bits)
84 (cond ((<= n-bits n-random-chunk-bits
)
85 (values n-random-chunk-bits
86 '(random-chunk (or state
*random-state
*))))
87 ((<= n-bits
(* 2 n-random-chunk-bits
))
88 (values (* 2 n-random-chunk-bits
)
89 '(big-random-chunk (or state
*random-state
*))))
91 (error "Unexpectedly small N-RANDOM-CHUNK-BITS")))))
92 (if (zerop (logand num
(1- num
)))
93 ;; NUM is a power of 2.
94 (let ((n-bits (integer-length (1- num
))))
95 (multiple-value-bind (n-chunk-bits chunk-expr
)
96 (chunk-n-bits-and-expr n-bits
)
97 (if (< n-bits n-chunk-bits
)
98 `(logand ,(1- (ash 1 n-bits
)) ,chunk-expr
)
100 ;; Generate an accept-reject loop.
101 (let ((n-bits (integer-length num
)))
102 (multiple-value-bind (n-chunk-bits chunk-expr
)
103 (chunk-n-bits-and-expr n-bits
)
104 (if (or (> (* num
3) (expt 2 n-chunk-bits
))
105 (logbitp (- n-bits
2) num
))
106 ;; Division can't help as the quotient is below 3,
107 ;; or is too costly as the rejection probability
108 ;; without it is already small (namely at most 1/4
109 ;; with the given test, which is experimentally a
110 ;; reasonable threshold and cheap to test for).
112 (let ((bits ,(generate-random-expr-for-power-of-2
113 n-bits
'(or state
*random-state
*))))
116 (let ((d (truncate (expt 2 n-chunk-bits
) num
)))
118 (let ((bits ,chunk-expr
))
119 (when (< bits
,(* num d
))
120 (return (values (truncate bits
,d
)))))))))))))))
125 (defknown make-single-float
((signed-byte 32)) single-float
128 (defknown make-double-float
((signed-byte 32) (unsigned-byte 32)) double-float
132 (deftransform make-single-float
((bits)
134 "Conditional constant folding"
135 (unless (constant-lvar-p bits
)
136 (give-up-ir1-transform))
137 (let* ((bits (lvar-value bits
))
138 (float (make-single-float bits
)))
139 (when (float-nan-p float
)
140 (give-up-ir1-transform))
144 (deftransform make-double-float
((hi lo
)
145 ((signed-byte 32) (unsigned-byte 32)))
146 "Conditional constant folding"
147 (unless (and (constant-lvar-p hi
)
148 (constant-lvar-p lo
))
149 (give-up-ir1-transform))
150 (let* ((hi (lvar-value hi
))
152 (float (make-double-float hi lo
)))
153 (when (float-nan-p float
)
154 (give-up-ir1-transform))
157 (defknown single-float-bits
(single-float) (signed-byte 32)
158 (movable foldable flushable
))
160 (defknown double-float-high-bits
(double-float) (signed-byte 32)
161 (movable foldable flushable
))
163 (defknown double-float-low-bits
(double-float) (unsigned-byte 32)
164 (movable foldable flushable
))
166 (deftransform float-sign
((float &optional float2
)
167 (single-float &optional single-float
) *)
169 (let ((temp (gensym)))
170 `(let ((,temp
(abs float2
)))
171 (if (minusp (single-float-bits float
)) (- ,temp
) ,temp
)))
172 '(if (minusp (single-float-bits float
)) -
1f0
1f0
)))
174 (deftransform float-sign
((float &optional float2
)
175 (double-float &optional double-float
) *)
177 (let ((temp (gensym)))
178 `(let ((,temp
(abs float2
)))
179 (if (minusp (double-float-high-bits float
)) (- ,temp
) ,temp
)))
180 '(if (minusp (double-float-high-bits float
)) -
1d0
1d0
)))
182 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
184 (defknown decode-single-float
(single-float)
185 (values single-float single-float-exponent
(single-float -
1f0
1f0
))
186 (movable foldable flushable
))
188 (defknown decode-double-float
(double-float)
189 (values double-float double-float-exponent
(double-float -
1d0
1d0
))
190 (movable foldable flushable
))
192 (defknown integer-decode-single-float
(single-float)
193 (values single-float-significand single-float-int-exponent
(integer -
1 1))
194 (movable foldable flushable
))
196 (defknown integer-decode-double-float
(double-float)
197 (values double-float-significand double-float-int-exponent
(integer -
1 1))
198 (movable foldable flushable
))
200 (defknown scale-single-float
(single-float integer
) single-float
201 (movable foldable flushable
))
203 (defknown scale-double-float
(double-float integer
) double-float
204 (movable foldable flushable
))
206 (deftransform decode-float
((x) (single-float) *)
207 '(decode-single-float x
))
209 (deftransform decode-float
((x) (double-float) *)
210 '(decode-double-float x
))
212 (deftransform integer-decode-float
((x) (single-float) *)
213 '(integer-decode-single-float x
))
215 (deftransform integer-decode-float
((x) (double-float) *)
216 '(integer-decode-double-float x
))
218 (deftransform scale-float
((f ex
) (single-float *) *)
219 (if (and #!+x86 t
#!-x86 nil
220 (csubtypep (lvar-type ex
)
221 (specifier-type '(signed-byte 32))))
222 '(coerce (%scalbn
(coerce f
'double-float
) ex
) 'single-float
)
223 '(scale-single-float f ex
)))
225 (deftransform scale-float
((f ex
) (double-float *) *)
226 (if (and #!+x86 t
#!-x86 nil
227 (csubtypep (lvar-type ex
)
228 (specifier-type '(signed-byte 32))))
230 '(scale-double-float f ex
)))
232 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
234 ;;; SBCL's own implementation of floating point supports floating
235 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
236 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
237 ;;; floating point support. Thus, we have to avoid running it on the
238 ;;; cross-compilation host, since we're not guaranteed that the
239 ;;; cross-compilation host will support floating point infinities.
241 ;;; If we wanted to live dangerously, we could conditionalize the code
242 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
243 ;;; host happened to be SBCL, we'd be able to run the infinity-using
245 ;;; * SBCL itself gets built with more complete optimization.
247 ;;; * You get a different SBCL depending on what your cross-compilation
249 ;;; So far the pros and cons seem seem to be mostly academic, since
250 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
251 ;;; actually important in compiling SBCL itself. If this changes, then
252 ;;; we have to decide:
253 ;;; * Go for simplicity, leaving things as they are.
254 ;;; * Go for performance at the expense of conceptual clarity,
255 ;;; using #+(OR SBCL SB-XC) and otherwise leaving the build
257 ;;; * Go for performance at the expense of build time, using
258 ;;; #+(OR SBCL SB-XC) and also making SBCL do not just
259 ;;; make-host-1.sh and make-host-2.sh, but a third step
260 ;;; make-host-3.sh where it builds itself under itself. (Such a
261 ;;; 3-step build process could also help with other things, e.g.
262 ;;; using specialized arrays to represent debug information.)
263 ;;; * Rewrite the code so that it doesn't depend on unportable
264 ;;; floating point infinities.
266 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
267 ;;; are computed for the result, if possible.
268 #-sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
271 (defun scale-float-derive-type-aux (f ex same-arg
)
272 (declare (ignore same-arg
))
273 (flet ((scale-bound (x n
)
274 ;; We need to be a bit careful here and catch any overflows
275 ;; that might occur. We can ignore underflows which become
279 (scale-float (type-bound-number x
) n
)
280 (floating-point-overflow ()
283 (when (and (numeric-type-p f
) (numeric-type-p ex
))
284 (let ((f-lo (numeric-type-low f
))
285 (f-hi (numeric-type-high f
))
286 (ex-lo (numeric-type-low ex
))
287 (ex-hi (numeric-type-high ex
))
291 (if (< (float-sign (type-bound-number f-hi
)) 0.0)
293 (setf new-hi
(scale-bound f-hi ex-lo
)))
295 (setf new-hi
(scale-bound f-hi ex-hi
)))))
297 (if (< (float-sign (type-bound-number f-lo
)) 0.0)
299 (setf new-lo
(scale-bound f-lo ex-hi
)))
301 (setf new-lo
(scale-bound f-lo ex-lo
)))))
302 (make-numeric-type :class
(numeric-type-class f
)
303 :format
(numeric-type-format f
)
307 (defoptimizer (scale-single-float derive-type
) ((f ex
))
308 (two-arg-derive-type f ex
#'scale-float-derive-type-aux
309 #'scale-single-float t
))
310 (defoptimizer (scale-double-float derive-type
) ((f ex
))
311 (two-arg-derive-type f ex
#'scale-float-derive-type-aux
312 #'scale-double-float t
))
314 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
315 ;;; FLOAT function return the correct ranges if the input has some
316 ;;; defined range. Quite useful if we want to convert some type of
317 ;;; bounded integer into a float.
319 ((frob (fun type most-negative most-positive
)
320 (let ((aux-name (symbolicate fun
"-DERIVE-TYPE-AUX")))
322 (defun ,aux-name
(num)
323 ;; When converting a number to a float, the limits are
325 (let* ((lo (bound-func (lambda (x)
326 (if (< x
,most-negative
)
329 (numeric-type-low num
)
331 (hi (bound-func (lambda (x)
332 (if (< ,most-positive x
)
335 (numeric-type-high num
)
337 (specifier-type `(,',type
,(or lo
'*) ,(or hi
'*)))))
339 (defoptimizer (,fun derive-type
) ((num))
341 (one-arg-derive-type num
#',aux-name
#',fun
)
344 (frob %single-float single-float
345 most-negative-single-float most-positive-single-float
)
346 (frob %double-float double-float
347 most-negative-double-float most-positive-double-float
))
352 (defun safe-ctype-for-single-coercion-p (x)
353 ;; See comment in SAFE-SINGLE-COERCION-P -- this deals with the same
354 ;; problem, but in the context of evaluated and compiled (+ <int> <single>)
355 ;; giving different result if we fail to check for this.
356 (or (not (csubtypep x
(specifier-type 'integer
)))
358 (csubtypep x
(specifier-type `(integer ,most-negative-exactly-single-float-fixnum
359 ,most-positive-exactly-single-float-fixnum
)))
361 (csubtypep x
(specifier-type 'fixnum
))))
363 ;;; Do some stuff to recognize when the loser is doing mixed float and
364 ;;; rational arithmetic, or different float types, and fix it up. If
365 ;;; we don't, he won't even get so much as an efficiency note.
366 (deftransform float-contagion-arg1
((x y
) * * :defun-only t
:node node
)
367 (if (or (not (types-equal-or-intersect (lvar-type y
) (specifier-type 'single-float
)))
368 (safe-ctype-for-single-coercion-p (lvar-type x
)))
369 `(,(lvar-fun-name (basic-combination-fun node
))
371 (give-up-ir1-transform)))
372 (deftransform float-contagion-arg2
((x y
) * * :defun-only t
:node node
)
373 (if (or (not (types-equal-or-intersect (lvar-type x
) (specifier-type 'single-float
)))
374 (safe-ctype-for-single-coercion-p (lvar-type y
)))
375 `(,(lvar-fun-name (basic-combination-fun node
))
377 (give-up-ir1-transform)))
379 (dolist (x '(+ * / -
))
380 (%deftransform x
'(function (rational float
) *) #'float-contagion-arg1
)
381 (%deftransform x
'(function (float rational
) *) #'float-contagion-arg2
))
383 (dolist (x '(= < > + * / -
))
384 (%deftransform x
'(function (single-float double-float
) *)
385 #'float-contagion-arg1
)
386 (%deftransform x
'(function (double-float single-float
) *)
387 #'float-contagion-arg2
))
389 (macrolet ((def (type &rest args
)
390 `(deftransform * ((x y
) (,type
(constant-arg (member ,@args
))) *
392 :policy
(zerop float-accuracy
))
393 "optimize multiplication by one"
394 (let ((y (lvar-value y
)))
398 (def single-float
1.0 -
1.0)
399 (def double-float
1.0d0 -
1.0d0
))
401 ;;; Return the reciprocal of X if it can be represented exactly, NIL otherwise.
402 (defun maybe-exact-reciprocal (x)
405 (multiple-value-bind (significand exponent sign
)
406 (integer-decode-float x
)
407 ;; only powers of 2 can be inverted exactly
408 (unless (zerop (logand significand
(1- significand
)))
409 (return-from maybe-exact-reciprocal nil
))
410 (let ((expected (/ sign significand
(expt 2 exponent
)))
412 (multiple-value-bind (significand exponent sign
)
413 (integer-decode-float reciprocal
)
414 ;; Denorms can't be inverted safely.
415 (and (eql expected
(* sign significand
(expt 2 exponent
)))
417 (error () (return-from maybe-exact-reciprocal nil
)))))
419 ;;; Replace constant division by multiplication with exact reciprocal,
421 (macrolet ((def (type)
422 `(deftransform / ((x y
) (,type
(constant-arg ,type
)) *
424 "convert to multiplication by reciprocal"
425 (let ((n (lvar-value y
)))
426 (if (policy node
(zerop float-accuracy
))
428 (let ((r (maybe-exact-reciprocal n
)))
431 (give-up-ir1-transform
432 "~S does not have an exact reciprocal"
437 ;;; Optimize addition and subtraction of zero
438 (macrolet ((def (op type
&rest args
)
439 `(deftransform ,op
((x y
) (,type
(constant-arg (member ,@args
))) *
441 :policy
(zerop float-accuracy
))
443 ;; No signed zeros, thanks.
444 (def + single-float
0 0.0)
445 (def - single-float
0 0.0)
446 (def + double-float
0 0.0 0.0d0
)
447 (def - double-float
0 0.0 0.0d0
))
449 ;;; On most platforms (+ x x) is faster than (* x 2)
450 (macrolet ((def (type &rest args
)
451 `(deftransform * ((x y
) (,type
(constant-arg (member ,@args
))))
453 (def single-float
2 2.0)
454 (def double-float
2 2.0 2.0d0
))
456 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
457 ;;; general float rational args to comparison, since Common Lisp
458 ;;; semantics says we are supposed to compare as rationals, but we can
459 ;;; do it for any rational that has a precise representation as a
460 ;;; float (such as 0).
461 (macrolet ((frob (op)
462 `(deftransform ,op
((x y
) (float rational
) *)
463 "open-code FLOAT to RATIONAL comparison"
464 (unless (constant-lvar-p y
)
465 (give-up-ir1-transform
466 "The RATIONAL value isn't known at compile time."))
467 (let ((val (lvar-value y
)))
468 (unless (eql (rational (float val
)) val
)
469 (give-up-ir1-transform
470 "~S doesn't have a precise float representation."
472 `(,',op x
(float y x
)))))
477 ;;;; irrational derive-type methods
479 ;;; Derive the result to be float for argument types in the
480 ;;; appropriate domain.
481 #+sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
482 (dolist (stuff '((asin (real -
1.0 1.0))
483 (acos (real -
1.0 1.0))
485 (atanh (real -
1.0 1.0))
487 (destructuring-bind (name type
) stuff
488 (let ((type (specifier-type type
)))
489 (setf (fun-info-derive-type (fun-info-or-lose name
))
491 (declare (type combination call
))
492 (when (csubtypep (lvar-type
493 (first (combination-args call
)))
495 (specifier-type 'float
)))))))
497 #+sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
498 (defoptimizer (log derive-type
) ((x &optional y
))
499 (when (and (csubtypep (lvar-type x
)
500 (specifier-type '(real 0.0)))
502 (csubtypep (lvar-type y
)
503 (specifier-type '(real 0.0)))))
504 (specifier-type 'float
)))
506 ;;;; irrational transforms
508 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick
)
509 (double-float) double-float
510 (movable foldable flushable
))
512 (defknown (%sin %cos %tanh %sin-quick %cos-quick
)
513 (double-float) (double-float -
1.0d0
1.0d0
)
514 (movable foldable flushable
))
516 (defknown (%asin %atan
)
518 (double-float #.
(coerce (- (/ pi
2)) 'double-float
)
519 #.
(coerce (/ pi
2) 'double-float
))
520 (movable foldable flushable
))
523 (double-float) (double-float 0.0d0
#.
(coerce pi
'double-float
))
524 (movable foldable flushable
))
527 (double-float) (double-float 1.0d0
)
528 (movable foldable flushable
))
530 (defknown (%acosh %exp %sqrt
)
531 (double-float) (double-float 0.0d0
)
532 (movable foldable flushable
))
535 (double-float) (double-float -
1d0
)
536 (movable foldable flushable
))
539 (double-float double-float
) (double-float 0d0
)
540 (movable foldable flushable
))
543 (double-float double-float
) double-float
544 (movable foldable flushable
))
547 (double-float double-float
)
548 (double-float #.
(coerce (- pi
) 'double-float
)
549 #.
(coerce pi
'double-float
))
550 (movable foldable flushable
))
553 (double-float double-float
) double-float
554 (movable foldable flushable
))
557 (double-float (signed-byte 32)) double-float
558 (movable foldable flushable
))
561 (double-float) double-float
562 (movable foldable flushable
))
564 (macrolet ((def (name prim rtype
)
566 (deftransform ,name
((x) (single-float) ,rtype
)
567 `(coerce (,',prim
(coerce x
'double-float
)) 'single-float
))
568 (deftransform ,name
((x) (double-float) ,rtype
)
572 (def sqrt %sqrt float
)
573 (def asin %asin float
)
574 (def acos %acos float
)
580 (def acosh %acosh float
)
581 (def atanh %atanh float
))
583 ;;; The argument range is limited on the x86 FP trig. functions. A
584 ;;; post-test can detect a failure (and load a suitable result), but
585 ;;; this test is avoided if possible.
586 (macrolet ((def (name prim prim-quick
)
587 (declare (ignorable prim-quick
))
589 (deftransform ,name
((x) (single-float) *)
590 #!+x86
(cond ((csubtypep (lvar-type x
)
591 (specifier-type '(single-float
592 (#.
(- (expt 2f0
63)))
594 `(coerce (,',prim-quick
(coerce x
'double-float
))
598 "unable to avoid inline argument range check~@
599 because the argument range (~S) was not within 2^63"
600 (type-specifier (lvar-type x
)))
601 `(coerce (,',prim
(coerce x
'double-float
)) 'single-float
)))
602 #!-x86
`(coerce (,',prim
(coerce x
'double-float
)) 'single-float
))
603 (deftransform ,name
((x) (double-float) *)
604 #!+x86
(cond ((csubtypep (lvar-type x
)
605 (specifier-type '(double-float
606 (#.
(- (expt 2d0
63)))
611 "unable to avoid inline argument range check~@
612 because the argument range (~S) was not within 2^63"
613 (type-specifier (lvar-type x
)))
615 #!-x86
`(,',prim x
)))))
616 (def sin %sin %sin-quick
)
617 (def cos %cos %cos-quick
)
618 (def tan %tan %tan-quick
))
620 (deftransform atan
((x y
) (single-float single-float
) *)
621 `(coerce (%atan2
(coerce x
'double-float
) (coerce y
'double-float
))
623 (deftransform atan
((x y
) (double-float double-float
) *)
626 (deftransform expt
((x y
) ((single-float 0f0
) single-float
) *)
627 `(coerce (%pow
(coerce x
'double-float
) (coerce y
'double-float
))
629 (deftransform expt
((x y
) ((double-float 0d0
) double-float
) *)
631 (deftransform expt
((x y
) ((single-float 0f0
) (signed-byte 32)) *)
632 `(coerce (%pow
(coerce x
'double-float
) (coerce y
'double-float
))
634 (deftransform expt
((x y
) ((double-float 0d0
) (signed-byte 32)) *)
635 `(%pow x
(coerce y
'double-float
)))
637 ;;; ANSI says log with base zero returns zero.
638 (deftransform log
((x y
) (float float
) float
)
639 '(if (zerop y
) y
(/ (log x
) (log y
))))
641 ;;; Handle some simple transformations.
643 (deftransform abs
((x) ((complex double-float
)) double-float
)
644 '(%hypot
(realpart x
) (imagpart x
)))
646 (deftransform abs
((x) ((complex single-float
)) single-float
)
647 '(coerce (%hypot
(coerce (realpart x
) 'double-float
)
648 (coerce (imagpart x
) 'double-float
))
651 (deftransform phase
((x) ((complex double-float
)) double-float
)
652 '(%atan2
(imagpart x
) (realpart x
)))
654 (deftransform phase
((x) ((complex single-float
)) single-float
)
655 '(coerce (%atan2
(coerce (imagpart x
) 'double-float
)
656 (coerce (realpart x
) 'double-float
))
659 (deftransform phase
((x) ((float)) float
)
660 '(if (minusp (float-sign x
))
664 ;;; The number is of type REAL.
665 (defun numeric-type-real-p (type)
666 (and (numeric-type-p type
)
667 (eq (numeric-type-complexp type
) :real
)))
669 ;;; Coerce a numeric type bound to the given type while handling
670 ;;; exclusive bounds.
671 (defun coerce-numeric-bound (bound type
)
674 (list (coerce (car bound
) type
))
675 (coerce bound type
))))
677 #-sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
680 ;;;; optimizers for elementary functions
682 ;;;; These optimizers compute the output range of the elementary
683 ;;;; function, based on the domain of the input.
685 ;;; Generate a specifier for a complex type specialized to the same
686 ;;; type as the argument.
687 (defun complex-float-type (arg)
688 (declare (type numeric-type arg
))
689 (let* ((format (case (numeric-type-class arg
)
690 ((integer rational
) 'single-float
)
691 (t (numeric-type-format arg
))))
692 (float-type (or format
'float
)))
693 (specifier-type `(complex ,float-type
))))
695 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
696 ;;; should be the right kind of float. Allow bounds for the float
698 (defun float-or-complex-float-type (arg &optional lo hi
)
699 (declare (type numeric-type arg
))
700 (let* ((format (case (numeric-type-class arg
)
701 ((integer rational
) 'single-float
)
702 (t (numeric-type-format arg
))))
703 (float-type (or format
'float
))
704 (lo (coerce-numeric-bound lo float-type
))
705 (hi (coerce-numeric-bound hi float-type
)))
706 (specifier-type `(or (,float-type
,(or lo
'*) ,(or hi
'*))
707 (complex ,float-type
)))))
711 (eval-when (:compile-toplevel
:execute
)
712 ;; So the problem with this hack is that it's actually broken. If
713 ;; the host does not have long floats, then setting *R-D-F-F* to
714 ;; LONG-FLOAT doesn't actually buy us anything. FIXME.
715 (setf *read-default-float-format
*
716 #!+long-float
'long-float
#!-long-float
'double-float
))
717 ;;; Test whether the numeric-type ARG is within the domain specified by
718 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
720 #-sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
721 (defun domain-subtypep (arg domain-low domain-high
)
722 (declare (type numeric-type arg
)
723 (type (or real null
) domain-low domain-high
))
724 (let* ((arg-lo (numeric-type-low arg
))
725 (arg-lo-val (type-bound-number arg-lo
))
726 (arg-hi (numeric-type-high arg
))
727 (arg-hi-val (type-bound-number arg-hi
)))
728 ;; Check that the ARG bounds are correctly canonicalized.
729 (when (and arg-lo
(floatp arg-lo-val
) (zerop arg-lo-val
) (consp arg-lo
)
730 (minusp (float-sign arg-lo-val
)))
731 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-lo
)
732 (setq arg-lo
0e0 arg-lo-val arg-lo
))
733 (when (and arg-hi
(zerop arg-hi-val
) (floatp arg-hi-val
) (consp arg-hi
)
734 (plusp (float-sign arg-hi-val
)))
735 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-hi
)
736 (setq arg-hi
(ecase *read-default-float-format
*
737 (double-float (load-time-value (make-unportable-float :double-float-negative-zero
)))
739 (long-float (load-time-value (make-unportable-float :long-float-negative-zero
))))
741 (flet ((fp-neg-zero-p (f) ; Is F -0.0?
742 (and (floatp f
) (zerop f
) (minusp (float-sign f
))))
743 (fp-pos-zero-p (f) ; Is F +0.0?
744 (and (floatp f
) (zerop f
) (plusp (float-sign f
)))))
745 (and (or (null domain-low
)
746 (and arg-lo
(>= arg-lo-val domain-low
)
747 (not (and (fp-pos-zero-p domain-low
)
748 (fp-neg-zero-p arg-lo
)))))
749 (or (null domain-high
)
750 (and arg-hi
(<= arg-hi-val domain-high
)
751 (not (and (fp-neg-zero-p domain-high
)
752 (fp-pos-zero-p arg-hi
)))))))))
753 (eval-when (:compile-toplevel
:execute
)
754 (setf *read-default-float-format
* 'single-float
))
756 #-sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
759 ;;; Handle monotonic functions of a single variable whose domain is
760 ;;; possibly part of the real line. ARG is the variable, FUN is the
761 ;;; function, and DOMAIN is a specifier that gives the (real) domain
762 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
763 ;;; bounds directly. Otherwise, we compute the bounds for the
764 ;;; intersection between ARG and DOMAIN, and then append a complex
765 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
767 ;;; Negative and positive zero are considered distinct within
768 ;;; DOMAIN-LOW and DOMAIN-HIGH.
770 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
771 ;;; can't compute the bounds using FUN.
772 (defun elfun-derive-type-simple (arg fun domain-low domain-high
773 default-low default-high
774 &optional
(increasingp t
))
775 (declare (type (or null real
) domain-low domain-high
))
778 (cond ((eq (numeric-type-complexp arg
) :complex
)
779 (complex-float-type arg
))
780 ((numeric-type-real-p arg
)
781 ;; The argument is real, so let's find the intersection
782 ;; between the argument and the domain of the function.
783 ;; We compute the bounds on the intersection, and for
784 ;; everything else, we return a complex number of the
786 (multiple-value-bind (intersection difference
)
787 (interval-intersection/difference
(numeric-type->interval arg
)
793 ;; Process the intersection.
794 (let* ((low (interval-low intersection
))
795 (high (interval-high intersection
))
796 (res-lo (or (bound-func fun
(if increasingp low high
) nil
)
798 (res-hi (or (bound-func fun
(if increasingp high low
) nil
)
800 (format (case (numeric-type-class arg
)
801 ((integer rational
) 'single-float
)
802 (t (numeric-type-format arg
))))
803 (bound-type (or format
'float
))
808 :low
(coerce-numeric-bound res-lo bound-type
)
809 :high
(coerce-numeric-bound res-hi bound-type
))))
810 ;; If the ARG is a subset of the domain, we don't
811 ;; have to worry about the difference, because that
813 (if (or (null difference
)
814 ;; Check whether the arg is within the domain.
815 (domain-subtypep arg domain-low domain-high
))
818 (specifier-type `(complex ,bound-type
))))))
820 ;; No intersection so the result must be purely complex.
821 (complex-float-type arg
)))))
823 (float-or-complex-float-type arg default-low default-high
))))))
826 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
827 &key
(increasingp t
))
828 (let ((num (gensym)))
829 `(defoptimizer (,name derive-type
) ((,num
))
833 (elfun-derive-type-simple arg
#',name
834 ,domain-low
,domain-high
835 ,def-low-bnd
,def-high-bnd
838 ;; These functions are easy because they are defined for the whole
840 (frob exp nil nil
0 nil
)
841 (frob sinh nil nil nil nil
)
842 (frob tanh nil nil -
1 1)
843 (frob asinh nil nil nil nil
)
845 ;; These functions are only defined for part of the real line. The
846 ;; condition selects the desired part of the line.
847 (frob asin -
1d0
1d0
(- (/ pi
2)) (/ pi
2))
848 ;; Acos is monotonic decreasing, so we need to swap the function
849 ;; values at the lower and upper bounds of the input domain.
850 (frob acos -
1d0
1d0
0 pi
:increasingp nil
)
851 (frob acosh
1d0 nil nil nil
)
852 (frob atanh -
1d0
1d0 -
1 1)
853 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
855 (frob sqrt
(load-time-value (make-unportable-float :double-float-negative-zero
)) nil
0 nil
))
857 ;;; Compute bounds for (expt x y). This should be easy since (expt x
858 ;;; y) = (exp (* y (log x))). However, computations done this way
859 ;;; have too much roundoff. Thus we have to do it the hard way.
860 (defun safe-expt (x y
)
862 (when (< (abs y
) 10000)
867 ;;; Handle the case when x >= 1.
868 (defun interval-expt-> (x y
)
869 (case (interval-range-info y
0d0
)
871 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
872 ;; obviously non-negative. We just have to be careful for
873 ;; infinite bounds (given by nil).
874 (let ((lo (safe-expt (type-bound-number (interval-low x
))
875 (type-bound-number (interval-low y
))))
876 (hi (safe-expt (type-bound-number (interval-high x
))
877 (type-bound-number (interval-high y
)))))
878 (list (make-interval :low
(or lo
1) :high hi
))))
880 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
881 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
883 (let ((lo (safe-expt (type-bound-number (interval-high x
))
884 (type-bound-number (interval-low y
))))
885 (hi (safe-expt (type-bound-number (interval-low x
))
886 (type-bound-number (interval-high y
)))))
887 (list (make-interval :low
(or lo
0) :high
(or hi
1)))))
889 ;; Split the interval in half.
890 (destructuring-bind (y- y
+)
891 (interval-split 0 y t
)
892 (list (interval-expt-> x y-
)
893 (interval-expt-> x y
+))))))
895 ;;; Handle the case when x <= 1
896 (defun interval-expt-< (x y
)
897 (case (interval-range-info x
0d0
)
899 ;; The case of 0 <= x <= 1 is easy
900 (case (interval-range-info y
)
902 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
903 ;; obviously [0, 1]. We just have to be careful for infinite bounds
905 (let ((lo (safe-expt (type-bound-number (interval-low x
))
906 (type-bound-number (interval-high y
))))
907 (hi (safe-expt (type-bound-number (interval-high x
))
908 (type-bound-number (interval-low y
)))))
909 (list (make-interval :low
(or lo
0) :high
(or hi
1)))))
911 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
912 ;; obviously [1, inf].
913 (let ((hi (safe-expt (type-bound-number (interval-low x
))
914 (type-bound-number (interval-low y
))))
915 (lo (safe-expt (type-bound-number (interval-high x
))
916 (type-bound-number (interval-high y
)))))
917 (list (make-interval :low
(or lo
1) :high hi
))))
919 ;; Split the interval in half
920 (destructuring-bind (y- y
+)
921 (interval-split 0 y t
)
922 (list (interval-expt-< x y-
)
923 (interval-expt-< x y
+))))))
925 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
926 ;; The calling function must insure this! For now we'll just
927 ;; return the appropriate unbounded float type.
928 (list (make-interval :low nil
:high nil
)))
930 (destructuring-bind (neg pos
)
931 (interval-split 0 x t t
)
932 (list (interval-expt-< neg y
)
933 (interval-expt-< pos y
))))))
935 ;;; Compute bounds for (expt x y).
936 (defun interval-expt (x y
)
937 (case (interval-range-info x
1)
940 (interval-expt-> x y
))
943 (interval-expt-< x y
))
945 (destructuring-bind (left right
)
946 (interval-split 1 x t t
)
947 (list (interval-expt left y
)
948 (interval-expt right y
))))))
950 (defun fixup-interval-expt (bnd x-int y-int x-type y-type
)
951 (declare (ignore x-int
))
952 ;; Figure out what the return type should be, given the argument
953 ;; types and bounds and the result type and bounds.
954 (cond ((csubtypep x-type
(specifier-type 'integer
))
955 ;; an integer to some power
956 (case (numeric-type-class y-type
)
958 ;; Positive integer to an integer power is either an
959 ;; integer or a rational.
960 (let ((lo (or (interval-low bnd
) '*))
961 (hi (or (interval-high bnd
) '*)))
962 (if (and (interval-low y-int
)
963 (>= (type-bound-number (interval-low y-int
)) 0))
964 (specifier-type `(integer ,lo
,hi
))
965 (specifier-type `(rational ,lo
,hi
)))))
967 ;; Positive integer to rational power is either a rational
968 ;; or a single-float.
969 (let* ((lo (interval-low bnd
))
970 (hi (interval-high bnd
))
972 (floor (type-bound-number lo
))
975 (ceiling (type-bound-number hi
))
977 (f-lo (or (bound-func #'float lo nil
)
979 (f-hi (or (bound-func #'float hi nil
)
981 (specifier-type `(or (rational ,int-lo
,int-hi
)
982 (single-float ,f-lo
, f-hi
)))))
984 ;; A positive integer to a float power is a float.
985 (let ((format (numeric-type-format y-type
)))
987 (modified-numeric-type
989 :low
(coerce-numeric-bound (interval-low bnd
) format
)
990 :high
(coerce-numeric-bound (interval-high bnd
) format
))))
992 ;; A positive integer to a number is a number (for now).
993 (specifier-type 'number
))))
994 ((csubtypep x-type
(specifier-type 'rational
))
995 ;; a rational to some power
996 (case (numeric-type-class y-type
)
998 ;; A positive rational to an integer power is always a rational.
999 (specifier-type `(rational ,(or (interval-low bnd
) '*)
1000 ,(or (interval-high bnd
) '*))))
1002 ;; A positive rational to rational power is either a rational
1003 ;; or a single-float.
1004 (let* ((lo (interval-low bnd
))
1005 (hi (interval-high bnd
))
1007 (floor (type-bound-number lo
))
1010 (ceiling (type-bound-number hi
))
1012 (f-lo (or (bound-func #'float lo nil
)
1014 (f-hi (or (bound-func #'float hi nil
)
1016 (specifier-type `(or (rational ,int-lo
,int-hi
)
1017 (single-float ,f-lo
, f-hi
)))))
1019 ;; A positive rational to a float power is a float.
1020 (let ((format (numeric-type-format y-type
)))
1022 (modified-numeric-type
1024 :low
(coerce-numeric-bound (interval-low bnd
) format
)
1025 :high
(coerce-numeric-bound (interval-high bnd
) format
))))
1027 ;; A positive rational to a number is a number (for now).
1028 (specifier-type 'number
))))
1029 ((csubtypep x-type
(specifier-type 'float
))
1030 ;; a float to some power
1031 (case (numeric-type-class y-type
)
1032 ((or integer rational
)
1033 ;; A positive float to an integer or rational power is
1035 (let ((format (numeric-type-format x-type
)))
1040 :low
(coerce-numeric-bound (interval-low bnd
) format
)
1041 :high
(coerce-numeric-bound (interval-high bnd
) format
))))
1043 ;; A positive float to a float power is a float of the
1045 (let ((format (float-format-max (numeric-type-format x-type
)
1046 (numeric-type-format y-type
))))
1051 :low
(coerce-numeric-bound (interval-low bnd
) format
)
1052 :high
(coerce-numeric-bound (interval-high bnd
) format
))))
1054 ;; A positive float to a number is a number (for now)
1055 (specifier-type 'number
))))
1057 ;; A number to some power is a number.
1058 (specifier-type 'number
))))
1060 (defun merged-interval-expt (x y
)
1061 (let* ((x-int (numeric-type->interval x
))
1062 (y-int (numeric-type->interval y
)))
1063 (mapcar (lambda (type)
1064 (fixup-interval-expt type x-int y-int x y
))
1065 (flatten-list (interval-expt x-int y-int
)))))
1067 (defun expt-derive-type-aux (x y same-arg
)
1068 (declare (ignore same-arg
))
1069 (cond ((or (not (numeric-type-real-p x
))
1070 (not (numeric-type-real-p y
)))
1071 ;; Use numeric contagion if either is not real.
1072 (numeric-contagion x y
))
1073 ((csubtypep y
(specifier-type 'integer
))
1074 ;; A real raised to an integer power is well-defined.
1075 (merged-interval-expt x y
))
1076 ;; A real raised to a non-integral power can be a float or a
1078 ((or (csubtypep x
(specifier-type '(rational 0)))
1079 (csubtypep x
(specifier-type '(float (0d0)))))
1080 ;; But a positive real to any power is well-defined.
1081 (merged-interval-expt x y
))
1082 ((and (csubtypep x
(specifier-type 'rational
))
1083 (csubtypep y
(specifier-type 'rational
)))
1084 ;; A rational to the power of a rational could be a rational
1085 ;; or a possibly-complex single float
1086 (specifier-type '(or rational single-float
(complex single-float
))))
1088 ;; a real to some power. The result could be a real or a
1090 (float-or-complex-float-type (numeric-contagion x y
)))))
1092 (defoptimizer (expt derive-type
) ((x y
))
1093 (two-arg-derive-type x y
#'expt-derive-type-aux
#'expt
))
1095 ;;; Note we must assume that a type including 0.0 may also include
1096 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
1097 (defun log-derive-type-aux-1 (x)
1098 (elfun-derive-type-simple x
#'log
0d0 nil nil nil
))
1100 (defun log-derive-type-aux-2 (x y same-arg
)
1101 (let ((log-x (log-derive-type-aux-1 x
))
1102 (log-y (log-derive-type-aux-1 y
))
1103 (accumulated-list nil
))
1104 ;; LOG-X or LOG-Y might be union types. We need to run through
1105 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
1106 (dolist (x-type (prepare-arg-for-derive-type log-x
))
1107 (dolist (y-type (prepare-arg-for-derive-type log-y
))
1108 (push (/-derive-type-aux x-type y-type same-arg
) accumulated-list
)))
1109 (apply #'type-union
(flatten-list accumulated-list
))))
1111 (defoptimizer (log derive-type
) ((x &optional y
))
1113 (two-arg-derive-type x y
#'log-derive-type-aux-2
#'log
)
1114 (one-arg-derive-type x
#'log-derive-type-aux-1
#'log
)))
1116 (defun atan-derive-type-aux-1 (y)
1117 (elfun-derive-type-simple y
#'atan nil nil
(- (/ pi
2)) (/ pi
2)))
1119 (defun atan-derive-type-aux-2 (y x same-arg
)
1120 (declare (ignore same-arg
))
1121 ;; The hard case with two args. We just return the max bounds.
1122 (let ((result-type (numeric-contagion y x
)))
1123 (cond ((and (numeric-type-real-p x
)
1124 (numeric-type-real-p y
))
1125 (let* (;; FIXME: This expression for FORMAT seems to
1126 ;; appear multiple times, and should be factored out.
1127 (format (case (numeric-type-class result-type
)
1128 ((integer rational
) 'single-float
)
1129 (t (numeric-type-format result-type
))))
1130 (bound-format (or format
'float
)))
1131 (make-numeric-type :class
'float
1134 :low
(coerce (- pi
) bound-format
)
1135 :high
(coerce pi bound-format
))))
1137 ;; The result is a float or a complex number
1138 (float-or-complex-float-type result-type
)))))
1140 (defoptimizer (atan derive-type
) ((y &optional x
))
1142 (two-arg-derive-type y x
#'atan-derive-type-aux-2
#'atan
)
1143 (one-arg-derive-type y
#'atan-derive-type-aux-1
#'atan
)))
1145 (defun cosh-derive-type-aux (x)
1146 ;; We note that cosh x = cosh |x| for all real x.
1147 (elfun-derive-type-simple
1148 (if (numeric-type-real-p x
)
1149 (abs-derive-type-aux x
)
1151 #'cosh nil nil
0 nil
))
1153 (defoptimizer (cosh derive-type
) ((num))
1154 (one-arg-derive-type num
#'cosh-derive-type-aux
#'cosh
))
1156 (defun phase-derive-type-aux (arg)
1157 (let* ((format (case (numeric-type-class arg
)
1158 ((integer rational
) 'single-float
)
1159 (t (numeric-type-format arg
))))
1160 (bound-type (or format
'float
)))
1161 (cond ((numeric-type-real-p arg
)
1162 (case (interval-range-info (numeric-type->interval arg
) 0.0)
1164 ;; The number is positive, so the phase is 0.
1165 (make-numeric-type :class
'float
1168 :low
(coerce 0 bound-type
)
1169 :high
(coerce 0 bound-type
)))
1171 ;; The number is always negative, so the phase is pi.
1172 (make-numeric-type :class
'float
1175 :low
(coerce pi bound-type
)
1176 :high
(coerce pi bound-type
)))
1178 ;; We can't tell. The result is 0 or pi. Use a union
1181 (make-numeric-type :class
'float
1184 :low
(coerce 0 bound-type
)
1185 :high
(coerce 0 bound-type
))
1186 (make-numeric-type :class
'float
1189 :low
(coerce pi bound-type
)
1190 :high
(coerce pi bound-type
))))))
1192 ;; We have a complex number. The answer is the range -pi
1193 ;; to pi. (-pi is included because we have -0.)
1194 (make-numeric-type :class
'float
1197 :low
(coerce (- pi
) bound-type
)
1198 :high
(coerce pi bound-type
))))))
1200 (defoptimizer (phase derive-type
) ((num))
1201 (one-arg-derive-type num
#'phase-derive-type-aux
#'phase
))
1205 (deftransform realpart
((x) ((complex rational
)) *)
1207 (deftransform imagpart
((x) ((complex rational
)) *)
1210 ;;; Make REALPART and IMAGPART return the appropriate types. This
1211 ;;; should help a lot in optimized code.
1212 (defun realpart-derive-type-aux (type)
1213 (let ((class (numeric-type-class type
))
1214 (format (numeric-type-format type
)))
1215 (cond ((numeric-type-real-p type
)
1216 ;; The realpart of a real has the same type and range as
1218 (make-numeric-type :class class
1221 :low
(numeric-type-low type
)
1222 :high
(numeric-type-high type
)))
1224 ;; We have a complex number. The result has the same type
1225 ;; as the real part, except that it's real, not complex,
1227 (make-numeric-type :class class
1230 :low
(numeric-type-low type
)
1231 :high
(numeric-type-high type
))))))
1233 (defoptimizer (realpart derive-type
) ((num))
1234 (one-arg-derive-type num
#'realpart-derive-type-aux
#'realpart
))
1236 (defun imagpart-derive-type-aux (type)
1237 (let ((class (numeric-type-class type
))
1238 (format (numeric-type-format type
)))
1239 (cond ((numeric-type-real-p type
)
1240 ;; The imagpart of a real has the same type as the input,
1241 ;; except that it's zero.
1242 (let ((bound-format (or format class
'real
)))
1243 (make-numeric-type :class class
1246 :low
(coerce 0 bound-format
)
1247 :high
(coerce 0 bound-format
))))
1249 ;; We have a complex number. The result has the same type as
1250 ;; the imaginary part, except that it's real, not complex,
1252 (make-numeric-type :class class
1255 :low
(numeric-type-low type
)
1256 :high
(numeric-type-high type
))))))
1258 (defoptimizer (imagpart derive-type
) ((num))
1259 (one-arg-derive-type num
#'imagpart-derive-type-aux
#'imagpart
))
1261 (defun complex-derive-type-aux-1 (re-type)
1262 (if (numeric-type-p re-type
)
1263 (make-numeric-type :class
(numeric-type-class re-type
)
1264 :format
(numeric-type-format re-type
)
1265 :complexp
(if (csubtypep re-type
1266 (specifier-type 'rational
))
1269 :low
(numeric-type-low re-type
)
1270 :high
(numeric-type-high re-type
))
1271 (specifier-type 'complex
)))
1273 (defun complex-derive-type-aux-2 (re-type im-type same-arg
)
1274 (declare (ignore same-arg
))
1275 (if (and (numeric-type-p re-type
)
1276 (numeric-type-p im-type
))
1277 ;; Need to check to make sure numeric-contagion returns the
1278 ;; right type for what we want here.
1280 ;; Also, what about rational canonicalization, like (complex 5 0)
1281 ;; is 5? So, if the result must be complex, we make it so.
1282 ;; If the result might be complex, which happens only if the
1283 ;; arguments are rational, we make it a union type of (or
1284 ;; rational (complex rational)).
1285 (let* ((element-type (numeric-contagion re-type im-type
))
1286 (rat-result-p (csubtypep element-type
1287 (specifier-type 'rational
))))
1289 (type-union element-type
1291 `(complex ,(numeric-type-class element-type
))))
1292 (make-numeric-type :class
(numeric-type-class element-type
)
1293 :format
(numeric-type-format element-type
)
1294 :complexp
(if rat-result-p
1297 (specifier-type 'complex
)))
1299 #-sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1300 (defoptimizer (complex derive-type
) ((re &optional im
))
1302 (two-arg-derive-type re im
#'complex-derive-type-aux-2
#'complex
)
1303 (one-arg-derive-type re
#'complex-derive-type-aux-1
#'complex
)))
1305 ;;; Define some transforms for complex operations. We do this in lieu
1306 ;;; of complex operation VOPs.
1307 (macrolet ((frob (type)
1309 (deftransform complex
((r) (,type
))
1310 '(complex r
,(coerce 0 type
)))
1311 (deftransform complex
((r i
) (,type
(and real
(not ,type
))))
1312 '(complex r
(truly-the ,type
(coerce i
',type
))))
1313 (deftransform complex
((r i
) ((and real
(not ,type
)) ,type
))
1314 '(complex (truly-the ,type
(coerce r
',type
)) i
))
1316 #!-complex-float-vops
1317 (deftransform %negate
((z) ((complex ,type
)) *)
1318 '(complex (%negate
(realpart z
)) (%negate
(imagpart z
))))
1319 ;; complex addition and subtraction
1320 #!-complex-float-vops
1321 (deftransform + ((w z
) ((complex ,type
) (complex ,type
)) *)
1322 '(complex (+ (realpart w
) (realpart z
))
1323 (+ (imagpart w
) (imagpart z
))))
1324 #!-complex-float-vops
1325 (deftransform -
((w z
) ((complex ,type
) (complex ,type
)) *)
1326 '(complex (- (realpart w
) (realpart z
))
1327 (- (imagpart w
) (imagpart z
))))
1328 ;; Add and subtract a complex and a real.
1329 #!-complex-float-vops
1330 (deftransform + ((w z
) ((complex ,type
) real
) *)
1331 `(complex (+ (realpart w
) z
)
1332 (+ (imagpart w
) ,(coerce 0 ',type
))))
1333 #!-complex-float-vops
1334 (deftransform + ((z w
) (real (complex ,type
)) *)
1335 `(complex (+ (realpart w
) z
)
1336 (+ (imagpart w
) ,(coerce 0 ',type
))))
1337 ;; Add and subtract a real and a complex number.
1338 #!-complex-float-vops
1339 (deftransform -
((w z
) ((complex ,type
) real
) *)
1340 `(complex (- (realpart w
) z
)
1341 (- (imagpart w
) ,(coerce 0 ',type
))))
1342 #!-complex-float-vops
1343 (deftransform -
((z w
) (real (complex ,type
)) *)
1344 `(complex (- z
(realpart w
))
1345 (- ,(coerce 0 ',type
) (imagpart w
))))
1346 ;; Multiply and divide two complex numbers.
1347 #!-complex-float-vops
1348 (deftransform * ((x y
) ((complex ,type
) (complex ,type
)) *)
1349 '(let* ((rx (realpart x
))
1353 (complex (- (* rx ry
) (* ix iy
))
1354 (+ (* rx iy
) (* ix ry
)))))
1355 (deftransform / ((x y
) ((complex ,type
) (complex ,type
)) *)
1356 #!-complex-float-vops
1357 '(let* ((rx (realpart x
))
1361 (if (> (abs ry
) (abs iy
))
1362 (let* ((r (/ iy ry
))
1363 (dn (+ ry
(* r iy
))))
1364 (complex (/ (+ rx
(* ix r
)) dn
)
1365 (/ (- ix
(* rx r
)) dn
)))
1366 (let* ((r (/ ry iy
))
1367 (dn (+ iy
(* r ry
))))
1368 (complex (/ (+ (* rx r
) ix
) dn
)
1369 (/ (- (* ix r
) rx
) dn
)))))
1370 #!+complex-float-vops
1371 `(let* ((cs (conjugate (sb!vm
::swap-complex x
)))
1374 (if (> (abs ry
) (abs iy
))
1375 (let* ((r (/ iy ry
))
1376 (dn (+ ry
(* r iy
))))
1377 (/ (+ x
(* cs r
)) dn
))
1378 (let* ((r (/ ry iy
))
1379 (dn (+ iy
(* r ry
))))
1380 (/ (+ (* x r
) cs
) dn
)))))
1381 ;; Multiply a complex by a real or vice versa.
1382 #!-complex-float-vops
1383 (deftransform * ((w z
) ((complex ,type
) real
) *)
1384 '(complex (* (realpart w
) z
) (* (imagpart w
) z
)))
1385 #!-complex-float-vops
1386 (deftransform * ((z w
) (real (complex ,type
)) *)
1387 '(complex (* (realpart w
) z
) (* (imagpart w
) z
)))
1388 ;; Divide a complex by a real or vice versa.
1389 #!-complex-float-vops
1390 (deftransform / ((w z
) ((complex ,type
) real
) *)
1391 '(complex (/ (realpart w
) z
) (/ (imagpart w
) z
)))
1392 (deftransform / ((x y
) (,type
(complex ,type
)) *)
1393 #!-complex-float-vops
1394 '(let* ((ry (realpart y
))
1396 (if (> (abs ry
) (abs iy
))
1397 (let* ((r (/ iy ry
))
1398 (dn (+ ry
(* r iy
))))
1400 (/ (- (* x r
)) dn
)))
1401 (let* ((r (/ ry iy
))
1402 (dn (+ iy
(* r ry
))))
1403 (complex (/ (* x r
) dn
)
1405 #!+complex-float-vops
1406 '(let* ((ry (realpart y
))
1408 (if (> (abs ry
) (abs iy
))
1409 (let* ((r (/ iy ry
))
1410 (dn (+ ry
(* r iy
))))
1411 (/ (complex x
(- (* x r
))) dn
))
1412 (let* ((r (/ ry iy
))
1413 (dn (+ iy
(* r ry
))))
1414 (/ (complex (* x r
) (- x
)) dn
)))))
1415 ;; conjugate of complex number
1416 #!-complex-float-vops
1417 (deftransform conjugate
((z) ((complex ,type
)) *)
1418 '(complex (realpart z
) (- (imagpart z
))))
1420 (deftransform cis
((z) ((,type
)) *)
1421 '(complex (cos z
) (sin z
)))
1423 #!-complex-float-vops
1424 (deftransform = ((w z
) ((complex ,type
) (complex ,type
)) *)
1425 '(and (= (realpart w
) (realpart z
))
1426 (= (imagpart w
) (imagpart z
))))
1427 #!-complex-float-vops
1428 (deftransform = ((w z
) ((complex ,type
) real
) *)
1429 '(and (= (realpart w
) z
) (zerop (imagpart w
))))
1430 #!-complex-float-vops
1431 (deftransform = ((w z
) (real (complex ,type
)) *)
1432 '(and (= (realpart z
) w
) (zerop (imagpart z
)))))))
1435 (frob double-float
))
1437 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1438 ;;; produce a minimal range for the result; the result is the widest
1439 ;;; possible answer. This gets around the problem of doing range
1440 ;;; reduction correctly but still provides useful results when the
1441 ;;; inputs are union types.
1442 #-sb-xc-host
; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1444 (defun trig-derive-type-aux (arg domain fun
1445 &optional def-lo def-hi
(increasingp t
))
1448 (cond ((eq (numeric-type-complexp arg
) :complex
)
1449 (make-numeric-type :class
(numeric-type-class arg
)
1450 :format
(numeric-type-format arg
)
1451 :complexp
:complex
))
1452 ((numeric-type-real-p arg
)
1453 (let* ((format (case (numeric-type-class arg
)
1454 ((integer rational
) 'single-float
)
1455 (t (numeric-type-format arg
))))
1456 (bound-type (or format
'float
)))
1457 ;; If the argument is a subset of the "principal" domain
1458 ;; of the function, we can compute the bounds because
1459 ;; the function is monotonic. We can't do this in
1460 ;; general for these periodic functions because we can't
1461 ;; (and don't want to) do the argument reduction in
1462 ;; exactly the same way as the functions themselves do
1464 (if (csubtypep arg domain
)
1465 (let ((res-lo (bound-func fun
(numeric-type-low arg
) nil
))
1466 (res-hi (bound-func fun
(numeric-type-high arg
) nil
)))
1468 (rotatef res-lo res-hi
))
1472 :low
(coerce-numeric-bound res-lo bound-type
)
1473 :high
(coerce-numeric-bound res-hi bound-type
)))
1477 :low
(and def-lo
(coerce def-lo bound-type
))
1478 :high
(and def-hi
(coerce def-hi bound-type
))))))
1480 (float-or-complex-float-type arg def-lo def-hi
))))))
1482 (defoptimizer (sin derive-type
) ((num))
1483 (one-arg-derive-type
1486 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1487 (trig-derive-type-aux
1489 (specifier-type `(float ,(- (/ pi
2)) ,(/ pi
2)))
1494 (defoptimizer (cos derive-type
) ((num))
1495 (one-arg-derive-type
1498 ;; Derive the bounds if the arg is in [0, pi].
1499 (trig-derive-type-aux arg
1500 (specifier-type `(float 0d0
,pi
))
1506 (defoptimizer (tan derive-type
) ((num))
1507 (one-arg-derive-type
1510 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1511 (trig-derive-type-aux arg
1512 (specifier-type `(float ,(- (/ pi
2)) ,(/ pi
2)))
1517 (defoptimizer (conjugate derive-type
) ((num))
1518 (one-arg-derive-type num
1520 (flet ((most-negative-bound (l h
)
1522 (if (< (type-bound-number l
) (- (type-bound-number h
)))
1524 (set-bound (- (type-bound-number h
)) (consp h
)))))
1525 (most-positive-bound (l h
)
1527 (if (> (type-bound-number h
) (- (type-bound-number l
)))
1529 (set-bound (- (type-bound-number l
)) (consp l
))))))
1530 (if (numeric-type-real-p arg
)
1532 (let ((low (numeric-type-low arg
))
1533 (high (numeric-type-high arg
)))
1534 (let ((new-low (most-negative-bound low high
))
1535 (new-high (most-positive-bound low high
)))
1536 (modified-numeric-type arg
:low new-low
:high new-high
))))))
1539 (defoptimizer (cis derive-type
) ((num))
1540 (one-arg-derive-type num
1543 `(complex ,(or (numeric-type-format arg
) 'float
))))
1548 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1550 (macrolet ((define-frobs (fun ufun
)
1552 (defknown ,ufun
(real) integer
(movable foldable flushable
))
1553 (deftransform ,fun
((x &optional by
)
1555 (constant-arg (member 1))))
1556 '(let ((res (,ufun x
)))
1557 (values res
(- x res
)))))))
1558 (define-frobs truncate %unary-truncate
)
1559 (define-frobs round %unary-round
))
1561 (deftransform %unary-truncate
((x) (single-float))
1562 `(%unary-truncate
/single-float x
))
1563 (deftransform %unary-truncate
((x) (double-float))
1564 `(%unary-truncate
/double-float x
))
1566 ;;; Convert (TRUNCATE x y) to the obvious implementation.
1568 ;;; ...plus hair: Insert explicit coercions to appropriate float types: Python
1569 ;;; is reluctant it generate explicit integer->float coercions due to
1570 ;;; precision issues (see SAFE-SINGLE-COERCION-P &co), but this is not an
1571 ;;; issue here as there is no DERIVE-TYPE optimizer on specialized versions of
1572 ;;; %UNARY-TRUNCATE, so the derived type of TRUNCATE remains the best we can
1573 ;;; do here -- which is fine. Also take care not to add unnecassary division
1574 ;;; or multiplication by 1, since we are not able to always eliminate them,
1575 ;;; depending on FLOAT-ACCURACY. Finally, leave out the secondary value when
1576 ;;; we know it is unused: COERCE is not flushable.
1577 (macrolet ((def (type other-float-arg-types
)
1578 (let ((unary (symbolicate "%UNARY-TRUNCATE/" type
))
1579 (coerce (symbolicate "%" type
)))
1580 `(deftransform truncate
((x &optional y
)
1582 &optional
(or ,type
,@other-float-arg-types integer
))
1584 (let* ((result-type (and result
1585 (lvar-derived-type result
)))
1586 (compute-all (and (values-type-p result-type
)
1587 (not (type-single-value-p result-type
)))))
1589 (and (constant-lvar-p y
) (= 1 (lvar-value y
))))
1591 `(let ((res (,',unary x
)))
1592 (values res
(- x
(,',coerce res
))))
1593 `(let ((res (,',unary x
)))
1594 ;; Dummy secondary value!
1597 `(let* ((f (,',coerce y
))
1598 (res (,',unary
(/ x f
))))
1599 (values res
(- x
(* f
(,',coerce res
)))))
1600 `(let* ((f (,',coerce y
))
1601 (res (,',unary
(/ x f
))))
1602 ;; Dummy secondary value!
1603 (values res x
)))))))))
1604 (def single-float
())
1605 (def double-float
(single-float)))
1607 (defknown %unary-ftruncate
(real) float
(movable foldable flushable
))
1608 (defknown %unary-ftruncate
/single
(single-float) single-float
1609 (movable foldable flushable
))
1610 (defknown %unary-ftruncate
/double
(double-float) double-float
1611 (movable foldable flushable
))
1613 (defun %unary-ftruncate
/single
(x)
1614 (declare (type single-float x
))
1615 (declare (optimize speed
(safety 0)))
1616 (let* ((bits (single-float-bits x
))
1617 (exp (ldb sb
!vm
:single-float-exponent-byte bits
))
1618 (biased (the single-float-exponent
1619 (- exp sb
!vm
:single-float-bias
))))
1620 (declare (type (signed-byte 32) bits
))
1622 ((= exp sb
!vm
:single-float-normal-exponent-max
) x
)
1623 ((<= biased
0) (* x
0f0
))
1624 ((>= biased
(float-digits x
)) x
)
1626 (let ((frac-bits (- (float-digits x
) biased
)))
1627 (setf bits
(logandc2 bits
(- (ash 1 frac-bits
) 1)))
1628 (make-single-float bits
))))))
1630 (defun %unary-ftruncate
/double
(x)
1631 (declare (type double-float x
))
1632 (declare (optimize speed
(safety 0)))
1633 (let* ((high (double-float-high-bits x
))
1634 (low (double-float-low-bits x
))
1635 (exp (ldb sb
!vm
:double-float-exponent-byte high
))
1636 (biased (the double-float-exponent
1637 (- exp sb
!vm
:double-float-bias
))))
1638 (declare (type (signed-byte 32) high
)
1639 (type (unsigned-byte 32) low
))
1641 ((= exp sb
!vm
:double-float-normal-exponent-max
) x
)
1642 ((<= biased
0) (* x
0d0
))
1643 ((>= biased
(float-digits x
)) x
)
1645 (let ((frac-bits (- (float-digits x
) biased
)))
1646 (cond ((< frac-bits
32)
1647 (setf low
(logandc2 low
(- (ash 1 frac-bits
) 1))))
1650 (setf high
(logandc2 high
(- (ash 1 (- frac-bits
32)) 1)))))
1651 (make-double-float high low
))))))
1654 ((def (float-type fun
)
1655 `(deftransform %unary-ftruncate
((x) (,float-type
))
1657 (def single-float %unary-ftruncate
/single
)
1658 (def double-float %unary-ftruncate
/double
))