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