1 ;;;; This file contains floating-point-specific transforms, and may be
2 ;;;; somewhat implementation-dependent in its assumptions of what the
5 ;;;; This software is part of the SBCL system. See the README file for
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.
18 (defknown %single-float (real) single-float (movable foldable))
19 (defknown %double-float (real) double-float (movable foldable))
21 (deftransform float ((n f) (* single-float) *)
24 (deftransform float ((n f) (* double-float) *)
27 (deftransform float ((n) *)
32 (deftransform %single-float ((n) (single-float) *)
35 (deftransform %double-float ((n) (double-float) *)
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
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
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
63 (64 'sb!kernel::big-random-chunk))))
64 (if (numeric-type-p type)
65 (let ((num-high (numeric-type-high (lvar-type num))))
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))
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*))
79 `(rem (,random-chunk (or state *random-state*)) num)
81 ;; Use multiplication, which is faster.
82 `(values (sb!bignum::%multiply
83 (,random-chunk (or state *random-state*))
85 ((> num-high random-fixnum-max)
86 (give-up-ir1-transform
87 "The range is too large to ensure an accurate result."))
90 `(values (sb!bignum::%multiply
91 (,random-chunk (or state *random-state*))
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
98 '(rem (random-chunk (or state *random-state*)) num))))
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) *)
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) *)
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))))
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
196 ;;; * SBCL itself gets built with more complete optimization.
198 ;;; * You get a different SBCL depending on what your cross-compilation
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
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.)
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
230 (scale-float (type-bound-number x) n)
231 (floating-point-overflow ()
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))
242 (if (< (float-sign (type-bound-number f-hi)) 0.0)
244 (setf new-hi (scale-bound f-hi ex-lo)))
246 (setf new-hi (scale-bound f-hi ex-hi)))))
248 (if (< (float-sign (type-bound-number f-lo)) 0.0)
250 (setf new-lo (scale-bound f-lo ex-hi)))
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)
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.
270 ((frob (fun type most-negative most-positive)
271 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
273 (defun ,aux-name (num)
274 ;; When converting a number to a float, the limits are
276 (let* ((lo (bound-func (lambda (x)
277 (if (< x ,most-negative)
280 (numeric-type-low num)))
281 (hi (bound-func (lambda (x)
282 (if (< ,most-positive x )
285 (numeric-type-high num))))
286 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
288 (defoptimizer (,fun derive-type) ((num))
290 (one-arg-derive-type num #',aux-name #',fun)
293 (frob %single-float single-float
294 most-negative-single-float most-positive-single-float)
295 (frob %double-float double-float
296 most-negative-double-float most-positive-double-float))
301 (defun safe-ctype-for-single-coercion-p (x)
302 ;; See comment in SAFE-SINGLE-COERCION-P -- this deals with the same
303 ;; problem, but in the context of evaluated and compiled (+ <int> <single>)
304 ;; giving different result if we fail to check for this.
305 (or (not (csubtypep x (specifier-type 'integer)))
306 (csubtypep x (specifier-type `(integer ,most-negative-exactly-single-float-fixnum
307 ,most-positive-exactly-single-float-fixnum)))))
309 ;;; Do some stuff to recognize when the loser is doing mixed float and
310 ;;; rational arithmetic, or different float types, and fix it up. If
311 ;;; we don't, he won't even get so much as an efficiency note.
312 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
313 (if (or (not (types-equal-or-intersect (lvar-type y) (specifier-type 'single-float)))
314 (safe-ctype-for-single-coercion-p (lvar-type x)))
315 `(,(lvar-fun-name (basic-combination-fun node))
317 (give-up-ir1-transform)))
318 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
319 (if (or (not (types-equal-or-intersect (lvar-type x) (specifier-type 'single-float)))
320 (safe-ctype-for-single-coercion-p (lvar-type y)))
321 `(,(lvar-fun-name (basic-combination-fun node))
323 (give-up-ir1-transform)))
325 (dolist (x '(+ * / -))
326 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
327 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
329 (dolist (x '(= < > + * / -))
330 (%deftransform x '(function (single-float double-float) *)
331 #'float-contagion-arg1)
332 (%deftransform x '(function (double-float single-float) *)
333 #'float-contagion-arg2))
335 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
336 ;;; general float rational args to comparison, since Common Lisp
337 ;;; semantics says we are supposed to compare as rationals, but we can
338 ;;; do it for any rational that has a precise representation as a
339 ;;; float (such as 0).
340 (macrolet ((frob (op)
341 `(deftransform ,op ((x y) (float rational) *)
342 "open-code FLOAT to RATIONAL comparison"
343 (unless (constant-lvar-p y)
344 (give-up-ir1-transform
345 "The RATIONAL value isn't known at compile time."))
346 (let ((val (lvar-value y)))
347 (unless (eql (rational (float val)) val)
348 (give-up-ir1-transform
349 "~S doesn't have a precise float representation."
351 `(,',op x (float y x)))))
356 ;;;; irrational derive-type methods
358 ;;; Derive the result to be float for argument types in the
359 ;;; appropriate domain.
360 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
361 (dolist (stuff '((asin (real -1.0 1.0))
362 (acos (real -1.0 1.0))
364 (atanh (real -1.0 1.0))
366 (destructuring-bind (name type) stuff
367 (let ((type (specifier-type type)))
368 (setf (fun-info-derive-type (fun-info-or-lose name))
370 (declare (type combination call))
371 (when (csubtypep (lvar-type
372 (first (combination-args call)))
374 (specifier-type 'float)))))))
376 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
377 (defoptimizer (log derive-type) ((x &optional y))
378 (when (and (csubtypep (lvar-type x)
379 (specifier-type '(real 0.0)))
381 (csubtypep (lvar-type y)
382 (specifier-type '(real 0.0)))))
383 (specifier-type 'float)))
385 ;;;; irrational transforms
387 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
388 (double-float) double-float
389 (movable foldable flushable))
391 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
392 (double-float) (double-float -1.0d0 1.0d0)
393 (movable foldable flushable))
395 (defknown (%asin %atan)
397 (double-float #.(coerce (- (/ pi 2)) 'double-float)
398 #.(coerce (/ pi 2) 'double-float))
399 (movable foldable flushable))
402 (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
403 (movable foldable flushable))
406 (double-float) (double-float 1.0d0)
407 (movable foldable flushable))
409 (defknown (%acosh %exp %sqrt)
410 (double-float) (double-float 0.0d0)
411 (movable foldable flushable))
414 (double-float) (double-float -1d0)
415 (movable foldable flushable))
418 (double-float double-float) (double-float 0d0)
419 (movable foldable flushable))
422 (double-float double-float) double-float
423 (movable foldable flushable))
426 (double-float double-float)
427 (double-float #.(coerce (- pi) 'double-float)
428 #.(coerce pi 'double-float))
429 (movable foldable flushable))
432 (double-float double-float) double-float
433 (movable foldable flushable))
436 (double-float (signed-byte 32)) double-float
437 (movable foldable flushable))
440 (double-float) double-float
441 (movable foldable flushable))
443 (macrolet ((def (name prim rtype)
445 (deftransform ,name ((x) (single-float) ,rtype)
446 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
447 (deftransform ,name ((x) (double-float) ,rtype)
451 (def sqrt %sqrt float)
452 (def asin %asin float)
453 (def acos %acos float)
459 (def acosh %acosh float)
460 (def atanh %atanh float))
462 ;;; The argument range is limited on the x86 FP trig. functions. A
463 ;;; post-test can detect a failure (and load a suitable result), but
464 ;;; this test is avoided if possible.
465 (macrolet ((def (name prim prim-quick)
466 (declare (ignorable prim-quick))
468 (deftransform ,name ((x) (single-float) *)
469 #!+x86 (cond ((csubtypep (lvar-type x)
470 (specifier-type '(single-float
471 (#.(- (expt 2f0 64)))
473 `(coerce (,',prim-quick (coerce x 'double-float))
477 "unable to avoid inline argument range check~@
478 because the argument range (~S) was not within 2^64"
479 (type-specifier (lvar-type x)))
480 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
481 #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
482 (deftransform ,name ((x) (double-float) *)
483 #!+x86 (cond ((csubtypep (lvar-type x)
484 (specifier-type '(double-float
485 (#.(- (expt 2d0 64)))
490 "unable to avoid inline argument range check~@
491 because the argument range (~S) was not within 2^64"
492 (type-specifier (lvar-type x)))
494 #!-x86 `(,',prim x)))))
495 (def sin %sin %sin-quick)
496 (def cos %cos %cos-quick)
497 (def tan %tan %tan-quick))
499 (deftransform atan ((x y) (single-float single-float) *)
500 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
502 (deftransform atan ((x y) (double-float double-float) *)
505 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
506 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
508 (deftransform expt ((x y) ((double-float 0d0) double-float) *)
510 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
511 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
513 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) *)
514 `(%pow x (coerce y 'double-float)))
515 (deftransform expt ((x y) ((integer -1 -1) integer) *)
516 `(if (evenp y) 1 -1))
518 ;;; ANSI says log with base zero returns zero.
519 (deftransform log ((x y) (float float) float)
520 '(if (zerop y) y (/ (log x) (log y))))
522 ;;; Handle some simple transformations.
524 (deftransform abs ((x) ((complex double-float)) double-float)
525 '(%hypot (realpart x) (imagpart x)))
527 (deftransform abs ((x) ((complex single-float)) single-float)
528 '(coerce (%hypot (coerce (realpart x) 'double-float)
529 (coerce (imagpart x) 'double-float))
532 (deftransform phase ((x) ((complex double-float)) double-float)
533 '(%atan2 (imagpart x) (realpart x)))
535 (deftransform phase ((x) ((complex single-float)) single-float)
536 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
537 (coerce (realpart x) 'double-float))
540 (deftransform phase ((x) ((float)) float)
541 '(if (minusp (float-sign x))
545 ;;; The number is of type REAL.
546 (defun numeric-type-real-p (type)
547 (and (numeric-type-p type)
548 (eq (numeric-type-complexp type) :real)))
550 ;;; Coerce a numeric type bound to the given type while handling
551 ;;; exclusive bounds.
552 (defun coerce-numeric-bound (bound type)
555 (list (coerce (car bound) type))
556 (coerce bound type))))
558 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
561 ;;;; optimizers for elementary functions
563 ;;;; These optimizers compute the output range of the elementary
564 ;;;; function, based on the domain of the input.
566 ;;; Generate a specifier for a complex type specialized to the same
567 ;;; type as the argument.
568 (defun complex-float-type (arg)
569 (declare (type numeric-type arg))
570 (let* ((format (case (numeric-type-class arg)
571 ((integer rational) 'single-float)
572 (t (numeric-type-format arg))))
573 (float-type (or format 'float)))
574 (specifier-type `(complex ,float-type))))
576 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
577 ;;; should be the right kind of float. Allow bounds for the float
579 (defun float-or-complex-float-type (arg &optional lo hi)
580 (declare (type numeric-type arg))
581 (let* ((format (case (numeric-type-class arg)
582 ((integer rational) 'single-float)
583 (t (numeric-type-format arg))))
584 (float-type (or format 'float))
585 (lo (coerce-numeric-bound lo float-type))
586 (hi (coerce-numeric-bound hi float-type)))
587 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
588 (complex ,float-type)))))
592 (eval-when (:compile-toplevel :execute)
593 ;; So the problem with this hack is that it's actually broken. If
594 ;; the host does not have long floats, then setting *R-D-F-F* to
595 ;; LONG-FLOAT doesn't actually buy us anything. FIXME.
596 (setf *read-default-float-format*
597 #!+long-float 'long-float #!-long-float 'double-float))
598 ;;; Test whether the numeric-type ARG is within in domain specified by
599 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
601 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
602 (defun domain-subtypep (arg domain-low domain-high)
603 (declare (type numeric-type arg)
604 (type (or real null) domain-low domain-high))
605 (let* ((arg-lo (numeric-type-low arg))
606 (arg-lo-val (type-bound-number arg-lo))
607 (arg-hi (numeric-type-high arg))
608 (arg-hi-val (type-bound-number arg-hi)))
609 ;; Check that the ARG bounds are correctly canonicalized.
610 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
611 (minusp (float-sign arg-lo-val)))
612 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-lo)
613 (setq arg-lo 0e0 arg-lo-val arg-lo))
614 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
615 (plusp (float-sign arg-hi-val)))
616 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-hi)
617 (setq arg-hi (ecase *read-default-float-format*
618 (double-float (load-time-value (make-unportable-float :double-float-negative-zero)))
620 (long-float (load-time-value (make-unportable-float :long-float-negative-zero))))
622 (flet ((fp-neg-zero-p (f) ; Is F -0.0?
623 (and (floatp f) (zerop f) (minusp (float-sign f))))
624 (fp-pos-zero-p (f) ; Is F +0.0?
625 (and (floatp f) (zerop f) (plusp (float-sign f)))))
626 (and (or (null domain-low)
627 (and arg-lo (>= arg-lo-val domain-low)
628 (not (and (fp-pos-zero-p domain-low)
629 (fp-neg-zero-p arg-lo)))))
630 (or (null domain-high)
631 (and arg-hi (<= arg-hi-val domain-high)
632 (not (and (fp-neg-zero-p domain-high)
633 (fp-pos-zero-p arg-hi)))))))))
634 (eval-when (:compile-toplevel :execute)
635 (setf *read-default-float-format* 'single-float))
637 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
640 ;;; Handle monotonic functions of a single variable whose domain is
641 ;;; possibly part of the real line. ARG is the variable, FUN is the
642 ;;; function, and DOMAIN is a specifier that gives the (real) domain
643 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
644 ;;; bounds directly. Otherwise, we compute the bounds for the
645 ;;; intersection between ARG and DOMAIN, and then append a complex
646 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
648 ;;; Negative and positive zero are considered distinct within
649 ;;; DOMAIN-LOW and DOMAIN-HIGH.
651 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
652 ;;; can't compute the bounds using FUN.
653 (defun elfun-derive-type-simple (arg fun domain-low domain-high
654 default-low default-high
655 &optional (increasingp t))
656 (declare (type (or null real) domain-low domain-high))
659 (cond ((eq (numeric-type-complexp arg) :complex)
660 (complex-float-type arg))
661 ((numeric-type-real-p arg)
662 ;; The argument is real, so let's find the intersection
663 ;; between the argument and the domain of the function.
664 ;; We compute the bounds on the intersection, and for
665 ;; everything else, we return a complex number of the
667 (multiple-value-bind (intersection difference)
668 (interval-intersection/difference (numeric-type->interval arg)
674 ;; Process the intersection.
675 (let* ((low (interval-low intersection))
676 (high (interval-high intersection))
677 (res-lo (or (bound-func fun (if increasingp low high))
679 (res-hi (or (bound-func fun (if increasingp high low))
681 (format (case (numeric-type-class arg)
682 ((integer rational) 'single-float)
683 (t (numeric-type-format arg))))
684 (bound-type (or format 'float))
689 :low (coerce-numeric-bound res-lo bound-type)
690 :high (coerce-numeric-bound res-hi bound-type))))
691 ;; If the ARG is a subset of the domain, we don't
692 ;; have to worry about the difference, because that
694 (if (or (null difference)
695 ;; Check whether the arg is within the domain.
696 (domain-subtypep arg domain-low domain-high))
699 (specifier-type `(complex ,bound-type))))))
701 ;; No intersection so the result must be purely complex.
702 (complex-float-type arg)))))
704 (float-or-complex-float-type arg default-low default-high))))))
707 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
708 &key (increasingp t))
709 (let ((num (gensym)))
710 `(defoptimizer (,name derive-type) ((,num))
714 (elfun-derive-type-simple arg #',name
715 ,domain-low ,domain-high
716 ,def-low-bnd ,def-high-bnd
719 ;; These functions are easy because they are defined for the whole
721 (frob exp nil nil 0 nil)
722 (frob sinh nil nil nil nil)
723 (frob tanh nil nil -1 1)
724 (frob asinh nil nil nil nil)
726 ;; These functions are only defined for part of the real line. The
727 ;; condition selects the desired part of the line.
728 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
729 ;; Acos is monotonic decreasing, so we need to swap the function
730 ;; values at the lower and upper bounds of the input domain.
731 (frob acos -1d0 1d0 0 pi :increasingp nil)
732 (frob acosh 1d0 nil nil nil)
733 (frob atanh -1d0 1d0 -1 1)
734 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
736 (frob sqrt (load-time-value (make-unportable-float :double-float-negative-zero)) nil 0 nil))
738 ;;; Compute bounds for (expt x y). This should be easy since (expt x
739 ;;; y) = (exp (* y (log x))). However, computations done this way
740 ;;; have too much roundoff. Thus we have to do it the hard way.
741 (defun safe-expt (x y)
743 (when (< (abs y) 10000)
748 ;;; Handle the case when x >= 1.
749 (defun interval-expt-> (x y)
750 (case (sb!c::interval-range-info y 0d0)
752 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
753 ;; obviously non-negative. We just have to be careful for
754 ;; infinite bounds (given by nil).
755 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
756 (type-bound-number (sb!c::interval-low y))))
757 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
758 (type-bound-number (sb!c::interval-high y)))))
759 (list (sb!c::make-interval :low (or lo 1) :high hi))))
761 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
762 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
764 (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
765 (type-bound-number (sb!c::interval-low y))))
766 (hi (safe-expt (type-bound-number (sb!c::interval-low x))
767 (type-bound-number (sb!c::interval-high y)))))
768 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
770 ;; Split the interval in half.
771 (destructuring-bind (y- y+)
772 (sb!c::interval-split 0 y t)
773 (list (interval-expt-> x y-)
774 (interval-expt-> x y+))))))
776 ;;; Handle the case when x <= 1
777 (defun interval-expt-< (x y)
778 (case (sb!c::interval-range-info x 0d0)
780 ;; The case of 0 <= x <= 1 is easy
781 (case (sb!c::interval-range-info y)
783 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
784 ;; obviously [0, 1]. We just have to be careful for infinite bounds
786 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
787 (type-bound-number (sb!c::interval-high y))))
788 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
789 (type-bound-number (sb!c::interval-low y)))))
790 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
792 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
793 ;; obviously [1, inf].
794 (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
795 (type-bound-number (sb!c::interval-low y))))
796 (lo (safe-expt (type-bound-number (sb!c::interval-high x))
797 (type-bound-number (sb!c::interval-high y)))))
798 (list (sb!c::make-interval :low (or lo 1) :high hi))))
800 ;; Split the interval in half
801 (destructuring-bind (y- y+)
802 (sb!c::interval-split 0 y t)
803 (list (interval-expt-< x y-)
804 (interval-expt-< x y+))))))
806 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
807 ;; The calling function must insure this! For now we'll just
808 ;; return the appropriate unbounded float type.
809 (list (sb!c::make-interval :low nil :high nil)))
811 (destructuring-bind (neg pos)
812 (interval-split 0 x t t)
813 (list (interval-expt-< neg y)
814 (interval-expt-< pos y))))))
816 ;;; Compute bounds for (expt x y).
817 (defun interval-expt (x y)
818 (case (interval-range-info x 1)
821 (interval-expt-> x y))
824 (interval-expt-< x y))
826 (destructuring-bind (left right)
827 (interval-split 1 x t t)
828 (list (interval-expt left y)
829 (interval-expt right y))))))
831 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
832 (declare (ignore x-int))
833 ;; Figure out what the return type should be, given the argument
834 ;; types and bounds and the result type and bounds.
835 (cond ((csubtypep x-type (specifier-type 'integer))
836 ;; an integer to some power
837 (case (numeric-type-class y-type)
839 ;; Positive integer to an integer power is either an
840 ;; integer or a rational.
841 (let ((lo (or (interval-low bnd) '*))
842 (hi (or (interval-high bnd) '*)))
843 (if (and (interval-low y-int)
844 (>= (type-bound-number (interval-low y-int)) 0))
845 (specifier-type `(integer ,lo ,hi))
846 (specifier-type `(rational ,lo ,hi)))))
848 ;; Positive integer to rational power is either a rational
849 ;; or a single-float.
850 (let* ((lo (interval-low bnd))
851 (hi (interval-high bnd))
853 (floor (type-bound-number lo))
856 (ceiling (type-bound-number hi))
859 (bound-func #'float lo)
862 (bound-func #'float hi)
864 (specifier-type `(or (rational ,int-lo ,int-hi)
865 (single-float ,f-lo, f-hi)))))
867 ;; A positive integer to a float power is a float.
868 (modified-numeric-type y-type
869 :low (interval-low bnd)
870 :high (interval-high bnd)))
872 ;; A positive integer to a number is a number (for now).
873 (specifier-type 'number))))
874 ((csubtypep x-type (specifier-type 'rational))
875 ;; a rational to some power
876 (case (numeric-type-class y-type)
878 ;; A positive rational to an integer power is always a rational.
879 (specifier-type `(rational ,(or (interval-low bnd) '*)
880 ,(or (interval-high bnd) '*))))
882 ;; A positive rational to rational power is either a rational
883 ;; or a single-float.
884 (let* ((lo (interval-low bnd))
885 (hi (interval-high bnd))
887 (floor (type-bound-number lo))
890 (ceiling (type-bound-number hi))
893 (bound-func #'float lo)
896 (bound-func #'float hi)
898 (specifier-type `(or (rational ,int-lo ,int-hi)
899 (single-float ,f-lo, f-hi)))))
901 ;; A positive rational to a float power is a float.
902 (modified-numeric-type y-type
903 :low (interval-low bnd)
904 :high (interval-high bnd)))
906 ;; A positive rational to a number is a number (for now).
907 (specifier-type 'number))))
908 ((csubtypep x-type (specifier-type 'float))
909 ;; a float to some power
910 (case (numeric-type-class y-type)
911 ((or integer rational)
912 ;; A positive float to an integer or rational power is
916 :format (numeric-type-format x-type)
917 :low (interval-low bnd)
918 :high (interval-high bnd)))
920 ;; A positive float to a float power is a float of the
924 :format (float-format-max (numeric-type-format x-type)
925 (numeric-type-format y-type))
926 :low (interval-low bnd)
927 :high (interval-high bnd)))
929 ;; A positive float to a number is a number (for now)
930 (specifier-type 'number))))
932 ;; A number to some power is a number.
933 (specifier-type 'number))))
935 (defun merged-interval-expt (x y)
936 (let* ((x-int (numeric-type->interval x))
937 (y-int (numeric-type->interval y)))
938 (mapcar (lambda (type)
939 (fixup-interval-expt type x-int y-int x y))
940 (flatten-list (interval-expt x-int y-int)))))
942 (defun expt-derive-type-aux (x y same-arg)
943 (declare (ignore same-arg))
944 (cond ((or (not (numeric-type-real-p x))
945 (not (numeric-type-real-p y)))
946 ;; Use numeric contagion if either is not real.
947 (numeric-contagion x y))
948 ((csubtypep y (specifier-type 'integer))
949 ;; A real raised to an integer power is well-defined.
950 (merged-interval-expt x y))
951 ;; A real raised to a non-integral power can be a float or a
953 ((or (csubtypep x (specifier-type '(rational 0)))
954 (csubtypep x (specifier-type '(float (0d0)))))
955 ;; But a positive real to any power is well-defined.
956 (merged-interval-expt x y))
957 ((and (csubtypep x (specifier-type 'rational))
958 (csubtypep x (specifier-type 'rational)))
959 ;; A rational to the power of a rational could be a rational
960 ;; or a possibly-complex single float
961 (specifier-type '(or rational single-float (complex single-float))))
963 ;; a real to some power. The result could be a real or a
965 (float-or-complex-float-type (numeric-contagion x y)))))
967 (defoptimizer (expt derive-type) ((x y))
968 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
970 ;;; Note we must assume that a type including 0.0 may also include
971 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
972 (defun log-derive-type-aux-1 (x)
973 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
975 (defun log-derive-type-aux-2 (x y same-arg)
976 (let ((log-x (log-derive-type-aux-1 x))
977 (log-y (log-derive-type-aux-1 y))
978 (accumulated-list nil))
979 ;; LOG-X or LOG-Y might be union types. We need to run through
980 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
981 (dolist (x-type (prepare-arg-for-derive-type log-x))
982 (dolist (y-type (prepare-arg-for-derive-type log-y))
983 (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
984 (apply #'type-union (flatten-list accumulated-list))))
986 (defoptimizer (log derive-type) ((x &optional y))
988 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
989 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
991 (defun atan-derive-type-aux-1 (y)
992 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
994 (defun atan-derive-type-aux-2 (y x same-arg)
995 (declare (ignore same-arg))
996 ;; The hard case with two args. We just return the max bounds.
997 (let ((result-type (numeric-contagion y x)))
998 (cond ((and (numeric-type-real-p x)
999 (numeric-type-real-p y))
1000 (let* (;; FIXME: This expression for FORMAT seems to
1001 ;; appear multiple times, and should be factored out.
1002 (format (case (numeric-type-class result-type)
1003 ((integer rational) 'single-float)
1004 (t (numeric-type-format result-type))))
1005 (bound-format (or format 'float)))
1006 (make-numeric-type :class 'float
1009 :low (coerce (- pi) bound-format)
1010 :high (coerce pi bound-format))))
1012 ;; The result is a float or a complex number
1013 (float-or-complex-float-type result-type)))))
1015 (defoptimizer (atan derive-type) ((y &optional x))
1017 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
1018 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
1020 (defun cosh-derive-type-aux (x)
1021 ;; We note that cosh x = cosh |x| for all real x.
1022 (elfun-derive-type-simple
1023 (if (numeric-type-real-p x)
1024 (abs-derive-type-aux x)
1026 #'cosh nil nil 0 nil))
1028 (defoptimizer (cosh derive-type) ((num))
1029 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
1031 (defun phase-derive-type-aux (arg)
1032 (let* ((format (case (numeric-type-class arg)
1033 ((integer rational) 'single-float)
1034 (t (numeric-type-format arg))))
1035 (bound-type (or format 'float)))
1036 (cond ((numeric-type-real-p arg)
1037 (case (interval-range-info (numeric-type->interval arg) 0.0)
1039 ;; The number is positive, so the phase is 0.
1040 (make-numeric-type :class 'float
1043 :low (coerce 0 bound-type)
1044 :high (coerce 0 bound-type)))
1046 ;; The number is always negative, so the phase is pi.
1047 (make-numeric-type :class 'float
1050 :low (coerce pi bound-type)
1051 :high (coerce pi bound-type)))
1053 ;; We can't tell. The result is 0 or pi. Use a union
1056 (make-numeric-type :class 'float
1059 :low (coerce 0 bound-type)
1060 :high (coerce 0 bound-type))
1061 (make-numeric-type :class 'float
1064 :low (coerce pi bound-type)
1065 :high (coerce pi bound-type))))))
1067 ;; We have a complex number. The answer is the range -pi
1068 ;; to pi. (-pi is included because we have -0.)
1069 (make-numeric-type :class 'float
1072 :low (coerce (- pi) bound-type)
1073 :high (coerce pi bound-type))))))
1075 (defoptimizer (phase derive-type) ((num))
1076 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1080 (deftransform realpart ((x) ((complex rational)) *)
1081 '(sb!kernel:%realpart x))
1082 (deftransform imagpart ((x) ((complex rational)) *)
1083 '(sb!kernel:%imagpart x))
1085 ;;; Make REALPART and IMAGPART return the appropriate types. This
1086 ;;; should help a lot in optimized code.
1087 (defun realpart-derive-type-aux (type)
1088 (let ((class (numeric-type-class type))
1089 (format (numeric-type-format type)))
1090 (cond ((numeric-type-real-p type)
1091 ;; The realpart of a real has the same type and range as
1093 (make-numeric-type :class class
1096 :low (numeric-type-low type)
1097 :high (numeric-type-high type)))
1099 ;; We have a complex number. The result has the same type
1100 ;; as the real part, except that it's real, not complex,
1102 (make-numeric-type :class class
1105 :low (numeric-type-low type)
1106 :high (numeric-type-high type))))))
1107 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1108 (defoptimizer (realpart derive-type) ((num))
1109 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1110 (defun imagpart-derive-type-aux (type)
1111 (let ((class (numeric-type-class type))
1112 (format (numeric-type-format type)))
1113 (cond ((numeric-type-real-p type)
1114 ;; The imagpart of a real has the same type as the input,
1115 ;; except that it's zero.
1116 (let ((bound-format (or format class 'real)))
1117 (make-numeric-type :class class
1120 :low (coerce 0 bound-format)
1121 :high (coerce 0 bound-format))))
1123 ;; We have a complex number. The result has the same type as
1124 ;; the imaginary part, except that it's real, not complex,
1126 (make-numeric-type :class class
1129 :low (numeric-type-low type)
1130 :high (numeric-type-high type))))))
1131 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1132 (defoptimizer (imagpart derive-type) ((num))
1133 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1135 (defun complex-derive-type-aux-1 (re-type)
1136 (if (numeric-type-p re-type)
1137 (make-numeric-type :class (numeric-type-class re-type)
1138 :format (numeric-type-format re-type)
1139 :complexp (if (csubtypep re-type
1140 (specifier-type 'rational))
1143 :low (numeric-type-low re-type)
1144 :high (numeric-type-high re-type))
1145 (specifier-type 'complex)))
1147 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1148 (declare (ignore same-arg))
1149 (if (and (numeric-type-p re-type)
1150 (numeric-type-p im-type))
1151 ;; Need to check to make sure numeric-contagion returns the
1152 ;; right type for what we want here.
1154 ;; Also, what about rational canonicalization, like (complex 5 0)
1155 ;; is 5? So, if the result must be complex, we make it so.
1156 ;; If the result might be complex, which happens only if the
1157 ;; arguments are rational, we make it a union type of (or
1158 ;; rational (complex rational)).
1159 (let* ((element-type (numeric-contagion re-type im-type))
1160 (rat-result-p (csubtypep element-type
1161 (specifier-type 'rational))))
1163 (type-union element-type
1165 `(complex ,(numeric-type-class element-type))))
1166 (make-numeric-type :class (numeric-type-class element-type)
1167 :format (numeric-type-format element-type)
1168 :complexp (if rat-result-p
1171 (specifier-type 'complex)))
1173 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1174 (defoptimizer (complex derive-type) ((re &optional im))
1176 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1177 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1179 ;;; Define some transforms for complex operations. We do this in lieu
1180 ;;; of complex operation VOPs.
1181 (macrolet ((frob (type)
1184 (deftransform %negate ((z) ((complex ,type)) *)
1185 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1186 ;; complex addition and subtraction
1187 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1188 '(complex (+ (realpart w) (realpart z))
1189 (+ (imagpart w) (imagpart z))))
1190 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1191 '(complex (- (realpart w) (realpart z))
1192 (- (imagpart w) (imagpart z))))
1193 ;; Add and subtract a complex and a real.
1194 (deftransform + ((w z) ((complex ,type) real) *)
1195 '(complex (+ (realpart w) z) (imagpart w)))
1196 (deftransform + ((z w) (real (complex ,type)) *)
1197 '(complex (+ (realpart w) z) (imagpart w)))
1198 ;; Add and subtract a real and a complex number.
1199 (deftransform - ((w z) ((complex ,type) real) *)
1200 '(complex (- (realpart w) z) (imagpart w)))
1201 (deftransform - ((z w) (real (complex ,type)) *)
1202 '(complex (- z (realpart w)) (- (imagpart w))))
1203 ;; Multiply and divide two complex numbers.
1204 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1205 '(let* ((rx (realpart x))
1209 (complex (- (* rx ry) (* ix iy))
1210 (+ (* rx iy) (* ix ry)))))
1211 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1212 '(let* ((rx (realpart x))
1216 (if (> (abs ry) (abs iy))
1217 (let* ((r (/ iy ry))
1218 (dn (* ry (+ 1 (* r r)))))
1219 (complex (/ (+ rx (* ix r)) dn)
1220 (/ (- ix (* rx r)) dn)))
1221 (let* ((r (/ ry iy))
1222 (dn (* iy (+ 1 (* r r)))))
1223 (complex (/ (+ (* rx r) ix) dn)
1224 (/ (- (* ix r) rx) dn))))))
1225 ;; Multiply a complex by a real or vice versa.
1226 (deftransform * ((w z) ((complex ,type) real) *)
1227 '(complex (* (realpart w) z) (* (imagpart w) z)))
1228 (deftransform * ((z w) (real (complex ,type)) *)
1229 '(complex (* (realpart w) z) (* (imagpart w) z)))
1230 ;; Divide a complex by a real.
1231 (deftransform / ((w z) ((complex ,type) real) *)
1232 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1233 ;; conjugate of complex number
1234 (deftransform conjugate ((z) ((complex ,type)) *)
1235 '(complex (realpart z) (- (imagpart z))))
1237 (deftransform cis ((z) ((,type)) *)
1238 '(complex (cos z) (sin z)))
1240 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1241 '(and (= (realpart w) (realpart z))
1242 (= (imagpart w) (imagpart z))))
1243 (deftransform = ((w z) ((complex ,type) real) *)
1244 '(and (= (realpart w) z) (zerop (imagpart w))))
1245 (deftransform = ((w z) (real (complex ,type)) *)
1246 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1249 (frob double-float))
1251 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1252 ;;; produce a minimal range for the result; the result is the widest
1253 ;;; possible answer. This gets around the problem of doing range
1254 ;;; reduction correctly but still provides useful results when the
1255 ;;; inputs are union types.
1256 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1258 (defun trig-derive-type-aux (arg domain fun
1259 &optional def-lo def-hi (increasingp t))
1262 (cond ((eq (numeric-type-complexp arg) :complex)
1263 (make-numeric-type :class (numeric-type-class arg)
1264 :format (numeric-type-format arg)
1265 :complexp :complex))
1266 ((numeric-type-real-p arg)
1267 (let* ((format (case (numeric-type-class arg)
1268 ((integer rational) 'single-float)
1269 (t (numeric-type-format arg))))
1270 (bound-type (or format 'float)))
1271 ;; If the argument is a subset of the "principal" domain
1272 ;; of the function, we can compute the bounds because
1273 ;; the function is monotonic. We can't do this in
1274 ;; general for these periodic functions because we can't
1275 ;; (and don't want to) do the argument reduction in
1276 ;; exactly the same way as the functions themselves do
1278 (if (csubtypep arg domain)
1279 (let ((res-lo (bound-func fun (numeric-type-low arg)))
1280 (res-hi (bound-func fun (numeric-type-high arg))))
1282 (rotatef res-lo res-hi))
1286 :low (coerce-numeric-bound res-lo bound-type)
1287 :high (coerce-numeric-bound res-hi bound-type)))
1291 :low (and def-lo (coerce def-lo bound-type))
1292 :high (and def-hi (coerce def-hi bound-type))))))
1294 (float-or-complex-float-type arg def-lo def-hi))))))
1296 (defoptimizer (sin derive-type) ((num))
1297 (one-arg-derive-type
1300 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1301 (trig-derive-type-aux
1303 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1308 (defoptimizer (cos derive-type) ((num))
1309 (one-arg-derive-type
1312 ;; Derive the bounds if the arg is in [0, pi].
1313 (trig-derive-type-aux arg
1314 (specifier-type `(float 0d0 ,pi))
1320 (defoptimizer (tan derive-type) ((num))
1321 (one-arg-derive-type
1324 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1325 (trig-derive-type-aux arg
1326 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1331 (defoptimizer (conjugate derive-type) ((num))
1332 (one-arg-derive-type num
1334 (flet ((most-negative-bound (l h)
1336 (if (< (type-bound-number l) (- (type-bound-number h)))
1338 (set-bound (- (type-bound-number h)) (consp h)))))
1339 (most-positive-bound (l h)
1341 (if (> (type-bound-number h) (- (type-bound-number l)))
1343 (set-bound (- (type-bound-number l)) (consp l))))))
1344 (if (numeric-type-real-p arg)
1346 (let ((low (numeric-type-low arg))
1347 (high (numeric-type-high arg)))
1348 (let ((new-low (most-negative-bound low high))
1349 (new-high (most-positive-bound low high)))
1350 (modified-numeric-type arg :low new-low :high new-high))))))
1353 (defoptimizer (cis derive-type) ((num))
1354 (one-arg-derive-type num
1356 (sb!c::specifier-type
1357 `(complex ,(or (numeric-type-format arg) 'float))))
1362 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1364 (macrolet ((define-frobs (fun ufun)
1366 (defknown ,ufun (real) integer (movable foldable flushable))
1367 (deftransform ,fun ((x &optional by)
1369 (constant-arg (member 1))))
1370 '(let ((res (,ufun x)))
1371 (values res (- x res)))))))
1372 (define-frobs truncate %unary-truncate)
1373 (define-frobs round %unary-round))
1375 ;;; Convert (TRUNCATE x y) to the obvious implementation. We only want
1376 ;;; this when under certain conditions and let the generic TRUNCATE
1377 ;;; handle the rest. (Note: if Y = 1, the divide and multiply by Y
1378 ;;; should be removed by other DEFTRANSFORMs.)
1379 (deftransform truncate ((x &optional y)
1380 (float &optional (or float integer)))
1381 (let ((defaulted-y (if y 'y 1)))
1382 `(let ((res (%unary-truncate (/ x ,defaulted-y))))
1383 (values res (- x (* ,defaulted-y res))))))
1385 (deftransform floor ((number &optional divisor)
1386 (float &optional (or integer float)))
1387 (let ((defaulted-divisor (if divisor 'divisor 1)))
1388 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1389 (if (and (not (zerop rem))
1390 (if (minusp ,defaulted-divisor)
1393 (values (1- tru) (+ rem ,defaulted-divisor))
1394 (values tru rem)))))
1396 (deftransform ceiling ((number &optional divisor)
1397 (float &optional (or integer float)))
1398 (let ((defaulted-divisor (if divisor 'divisor 1)))
1399 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1400 (if (and (not (zerop rem))
1401 (if (minusp ,defaulted-divisor)
1404 (values (1+ tru) (- rem ,defaulted-divisor))
1405 (values tru rem)))))
1407 (defknown %unary-ftruncate (real) float (movable foldable flushable))
1408 (defknown %unary-ftruncate/single (single-float) single-float
1409 (movable foldable flushable))
1410 (defknown %unary-ftruncate/double (double-float) double-float
1411 (movable foldable flushable))
1413 (defun %unary-ftruncate/single (x)
1414 (declare (type single-float x))
1415 (declare (optimize speed (safety 0)))
1416 (let* ((bits (single-float-bits x))
1417 (exp (ldb sb!vm:single-float-exponent-byte bits))
1418 (biased (the single-float-exponent
1419 (- exp sb!vm:single-float-bias))))
1420 (declare (type (signed-byte 32) bits))
1422 ((= exp sb!vm:single-float-normal-exponent-max) x)
1423 ((<= biased 0) (* x 0f0))
1424 ((>= biased (float-digits x)) x)
1426 (let ((frac-bits (- (float-digits x) biased)))
1427 (setf bits (logandc2 bits (- (ash 1 frac-bits) 1)))
1428 (make-single-float bits))))))
1430 (defun %unary-ftruncate/double (x)
1431 (declare (type double-float x))
1432 (declare (optimize speed (safety 0)))
1433 (let* ((high (double-float-high-bits x))
1434 (low (double-float-low-bits x))
1435 (exp (ldb sb!vm:double-float-exponent-byte high))
1436 (biased (the double-float-exponent
1437 (- exp sb!vm:double-float-bias))))
1438 (declare (type (signed-byte 32) high)
1439 (type (unsigned-byte 32) low))
1441 ((= exp sb!vm:double-float-normal-exponent-max) x)
1442 ((<= biased 0) (* x 0d0))
1443 ((>= biased (float-digits x)) x)
1445 (let ((frac-bits (- (float-digits x) biased)))
1446 (cond ((< frac-bits 32)
1447 (setf low (logandc2 low (- (ash 1 frac-bits) 1))))
1450 (setf high (logandc2 high (- (ash 1 (- frac-bits 32)) 1)))))
1451 (make-double-float high low))))))
1454 ((def (float-type fun)
1455 `(deftransform %unary-ftruncate ((x) (,float-type))
1457 (def single-float %unary-ftruncate/single)
1458 (def double-float %unary-ftruncate/double))