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