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 flushable))
19 (defknown %double-float (real) double-float (movable foldable flushable))
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))
49 (defknown make-single-float ((signed-byte 32)) single-float
50 (movable foldable flushable))
52 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
53 (movable foldable flushable))
55 (defknown single-float-bits (single-float) (signed-byte 32)
56 (movable foldable flushable))
58 (defknown double-float-high-bits (double-float) (signed-byte 32)
59 (movable foldable flushable))
61 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
62 (movable foldable flushable))
64 (deftransform float-sign ((float &optional float2)
65 (single-float &optional single-float) *)
67 (let ((temp (gensym)))
68 `(let ((,temp (abs float2)))
69 (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
70 '(if (minusp (single-float-bits float)) -1f0 1f0)))
72 (deftransform float-sign ((float &optional float2)
73 (double-float &optional double-float) *)
75 (let ((temp (gensym)))
76 `(let ((,temp (abs float2)))
77 (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
78 '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
80 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
82 (defknown decode-single-float (single-float)
83 (values single-float single-float-exponent (single-float -1f0 1f0))
84 (movable foldable flushable))
86 (defknown decode-double-float (double-float)
87 (values double-float double-float-exponent (double-float -1d0 1d0))
88 (movable foldable flushable))
90 (defknown integer-decode-single-float (single-float)
91 (values single-float-significand single-float-int-exponent (integer -1 1))
92 (movable foldable flushable))
94 (defknown integer-decode-double-float (double-float)
95 (values double-float-significand double-float-int-exponent (integer -1 1))
96 (movable foldable flushable))
98 (defknown scale-single-float (single-float integer) single-float
99 (movable foldable flushable))
101 (defknown scale-double-float (double-float integer) double-float
102 (movable foldable flushable))
104 (deftransform decode-float ((x) (single-float) *)
105 '(decode-single-float x))
107 (deftransform decode-float ((x) (double-float) *)
108 '(decode-double-float x))
110 (deftransform integer-decode-float ((x) (single-float) *)
111 '(integer-decode-single-float x))
113 (deftransform integer-decode-float ((x) (double-float) *)
114 '(integer-decode-double-float x))
116 (deftransform scale-float ((f ex) (single-float *) *)
117 (if (and #!+x86 t #!-x86 nil
118 (csubtypep (lvar-type ex)
119 (specifier-type '(signed-byte 32))))
120 '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
121 '(scale-single-float f ex)))
123 (deftransform scale-float ((f ex) (double-float *) *)
124 (if (and #!+x86 t #!-x86 nil
125 (csubtypep (lvar-type ex)
126 (specifier-type '(signed-byte 32))))
128 '(scale-double-float f ex)))
130 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
132 ;;; SBCL's own implementation of floating point supports floating
133 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
134 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
135 ;;; floating point support. Thus, we have to avoid running it on the
136 ;;; cross-compilation host, since we're not guaranteed that the
137 ;;; cross-compilation host will support floating point infinities.
139 ;;; If we wanted to live dangerously, we could conditionalize the code
140 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
141 ;;; host happened to be SBCL, we'd be able to run the infinity-using
143 ;;; * SBCL itself gets built with more complete optimization.
145 ;;; * You get a different SBCL depending on what your cross-compilation
147 ;;; So far the pros and cons seem seem to be mostly academic, since
148 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
149 ;;; actually important in compiling SBCL itself. If this changes, then
150 ;;; we have to decide:
151 ;;; * Go for simplicity, leaving things as they are.
152 ;;; * Go for performance at the expense of conceptual clarity,
153 ;;; using #+(OR SBCL SB-XC) and otherwise leaving the build
155 ;;; * Go for performance at the expense of build time, using
156 ;;; #+(OR SBCL SB-XC) and also making SBCL do not just
157 ;;; make-host-1.sh and make-host-2.sh, but a third step
158 ;;; make-host-3.sh where it builds itself under itself. (Such a
159 ;;; 3-step build process could also help with other things, e.g.
160 ;;; using specialized arrays to represent debug information.)
161 ;;; * Rewrite the code so that it doesn't depend on unportable
162 ;;; floating point infinities.
164 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
165 ;;; are computed for the result, if possible.
166 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
169 (defun scale-float-derive-type-aux (f ex same-arg)
170 (declare (ignore same-arg))
171 (flet ((scale-bound (x n)
172 ;; We need to be a bit careful here and catch any overflows
173 ;; that might occur. We can ignore underflows which become
177 (scale-float (type-bound-number x) n)
178 (floating-point-overflow ()
181 (when (and (numeric-type-p f) (numeric-type-p ex))
182 (let ((f-lo (numeric-type-low f))
183 (f-hi (numeric-type-high f))
184 (ex-lo (numeric-type-low ex))
185 (ex-hi (numeric-type-high ex))
189 (if (< (float-sign (type-bound-number f-hi)) 0.0)
191 (setf new-hi (scale-bound f-hi ex-lo)))
193 (setf new-hi (scale-bound f-hi ex-hi)))))
195 (if (< (float-sign (type-bound-number f-lo)) 0.0)
197 (setf new-lo (scale-bound f-lo ex-hi)))
199 (setf new-lo (scale-bound f-lo ex-lo)))))
200 (make-numeric-type :class (numeric-type-class f)
201 :format (numeric-type-format f)
205 (defoptimizer (scale-single-float derive-type) ((f ex))
206 (two-arg-derive-type f ex #'scale-float-derive-type-aux
207 #'scale-single-float t))
208 (defoptimizer (scale-double-float derive-type) ((f ex))
209 (two-arg-derive-type f ex #'scale-float-derive-type-aux
210 #'scale-double-float t))
212 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
213 ;;; FLOAT function return the correct ranges if the input has some
214 ;;; defined range. Quite useful if we want to convert some type of
215 ;;; bounded integer into a float.
217 ((frob (fun type most-negative most-positive)
218 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
220 (defun ,aux-name (num)
221 ;; When converting a number to a float, the limits are
223 (let* ((lo (bound-func (lambda (x)
224 (if (< x ,most-negative)
227 (numeric-type-low num)))
228 (hi (bound-func (lambda (x)
229 (if (< ,most-positive x )
232 (numeric-type-high num))))
233 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
235 (defoptimizer (,fun derive-type) ((num))
236 (one-arg-derive-type num #',aux-name #',fun))))))
237 (frob %single-float single-float
238 most-negative-single-float most-positive-single-float)
239 (frob %double-float double-float
240 most-negative-double-float most-positive-double-float))
245 ;;; Do some stuff to recognize when the loser is doing mixed float and
246 ;;; rational arithmetic, or different float types, and fix it up. If
247 ;;; we don't, he won't even get so much as an efficiency note.
248 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
249 `(,(lvar-fun-name (basic-combination-fun node))
251 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
252 `(,(lvar-fun-name (basic-combination-fun node))
255 (dolist (x '(+ * / -))
256 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
257 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
259 (dolist (x '(= < > + * / -))
260 (%deftransform x '(function (single-float double-float) *)
261 #'float-contagion-arg1)
262 (%deftransform x '(function (double-float single-float) *)
263 #'float-contagion-arg2))
265 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
266 ;;; general float rational args to comparison, since Common Lisp
267 ;;; semantics says we are supposed to compare as rationals, but we can
268 ;;; do it for any rational that has a precise representation as a
269 ;;; float (such as 0).
270 (macrolet ((frob (op)
271 `(deftransform ,op ((x y) (float rational) *)
272 "open-code FLOAT to RATIONAL comparison"
273 (unless (constant-lvar-p y)
274 (give-up-ir1-transform
275 "The RATIONAL value isn't known at compile time."))
276 (let ((val (lvar-value y)))
277 (unless (eql (rational (float val)) val)
278 (give-up-ir1-transform
279 "~S doesn't have a precise float representation."
281 `(,',op x (float y x)))))
286 ;;;; irrational derive-type methods
288 ;;; Derive the result to be float for argument types in the
289 ;;; appropriate domain.
290 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
291 (dolist (stuff '((asin (real -1.0 1.0))
292 (acos (real -1.0 1.0))
294 (atanh (real -1.0 1.0))
296 (destructuring-bind (name type) stuff
297 (let ((type (specifier-type type)))
298 (setf (fun-info-derive-type (fun-info-or-lose name))
300 (declare (type combination call))
301 (when (csubtypep (lvar-type
302 (first (combination-args call)))
304 (specifier-type 'float)))))))
306 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
307 (defoptimizer (log derive-type) ((x &optional y))
308 (when (and (csubtypep (lvar-type x)
309 (specifier-type '(real 0.0)))
311 (csubtypep (lvar-type y)
312 (specifier-type '(real 0.0)))))
313 (specifier-type 'float)))
315 ;;;; irrational transforms
317 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
318 (double-float) double-float
319 (movable foldable flushable))
321 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
322 (double-float) (double-float -1.0d0 1.0d0)
323 (movable foldable flushable))
325 (defknown (%asin %atan)
327 (double-float #.(coerce (- (/ pi 2)) 'double-float)
328 #.(coerce (/ pi 2) 'double-float))
329 (movable foldable flushable))
332 (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
333 (movable foldable flushable))
336 (double-float) (double-float 1.0d0)
337 (movable foldable flushable))
339 (defknown (%acosh %exp %sqrt)
340 (double-float) (double-float 0.0d0)
341 (movable foldable flushable))
344 (double-float) (double-float -1d0)
345 (movable foldable flushable))
348 (double-float double-float) (double-float 0d0)
349 (movable foldable flushable))
352 (double-float double-float) double-float
353 (movable foldable flushable))
356 (double-float double-float)
357 (double-float #.(coerce (- pi) 'double-float)
358 #.(coerce pi 'double-float))
359 (movable foldable flushable))
362 (double-float double-float) double-float
363 (movable foldable flushable))
366 (double-float (signed-byte 32)) double-float
367 (movable foldable flushable))
370 (double-float) double-float
371 (movable foldable flushable))
373 (macrolet ((def (name prim rtype)
375 (deftransform ,name ((x) (single-float) ,rtype)
376 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
377 (deftransform ,name ((x) (double-float) ,rtype)
381 (def sqrt %sqrt float)
382 (def asin %asin float)
383 (def acos %acos float)
389 (def acosh %acosh float)
390 (def atanh %atanh float))
392 ;;; The argument range is limited on the x86 FP trig. functions. A
393 ;;; post-test can detect a failure (and load a suitable result), but
394 ;;; this test is avoided if possible.
395 (macrolet ((def (name prim prim-quick)
396 (declare (ignorable prim-quick))
398 (deftransform ,name ((x) (single-float) *)
399 #!+x86 (cond ((csubtypep (lvar-type x)
400 (specifier-type '(single-float
401 (#.(- (expt 2f0 64)))
403 `(coerce (,',prim-quick (coerce x 'double-float))
407 "unable to avoid inline argument range check~@
408 because the argument range (~S) was not within 2^64"
409 (type-specifier (lvar-type x)))
410 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
411 #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
412 (deftransform ,name ((x) (double-float) *)
413 #!+x86 (cond ((csubtypep (lvar-type x)
414 (specifier-type '(double-float
415 (#.(- (expt 2d0 64)))
420 "unable to avoid inline argument range check~@
421 because the argument range (~S) was not within 2^64"
422 (type-specifier (lvar-type x)))
424 #!-x86 `(,',prim x)))))
425 (def sin %sin %sin-quick)
426 (def cos %cos %cos-quick)
427 (def tan %tan %tan-quick))
429 (deftransform atan ((x y) (single-float single-float) *)
430 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
432 (deftransform atan ((x y) (double-float double-float) *)
435 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
436 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
438 (deftransform expt ((x y) ((double-float 0d0) double-float) *)
440 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
441 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
443 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) *)
444 `(%pow x (coerce y 'double-float)))
446 ;;; ANSI says log with base zero returns zero.
447 (deftransform log ((x y) (float float) float)
448 '(if (zerop y) y (/ (log x) (log y))))
450 ;;; Handle some simple transformations.
452 (deftransform abs ((x) ((complex double-float)) double-float)
453 '(%hypot (realpart x) (imagpart x)))
455 (deftransform abs ((x) ((complex single-float)) single-float)
456 '(coerce (%hypot (coerce (realpart x) 'double-float)
457 (coerce (imagpart x) 'double-float))
460 (deftransform phase ((x) ((complex double-float)) double-float)
461 '(%atan2 (imagpart x) (realpart x)))
463 (deftransform phase ((x) ((complex single-float)) single-float)
464 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
465 (coerce (realpart x) 'double-float))
468 (deftransform phase ((x) ((float)) float)
469 '(if (minusp (float-sign x))
473 ;;; The number is of type REAL.
474 (defun numeric-type-real-p (type)
475 (and (numeric-type-p type)
476 (eq (numeric-type-complexp type) :real)))
478 ;;; Coerce a numeric type bound to the given type while handling
479 ;;; exclusive bounds.
480 (defun coerce-numeric-bound (bound type)
483 (list (coerce (car bound) type))
484 (coerce bound type))))
486 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
489 ;;;; optimizers for elementary functions
491 ;;;; These optimizers compute the output range of the elementary
492 ;;;; function, based on the domain of the input.
494 ;;; Generate a specifier for a complex type specialized to the same
495 ;;; type as the argument.
496 (defun complex-float-type (arg)
497 (declare (type numeric-type arg))
498 (let* ((format (case (numeric-type-class arg)
499 ((integer rational) 'single-float)
500 (t (numeric-type-format arg))))
501 (float-type (or format 'float)))
502 (specifier-type `(complex ,float-type))))
504 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
505 ;;; should be the right kind of float. Allow bounds for the float
507 (defun float-or-complex-float-type (arg &optional lo hi)
508 (declare (type numeric-type arg))
509 (let* ((format (case (numeric-type-class arg)
510 ((integer rational) 'single-float)
511 (t (numeric-type-format arg))))
512 (float-type (or format 'float))
513 (lo (coerce-numeric-bound lo float-type))
514 (hi (coerce-numeric-bound hi float-type)))
515 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
516 (complex ,float-type)))))
520 (eval-when (:compile-toplevel :execute)
521 ;; So the problem with this hack is that it's actually broken. If
522 ;; the host does not have long floats, then setting *R-D-F-F* to
523 ;; LONG-FLOAT doesn't actually buy us anything. FIXME.
524 (setf *read-default-float-format*
525 #!+long-float 'long-float #!-long-float 'double-float))
526 ;;; Test whether the numeric-type ARG is within in domain specified by
527 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
529 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
530 (defun domain-subtypep (arg domain-low domain-high)
531 (declare (type numeric-type arg)
532 (type (or real null) domain-low domain-high))
533 (let* ((arg-lo (numeric-type-low arg))
534 (arg-lo-val (type-bound-number arg-lo))
535 (arg-hi (numeric-type-high arg))
536 (arg-hi-val (type-bound-number arg-hi)))
537 ;; Check that the ARG bounds are correctly canonicalized.
538 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
539 (minusp (float-sign arg-lo-val)))
540 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-lo)
541 (setq arg-lo 0e0 arg-lo-val arg-lo))
542 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
543 (plusp (float-sign arg-hi-val)))
544 (compiler-notify "float zero bound ~S not correctly canonicalized?" arg-hi)
545 (setq arg-hi (ecase *read-default-float-format*
546 (double-float (load-time-value (make-unportable-float :double-float-negative-zero)))
548 (long-float (load-time-value (make-unportable-float :long-float-negative-zero))))
550 (flet ((fp-neg-zero-p (f) ; Is F -0.0?
551 (and (floatp f) (zerop f) (minusp (float-sign f))))
552 (fp-pos-zero-p (f) ; Is F +0.0?
553 (and (floatp f) (zerop f) (plusp (float-sign f)))))
554 (and (or (null domain-low)
555 (and arg-lo (>= arg-lo-val domain-low)
556 (not (and (fp-pos-zero-p domain-low)
557 (fp-neg-zero-p arg-lo)))))
558 (or (null domain-high)
559 (and arg-hi (<= arg-hi-val domain-high)
560 (not (and (fp-neg-zero-p domain-high)
561 (fp-pos-zero-p arg-hi)))))))))
562 (eval-when (:compile-toplevel :execute)
563 (setf *read-default-float-format* 'single-float))
565 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
568 ;;; Handle monotonic functions of a single variable whose domain is
569 ;;; possibly part of the real line. ARG is the variable, FCN is the
570 ;;; function, and DOMAIN is a specifier that gives the (real) domain
571 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
572 ;;; bounds directly. Otherwise, we compute the bounds for the
573 ;;; intersection between ARG and DOMAIN, and then append a complex
574 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
576 ;;; Negative and positive zero are considered distinct within
577 ;;; DOMAIN-LOW and DOMAIN-HIGH.
579 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
580 ;;; can't compute the bounds using FCN.
581 (defun elfun-derive-type-simple (arg fcn domain-low domain-high
582 default-low default-high
583 &optional (increasingp t))
584 (declare (type (or null real) domain-low domain-high))
587 (cond ((eq (numeric-type-complexp arg) :complex)
588 (complex-float-type arg))
589 ((numeric-type-real-p arg)
590 ;; The argument is real, so let's find the intersection
591 ;; between the argument and the domain of the function.
592 ;; We compute the bounds on the intersection, and for
593 ;; everything else, we return a complex number of the
595 (multiple-value-bind (intersection difference)
596 (interval-intersection/difference (numeric-type->interval arg)
602 ;; Process the intersection.
603 (let* ((low (interval-low intersection))
604 (high (interval-high intersection))
605 (res-lo (or (bound-func fcn (if increasingp low high))
607 (res-hi (or (bound-func fcn (if increasingp high low))
609 (format (case (numeric-type-class arg)
610 ((integer rational) 'single-float)
611 (t (numeric-type-format arg))))
612 (bound-type (or format 'float))
617 :low (coerce-numeric-bound res-lo bound-type)
618 :high (coerce-numeric-bound res-hi bound-type))))
619 ;; If the ARG is a subset of the domain, we don't
620 ;; have to worry about the difference, because that
622 (if (or (null difference)
623 ;; Check whether the arg is within the domain.
624 (domain-subtypep arg domain-low domain-high))
627 (specifier-type `(complex ,bound-type))))))
629 ;; No intersection so the result must be purely complex.
630 (complex-float-type arg)))))
632 (float-or-complex-float-type arg default-low default-high))))))
635 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
636 &key (increasingp t))
637 (let ((num (gensym)))
638 `(defoptimizer (,name derive-type) ((,num))
642 (elfun-derive-type-simple arg #',name
643 ,domain-low ,domain-high
644 ,def-low-bnd ,def-high-bnd
647 ;; These functions are easy because they are defined for the whole
649 (frob exp nil nil 0 nil)
650 (frob sinh nil nil nil nil)
651 (frob tanh nil nil -1 1)
652 (frob asinh nil nil nil nil)
654 ;; These functions are only defined for part of the real line. The
655 ;; condition selects the desired part of the line.
656 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
657 ;; Acos is monotonic decreasing, so we need to swap the function
658 ;; values at the lower and upper bounds of the input domain.
659 (frob acos -1d0 1d0 0 pi :increasingp nil)
660 (frob acosh 1d0 nil nil nil)
661 (frob atanh -1d0 1d0 -1 1)
662 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
664 (frob sqrt (load-time-value (make-unportable-float :double-float-negative-zero)) nil 0 nil))
666 ;;; Compute bounds for (expt x y). This should be easy since (expt x
667 ;;; y) = (exp (* y (log x))). However, computations done this way
668 ;;; have too much roundoff. Thus we have to do it the hard way.
669 (defun safe-expt (x y)
671 (when (< (abs y) 10000)
676 ;;; Handle the case when x >= 1.
677 (defun interval-expt-> (x y)
678 (case (sb!c::interval-range-info y 0d0)
680 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
681 ;; obviously non-negative. We just have to be careful for
682 ;; infinite bounds (given by nil).
683 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
684 (type-bound-number (sb!c::interval-low y))))
685 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
686 (type-bound-number (sb!c::interval-high y)))))
687 (list (sb!c::make-interval :low (or lo 1) :high hi))))
689 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
690 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
692 (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
693 (type-bound-number (sb!c::interval-low y))))
694 (hi (safe-expt (type-bound-number (sb!c::interval-low x))
695 (type-bound-number (sb!c::interval-high y)))))
696 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
698 ;; Split the interval in half.
699 (destructuring-bind (y- y+)
700 (sb!c::interval-split 0 y t)
701 (list (interval-expt-> x y-)
702 (interval-expt-> x y+))))))
704 ;;; Handle the case when x <= 1
705 (defun interval-expt-< (x y)
706 (case (sb!c::interval-range-info x 0d0)
708 ;; The case of 0 <= x <= 1 is easy
709 (case (sb!c::interval-range-info y)
711 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
712 ;; obviously [0, 1]. We just have to be careful for infinite bounds
714 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
715 (type-bound-number (sb!c::interval-high y))))
716 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
717 (type-bound-number (sb!c::interval-low y)))))
718 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
720 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
721 ;; obviously [1, inf].
722 (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
723 (type-bound-number (sb!c::interval-low y))))
724 (lo (safe-expt (type-bound-number (sb!c::interval-high x))
725 (type-bound-number (sb!c::interval-high y)))))
726 (list (sb!c::make-interval :low (or lo 1) :high hi))))
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 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
735 ;; The calling function must insure this! For now we'll just
736 ;; return the appropriate unbounded float type.
737 (list (sb!c::make-interval :low nil :high nil)))
739 (destructuring-bind (neg pos)
740 (interval-split 0 x t t)
741 (list (interval-expt-< neg y)
742 (interval-expt-< pos y))))))
744 ;;; Compute bounds for (expt x y).
745 (defun interval-expt (x y)
746 (case (interval-range-info x 1)
749 (interval-expt-> x y))
752 (interval-expt-< x y))
754 (destructuring-bind (left right)
755 (interval-split 1 x t t)
756 (list (interval-expt left y)
757 (interval-expt right y))))))
759 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
760 (declare (ignore x-int))
761 ;; Figure out what the return type should be, given the argument
762 ;; types and bounds and the result type and bounds.
763 (cond ((csubtypep x-type (specifier-type 'integer))
764 ;; an integer to some power
765 (case (numeric-type-class y-type)
767 ;; Positive integer to an integer power is either an
768 ;; integer or a rational.
769 (let ((lo (or (interval-low bnd) '*))
770 (hi (or (interval-high bnd) '*)))
771 (if (and (interval-low y-int)
772 (>= (type-bound-number (interval-low y-int)) 0))
773 (specifier-type `(integer ,lo ,hi))
774 (specifier-type `(rational ,lo ,hi)))))
776 ;; Positive integer to rational power is either a rational
777 ;; or a single-float.
778 (let* ((lo (interval-low bnd))
779 (hi (interval-high bnd))
781 (floor (type-bound-number lo))
784 (ceiling (type-bound-number hi))
787 (bound-func #'float lo)
790 (bound-func #'float hi)
792 (specifier-type `(or (rational ,int-lo ,int-hi)
793 (single-float ,f-lo, f-hi)))))
795 ;; A positive integer to a float power is a float.
796 (modified-numeric-type y-type
797 :low (interval-low bnd)
798 :high (interval-high bnd)))
800 ;; A positive integer to a number is a number (for now).
801 (specifier-type 'number))))
802 ((csubtypep x-type (specifier-type 'rational))
803 ;; a rational to some power
804 (case (numeric-type-class y-type)
806 ;; A positive rational to an integer power is always a rational.
807 (specifier-type `(rational ,(or (interval-low bnd) '*)
808 ,(or (interval-high bnd) '*))))
810 ;; A positive rational to rational power is either a rational
811 ;; or a single-float.
812 (let* ((lo (interval-low bnd))
813 (hi (interval-high bnd))
815 (floor (type-bound-number lo))
818 (ceiling (type-bound-number hi))
821 (bound-func #'float lo)
824 (bound-func #'float hi)
826 (specifier-type `(or (rational ,int-lo ,int-hi)
827 (single-float ,f-lo, f-hi)))))
829 ;; A positive rational to a float power is a float.
830 (modified-numeric-type y-type
831 :low (interval-low bnd)
832 :high (interval-high bnd)))
834 ;; A positive rational to a number is a number (for now).
835 (specifier-type 'number))))
836 ((csubtypep x-type (specifier-type 'float))
837 ;; a float to some power
838 (case (numeric-type-class y-type)
839 ((or integer rational)
840 ;; A positive float to an integer or rational power is
844 :format (numeric-type-format x-type)
845 :low (interval-low bnd)
846 :high (interval-high bnd)))
848 ;; A positive float to a float power is a float of the
852 :format (float-format-max (numeric-type-format x-type)
853 (numeric-type-format y-type))
854 :low (interval-low bnd)
855 :high (interval-high bnd)))
857 ;; A positive float to a number is a number (for now)
858 (specifier-type 'number))))
860 ;; A number to some power is a number.
861 (specifier-type 'number))))
863 (defun merged-interval-expt (x y)
864 (let* ((x-int (numeric-type->interval x))
865 (y-int (numeric-type->interval y)))
866 (mapcar (lambda (type)
867 (fixup-interval-expt type x-int y-int x y))
868 (flatten-list (interval-expt x-int y-int)))))
870 (defun expt-derive-type-aux (x y same-arg)
871 (declare (ignore same-arg))
872 (cond ((or (not (numeric-type-real-p x))
873 (not (numeric-type-real-p y)))
874 ;; Use numeric contagion if either is not real.
875 (numeric-contagion x y))
876 ((csubtypep y (specifier-type 'integer))
877 ;; A real raised to an integer power is well-defined.
878 (merged-interval-expt x y))
879 ;; A real raised to a non-integral power can be a float or a
881 ((or (csubtypep x (specifier-type '(rational 0)))
882 (csubtypep x (specifier-type '(float (0d0)))))
883 ;; But a positive real to any power is well-defined.
884 (merged-interval-expt x y))
885 ((and (csubtypep x (specifier-type 'rational))
886 (csubtypep x (specifier-type 'rational)))
887 ;; A rational to the power of a rational could be a rational
888 ;; or a possibly-complex single float
889 (specifier-type '(or rational single-float (complex single-float))))
891 ;; a real to some power. The result could be a real or a
893 (float-or-complex-float-type (numeric-contagion x y)))))
895 (defoptimizer (expt derive-type) ((x y))
896 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
898 ;;; Note we must assume that a type including 0.0 may also include
899 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
900 (defun log-derive-type-aux-1 (x)
901 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
903 (defun log-derive-type-aux-2 (x y same-arg)
904 (let ((log-x (log-derive-type-aux-1 x))
905 (log-y (log-derive-type-aux-1 y))
906 (accumulated-list nil))
907 ;; LOG-X or LOG-Y might be union types. We need to run through
908 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
909 (dolist (x-type (prepare-arg-for-derive-type log-x))
910 (dolist (y-type (prepare-arg-for-derive-type log-y))
911 (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
912 (apply #'type-union (flatten-list accumulated-list))))
914 (defoptimizer (log derive-type) ((x &optional y))
916 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
917 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
919 (defun atan-derive-type-aux-1 (y)
920 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
922 (defun atan-derive-type-aux-2 (y x same-arg)
923 (declare (ignore same-arg))
924 ;; The hard case with two args. We just return the max bounds.
925 (let ((result-type (numeric-contagion y x)))
926 (cond ((and (numeric-type-real-p x)
927 (numeric-type-real-p y))
928 (let* (;; FIXME: This expression for FORMAT seems to
929 ;; appear multiple times, and should be factored out.
930 (format (case (numeric-type-class result-type)
931 ((integer rational) 'single-float)
932 (t (numeric-type-format result-type))))
933 (bound-format (or format 'float)))
934 (make-numeric-type :class 'float
937 :low (coerce (- pi) bound-format)
938 :high (coerce pi bound-format))))
940 ;; The result is a float or a complex number
941 (float-or-complex-float-type result-type)))))
943 (defoptimizer (atan derive-type) ((y &optional x))
945 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
946 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
948 (defun cosh-derive-type-aux (x)
949 ;; We note that cosh x = cosh |x| for all real x.
950 (elfun-derive-type-simple
951 (if (numeric-type-real-p x)
952 (abs-derive-type-aux x)
954 #'cosh nil nil 0 nil))
956 (defoptimizer (cosh derive-type) ((num))
957 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
959 (defun phase-derive-type-aux (arg)
960 (let* ((format (case (numeric-type-class arg)
961 ((integer rational) 'single-float)
962 (t (numeric-type-format arg))))
963 (bound-type (or format 'float)))
964 (cond ((numeric-type-real-p arg)
965 (case (interval-range-info (numeric-type->interval arg) 0.0)
967 ;; The number is positive, so the phase is 0.
968 (make-numeric-type :class 'float
971 :low (coerce 0 bound-type)
972 :high (coerce 0 bound-type)))
974 ;; The number is always negative, so the phase is pi.
975 (make-numeric-type :class 'float
978 :low (coerce pi bound-type)
979 :high (coerce pi bound-type)))
981 ;; We can't tell. The result is 0 or pi. Use a union
984 (make-numeric-type :class 'float
987 :low (coerce 0 bound-type)
988 :high (coerce 0 bound-type))
989 (make-numeric-type :class 'float
992 :low (coerce pi bound-type)
993 :high (coerce pi bound-type))))))
995 ;; We have a complex number. The answer is the range -pi
996 ;; to pi. (-pi is included because we have -0.)
997 (make-numeric-type :class 'float
1000 :low (coerce (- pi) bound-type)
1001 :high (coerce pi bound-type))))))
1003 (defoptimizer (phase derive-type) ((num))
1004 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1008 (deftransform realpart ((x) ((complex rational)) *)
1009 '(sb!kernel:%realpart x))
1010 (deftransform imagpart ((x) ((complex rational)) *)
1011 '(sb!kernel:%imagpart x))
1013 ;;; Make REALPART and IMAGPART return the appropriate types. This
1014 ;;; should help a lot in optimized code.
1015 (defun realpart-derive-type-aux (type)
1016 (let ((class (numeric-type-class type))
1017 (format (numeric-type-format type)))
1018 (cond ((numeric-type-real-p type)
1019 ;; The realpart of a real has the same type and range as
1021 (make-numeric-type :class class
1024 :low (numeric-type-low type)
1025 :high (numeric-type-high type)))
1027 ;; We have a complex number. The result has the same type
1028 ;; as the real part, except that it's real, not complex,
1030 (make-numeric-type :class class
1033 :low (numeric-type-low type)
1034 :high (numeric-type-high type))))))
1035 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1036 (defoptimizer (realpart derive-type) ((num))
1037 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1038 (defun imagpart-derive-type-aux (type)
1039 (let ((class (numeric-type-class type))
1040 (format (numeric-type-format type)))
1041 (cond ((numeric-type-real-p type)
1042 ;; The imagpart of a real has the same type as the input,
1043 ;; except that it's zero.
1044 (let ((bound-format (or format class 'real)))
1045 (make-numeric-type :class class
1048 :low (coerce 0 bound-format)
1049 :high (coerce 0 bound-format))))
1051 ;; We have a complex number. The result has the same type as
1052 ;; the imaginary part, except that it's real, not complex,
1054 (make-numeric-type :class class
1057 :low (numeric-type-low type)
1058 :high (numeric-type-high type))))))
1059 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1060 (defoptimizer (imagpart derive-type) ((num))
1061 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1063 (defun complex-derive-type-aux-1 (re-type)
1064 (if (numeric-type-p re-type)
1065 (make-numeric-type :class (numeric-type-class re-type)
1066 :format (numeric-type-format re-type)
1067 :complexp (if (csubtypep re-type
1068 (specifier-type 'rational))
1071 :low (numeric-type-low re-type)
1072 :high (numeric-type-high re-type))
1073 (specifier-type 'complex)))
1075 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1076 (declare (ignore same-arg))
1077 (if (and (numeric-type-p re-type)
1078 (numeric-type-p im-type))
1079 ;; Need to check to make sure numeric-contagion returns the
1080 ;; right type for what we want here.
1082 ;; Also, what about rational canonicalization, like (complex 5 0)
1083 ;; is 5? So, if the result must be complex, we make it so.
1084 ;; If the result might be complex, which happens only if the
1085 ;; arguments are rational, we make it a union type of (or
1086 ;; rational (complex rational)).
1087 (let* ((element-type (numeric-contagion re-type im-type))
1088 (rat-result-p (csubtypep element-type
1089 (specifier-type 'rational))))
1091 (type-union element-type
1093 `(complex ,(numeric-type-class element-type))))
1094 (make-numeric-type :class (numeric-type-class element-type)
1095 :format (numeric-type-format element-type)
1096 :complexp (if rat-result-p
1099 (specifier-type 'complex)))
1101 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1102 (defoptimizer (complex derive-type) ((re &optional im))
1104 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1105 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1107 ;;; Define some transforms for complex operations. We do this in lieu
1108 ;;; of complex operation VOPs.
1109 (macrolet ((frob (type)
1112 (deftransform %negate ((z) ((complex ,type)) *)
1113 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1114 ;; complex addition and subtraction
1115 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1116 '(complex (+ (realpart w) (realpart z))
1117 (+ (imagpart w) (imagpart z))))
1118 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1119 '(complex (- (realpart w) (realpart z))
1120 (- (imagpart w) (imagpart z))))
1121 ;; Add and subtract a complex and a real.
1122 (deftransform + ((w z) ((complex ,type) real) *)
1123 '(complex (+ (realpart w) z) (imagpart w)))
1124 (deftransform + ((z w) (real (complex ,type)) *)
1125 '(complex (+ (realpart w) z) (imagpart w)))
1126 ;; Add and subtract a real and a complex number.
1127 (deftransform - ((w z) ((complex ,type) real) *)
1128 '(complex (- (realpart w) z) (imagpart w)))
1129 (deftransform - ((z w) (real (complex ,type)) *)
1130 '(complex (- z (realpart w)) (- (imagpart w))))
1131 ;; Multiply and divide two complex numbers.
1132 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1133 '(let* ((rx (realpart x))
1137 (complex (- (* rx ry) (* ix iy))
1138 (+ (* rx iy) (* ix ry)))))
1139 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1140 '(let* ((rx (realpart x))
1144 (if (> (abs ry) (abs iy))
1145 (let* ((r (/ iy ry))
1146 (dn (* ry (+ 1 (* r r)))))
1147 (complex (/ (+ rx (* ix r)) dn)
1148 (/ (- ix (* rx r)) dn)))
1149 (let* ((r (/ ry iy))
1150 (dn (* iy (+ 1 (* r r)))))
1151 (complex (/ (+ (* rx r) ix) dn)
1152 (/ (- (* ix r) rx) dn))))))
1153 ;; Multiply a complex by a real or vice versa.
1154 (deftransform * ((w z) ((complex ,type) real) *)
1155 '(complex (* (realpart w) z) (* (imagpart w) z)))
1156 (deftransform * ((z w) (real (complex ,type)) *)
1157 '(complex (* (realpart w) z) (* (imagpart w) z)))
1158 ;; Divide a complex by a real.
1159 (deftransform / ((w z) ((complex ,type) real) *)
1160 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1161 ;; conjugate of complex number
1162 (deftransform conjugate ((z) ((complex ,type)) *)
1163 '(complex (realpart z) (- (imagpart z))))
1165 (deftransform cis ((z) ((,type)) *)
1166 '(complex (cos z) (sin z)))
1168 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1169 '(and (= (realpart w) (realpart z))
1170 (= (imagpart w) (imagpart z))))
1171 (deftransform = ((w z) ((complex ,type) real) *)
1172 '(and (= (realpart w) z) (zerop (imagpart w))))
1173 (deftransform = ((w z) (real (complex ,type)) *)
1174 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1177 (frob double-float))
1179 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1180 ;;; produce a minimal range for the result; the result is the widest
1181 ;;; possible answer. This gets around the problem of doing range
1182 ;;; reduction correctly but still provides useful results when the
1183 ;;; inputs are union types.
1184 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1186 (defun trig-derive-type-aux (arg domain fcn
1187 &optional def-lo def-hi (increasingp t))
1190 (cond ((eq (numeric-type-complexp arg) :complex)
1191 (make-numeric-type :class (numeric-type-class arg)
1192 :format (numeric-type-format arg)
1193 :complexp :complex))
1194 ((numeric-type-real-p arg)
1195 (let* ((format (case (numeric-type-class arg)
1196 ((integer rational) 'single-float)
1197 (t (numeric-type-format arg))))
1198 (bound-type (or format 'float)))
1199 ;; If the argument is a subset of the "principal" domain
1200 ;; of the function, we can compute the bounds because
1201 ;; the function is monotonic. We can't do this in
1202 ;; general for these periodic functions because we can't
1203 ;; (and don't want to) do the argument reduction in
1204 ;; exactly the same way as the functions themselves do
1206 (if (csubtypep arg domain)
1207 (let ((res-lo (bound-func fcn (numeric-type-low arg)))
1208 (res-hi (bound-func fcn (numeric-type-high arg))))
1210 (rotatef res-lo res-hi))
1214 :low (coerce-numeric-bound res-lo bound-type)
1215 :high (coerce-numeric-bound res-hi bound-type)))
1219 :low (and def-lo (coerce def-lo bound-type))
1220 :high (and def-hi (coerce def-hi bound-type))))))
1222 (float-or-complex-float-type arg def-lo def-hi))))))
1224 (defoptimizer (sin derive-type) ((num))
1225 (one-arg-derive-type
1228 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1229 (trig-derive-type-aux
1231 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1236 (defoptimizer (cos derive-type) ((num))
1237 (one-arg-derive-type
1240 ;; Derive the bounds if the arg is in [0, pi].
1241 (trig-derive-type-aux arg
1242 (specifier-type `(float 0d0 ,pi))
1248 (defoptimizer (tan derive-type) ((num))
1249 (one-arg-derive-type
1252 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1253 (trig-derive-type-aux arg
1254 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1259 (defoptimizer (conjugate derive-type) ((num))
1260 (one-arg-derive-type num
1262 (flet ((most-negative-bound (l h)
1264 (if (< (type-bound-number l) (- (type-bound-number h)))
1266 (set-bound (- (type-bound-number h)) (consp h)))))
1267 (most-positive-bound (l h)
1269 (if (> (type-bound-number h) (- (type-bound-number l)))
1271 (set-bound (- (type-bound-number l)) (consp l))))))
1272 (if (numeric-type-real-p arg)
1274 (let ((low (numeric-type-low arg))
1275 (high (numeric-type-high arg)))
1276 (let ((new-low (most-negative-bound low high))
1277 (new-high (most-positive-bound low high)))
1278 (modified-numeric-type arg :low new-low :high new-high))))))
1281 (defoptimizer (cis derive-type) ((num))
1282 (one-arg-derive-type num
1284 (sb!c::specifier-type
1285 `(complex ,(or (numeric-type-format arg) 'float))))
1290 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1292 (macrolet ((define-frobs (fun ufun)
1294 (defknown ,ufun (real) integer (movable foldable flushable))
1295 (deftransform ,fun ((x &optional by)
1297 (constant-arg (member 1))))
1298 '(let ((res (,ufun x)))
1299 (values res (- x res)))))))
1300 (define-frobs truncate %unary-truncate)
1301 (define-frobs round %unary-round))
1303 ;;; Convert (TRUNCATE x y) to the obvious implementation. We only want
1304 ;;; this when under certain conditions and let the generic TRUNCATE
1305 ;;; handle the rest. (Note: if Y = 1, the divide and multiply by Y
1306 ;;; should be removed by other DEFTRANSFORMs.)
1307 (deftransform truncate ((x &optional y)
1308 (float &optional (or float integer)))
1309 (let ((defaulted-y (if y 'y 1)))
1310 `(let ((res (%unary-truncate (/ x ,defaulted-y))))
1311 (values res (- x (* ,defaulted-y res))))))
1313 (deftransform floor ((number &optional divisor)
1314 (float &optional (or integer float)))
1315 (let ((defaulted-divisor (if divisor 'divisor 1)))
1316 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1317 (if (and (not (zerop rem))
1318 (if (minusp ,defaulted-divisor)
1321 (values (1- tru) (+ rem ,defaulted-divisor))
1322 (values tru rem)))))
1324 (deftransform ceiling ((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)
1332 (values (1+ tru) (- rem ,defaulted-divisor))
1333 (values tru rem)))))
1335 (defknown %unary-ftruncate (real) float (movable foldable flushable))
1336 (defknown %unary-ftruncate/single (single-float) single-float
1337 (movable foldable flushable))
1338 (defknown %unary-ftruncate/double (double-float) double-float
1339 (movable foldable flushable))
1341 (defun %unary-ftruncate/single (x)
1342 (declare (type single-float x))
1343 (declare (optimize speed (safety 0)))
1344 (let* ((bits (single-float-bits x))
1345 (exp (ldb sb!vm:single-float-exponent-byte bits))
1346 (biased (the single-float-exponent
1347 (- exp sb!vm:single-float-bias))))
1348 (declare (type (signed-byte 32) bits))
1350 ((= exp sb!vm:single-float-normal-exponent-max) x)
1351 ((<= biased 0) (* x 0f0))
1352 ((>= biased (float-digits x)) x)
1354 (let ((frac-bits (- (float-digits x) biased)))
1355 (setf bits (logandc2 bits (- (ash 1 frac-bits) 1)))
1356 (make-single-float bits))))))
1358 (defun %unary-ftruncate/double (x)
1359 (declare (type double-float x))
1360 (declare (optimize speed (safety 0)))
1361 (let* ((high (double-float-high-bits x))
1362 (low (double-float-low-bits x))
1363 (exp (ldb sb!vm:double-float-exponent-byte high))
1364 (biased (the double-float-exponent
1365 (- exp sb!vm:double-float-bias))))
1366 (declare (type (signed-byte 32) high)
1367 (type (unsigned-byte 32) low))
1369 ((= exp sb!vm:double-float-normal-exponent-max) x)
1370 ((<= biased 0) (* x 0d0))
1371 ((>= biased (float-digits x)) x)
1373 (let ((frac-bits (- (float-digits x) biased)))
1374 (cond ((< frac-bits 32)
1375 (setf low (logandc2 low (- (ash 1 frac-bits) 1))))
1378 (setf high (logandc2 high (- (ash 1 (- frac-bits 32)) 1)))))
1379 (make-double-float high low))))))
1382 ((def (float-type fun)
1383 `(deftransform %unary-ftruncate ((x) (,float-type))
1385 (def single-float %unary-ftruncate/single)
1386 (def double-float %unary-ftruncate/double))