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