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