1.0.4.6:
[sbcl.git] / src / compiler / float-tran.lisp
1 ;;;; This file contains floating-point-specific transforms, and may be
2 ;;;; somewhat implementation-dependent in its assumptions of what the
3 ;;;; formats are.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
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.
13
14 (in-package "SB!C")
15 \f
16 ;;;; coercions
17
18 (defknown %single-float (real) single-float (movable foldable flushable))
19 (defknown %double-float (real) double-float (movable foldable flushable))
20
21 (deftransform float ((n f) (* single-float) *)
22   '(%single-float n))
23
24 (deftransform float ((n f) (* double-float) *)
25   '(%double-float n))
26
27 (deftransform float ((n) *)
28   '(if (floatp n)
29        n
30        (%single-float n)))
31
32 (deftransform %single-float ((n) (single-float) *)
33   'n)
34
35 (deftransform %double-float ((n) (double-float) *)
36   'n)
37
38 ;;; RANDOM
39 (macrolet ((frob (fun type)
40              `(deftransform random ((num &optional state)
41                                     (,type &optional *) *)
42                 "Use inline float operations."
43                 '(,fun num (or state *random-state*)))))
44   (frob %random-single-float single-float)
45   (frob %random-double-float double-float))
46 \f
47 ;;;; float accessors
48
49 (defknown make-single-float ((signed-byte 32)) single-float
50   (movable foldable flushable))
51
52 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
53   (movable foldable flushable))
54
55 (defknown single-float-bits (single-float) (signed-byte 32)
56   (movable foldable flushable))
57
58 (defknown double-float-high-bits (double-float) (signed-byte 32)
59   (movable foldable flushable))
60
61 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
62   (movable foldable flushable))
63
64 (deftransform float-sign ((float &optional float2)
65                           (single-float &optional single-float) *)
66   (if float2
67       (let ((temp (gensym)))
68         `(let ((,temp (abs float2)))
69           (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
70       '(if (minusp (single-float-bits float)) -1f0 1f0)))
71
72 (deftransform float-sign ((float &optional float2)
73                           (double-float &optional double-float) *)
74   (if float2
75       (let ((temp (gensym)))
76         `(let ((,temp (abs float2)))
77           (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
78       '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
79 \f
80 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
81
82 (defknown decode-single-float (single-float)
83   (values single-float single-float-exponent (single-float -1f0 1f0))
84   (movable foldable flushable))
85
86 (defknown decode-double-float (double-float)
87   (values double-float double-float-exponent (double-float -1d0 1d0))
88   (movable foldable flushable))
89
90 (defknown integer-decode-single-float (single-float)
91   (values single-float-significand single-float-int-exponent (integer -1 1))
92   (movable foldable flushable))
93
94 (defknown integer-decode-double-float (double-float)
95   (values double-float-significand double-float-int-exponent (integer -1 1))
96   (movable foldable flushable))
97
98 (defknown scale-single-float (single-float integer) single-float
99   (movable foldable flushable))
100
101 (defknown scale-double-float (double-float integer) double-float
102   (movable foldable flushable))
103
104 (deftransform decode-float ((x) (single-float) *)
105   '(decode-single-float x))
106
107 (deftransform decode-float ((x) (double-float) *)
108   '(decode-double-float x))
109
110 (deftransform integer-decode-float ((x) (single-float) *)
111   '(integer-decode-single-float x))
112
113 (deftransform integer-decode-float ((x) (double-float) *)
114   '(integer-decode-double-float x))
115
116 (deftransform scale-float ((f ex) (single-float *) *)
117   (if (and #!+x86 t #!-x86 nil
118            (csubtypep (lvar-type ex)
119                       (specifier-type '(signed-byte 32))))
120       '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
121       '(scale-single-float f ex)))
122
123 (deftransform scale-float ((f ex) (double-float *) *)
124   (if (and #!+x86 t #!-x86 nil
125            (csubtypep (lvar-type ex)
126                       (specifier-type '(signed-byte 32))))
127       '(%scalbn f ex)
128       '(scale-double-float f ex)))
129
130 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
131 ;;;
132 ;;; SBCL's own implementation of floating point supports floating
133 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
134 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
135 ;;; floating point support. Thus, we have to avoid running it on the
136 ;;; cross-compilation host, since we're not guaranteed that the
137 ;;; cross-compilation host will support floating point infinities.
138 ;;;
139 ;;; If we wanted to live dangerously, we could conditionalize the code
140 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
141 ;;; host happened to be SBCL, we'd be able to run the infinity-using
142 ;;; code. Pro:
143 ;;;   * SBCL itself gets built with more complete optimization.
144 ;;; Con:
145 ;;;   * You get a different SBCL depending on what your cross-compilation
146 ;;;     host is.
147 ;;; So far the pros and cons seem seem to be mostly academic, since
148 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
149 ;;; actually important in compiling SBCL itself. If this changes, then
150 ;;; we have to decide:
151 ;;;   * Go for simplicity, leaving things as they are.
152 ;;;   * Go for performance at the expense of conceptual clarity,
153 ;;;     using #+(OR SBCL SB-XC) and otherwise leaving the build
154 ;;;     process as is.
155 ;;;   * Go for performance at the expense of build time, using
156 ;;;     #+(OR SBCL SB-XC) and also making SBCL do not just
157 ;;;     make-host-1.sh and make-host-2.sh, but a third step
158 ;;;     make-host-3.sh where it builds itself under itself. (Such a
159 ;;;     3-step build process could also help with other things, e.g.
160 ;;;     using specialized arrays to represent debug information.)
161 ;;;   * Rewrite the code so that it doesn't depend on unportable
162 ;;;     floating point infinities.
163
164 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
165 ;;; are computed for the result, if possible.
166 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
167 (progn
168
169 (defun scale-float-derive-type-aux (f ex same-arg)
170   (declare (ignore same-arg))
171   (flet ((scale-bound (x n)
172            ;; We need to be a bit careful here and catch any overflows
173            ;; that might occur. We can ignore underflows which become
174            ;; zeros.
175            (set-bound
176             (handler-case
177              (scale-float (type-bound-number x) n)
178              (floating-point-overflow ()
179                 nil))
180             (consp x))))
181     (when (and (numeric-type-p f) (numeric-type-p ex))
182       (let ((f-lo (numeric-type-low f))
183             (f-hi (numeric-type-high f))
184             (ex-lo (numeric-type-low ex))
185             (ex-hi (numeric-type-high ex))
186             (new-lo nil)
187             (new-hi nil))
188         (when f-hi
189           (if (< (float-sign (type-bound-number f-hi)) 0.0)
190               (when ex-lo
191                 (setf new-hi (scale-bound f-hi ex-lo)))
192               (when ex-hi
193                 (setf new-hi (scale-bound f-hi ex-hi)))))
194         (when f-lo
195           (if (< (float-sign (type-bound-number f-lo)) 0.0)
196               (when ex-hi
197                 (setf new-lo (scale-bound f-lo ex-hi)))
198               (when ex-lo
199                 (setf new-lo (scale-bound f-lo ex-lo)))))
200         (make-numeric-type :class (numeric-type-class f)
201                            :format (numeric-type-format f)
202                            :complexp :real
203                            :low new-lo
204                            :high new-hi)))))
205 (defoptimizer (scale-single-float derive-type) ((f ex))
206   (two-arg-derive-type f ex #'scale-float-derive-type-aux
207                        #'scale-single-float t))
208 (defoptimizer (scale-double-float derive-type) ((f ex))
209   (two-arg-derive-type f ex #'scale-float-derive-type-aux
210                        #'scale-double-float t))
211
212 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
213 ;;; FLOAT function return the correct ranges if the input has some
214 ;;; defined range. Quite useful if we want to convert some type of
215 ;;; bounded integer into a float.
216 (macrolet
217     ((frob (fun type most-negative most-positive)
218        (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
219          `(progn
220             (defun ,aux-name (num)
221               ;; When converting a number to a float, the limits are
222               ;; the same.
223               (let* ((lo (bound-func (lambda (x)
224                                        (if (< x ,most-negative)
225                                            ,most-negative
226                                            (coerce x ',type)))
227                                      (numeric-type-low num)))
228                      (hi (bound-func (lambda (x)
229                                        (if (< ,most-positive x )
230                                            ,most-positive
231                                            (coerce x ',type)))
232                                      (numeric-type-high num))))
233                 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
234
235             (defoptimizer (,fun derive-type) ((num))
236               (one-arg-derive-type num #',aux-name #',fun))))))
237   (frob %single-float single-float
238         most-negative-single-float most-positive-single-float)
239   (frob %double-float double-float
240         most-negative-double-float most-positive-double-float))
241 ) ; PROGN
242 \f
243 ;;;; float contagion
244
245 ;;; Do some stuff to recognize when the loser is doing mixed float and
246 ;;; rational arithmetic, or different float types, and fix it up. If
247 ;;; we don't, he won't even get so much as an efficiency note.
248 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
249   `(,(lvar-fun-name (basic-combination-fun node))
250     (float x y) y))
251 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
252   `(,(lvar-fun-name (basic-combination-fun node))
253     x (float y x)))
254
255 (dolist (x '(+ * / -))
256   (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
257   (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
258
259 (dolist (x '(= < > + * / -))
260   (%deftransform x '(function (single-float double-float) *)
261                  #'float-contagion-arg1)
262   (%deftransform x '(function (double-float single-float) *)
263                  #'float-contagion-arg2))
264
265 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
266 ;;; general float rational args to comparison, since Common Lisp
267 ;;; semantics says we are supposed to compare as rationals, but we can
268 ;;; do it for any rational that has a precise representation as a
269 ;;; float (such as 0).
270 (macrolet ((frob (op)
271              `(deftransform ,op ((x y) (float rational) *)
272                 "open-code FLOAT to RATIONAL comparison"
273                 (unless (constant-lvar-p y)
274                   (give-up-ir1-transform
275                    "The RATIONAL value isn't known at compile time."))
276                 (let ((val (lvar-value y)))
277                   (unless (eql (rational (float val)) val)
278                     (give-up-ir1-transform
279                      "~S doesn't have a precise float representation."
280                      val)))
281                 `(,',op x (float y x)))))
282   (frob <)
283   (frob >)
284   (frob =))
285 \f
286 ;;;; irrational derive-type methods
287
288 ;;; Derive the result to be float for argument types in the
289 ;;; appropriate domain.
290 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
291 (dolist (stuff '((asin (real -1.0 1.0))
292                  (acos (real -1.0 1.0))
293                  (acosh (real 1.0))
294                  (atanh (real -1.0 1.0))
295                  (sqrt (real 0.0))))
296   (destructuring-bind (name type) stuff
297     (let ((type (specifier-type type)))
298       (setf (fun-info-derive-type (fun-info-or-lose name))
299             (lambda (call)
300               (declare (type combination call))
301               (when (csubtypep (lvar-type
302                                 (first (combination-args call)))
303                                type)
304                 (specifier-type 'float)))))))
305
306 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
307 (defoptimizer (log derive-type) ((x &optional y))
308   (when (and (csubtypep (lvar-type x)
309                         (specifier-type '(real 0.0)))
310              (or (null y)
311                  (csubtypep (lvar-type y)
312                             (specifier-type '(real 0.0)))))
313     (specifier-type 'float)))
314 \f
315 ;;;; irrational transforms
316
317 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
318           (double-float) double-float
319   (movable foldable flushable))
320
321 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
322   (double-float) (double-float -1.0d0 1.0d0)
323   (movable foldable flushable))
324
325 (defknown (%asin %atan)
326   (double-float)
327   (double-float #.(coerce (- (/ pi 2)) 'double-float)
328                 #.(coerce (/ pi 2) 'double-float))
329   (movable foldable flushable))
330
331 (defknown (%acos)
332   (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
333   (movable foldable flushable))
334
335 (defknown (%cosh)
336   (double-float) (double-float 1.0d0)
337   (movable foldable flushable))
338
339 (defknown (%acosh %exp %sqrt)
340   (double-float) (double-float 0.0d0)
341   (movable foldable flushable))
342
343 (defknown %expm1
344   (double-float) (double-float -1d0)
345   (movable foldable flushable))
346
347 (defknown (%hypot)
348   (double-float double-float) (double-float 0d0)
349   (movable foldable flushable))
350
351 (defknown (%pow)
352   (double-float double-float) double-float
353   (movable foldable flushable))
354
355 (defknown (%atan2)
356   (double-float double-float)
357   (double-float #.(coerce (- pi) 'double-float)
358                 #.(coerce pi 'double-float))
359   (movable foldable flushable))
360
361 (defknown (%scalb)
362   (double-float double-float) double-float
363   (movable foldable flushable))
364
365 (defknown (%scalbn)
366   (double-float (signed-byte 32)) double-float
367   (movable foldable flushable))
368
369 (defknown (%log1p)
370   (double-float) double-float
371   (movable foldable flushable))
372
373 (macrolet ((def (name prim rtype)
374              `(progn
375                (deftransform ,name ((x) (single-float) ,rtype)
376                  `(coerce (,',prim (coerce x 'double-float)) 'single-float))
377                (deftransform ,name ((x) (double-float) ,rtype)
378                  `(,',prim x)))))
379   (def exp %exp *)
380   (def log %log float)
381   (def sqrt %sqrt float)
382   (def asin %asin float)
383   (def acos %acos float)
384   (def atan %atan *)
385   (def sinh %sinh *)
386   (def cosh %cosh *)
387   (def tanh %tanh *)
388   (def asinh %asinh *)
389   (def acosh %acosh float)
390   (def atanh %atanh float))
391
392 ;;; The argument range is limited on the x86 FP trig. functions. A
393 ;;; post-test can detect a failure (and load a suitable result), but
394 ;;; this test is avoided if possible.
395 (macrolet ((def (name prim prim-quick)
396              (declare (ignorable prim-quick))
397              `(progn
398                 (deftransform ,name ((x) (single-float) *)
399                   #!+x86 (cond ((csubtypep (lvar-type x)
400                                            (specifier-type '(single-float
401                                                              (#.(- (expt 2f0 64)))
402                                                              (#.(expt 2f0 64)))))
403                                 `(coerce (,',prim-quick (coerce x 'double-float))
404                                   'single-float))
405                                (t
406                                 (compiler-notify
407                                  "unable to avoid inline argument range check~@
408                                   because the argument range (~S) was not within 2^64"
409                                  (type-specifier (lvar-type x)))
410                                 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
411                   #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
412                (deftransform ,name ((x) (double-float) *)
413                  #!+x86 (cond ((csubtypep (lvar-type x)
414                                           (specifier-type '(double-float
415                                                             (#.(- (expt 2d0 64)))
416                                                             (#.(expt 2d0 64)))))
417                                `(,',prim-quick x))
418                               (t
419                                (compiler-notify
420                                 "unable to avoid inline argument range check~@
421                                  because the argument range (~S) was not within 2^64"
422                                 (type-specifier (lvar-type x)))
423                                `(,',prim x)))
424                  #!-x86 `(,',prim x)))))
425   (def sin %sin %sin-quick)
426   (def cos %cos %cos-quick)
427   (def tan %tan %tan-quick))
428
429 (deftransform atan ((x y) (single-float single-float) *)
430   `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
431     'single-float))
432 (deftransform atan ((x y) (double-float double-float) *)
433   `(%atan2 x y))
434
435 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
436   `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
437     'single-float))
438 (deftransform expt ((x y) ((double-float 0d0) double-float) *)
439   `(%pow x y))
440 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
441   `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
442     'single-float))
443 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) *)
444   `(%pow x (coerce y 'double-float)))
445
446 ;;; ANSI says log with base zero returns zero.
447 (deftransform log ((x y) (float float) float)
448   '(if (zerop y) y (/ (log x) (log y))))
449 \f
450 ;;; Handle some simple transformations.
451
452 (deftransform abs ((x) ((complex double-float)) double-float)
453   '(%hypot (realpart x) (imagpart x)))
454
455 (deftransform abs ((x) ((complex single-float)) single-float)
456   '(coerce (%hypot (coerce (realpart x) 'double-float)
457                    (coerce (imagpart x) 'double-float))
458           'single-float))
459
460 (deftransform phase ((x) ((complex double-float)) double-float)
461   '(%atan2 (imagpart x) (realpart x)))
462
463 (deftransform phase ((x) ((complex single-float)) single-float)
464   '(coerce (%atan2 (coerce (imagpart x) 'double-float)
465                    (coerce (realpart x) 'double-float))
466           'single-float))
467
468 (deftransform phase ((x) ((float)) float)
469   '(if (minusp (float-sign x))
470        (float pi x)
471        (float 0 x)))
472
473 ;;; The number is of type REAL.
474 (defun numeric-type-real-p (type)
475   (and (numeric-type-p type)
476        (eq (numeric-type-complexp type) :real)))
477
478 ;;; Coerce a numeric type bound to the given type while handling
479 ;;; exclusive bounds.
480 (defun coerce-numeric-bound (bound type)
481   (when bound
482     (if (consp bound)
483         (list (coerce (car bound) type))
484         (coerce bound type))))
485
486 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
487 (progn
488
489 ;;;; optimizers for elementary functions
490 ;;;;
491 ;;;; These optimizers compute the output range of the elementary
492 ;;;; function, based on the domain of the input.
493
494 ;;; Generate a specifier for a complex type specialized to the same
495 ;;; type as the argument.
496 (defun complex-float-type (arg)
497   (declare (type numeric-type arg))
498   (let* ((format (case (numeric-type-class arg)
499                    ((integer rational) 'single-float)
500                    (t (numeric-type-format arg))))
501          (float-type (or format 'float)))
502     (specifier-type `(complex ,float-type))))
503
504 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
505 ;;; should be the right kind of float. Allow bounds for the float
506 ;;; part too.
507 (defun float-or-complex-float-type (arg &optional lo hi)
508   (declare (type numeric-type arg))
509   (let* ((format (case (numeric-type-class arg)
510                    ((integer rational) 'single-float)
511                    (t (numeric-type-format arg))))
512          (float-type (or format 'float))
513          (lo (coerce-numeric-bound lo float-type))
514          (hi (coerce-numeric-bound hi float-type)))
515     (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
516                          (complex ,float-type)))))
517
518 ) ; PROGN
519
520 (eval-when (:compile-toplevel :execute)
521   ;; So the problem with this hack is that it's actually broken.  If
522   ;; the host does not have long floats, then setting *R-D-F-F* to
523   ;; LONG-FLOAT doesn't actually buy us anything.  FIXME.
524   (setf *read-default-float-format*
525         #!+long-float 'long-float #!-long-float 'double-float))
526 ;;; Test whether the numeric-type ARG is within in domain specified by
527 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
528 ;;; be distinct.
529 #-sb-xc-host  ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
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 (type-bound-number arg-lo))
535          (arg-hi (numeric-type-high arg))
536          (arg-hi-val (type-bound-number 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-notify "float zero bound ~S not correctly canonicalized?" arg-lo)
541       (setq arg-lo 0e0 arg-lo-val arg-lo))
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-notify "float zero bound ~S not correctly canonicalized?" arg-hi)
545       (setq arg-hi (ecase *read-default-float-format*
546                      (double-float (load-time-value (make-unportable-float :double-float-negative-zero)))
547                      #!+long-float
548                      (long-float (load-time-value (make-unportable-float :long-float-negative-zero))))
549             arg-hi-val arg-hi))
550     (flet ((fp-neg-zero-p (f)           ; Is F -0.0?
551              (and (floatp f) (zerop f) (minusp (float-sign f))))
552            (fp-pos-zero-p (f)           ; Is F +0.0?
553              (and (floatp f) (zerop f) (plusp (float-sign f)))))
554       (and (or (null domain-low)
555                (and arg-lo (>= arg-lo-val domain-low)
556                     (not (and (fp-pos-zero-p domain-low)
557                               (fp-neg-zero-p arg-lo)))))
558            (or (null domain-high)
559                (and arg-hi (<= arg-hi-val domain-high)
560                     (not (and (fp-neg-zero-p domain-high)
561                               (fp-pos-zero-p arg-hi)))))))))
562 (eval-when (:compile-toplevel :execute)
563   (setf *read-default-float-format* 'single-float))
564
565 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
566 (progn
567
568 ;;; Handle monotonic functions of a single variable whose domain is
569 ;;; possibly part of the real line. ARG is the variable, FCN is the
570 ;;; function, and DOMAIN is a specifier that gives the (real) domain
571 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
572 ;;; bounds directly. Otherwise, we compute the bounds for the
573 ;;; intersection between ARG and DOMAIN, and then append a complex
574 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
575 ;;;
576 ;;; Negative and positive zero are considered distinct within
577 ;;; DOMAIN-LOW and DOMAIN-HIGH.
578 ;;;
579 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
580 ;;; can't compute the bounds using FCN.
581 (defun elfun-derive-type-simple (arg fcn domain-low domain-high
582                                      default-low default-high
583                                      &optional (increasingp t))
584   (declare (type (or null real) domain-low domain-high))
585   (etypecase arg
586     (numeric-type
587      (cond ((eq (numeric-type-complexp arg) :complex)
588             (complex-float-type arg))
589            ((numeric-type-real-p arg)
590             ;; The argument is real, so let's find the intersection
591             ;; between the argument and the domain of the function.
592             ;; We compute the bounds on the intersection, and for
593             ;; everything else, we return a complex number of the
594             ;; appropriate type.
595             (multiple-value-bind (intersection difference)
596                 (interval-intersection/difference (numeric-type->interval arg)
597                                                   (make-interval
598                                                    :low domain-low
599                                                    :high domain-high))
600               (cond
601                 (intersection
602                  ;; Process the intersection.
603                  (let* ((low (interval-low intersection))
604                         (high (interval-high intersection))
605                         (res-lo (or (bound-func fcn (if increasingp low high))
606                                     default-low))
607                         (res-hi (or (bound-func fcn (if increasingp high low))
608                                     default-high))
609                         (format (case (numeric-type-class arg)
610                                   ((integer rational) 'single-float)
611                                   (t (numeric-type-format arg))))
612                         (bound-type (or format 'float))
613                         (result-type
614                          (make-numeric-type
615                           :class 'float
616                           :format format
617                           :low (coerce-numeric-bound res-lo bound-type)
618                           :high (coerce-numeric-bound res-hi bound-type))))
619                    ;; If the ARG is a subset of the domain, we don't
620                    ;; have to worry about the difference, because that
621                    ;; can't occur.
622                    (if (or (null difference)
623                            ;; Check whether the arg is within the domain.
624                            (domain-subtypep arg domain-low domain-high))
625                        result-type
626                        (list result-type
627                              (specifier-type `(complex ,bound-type))))))
628                 (t
629                  ;; No intersection so the result must be purely complex.
630                  (complex-float-type arg)))))
631            (t
632             (float-or-complex-float-type arg default-low default-high))))))
633
634 (macrolet
635     ((frob (name domain-low domain-high def-low-bnd def-high-bnd
636                  &key (increasingp t))
637        (let ((num (gensym)))
638          `(defoptimizer (,name derive-type) ((,num))
639            (one-arg-derive-type
640             ,num
641             (lambda (arg)
642               (elfun-derive-type-simple arg #',name
643                                         ,domain-low ,domain-high
644                                         ,def-low-bnd ,def-high-bnd
645                                         ,increasingp))
646             #',name)))))
647   ;; These functions are easy because they are defined for the whole
648   ;; real line.
649   (frob exp nil nil 0 nil)
650   (frob sinh nil nil nil nil)
651   (frob tanh nil nil -1 1)
652   (frob asinh nil nil nil nil)
653
654   ;; These functions are only defined for part of the real line. The
655   ;; condition selects the desired part of the line.
656   (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
657   ;; Acos is monotonic decreasing, so we need to swap the function
658   ;; values at the lower and upper bounds of the input domain.
659   (frob acos -1d0 1d0 0 pi :increasingp nil)
660   (frob acosh 1d0 nil nil nil)
661   (frob atanh -1d0 1d0 -1 1)
662   ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
663   ;; includes -0.0.
664   (frob sqrt (load-time-value (make-unportable-float :double-float-negative-zero)) nil 0 nil))
665
666 ;;; Compute bounds for (expt x y). This should be easy since (expt x
667 ;;; y) = (exp (* y (log x))). However, computations done this way
668 ;;; have too much roundoff. Thus we have to do it the hard way.
669 (defun safe-expt (x y)
670   (handler-case
671       (when (< (abs y) 10000)
672         (expt x y))
673     (error ()
674       nil)))
675
676 ;;; Handle the case when x >= 1.
677 (defun interval-expt-> (x y)
678   (case (sb!c::interval-range-info y 0d0)
679     (+
680      ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
681      ;; obviously non-negative. We just have to be careful for
682      ;; infinite bounds (given by nil).
683      (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
684                           (type-bound-number (sb!c::interval-low y))))
685            (hi (safe-expt (type-bound-number (sb!c::interval-high x))
686                           (type-bound-number (sb!c::interval-high y)))))
687        (list (sb!c::make-interval :low (or lo 1) :high hi))))
688     (-
689      ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
690      ;; obviously [0, 1]. However, underflow (nil) means 0 is the
691      ;; result.
692      (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
693                           (type-bound-number (sb!c::interval-low y))))
694            (hi (safe-expt (type-bound-number (sb!c::interval-low x))
695                           (type-bound-number (sb!c::interval-high y)))))
696        (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
697     (t
698      ;; Split the interval in half.
699      (destructuring-bind (y- y+)
700          (sb!c::interval-split 0 y t)
701        (list (interval-expt-> x y-)
702              (interval-expt-> x y+))))))
703
704 ;;; Handle the case when x <= 1
705 (defun interval-expt-< (x y)
706   (case (sb!c::interval-range-info x 0d0)
707     (+
708      ;; The case of 0 <= x <= 1 is easy
709      (case (sb!c::interval-range-info y)
710        (+
711         ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
712         ;; obviously [0, 1]. We just have to be careful for infinite bounds
713         ;; (given by nil).
714         (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
715                              (type-bound-number (sb!c::interval-high y))))
716               (hi (safe-expt (type-bound-number (sb!c::interval-high x))
717                              (type-bound-number (sb!c::interval-low y)))))
718           (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
719        (-
720         ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
721         ;; obviously [1, inf].
722         (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
723                              (type-bound-number (sb!c::interval-low y))))
724               (lo (safe-expt (type-bound-number (sb!c::interval-high x))
725                              (type-bound-number (sb!c::interval-high y)))))
726           (list (sb!c::make-interval :low (or lo 1) :high hi))))
727        (t
728         ;; Split the interval in half
729         (destructuring-bind (y- y+)
730             (sb!c::interval-split 0 y t)
731           (list (interval-expt-< x y-)
732                 (interval-expt-< x y+))))))
733     (-
734      ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
735      ;; The calling function must insure this! For now we'll just
736      ;; return the appropriate unbounded float type.
737      (list (sb!c::make-interval :low nil :high nil)))
738     (t
739      (destructuring-bind (neg pos)
740          (interval-split 0 x t t)
741        (list (interval-expt-< neg y)
742              (interval-expt-< pos y))))))
743
744 ;;; Compute bounds for (expt x y).
745 (defun interval-expt (x y)
746   (case (interval-range-info x 1)
747     (+
748      ;; X >= 1
749          (interval-expt-> x y))
750     (-
751      ;; X <= 1
752      (interval-expt-< x y))
753     (t
754      (destructuring-bind (left right)
755          (interval-split 1 x t t)
756        (list (interval-expt left y)
757              (interval-expt right y))))))
758
759 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
760   (declare (ignore x-int))
761   ;; Figure out what the return type should be, given the argument
762   ;; types and bounds and the result type and bounds.
763   (cond ((csubtypep x-type (specifier-type 'integer))
764          ;; an integer to some power
765          (case (numeric-type-class y-type)
766            (integer
767             ;; Positive integer to an integer power is either an
768             ;; integer or a rational.
769             (let ((lo (or (interval-low bnd) '*))
770                   (hi (or (interval-high bnd) '*)))
771               (if (and (interval-low y-int)
772                        (>= (type-bound-number (interval-low y-int)) 0))
773                   (specifier-type `(integer ,lo ,hi))
774                   (specifier-type `(rational ,lo ,hi)))))
775            (rational
776             ;; Positive integer to rational power is either a rational
777             ;; or a single-float.
778             (let* ((lo (interval-low bnd))
779                    (hi (interval-high bnd))
780                    (int-lo (if lo
781                                (floor (type-bound-number lo))
782                                '*))
783                    (int-hi (if hi
784                                (ceiling (type-bound-number hi))
785                                '*))
786                    (f-lo (if lo
787                              (bound-func #'float lo)
788                              '*))
789                    (f-hi (if hi
790                              (bound-func #'float hi)
791                              '*)))
792               (specifier-type `(or (rational ,int-lo ,int-hi)
793                                 (single-float ,f-lo, f-hi)))))
794            (float
795             ;; A positive integer to a float power is a float.
796             (modified-numeric-type y-type
797                                    :low (interval-low bnd)
798                                    :high (interval-high bnd)))
799            (t
800             ;; A positive integer to a number is a number (for now).
801             (specifier-type 'number))))
802         ((csubtypep x-type (specifier-type 'rational))
803          ;; a rational to some power
804          (case (numeric-type-class y-type)
805            (integer
806             ;; A positive rational to an integer power is always a rational.
807             (specifier-type `(rational ,(or (interval-low bnd) '*)
808                                        ,(or (interval-high bnd) '*))))
809            (rational
810             ;; A positive rational to rational power is either a rational
811             ;; or a single-float.
812             (let* ((lo (interval-low bnd))
813                    (hi (interval-high bnd))
814                    (int-lo (if lo
815                                (floor (type-bound-number lo))
816                                '*))
817                    (int-hi (if hi
818                                (ceiling (type-bound-number hi))
819                                '*))
820                    (f-lo (if lo
821                              (bound-func #'float lo)
822                              '*))
823                    (f-hi (if hi
824                              (bound-func #'float hi)
825                              '*)))
826               (specifier-type `(or (rational ,int-lo ,int-hi)
827                                 (single-float ,f-lo, f-hi)))))
828            (float
829             ;; A positive rational to a float power is a float.
830             (modified-numeric-type y-type
831                                    :low (interval-low bnd)
832                                    :high (interval-high bnd)))
833            (t
834             ;; A positive rational to a number is a number (for now).
835             (specifier-type 'number))))
836         ((csubtypep x-type (specifier-type 'float))
837          ;; a float to some power
838          (case (numeric-type-class y-type)
839            ((or integer rational)
840             ;; A positive float to an integer or rational power is
841             ;; always a float.
842             (make-numeric-type
843              :class 'float
844              :format (numeric-type-format x-type)
845              :low (interval-low bnd)
846              :high (interval-high bnd)))
847            (float
848             ;; A positive float to a float power is a float of the
849             ;; higher type.
850             (make-numeric-type
851              :class 'float
852              :format (float-format-max (numeric-type-format x-type)
853                                        (numeric-type-format y-type))
854              :low (interval-low bnd)
855              :high (interval-high bnd)))
856            (t
857             ;; A positive float to a number is a number (for now)
858             (specifier-type 'number))))
859         (t
860          ;; A number to some power is a number.
861          (specifier-type 'number))))
862
863 (defun merged-interval-expt (x y)
864   (let* ((x-int (numeric-type->interval x))
865          (y-int (numeric-type->interval y)))
866     (mapcar (lambda (type)
867               (fixup-interval-expt type x-int y-int x y))
868             (flatten-list (interval-expt x-int y-int)))))
869
870 (defun expt-derive-type-aux (x y same-arg)
871   (declare (ignore same-arg))
872   (cond ((or (not (numeric-type-real-p x))
873              (not (numeric-type-real-p y)))
874          ;; Use numeric contagion if either is not real.
875          (numeric-contagion x y))
876         ((csubtypep y (specifier-type 'integer))
877          ;; A real raised to an integer power is well-defined.
878          (merged-interval-expt x y))
879         ;; A real raised to a non-integral power can be a float or a
880         ;; complex number.
881         ((or (csubtypep x (specifier-type '(rational 0)))
882              (csubtypep x (specifier-type '(float (0d0)))))
883          ;; But a positive real to any power is well-defined.
884          (merged-interval-expt x y))
885         ((and (csubtypep x (specifier-type 'rational))
886               (csubtypep x (specifier-type 'rational)))
887          ;; A rational to the power of a rational could be a rational
888          ;; or a possibly-complex single float
889          (specifier-type '(or rational single-float (complex single-float))))
890         (t
891          ;; a real to some power. The result could be a real or a
892          ;; complex.
893          (float-or-complex-float-type (numeric-contagion x y)))))
894
895 (defoptimizer (expt derive-type) ((x y))
896   (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
897
898 ;;; Note we must assume that a type including 0.0 may also include
899 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
900 (defun log-derive-type-aux-1 (x)
901   (elfun-derive-type-simple x #'log 0d0 nil nil nil))
902
903 (defun log-derive-type-aux-2 (x y same-arg)
904   (let ((log-x (log-derive-type-aux-1 x))
905         (log-y (log-derive-type-aux-1 y))
906         (accumulated-list nil))
907     ;; LOG-X or LOG-Y might be union types. We need to run through
908     ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
909     (dolist (x-type (prepare-arg-for-derive-type log-x))
910       (dolist (y-type (prepare-arg-for-derive-type log-y))
911         (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
912     (apply #'type-union (flatten-list accumulated-list))))
913
914 (defoptimizer (log derive-type) ((x &optional y))
915   (if y
916       (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
917       (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
918
919 (defun atan-derive-type-aux-1 (y)
920   (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
921
922 (defun atan-derive-type-aux-2 (y x same-arg)
923   (declare (ignore same-arg))
924   ;; The hard case with two args. We just return the max bounds.
925   (let ((result-type (numeric-contagion y x)))
926     (cond ((and (numeric-type-real-p x)
927                 (numeric-type-real-p y))
928            (let* (;; FIXME: This expression for FORMAT seems to
929                   ;; appear multiple times, and should be factored out.
930                   (format (case (numeric-type-class result-type)
931                             ((integer rational) 'single-float)
932                             (t (numeric-type-format result-type))))
933                   (bound-format (or format 'float)))
934              (make-numeric-type :class 'float
935                                 :format format
936                                 :complexp :real
937                                 :low (coerce (- pi) bound-format)
938                                 :high (coerce pi bound-format))))
939           (t
940            ;; The result is a float or a complex number
941            (float-or-complex-float-type result-type)))))
942
943 (defoptimizer (atan derive-type) ((y &optional x))
944   (if x
945       (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
946       (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
947
948 (defun cosh-derive-type-aux (x)
949   ;; We note that cosh x = cosh |x| for all real x.
950   (elfun-derive-type-simple
951    (if (numeric-type-real-p x)
952        (abs-derive-type-aux x)
953        x)
954    #'cosh nil nil 0 nil))
955
956 (defoptimizer (cosh derive-type) ((num))
957   (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
958
959 (defun phase-derive-type-aux (arg)
960   (let* ((format (case (numeric-type-class arg)
961                    ((integer rational) 'single-float)
962                    (t (numeric-type-format arg))))
963          (bound-type (or format 'float)))
964     (cond ((numeric-type-real-p arg)
965            (case (interval-range-info (numeric-type->interval arg) 0.0)
966              (+
967               ;; The number is positive, so the phase is 0.
968               (make-numeric-type :class 'float
969                                  :format format
970                                  :complexp :real
971                                  :low (coerce 0 bound-type)
972                                  :high (coerce 0 bound-type)))
973              (-
974               ;; The number is always negative, so the phase is pi.
975               (make-numeric-type :class 'float
976                                  :format format
977                                  :complexp :real
978                                  :low (coerce pi bound-type)
979                                  :high (coerce pi bound-type)))
980              (t
981               ;; We can't tell. The result is 0 or pi. Use a union
982               ;; type for this.
983               (list
984                (make-numeric-type :class 'float
985                                   :format format
986                                   :complexp :real
987                                   :low (coerce 0 bound-type)
988                                   :high (coerce 0 bound-type))
989                (make-numeric-type :class 'float
990                                   :format format
991                                   :complexp :real
992                                   :low (coerce pi bound-type)
993                                   :high (coerce pi bound-type))))))
994           (t
995            ;; We have a complex number. The answer is the range -pi
996            ;; to pi. (-pi is included because we have -0.)
997            (make-numeric-type :class 'float
998                               :format format
999                               :complexp :real
1000                               :low (coerce (- pi) bound-type)
1001                               :high (coerce pi bound-type))))))
1002
1003 (defoptimizer (phase derive-type) ((num))
1004   (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1005
1006 ) ; PROGN
1007
1008 (deftransform realpart ((x) ((complex rational)) *)
1009   '(sb!kernel:%realpart x))
1010 (deftransform imagpart ((x) ((complex rational)) *)
1011   '(sb!kernel:%imagpart x))
1012
1013 ;;; Make REALPART and IMAGPART return the appropriate types. This
1014 ;;; should help a lot in optimized code.
1015 (defun realpart-derive-type-aux (type)
1016   (let ((class (numeric-type-class type))
1017         (format (numeric-type-format type)))
1018     (cond ((numeric-type-real-p type)
1019            ;; The realpart of a real has the same type and range as
1020            ;; the input.
1021            (make-numeric-type :class class
1022                               :format format
1023                               :complexp :real
1024                               :low (numeric-type-low type)
1025                               :high (numeric-type-high type)))
1026           (t
1027            ;; We have a complex number. The result has the same type
1028            ;; as the real part, except that it's real, not complex,
1029            ;; obviously.
1030            (make-numeric-type :class class
1031                               :format format
1032                               :complexp :real
1033                               :low (numeric-type-low type)
1034                               :high (numeric-type-high type))))))
1035 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1036 (defoptimizer (realpart derive-type) ((num))
1037   (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1038 (defun imagpart-derive-type-aux (type)
1039   (let ((class (numeric-type-class type))
1040         (format (numeric-type-format type)))
1041     (cond ((numeric-type-real-p type)
1042            ;; The imagpart of a real has the same type as the input,
1043            ;; except that it's zero.
1044            (let ((bound-format (or format class 'real)))
1045              (make-numeric-type :class class
1046                                 :format format
1047                                 :complexp :real
1048                                 :low (coerce 0 bound-format)
1049                                 :high (coerce 0 bound-format))))
1050           (t
1051            ;; We have a complex number. The result has the same type as
1052            ;; the imaginary part, except that it's real, not complex,
1053            ;; obviously.
1054            (make-numeric-type :class class
1055                               :format format
1056                               :complexp :real
1057                               :low (numeric-type-low type)
1058                               :high (numeric-type-high type))))))
1059 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1060 (defoptimizer (imagpart derive-type) ((num))
1061   (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1062
1063 (defun complex-derive-type-aux-1 (re-type)
1064   (if (numeric-type-p re-type)
1065       (make-numeric-type :class (numeric-type-class re-type)
1066                          :format (numeric-type-format re-type)
1067                          :complexp (if (csubtypep re-type
1068                                                   (specifier-type 'rational))
1069                                        :real
1070                                        :complex)
1071                          :low (numeric-type-low re-type)
1072                          :high (numeric-type-high re-type))
1073       (specifier-type 'complex)))
1074
1075 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1076   (declare (ignore same-arg))
1077   (if (and (numeric-type-p re-type)
1078            (numeric-type-p im-type))
1079       ;; Need to check to make sure numeric-contagion returns the
1080       ;; right type for what we want here.
1081
1082       ;; Also, what about rational canonicalization, like (complex 5 0)
1083       ;; is 5?  So, if the result must be complex, we make it so.
1084       ;; If the result might be complex, which happens only if the
1085       ;; arguments are rational, we make it a union type of (or
1086       ;; rational (complex rational)).
1087       (let* ((element-type (numeric-contagion re-type im-type))
1088              (rat-result-p (csubtypep element-type
1089                                       (specifier-type 'rational))))
1090         (if rat-result-p
1091             (type-union element-type
1092                         (specifier-type
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
1097                                              :real
1098                                              :complex))))
1099       (specifier-type 'complex)))
1100
1101 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1102 (defoptimizer (complex derive-type) ((re &optional im))
1103   (if 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)))
1106
1107 ;;; Define some transforms for complex operations. We do this in lieu
1108 ;;; of complex operation VOPs.
1109 (macrolet ((frob (type)
1110              `(progn
1111                ;; negation
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))
1134                          (ix (imagpart x))
1135                          (ry (realpart y))
1136                          (iy (imagpart y)))
1137                     (complex (- (* rx ry) (* ix iy))
1138                              (+ (* rx iy) (* ix ry)))))
1139                (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1140                  '(let* ((rx (realpart x))
1141                          (ix (imagpart x))
1142                          (ry (realpart y))
1143                          (iy (imagpart y)))
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))))
1164                ;; CIS
1165                (deftransform cis ((z) ((,type)) *)
1166                  '(complex (cos z) (sin z)))
1167                ;; comparison
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)))))))
1175
1176   (frob single-float)
1177   (frob double-float))
1178
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.
1184 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1185 (progn
1186 (defun trig-derive-type-aux (arg domain fcn
1187                                  &optional def-lo def-hi (increasingp t))
1188   (etypecase arg
1189     (numeric-type
1190      (cond ((eq (numeric-type-complexp arg) :complex)
1191             (make-numeric-type :class (numeric-type-class arg)
1192                                :format (numeric-type-format arg)
1193                                :complexp :complex))
1194            ((numeric-type-real-p arg)
1195             (let* ((format (case (numeric-type-class arg)
1196                              ((integer rational) 'single-float)
1197                              (t (numeric-type-format arg))))
1198                    (bound-type (or format 'float)))
1199               ;; If the argument is a subset of the "principal" domain
1200               ;; of the function, we can compute the bounds because
1201               ;; the function is monotonic. We can't do this in
1202               ;; general for these periodic functions because we can't
1203               ;; (and don't want to) do the argument reduction in
1204               ;; exactly the same way as the functions themselves do
1205               ;; it.
1206               (if (csubtypep arg domain)
1207                   (let ((res-lo (bound-func fcn (numeric-type-low arg)))
1208                         (res-hi (bound-func fcn (numeric-type-high arg))))
1209                     (unless increasingp
1210                       (rotatef res-lo res-hi))
1211                     (make-numeric-type
1212                      :class 'float
1213                      :format format
1214                      :low (coerce-numeric-bound res-lo bound-type)
1215                      :high (coerce-numeric-bound res-hi bound-type)))
1216                   (make-numeric-type
1217                    :class 'float
1218                    :format format
1219                    :low (and def-lo (coerce def-lo bound-type))
1220                    :high (and def-hi (coerce def-hi bound-type))))))
1221            (t
1222             (float-or-complex-float-type arg def-lo def-hi))))))
1223
1224 (defoptimizer (sin derive-type) ((num))
1225   (one-arg-derive-type
1226    num
1227    (lambda (arg)
1228      ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1229      (trig-derive-type-aux
1230       arg
1231       (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1232       #'sin
1233       -1 1))
1234    #'sin))
1235
1236 (defoptimizer (cos derive-type) ((num))
1237   (one-arg-derive-type
1238    num
1239    (lambda (arg)
1240      ;; Derive the bounds if the arg is in [0, pi].
1241      (trig-derive-type-aux arg
1242                            (specifier-type `(float 0d0 ,pi))
1243                            #'cos
1244                            -1 1
1245                            nil))
1246    #'cos))
1247
1248 (defoptimizer (tan derive-type) ((num))
1249   (one-arg-derive-type
1250    num
1251    (lambda (arg)
1252      ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1253      (trig-derive-type-aux arg
1254                            (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1255                            #'tan
1256                            nil nil))
1257    #'tan))
1258
1259 (defoptimizer (conjugate derive-type) ((num))
1260   (one-arg-derive-type num
1261     (lambda (arg)
1262       (flet ((most-negative-bound (l h)
1263                (and l h
1264                     (if (< (type-bound-number l) (- (type-bound-number h)))
1265                         l
1266                         (set-bound (- (type-bound-number h)) (consp h)))))
1267              (most-positive-bound (l h)
1268                (and l h
1269                     (if (> (type-bound-number h) (- (type-bound-number l)))
1270                         h
1271                         (set-bound (- (type-bound-number l)) (consp l))))))
1272         (if (numeric-type-real-p arg)
1273             (lvar-type num)
1274             (let ((low (numeric-type-low arg))
1275                   (high (numeric-type-high arg)))
1276               (let ((new-low (most-negative-bound low high))
1277                     (new-high (most-positive-bound low high)))
1278               (modified-numeric-type arg :low new-low :high new-high))))))
1279     #'conjugate))
1280
1281 (defoptimizer (cis derive-type) ((num))
1282   (one-arg-derive-type num
1283     (lambda (arg)
1284       (sb!c::specifier-type
1285        `(complex ,(or (numeric-type-format arg) 'float))))
1286     #'cis))
1287
1288 ) ; PROGN
1289 \f
1290 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1291
1292 (macrolet ((define-frobs (fun ufun)
1293              `(progn
1294                 (defknown ,ufun (real) integer (movable foldable flushable))
1295                 (deftransform ,fun ((x &optional by)
1296                                     (* &optional
1297                                        (constant-arg (member 1))))
1298                   '(let ((res (,ufun x)))
1299                      (values res (- x res)))))))
1300   (define-frobs truncate %unary-truncate)
1301   (define-frobs round %unary-round))
1302
1303 ;;; Convert (TRUNCATE x y) to the obvious implementation.  We only want
1304 ;;; this when under certain conditions and let the generic TRUNCATE
1305 ;;; handle the rest.  (Note: if Y = 1, the divide and multiply by Y
1306 ;;; should be removed by other DEFTRANSFORMs.)
1307 (deftransform truncate ((x &optional y)
1308                         (float &optional (or float integer)))
1309   (let ((defaulted-y (if y 'y 1)))
1310     `(let ((res (%unary-truncate (/ x ,defaulted-y))))
1311        (values res (- x (* ,defaulted-y res))))))
1312
1313 (deftransform floor ((number &optional divisor)
1314                      (float &optional (or integer float)))
1315   (let ((defaulted-divisor (if divisor 'divisor 1)))
1316     `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1317        (if (and (not (zerop rem))
1318                 (if (minusp ,defaulted-divisor)
1319                     (plusp number)
1320                     (minusp number)))
1321            (values (1- tru) (+ rem ,defaulted-divisor))
1322            (values tru rem)))))
1323
1324 (deftransform ceiling ((number &optional divisor)
1325                        (float &optional (or integer float)))
1326   (let ((defaulted-divisor (if divisor 'divisor 1)))
1327     `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1328        (if (and (not (zerop rem))
1329                 (if (minusp ,defaulted-divisor)
1330                     (minusp number)
1331                     (plusp number)))
1332            (values (1+ tru) (- rem ,defaulted-divisor))
1333            (values tru rem)))))
1334
1335 (defknown %unary-ftruncate (real) float (movable foldable flushable))
1336 (defknown %unary-ftruncate/single (single-float) single-float
1337   (movable foldable flushable))
1338 (defknown %unary-ftruncate/double (double-float) double-float
1339   (movable foldable flushable))
1340
1341 (defun %unary-ftruncate/single (x)
1342   (declare (type single-float x))
1343   (declare (optimize speed (safety 0)))
1344   (let* ((bits (single-float-bits x))
1345          (exp (ldb sb!vm:single-float-exponent-byte bits))
1346          (biased (the single-float-exponent
1347                    (- exp sb!vm:single-float-bias))))
1348     (declare (type (signed-byte 32) bits))
1349     (cond
1350       ((= exp sb!vm:single-float-normal-exponent-max) x)
1351       ((<= biased 0) (* x 0f0))
1352       ((>= biased (float-digits x)) x)
1353       (t
1354        (let ((frac-bits (- (float-digits x) biased)))
1355          (setf bits (logandc2 bits (- (ash 1 frac-bits) 1)))
1356          (make-single-float bits))))))
1357
1358 (defun %unary-ftruncate/double (x)
1359   (declare (type double-float x))
1360   (declare (optimize speed (safety 0)))
1361   (let* ((high (double-float-high-bits x))
1362          (low (double-float-low-bits x))
1363          (exp (ldb sb!vm:double-float-exponent-byte high))
1364          (biased (the double-float-exponent
1365                    (- exp sb!vm:double-float-bias))))
1366     (declare (type (signed-byte 32) high)
1367              (type (unsigned-byte 32) low))
1368     (cond
1369       ((= exp sb!vm:double-float-normal-exponent-max) x)
1370       ((<= biased 0) (* x 0d0))
1371       ((>= biased (float-digits x)) x)
1372       (t
1373        (let ((frac-bits (- (float-digits x) biased)))
1374          (cond ((< frac-bits 32)
1375                 (setf low (logandc2 low (- (ash 1 frac-bits) 1))))
1376                (t
1377                 (setf low 0)
1378                 (setf high (logandc2 high (- (ash 1 (- frac-bits 32)) 1)))))
1379          (make-double-float high low))))))
1380
1381 (macrolet
1382     ((def (float-type fun)
1383          `(deftransform %unary-ftruncate ((x) (,float-type))
1384             '(,fun x))))
1385   (def single-float %unary-ftruncate/single)
1386   (def double-float %unary-ftruncate/double))