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