1 ;;;; This file contains the definitions of float specific number
2 ;;;; support (other than irrational stuff, which is in irrat.) There is
3 ;;;; code in here that assumes there are only two float formats: IEEE
4 ;;;; single and double. (Long-float support has been added, but bugs
5 ;;;; may still remain due to old code which assumes this dichotomy.)
7 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!KERNEL")
20 (eval-when (:compile-toplevel :load-toplevel :execute)
22 ;;; These functions let us create floats from bits with the significand
23 ;;; uniformly represented as an integer. This is less efficient for double
24 ;;; floats, but is more convenient when making special values, etc.
25 (defun single-from-bits (sign exp sig)
26 (declare (type bit sign) (type (unsigned-byte 24) sig)
27 (type (unsigned-byte 8) exp))
29 (dpb exp sb!vm:single-float-exponent-byte
30 (dpb sig sb!vm:single-float-significand-byte
31 (if (zerop sign) 0 -1)))))
32 (defun double-from-bits (sign exp sig)
33 (declare (type bit sign) (type (unsigned-byte 53) sig)
34 (type (unsigned-byte 11) exp))
35 (make-double-float (dpb exp sb!vm:double-float-exponent-byte
36 (dpb (ash sig -32) sb!vm:double-float-significand-byte
37 (if (zerop sign) 0 -1)))
38 (ldb (byte 32 0) sig)))
39 #!+(and long-float x86)
40 (defun long-from-bits (sign exp sig)
41 (declare (type bit sign) (type (unsigned-byte 64) sig)
42 (type (unsigned-byte 15) exp))
43 (make-long-float (logior (ash sign 15) exp)
44 (ldb (byte 32 32) sig)
45 (ldb (byte 32 0) sig)))
51 (defconstant least-positive-single-float (single-from-bits 0 0 1))
52 (defconstant least-positive-short-float least-positive-single-float)
53 (defconstant least-negative-single-float (single-from-bits 1 0 1))
54 (defconstant least-negative-short-float least-negative-single-float)
55 (defconstant least-positive-double-float (double-from-bits 0 0 1))
57 (defconstant least-positive-long-float least-positive-double-float)
58 #!+(and long-float x86)
59 (defconstant least-positive-long-float (long-from-bits 0 0 1))
60 (defconstant least-negative-double-float (double-from-bits 1 0 1))
62 (defconstant least-negative-long-float least-negative-double-float)
63 #!+(and long-float x86)
64 (defconstant least-negative-long-float (long-from-bits 1 0 1))
66 (defconstant least-positive-normalized-single-float
67 (single-from-bits 0 sb!vm:single-float-normal-exponent-min 0))
68 (defconstant least-positive-normalized-short-float
69 least-positive-normalized-single-float)
70 (defconstant least-negative-normalized-single-float
71 (single-from-bits 1 sb!vm:single-float-normal-exponent-min 0))
72 (defconstant least-negative-normalized-short-float
73 least-negative-normalized-single-float)
74 (defconstant least-positive-normalized-double-float
75 (double-from-bits 0 sb!vm:double-float-normal-exponent-min 0))
77 (defconstant least-positive-normalized-long-float
78 least-positive-normalized-double-float)
79 #!+(and long-float x86)
80 (defconstant least-positive-normalized-long-float
81 (long-from-bits 0 sb!vm:long-float-normal-exponent-min
82 (ash sb!vm:long-float-hidden-bit 32)))
83 (defconstant least-negative-normalized-double-float
84 (double-from-bits 1 sb!vm:double-float-normal-exponent-min 0))
86 (defconstant least-negative-normalized-long-float
87 least-negative-normalized-double-float)
88 #!+(and long-float x86)
89 (defconstant least-negative-normalized-long-float
90 (long-from-bits 1 sb!vm:long-float-normal-exponent-min
91 (ash sb!vm:long-float-hidden-bit 32)))
93 (defconstant most-positive-single-float
94 (single-from-bits 0 sb!vm:single-float-normal-exponent-max
95 (ldb sb!vm:single-float-significand-byte -1)))
96 (defconstant most-positive-short-float most-positive-single-float)
97 (defconstant most-negative-single-float
98 (single-from-bits 1 sb!vm:single-float-normal-exponent-max
99 (ldb sb!vm:single-float-significand-byte -1)))
100 (defconstant most-negative-short-float most-negative-single-float)
101 (defconstant most-positive-double-float
102 (double-from-bits 0 sb!vm:double-float-normal-exponent-max
103 (ldb (byte sb!vm:double-float-digits 0) -1)))
105 (defconstant most-positive-long-float most-positive-double-float)
106 #!+(and long-float x86)
107 (defconstant most-positive-long-float
108 (long-from-bits 0 sb!vm:long-float-normal-exponent-max
109 (ldb (byte sb!vm:long-float-digits 0) -1)))
110 (defconstant most-negative-double-float
111 (double-from-bits 1 sb!vm:double-float-normal-exponent-max
112 (ldb (byte sb!vm:double-float-digits 0) -1)))
114 (defconstant most-negative-long-float most-negative-double-float)
115 #!+(and long-float x86)
116 (defconstant most-negative-long-float
117 (long-from-bits 1 sb!vm:long-float-normal-exponent-max
118 (ldb (byte sb!vm:long-float-digits 0) -1)))
120 ;;; We don't want to do these DEFCONSTANTs at cross-compilation time,
121 ;;; because the cross-compilation host might not support floating
122 ;;; point infinities.
123 (eval-when (:load-toplevel :execute)
124 (defconstant single-float-positive-infinity
125 (single-from-bits 0 (1+ sb!vm:single-float-normal-exponent-max) 0))
126 (defconstant short-float-positive-infinity single-float-positive-infinity)
127 (defconstant single-float-negative-infinity
128 (single-from-bits 1 (1+ sb!vm:single-float-normal-exponent-max) 0))
129 (defconstant short-float-negative-infinity single-float-negative-infinity)
130 (defconstant double-float-positive-infinity
131 (double-from-bits 0 (1+ sb!vm:double-float-normal-exponent-max) 0))
133 (defconstant long-float-positive-infinity double-float-positive-infinity)
134 #!+(and long-float x86)
135 (defconstant long-float-positive-infinity
136 (long-from-bits 0 (1+ sb!vm:long-float-normal-exponent-max)
137 (ash sb!vm:long-float-hidden-bit 32)))
138 (defconstant double-float-negative-infinity
139 (double-from-bits 1 (1+ sb!vm:double-float-normal-exponent-max) 0))
141 (defconstant long-float-negative-infinity double-float-negative-infinity)
142 #!+(and long-float x86)
143 (defconstant long-float-negative-infinity
144 (long-from-bits 1 (1+ sb!vm:long-float-normal-exponent-max)
145 (ash sb!vm:long-float-hidden-bit 32)))
148 (defconstant single-float-epsilon
149 (single-from-bits 0 (- sb!vm:single-float-bias
150 (1- sb!vm:single-float-digits)) 1))
151 (defconstant short-float-epsilon single-float-epsilon)
152 (defconstant single-float-negative-epsilon
153 (single-from-bits 0 (- sb!vm:single-float-bias sb!vm:single-float-digits) 1))
154 (defconstant short-float-negative-epsilon single-float-negative-epsilon)
155 (defconstant double-float-epsilon
156 (double-from-bits 0 (- sb!vm:double-float-bias
157 (1- sb!vm:double-float-digits)) 1))
159 (defconstant long-float-epsilon double-float-epsilon)
160 #!+(and long-float x86)
161 (defconstant long-float-epsilon
162 (long-from-bits 0 (- sb!vm:long-float-bias (1- sb!vm:long-float-digits))
163 (+ 1 (ash sb!vm:long-float-hidden-bit 32))))
164 (defconstant double-float-negative-epsilon
165 (double-from-bits 0 (- sb!vm:double-float-bias sb!vm:double-float-digits) 1))
167 (defconstant long-float-negative-epsilon double-float-negative-epsilon)
168 #!+(and long-float x86)
169 (defconstant long-float-negative-epsilon
170 (long-from-bits 0 (- sb!vm:long-float-bias sb!vm:long-float-digits)
171 (+ 1 (ash sb!vm:long-float-hidden-bit 32))))
173 ;;;; float predicates and environment query
176 (declaim (maybe-inline float-denormalized-p float-infinity-p float-nan-p
177 float-trapping-nan-p))
179 (defun float-denormalized-p (x)
181 "Return true if the float X is denormalized."
182 (number-dispatch ((x float))
184 (and (zerop (ldb sb!vm:single-float-exponent-byte (single-float-bits x)))
187 (and (zerop (ldb sb!vm:double-float-exponent-byte
188 (double-float-high-bits x)))
190 #!+(and long-float x86)
192 (and (zerop (ldb sb!vm:long-float-exponent-byte (long-float-exp-bits x)))
195 (macrolet ((def-frob (name doc single double #!+(and long-float x86) long)
198 (number-dispatch ((x float))
200 (let ((bits (single-float-bits x)))
201 (and (> (ldb sb!vm:single-float-exponent-byte bits)
202 sb!vm:single-float-normal-exponent-max)
205 (let ((hi (double-float-high-bits x))
206 (lo (double-float-low-bits x)))
207 (declare (ignorable lo))
208 (and (> (ldb sb!vm:double-float-exponent-byte hi)
209 sb!vm:double-float-normal-exponent-max)
211 #!+(and long-float x86)
213 (let ((exp (long-float-exp-bits x))
214 (hi (long-float-high-bits x))
215 (lo (long-float-low-bits x)))
216 (declare (ignorable lo))
217 (and (> (ldb sb!vm:long-float-exponent-byte exp)
218 sb!vm:long-float-normal-exponent-max)
221 (def-frob float-infinity-p
222 "Return true if the float X is an infinity (+ or -)."
223 (zerop (ldb sb!vm:single-float-significand-byte bits))
224 (and (zerop (ldb sb!vm:double-float-significand-byte hi))
226 #!+(and long-float x86)
227 (and (zerop (ldb sb!vm:long-float-significand-byte hi))
230 (def-frob float-nan-p
231 "Return true if the float X is a NaN (Not a Number)."
232 (not (zerop (ldb sb!vm:single-float-significand-byte bits)))
233 (or (not (zerop (ldb sb!vm:double-float-significand-byte hi)))
235 #!+(and long-float x86)
236 (or (not (zerop (ldb sb!vm:long-float-significand-byte hi)))
239 (def-frob float-trapping-nan-p
240 "Return true if the float X is a trapping NaN (Not a Number)."
241 (zerop (logand (ldb sb!vm:single-float-significand-byte bits)
242 sb!vm:single-float-trapping-nan-bit))
243 (zerop (logand (ldb sb!vm:double-float-significand-byte hi)
244 sb!vm:double-float-trapping-nan-bit))
245 #!+(and long-float x86)
246 (zerop (logand (ldb sb!vm:long-float-significand-byte hi)
247 sb!vm:long-float-trapping-nan-bit))))
249 ;;; If denormalized, use a subfunction from INTEGER-DECODE-FLOAT to find the
250 ;;; actual exponent (and hence how denormalized it is), otherwise we just
251 ;;; return the number of digits or 0.
252 #!-sb-fluid (declaim (maybe-inline float-precision))
253 (defun float-precision (f)
255 "Returns a non-negative number of significant digits in its float argument.
256 Will be less than FLOAT-DIGITS if denormalized or zero."
257 (macrolet ((frob (digits bias decode)
259 ((float-denormalized-p f)
260 (multiple-value-bind (ignore exp) (,decode f)
261 (declare (ignore ignore))
263 (+ ,digits (1- ,digits) ,bias exp))))
266 (number-dispatch ((f float))
268 (frob sb!vm:single-float-digits sb!vm:single-float-bias
269 integer-decode-single-denorm))
271 (frob sb!vm:double-float-digits sb!vm:double-float-bias
272 integer-decode-double-denorm))
275 (frob sb!vm:long-float-digits sb!vm:long-float-bias
276 integer-decode-long-denorm)))))
278 (defun float-sign (float1 &optional (float2 (float 1 float1)))
280 "Returns a floating-point number that has the same sign as
281 float1 and, if float2 is given, has the same absolute value
283 (declare (float float1 float2))
284 (* (if (etypecase float1
285 (single-float (minusp (single-float-bits float1)))
286 (double-float (minusp (double-float-high-bits float1)))
288 (long-float (minusp (long-float-exp-bits float1))))
293 (defun float-format-digits (format)
295 ((short-float single-float) sb!vm:single-float-digits)
296 ((double-float #!-long-float long-float) sb!vm:double-float-digits)
298 (long-float sb!vm:long-float-digits)))
300 #!-sb-fluid (declaim (inline float-digits float-radix))
302 (defun float-digits (f)
303 (number-dispatch ((f float))
304 ((single-float) sb!vm:single-float-digits)
305 ((double-float) sb!vm:double-float-digits)
307 ((long-float) sb!vm:long-float-digits)))
309 (defun float-radix (x)
311 "Return (as an integer) the radix b of its floating-point argument."
312 (declare (type float x))
313 ;; ANSI says this function "should signal an error if [..] argument
314 ;; is not a float". Since X is otherwise ignored, Python doesn't
315 ;; check the type by default, so we have to do it ourself:
317 (error 'type-error :datum x :expected-type 'float))
320 ;;;; INTEGER-DECODE-FLOAT and DECODE-FLOAT
323 (declaim (maybe-inline integer-decode-single-float
324 integer-decode-double-float))
326 ;;; Handle the denormalized case of INTEGER-DECODE-FLOAT for SINGLE-FLOAT.
327 (defun integer-decode-single-denorm (x)
328 (declare (type single-float x))
329 (let* ((bits (single-float-bits (abs x)))
330 (sig (ash (ldb sb!vm:single-float-significand-byte bits) 1))
332 (declare (type (unsigned-byte 24) sig)
333 (type (integer 0 23) extra-bias))
335 (unless (zerop (logand sig sb!vm:single-float-hidden-bit))
337 (setq sig (ash sig 1))
340 (- (- sb!vm:single-float-bias)
341 sb!vm:single-float-digits
343 (if (minusp (float-sign x)) -1 1))))
345 ;;; Handle the single-float case of INTEGER-DECODE-FLOAT. If an infinity or
346 ;;; NaN, error. If a denorm, call i-d-s-DENORM to handle it.
347 (defun integer-decode-single-float (x)
348 (declare (single-float x))
349 (let* ((bits (single-float-bits (abs x)))
350 (exp (ldb sb!vm:single-float-exponent-byte bits))
351 (sig (ldb sb!vm:single-float-significand-byte bits))
352 (sign (if (minusp (float-sign x)) -1 1))
353 (biased (- exp sb!vm:single-float-bias sb!vm:single-float-digits)))
354 (declare (fixnum biased))
355 (unless (<= exp sb!vm:single-float-normal-exponent-max)
356 (error "can't decode NaN or infinity: ~S" x))
357 (cond ((and (zerop exp) (zerop sig))
358 (values 0 biased sign))
359 ((< exp sb!vm:single-float-normal-exponent-min)
360 (integer-decode-single-denorm x))
362 (values (logior sig sb!vm:single-float-hidden-bit) biased sign)))))
364 ;;; Like INTEGER-DECODE-SINGLE-DENORM, only doubly so.
365 (defun integer-decode-double-denorm (x)
366 (declare (type double-float x))
367 (let* ((high-bits (double-float-high-bits (abs x)))
368 (sig-high (ldb sb!vm:double-float-significand-byte high-bits))
369 (low-bits (double-float-low-bits x))
370 (sign (if (minusp (float-sign x)) -1 1))
371 (biased (- (- sb!vm:double-float-bias) sb!vm:double-float-digits)))
374 (extra-bias (- sb!vm:double-float-digits 33))
376 (declare (type (unsigned-byte 32) sig) (fixnum extra-bias))
378 (unless (zerop (logand sig bit)) (return))
379 (setq sig (ash sig 1))
381 (values (ash sig (- sb!vm:double-float-digits 32))
382 (truly-the fixnum (- biased extra-bias))
384 (let ((sig (ash sig-high 1))
386 (declare (type (unsigned-byte 32) sig) (fixnum extra-bias))
388 (unless (zerop (logand sig sb!vm:double-float-hidden-bit))
390 (setq sig (ash sig 1))
392 (values (logior (ash sig 32) (ash low-bits (1- extra-bias)))
393 (truly-the fixnum (- biased extra-bias))
396 ;;; Like INTEGER-DECODE-SINGLE-FLOAT, only doubly so.
397 (defun integer-decode-double-float (x)
398 (declare (double-float x))
400 (hi (double-float-high-bits abs))
401 (lo (double-float-low-bits abs))
402 (exp (ldb sb!vm:double-float-exponent-byte hi))
403 (sig (ldb sb!vm:double-float-significand-byte hi))
404 (sign (if (minusp (float-sign x)) -1 1))
405 (biased (- exp sb!vm:double-float-bias sb!vm:double-float-digits)))
406 (declare (fixnum biased))
407 (unless (<= exp sb!vm:double-float-normal-exponent-max)
408 (error "Can't decode NaN or infinity: ~S." x))
409 (cond ((and (zerop exp) (zerop sig) (zerop lo))
410 (values 0 biased sign))
411 ((< exp sb!vm:double-float-normal-exponent-min)
412 (integer-decode-double-denorm x))
415 (logior (ash (logior (ldb sb!vm:double-float-significand-byte hi)
416 sb!vm:double-float-hidden-bit)
421 #!+(and long-float x86)
422 (defun integer-decode-long-denorm (x)
423 (declare (type long-float x))
424 (let* ((high-bits (long-float-high-bits (abs x)))
425 (sig-high (ldb sb!vm:long-float-significand-byte high-bits))
426 (low-bits (long-float-low-bits x))
427 (sign (if (minusp (float-sign x)) -1 1))
428 (biased (- (- sb!vm:long-float-bias) sb!vm:long-float-digits)))
431 (extra-bias (- sb!vm:long-float-digits 33))
433 (declare (type (unsigned-byte 32) sig) (fixnum extra-bias))
435 (unless (zerop (logand sig bit)) (return))
436 (setq sig (ash sig 1))
438 (values (ash sig (- sb!vm:long-float-digits 32))
439 (truly-the fixnum (- biased extra-bias))
441 (let ((sig (ash sig-high 1))
443 (declare (type (unsigned-byte 32) sig) (fixnum extra-bias))
445 (unless (zerop (logand sig sb!vm:long-float-hidden-bit))
447 (setq sig (ash sig 1))
449 (values (logior (ash sig 32) (ash low-bits (1- extra-bias)))
450 (truly-the fixnum (- biased extra-bias))
453 #!+(and long-float x86)
454 (defun integer-decode-long-float (x)
455 (declare (long-float x))
456 (let* ((hi (long-float-high-bits x))
457 (lo (long-float-low-bits x))
458 (exp-bits (long-float-exp-bits x))
459 (exp (ldb sb!vm:long-float-exponent-byte exp-bits))
460 (sign (if (minusp exp-bits) -1 1))
461 (biased (- exp sb!vm:long-float-bias sb!vm:long-float-digits)))
462 (declare (fixnum biased))
463 (unless (<= exp sb!vm:long-float-normal-exponent-max)
464 (error "can't decode NaN or infinity: ~S" x))
465 (cond ((and (zerop exp) (zerop hi) (zerop lo))
466 (values 0 biased sign))
467 ((< exp sb!vm:long-float-normal-exponent-min)
468 (integer-decode-long-denorm x))
470 (values (logior (ash hi 32) lo) biased sign)))))
472 ;;; Dispatch to the correct type-specific i-d-f function.
473 (defun integer-decode-float (x)
475 "Returns three values:
476 1) an integer representation of the significand.
477 2) the exponent for the power of 2 that the significand must be multiplied
478 by to get the actual value. This differs from the DECODE-FLOAT exponent
479 by FLOAT-DIGITS, since the significand has been scaled to have all its
480 digits before the radix point.
481 3) -1 or 1 (i.e. the sign of the argument.)"
482 (number-dispatch ((x float))
484 (integer-decode-single-float x))
486 (integer-decode-double-float x))
489 (integer-decode-long-float x))))
491 #!-sb-fluid (declaim (maybe-inline decode-single-float decode-double-float))
493 ;;; Handle the denormalized case of DECODE-SINGLE-FLOAT. We call
494 ;;; INTEGER-DECODE-SINGLE-DENORM and then make the result into a float.
495 (defun decode-single-denorm (x)
496 (declare (type single-float x))
497 (multiple-value-bind (sig exp sign) (integer-decode-single-denorm x)
498 (values (make-single-float
499 (dpb sig sb!vm:single-float-significand-byte
500 (dpb sb!vm:single-float-bias
501 sb!vm:single-float-exponent-byte
503 (truly-the fixnum (+ exp sb!vm:single-float-digits))
506 ;;; Handle the single-float case of DECODE-FLOAT. If an infinity or NaN,
507 ;;; error. If a denorm, call d-s-DENORM to handle it.
508 (defun decode-single-float (x)
509 (declare (single-float x))
510 (let* ((bits (single-float-bits (abs x)))
511 (exp (ldb sb!vm:single-float-exponent-byte bits))
512 (sign (float-sign x))
513 (biased (truly-the single-float-exponent
514 (- exp sb!vm:single-float-bias))))
515 (unless (<= exp sb!vm:single-float-normal-exponent-max)
516 (error "can't decode NaN or infinity: ~S" x))
518 (values 0.0f0 biased sign))
519 ((< exp sb!vm:single-float-normal-exponent-min)
520 (decode-single-denorm x))
522 (values (make-single-float
523 (dpb sb!vm:single-float-bias
524 sb!vm:single-float-exponent-byte
528 ;;; Like DECODE-SINGLE-DENORM, only doubly so.
529 (defun decode-double-denorm (x)
530 (declare (double-float x))
531 (multiple-value-bind (sig exp sign) (integer-decode-double-denorm x)
532 (values (make-double-float
533 (dpb (logand (ash sig -32) (lognot sb!vm:double-float-hidden-bit))
534 sb!vm:double-float-significand-byte
535 (dpb sb!vm:double-float-bias
536 sb!vm:double-float-exponent-byte 0))
537 (ldb (byte 32 0) sig))
538 (truly-the fixnum (+ exp sb!vm:double-float-digits))
541 ;;; Like DECODE-SINGLE-FLOAT, only doubly so.
542 (defun decode-double-float (x)
543 (declare (double-float x))
545 (hi (double-float-high-bits abs))
546 (lo (double-float-low-bits abs))
547 (exp (ldb sb!vm:double-float-exponent-byte hi))
548 (sign (float-sign x))
549 (biased (truly-the double-float-exponent
550 (- exp sb!vm:double-float-bias))))
551 (unless (<= exp sb!vm:double-float-normal-exponent-max)
552 (error "can't decode NaN or infinity: ~S" x))
554 (values 0.0d0 biased sign))
555 ((< exp sb!vm:double-float-normal-exponent-min)
556 (decode-double-denorm x))
558 (values (make-double-float
559 (dpb sb!vm:double-float-bias
560 sb!vm:double-float-exponent-byte hi)
564 #!+(and long-float x86)
565 (defun decode-long-denorm (x)
566 (declare (long-float x))
567 (multiple-value-bind (sig exp sign) (integer-decode-long-denorm x)
568 (values (make-long-float sb!vm:long-float-bias (ash sig -32)
569 (ldb (byte 32 0) sig))
570 (truly-the fixnum (+ exp sb!vm:long-float-digits))
573 #!+(and long-float x86)
574 (defun decode-long-float (x)
575 (declare (long-float x))
576 (let* ((hi (long-float-high-bits x))
577 (lo (long-float-low-bits x))
578 (exp-bits (long-float-exp-bits x))
579 (exp (ldb sb!vm:long-float-exponent-byte exp-bits))
580 (sign (if (minusp exp-bits) -1l0 1l0))
581 (biased (truly-the long-float-exponent
582 (- exp sb!vm:long-float-bias))))
583 (unless (<= exp sb!vm:long-float-normal-exponent-max)
584 (error "can't decode NaN or infinity: ~S" x))
586 (values 0.0l0 biased sign))
587 ((< exp sb!vm:long-float-normal-exponent-min)
588 (decode-long-denorm x))
590 (values (make-long-float
591 (dpb sb!vm:long-float-bias sb!vm:long-float-exponent-byte
597 ;;; Dispatch to the appropriate type-specific function.
598 (defun decode-float (f)
600 "Returns three values:
601 1) a floating-point number representing the significand. This is always
602 between 0.5 (inclusive) and 1.0 (exclusive).
603 2) an integer representing the exponent.
604 3) -1.0 or 1.0 (i.e. the sign of the argument.)"
605 (number-dispatch ((f float))
607 (decode-single-float f))
609 (decode-double-float f))
612 (decode-long-float f))))
616 #!-sb-fluid (declaim (maybe-inline scale-single-float scale-double-float))
618 ;;; Handle float scaling where the X is denormalized or the result is
619 ;;; denormalized or underflows to 0.
620 (defun scale-float-maybe-underflow (x exp)
621 (multiple-value-bind (sig old-exp) (integer-decode-float x)
622 (let* ((digits (float-digits x))
623 (new-exp (+ exp old-exp digits
625 (single-float sb!vm:single-float-bias)
626 (double-float sb!vm:double-float-bias))))
627 (sign (if (minusp (float-sign x)) 1 0)))
631 (single-float sb!vm:single-float-normal-exponent-min)
632 (double-float sb!vm:double-float-normal-exponent-min)))
633 (when (sb!vm:current-float-trap :inexact)
634 (error 'floating-point-inexact :operation 'scale-float
635 :operands (list x exp)))
636 (when (sb!vm:current-float-trap :underflow)
637 (error 'floating-point-underflow :operation 'scale-float
638 :operands (list x exp)))
639 (let ((shift (1- new-exp)))
640 (if (< shift (- (1- digits)))
643 (single-float (single-from-bits sign 0 (ash sig shift)))
644 (double-float (double-from-bits sign 0 (ash sig shift)))))))
647 (single-float (single-from-bits sign new-exp sig))
648 (double-float (double-from-bits sign new-exp sig))))))))
650 ;;; Called when scaling a float overflows, or the original float was a
651 ;;; NaN or infinity. If overflow errors are trapped, then error,
652 ;;; otherwise return the appropriate infinity. If a NaN, signal or not
654 (defun scale-float-maybe-overflow (x exp)
656 ((float-infinity-p x)
657 ;; Infinity is infinity, no matter how small...
660 (when (and (float-trapping-nan-p x)
661 (sb!vm:current-float-trap :invalid))
662 (error 'floating-point-invalid-operation :operation 'scale-float
663 :operands (list x exp)))
666 (when (sb!vm:current-float-trap :overflow)
667 (error 'floating-point-overflow :operation 'scale-float
668 :operands (list x exp)))
669 (when (sb!vm:current-float-trap :inexact)
670 (error 'floating-point-inexact :operation 'scale-float
671 :operands (list x exp)))
674 (single-float single-float-positive-infinity)
675 (double-float double-float-positive-infinity))))))
677 ;;; Scale a single or double float, calling the correct over/underflow
679 (defun scale-single-float (x exp)
680 (declare (single-float x) (fixnum exp))
681 (let* ((bits (single-float-bits x))
682 (old-exp (ldb sb!vm:single-float-exponent-byte bits))
683 (new-exp (+ old-exp exp)))
686 ((or (< old-exp sb!vm:single-float-normal-exponent-min)
687 (< new-exp sb!vm:single-float-normal-exponent-min))
688 (scale-float-maybe-underflow x exp))
689 ((or (> old-exp sb!vm:single-float-normal-exponent-max)
690 (> new-exp sb!vm:single-float-normal-exponent-max))
691 (scale-float-maybe-overflow x exp))
693 (make-single-float (dpb new-exp
694 sb!vm:single-float-exponent-byte
696 (defun scale-double-float (x exp)
697 (declare (double-float x) (fixnum exp))
698 (let* ((hi (double-float-high-bits x))
699 (lo (double-float-low-bits x))
700 (old-exp (ldb sb!vm:double-float-exponent-byte hi))
701 (new-exp (+ old-exp exp)))
704 ((or (< old-exp sb!vm:double-float-normal-exponent-min)
705 (< new-exp sb!vm:double-float-normal-exponent-min))
706 (scale-float-maybe-underflow x exp))
707 ((or (> old-exp sb!vm:double-float-normal-exponent-max)
708 (> new-exp sb!vm:double-float-normal-exponent-max))
709 (scale-float-maybe-overflow x exp))
711 (make-double-float (dpb new-exp sb!vm:double-float-exponent-byte hi)
714 #!+(and x86 long-float)
715 (defun scale-long-float (x exp)
716 (declare (long-float x) (fixnum exp))
719 ;;; Dispatch to the correct type-specific scale-float function.
720 (defun scale-float (f ex)
722 "Returns the value (* f (expt (float 2 f) ex)), but with no unnecessary loss
723 of precision or overflow."
724 (number-dispatch ((f float))
726 (scale-single-float f ex))
728 (scale-double-float f ex))
731 (scale-long-float f ex))))
733 ;;;; converting to/from floats
735 (defun float (number &optional (other () otherp))
737 "Converts any REAL to a float. If OTHER is not provided, it returns a
738 SINGLE-FLOAT if NUMBER is not already a FLOAT. If OTHER is provided, the
739 result is the same float format as OTHER."
741 (number-dispatch ((number real) (other float))
742 (((foreach rational single-float double-float #!+long-float long-float)
743 (foreach single-float double-float #!+long-float long-float))
744 (coerce number '(dispatch-type other))))
747 (coerce number 'single-float))))
749 (macrolet ((frob (name type)
751 (number-dispatch ((x real))
752 (((foreach single-float double-float #!+long-float long-float
756 (bignum-to-float x ',type))
758 (float-ratio x ',type))))))
759 (frob %single-float single-float)
760 (frob %double-float double-float)
762 (frob %long-float long-float))
764 ;;; Convert a ratio to a float. We avoid any rounding error by doing an
765 ;;; integer division. Accuracy is important to preserve read/print
766 ;;; consistency, since this is ultimately how the reader reads a float. We
767 ;;; scale the numerator by a power of two until the division results in the
768 ;;; desired number of fraction bits, then do round-to-nearest.
769 (defun float-ratio (x format)
770 (let* ((signed-num (numerator x))
771 (plusp (plusp signed-num))
772 (num (if plusp signed-num (- signed-num)))
773 (den (denominator x))
774 (digits (float-format-digits format))
776 (declare (fixnum digits scale))
777 ;; Strip any trailing zeros from the denominator and move it into the scale
778 ;; factor (to minimize the size of the operands.)
779 (let ((den-twos (1- (integer-length (logxor den (1- den))))))
780 (declare (fixnum den-twos))
781 (decf scale den-twos)
782 (setq den (ash den (- den-twos))))
783 ;; Guess how much we need to scale by from the magnitudes of the numerator
784 ;; and denominator. We want one extra bit for a guard bit.
785 (let* ((num-len (integer-length num))
786 (den-len (integer-length den))
787 (delta (- den-len num-len))
788 (shift (1+ (the fixnum (+ delta digits))))
789 (shifted-num (ash num shift)))
790 (declare (fixnum delta shift))
792 (labels ((float-and-scale (bits)
793 (let* ((bits (ash bits -1))
794 (len (integer-length bits)))
795 (cond ((> len digits)
796 (aver (= len (the fixnum (1+ digits))))
797 (scale-float (floatit (ash bits -1)) (1+ scale)))
799 (scale-float (floatit bits) scale)))))
801 (let ((sign (if plusp 0 1)))
804 (single-from-bits sign sb!vm:single-float-bias bits))
806 (double-from-bits sign sb!vm:double-float-bias bits))
809 (long-from-bits sign sb!vm:long-float-bias bits))))))
811 (multiple-value-bind (fraction-and-guard rem)
812 (truncate shifted-num den)
813 (let ((extra (- (integer-length fraction-and-guard) digits)))
814 (declare (fixnum extra))
817 ((oddp fraction-and-guard)
821 (if (zerop (logand fraction-and-guard 2))
823 (1+ fraction-and-guard)))
824 (float-and-scale (1+ fraction-and-guard)))))
826 (return (float-and-scale fraction-and-guard)))))
827 (setq shifted-num (ash shifted-num -1))
831 These might be useful if we ever have a machine without float/integer
832 conversion hardware. For now, we'll use special ops that
833 uninterruptibly frob the rounding modes & do ieee round-to-integer.
835 ;;; The compiler compiles a call to this when we are doing %UNARY-TRUNCATE
836 ;;; and the result is known to be a fixnum. We can avoid some generic
837 ;;; arithmetic in this case.
838 (defun %unary-truncate-single-float/fixnum (x)
839 (declare (single-float x) (values fixnum))
840 (locally (declare (optimize (speed 3) (safety 0)))
841 (let* ((bits (single-float-bits x))
842 (exp (ldb sb!vm:single-float-exponent-byte bits))
843 (frac (logior (ldb sb!vm:single-float-significand-byte bits)
844 sb!vm:single-float-hidden-bit))
845 (shift (- exp sb!vm:single-float-digits sb!vm:single-float-bias)))
846 (when (> exp sb!vm:single-float-normal-exponent-max)
847 (error 'floating-point-invalid-operation :operator 'truncate
849 (if (<= shift (- sb!vm:single-float-digits))
851 (let ((res (ash frac shift)))
852 (declare (type (unsigned-byte 31) res))
857 ;;; Double-float version of this operation (see above single op).
858 (defun %unary-truncate-double-float/fixnum (x)
859 (declare (double-float x) (values fixnum))
860 (locally (declare (optimize (speed 3) (safety 0)))
861 (let* ((hi-bits (double-float-high-bits x))
862 (exp (ldb sb!vm:double-float-exponent-byte hi-bits))
863 (frac (logior (ldb sb!vm:double-float-significand-byte hi-bits)
864 sb!vm:double-float-hidden-bit))
865 (shift (- exp (- sb!vm:double-float-digits sb!vm:word-bits)
866 sb!vm:double-float-bias)))
867 (when (> exp sb!vm:double-float-normal-exponent-max)
868 (error 'floating-point-invalid-operation :operator 'truncate
870 (if (<= shift (- sb!vm:word-bits sb!vm:double-float-digits))
872 (let* ((res-hi (ash frac shift))
873 (res (if (plusp shift)
876 (ash (double-float-low-bits x)
877 (- shift sb!vm:word-bits))))
879 (declare (type (unsigned-byte 31) res-hi res))
885 ;;; This function is called when we are doing a truncate without any funky
886 ;;; divisor, i.e. converting a float or ratio to an integer. Note that we do
887 ;;; *not* return the second value of truncate, so it must be computed by the
888 ;;; caller if needed.
890 ;;; In the float case, we pick off small arguments so that compiler can use
891 ;;; special-case operations. We use an exclusive test, since (due to round-off
892 ;;; error), (float most-positive-fixnum) may be greater than
893 ;;; most-positive-fixnum.
894 (defun %unary-truncate (number)
895 (number-dispatch ((number real))
897 ((ratio) (values (truncate (numerator number) (denominator number))))
898 (((foreach single-float double-float #!+long-float long-float))
899 (if (< (float most-negative-fixnum number)
901 (float most-positive-fixnum number))
902 (truly-the fixnum (%unary-truncate number))
903 (multiple-value-bind (bits exp) (integer-decode-float number)
904 (let ((res (ash bits exp)))
909 ;;; Similar to %UNARY-TRUNCATE, but rounds to the nearest integer. If we
910 ;;; can't use the round primitive, then we do our own round-to-nearest on the
911 ;;; result of i-d-f. [Note that this rounding will really only happen with
912 ;;; double floats, since the whole single-float fraction will fit in a fixnum,
913 ;;; so all single-floats larger than most-positive-fixnum can be precisely
914 ;;; represented by an integer.]
915 (defun %unary-round (number)
916 (number-dispatch ((number real))
918 ((ratio) (values (round (numerator number) (denominator number))))
919 (((foreach single-float double-float #!+long-float long-float))
920 (if (< (float most-negative-fixnum number)
922 (float most-positive-fixnum number))
923 (truly-the fixnum (%unary-round number))
924 (multiple-value-bind (bits exp) (integer-decode-float number)
925 (let* ((shifted (ash bits exp))
926 (rounded (if (and (minusp exp)
929 (lognot (ash -1 (- exp))))
939 "RATIONAL produces a rational number for any real numeric argument. This is
940 more efficient than RATIONALIZE, but it assumes that floating-point is
941 completely accurate, giving a result that isn't as pretty."
942 (number-dispatch ((x real))
943 (((foreach single-float double-float #!+long-float long-float))
944 (multiple-value-bind (bits exp) (integer-decode-float x)
947 (let* ((int (if (minusp x) (- bits) bits))
948 (digits (float-digits x))
951 (integer-/-integer int (ash 1 (+ digits (- ex))))
952 (integer-/-integer (ash int ex) (ash 1 digits)))))))
955 (defun rationalize (x)
957 "Converts any REAL to a RATIONAL. Floats are converted to a simple rational
958 representation exploiting the assumption that floats are only accurate to
959 their precision. RATIONALIZE (and also RATIONAL) preserve the invariant:
960 (= x (float (rationalize x) x))"
961 (number-dispatch ((x real))
962 (((foreach single-float double-float #!+long-float long-float))
963 ;; Thanks to Kim Fateman, who stole this function rationalize-float from
964 ;; macsyma's rational. Macsyma'a rationalize was written by the legendary
965 ;; Gosper (rwg). Guy Steele said about Gosper, "He has been called the
966 ;; only living 17th century mathematician and is also the best pdp-10
967 ;; hacker I know." So, if you can understand or debug this code you win
969 (cond ((minusp x) (- (rationalize (- x))))
972 (let ((eps (etypecase x
973 (single-float single-float-epsilon)
974 (double-float double-float-epsilon)
976 (long-float long-float-epsilon)))
979 (do ((xx x (setq y (/ (float 1.0 x) (- xx (float a x)))))
980 (num (setq a (truncate x))
981 (+ (* (setq a (truncate y)) num) onum))
982 (den 1 (+ (* a den) oden))
985 ((and (not (zerop den))
986 (not (> (abs (/ (- x (/ (float num x)
990 (integer-/-integer num den))
991 (declare ((dispatch-type x) xx)))))))