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")
18 ;;;; float predicates and environment query
21 (declaim (maybe-inline float-denormalized-p float-infinity-p float-nan-p
22 float-trapping-nan-p
))
24 (defun float-denormalized-p (x)
25 "Return true if the float X is denormalized."
26 (number-dispatch ((x float
))
28 (and (zerop (ldb sb
!vm
:single-float-exponent-byte
(single-float-bits x
)))
31 (and (zerop (ldb sb
!vm
:double-float-exponent-byte
32 (double-float-high-bits x
)))
34 #!+(and long-float x86
)
36 (and (zerop (ldb sb
!vm
:long-float-exponent-byte
(long-float-exp-bits x
)))
39 (defmacro !define-float-dispatching-function
40 (name doc single double
#!+(and long-float x86
) long
)
43 (number-dispatch ((x float
))
45 (let ((bits (single-float-bits x
)))
46 (and (> (ldb sb
!vm
:single-float-exponent-byte bits
)
47 sb
!vm
:single-float-normal-exponent-max
)
50 (let ((hi (double-float-high-bits x
))
51 (lo (double-float-low-bits x
)))
52 (declare (ignorable lo
))
53 (and (> (ldb sb
!vm
:double-float-exponent-byte hi
)
54 sb
!vm
:double-float-normal-exponent-max
)
56 #!+(and long-float x86
)
58 (let ((exp (long-float-exp-bits x
))
59 (hi (long-float-high-bits x
))
60 (lo (long-float-low-bits x
)))
61 (declare (ignorable lo
))
62 (and (> (ldb sb
!vm
:long-float-exponent-byte exp
)
63 sb
!vm
:long-float-normal-exponent-max
)
66 (!define-float-dispatching-function float-infinity-p
67 "Return true if the float X is an infinity (+ or -)."
68 (zerop (ldb sb
!vm
:single-float-significand-byte bits
))
69 (and (zerop (ldb sb
!vm
:double-float-significand-byte hi
))
71 #!+(and long-float x86
)
72 (and (zerop (ldb sb
!vm
:long-float-significand-byte hi
))
75 (!define-float-dispatching-function float-nan-p
76 "Return true if the float X is a NaN (Not a Number)."
77 (not (zerop (ldb sb
!vm
:single-float-significand-byte bits
)))
78 (or (not (zerop (ldb sb
!vm
:double-float-significand-byte hi
)))
80 #!+(and long-float x86
)
81 (or (not (zerop (ldb sb
!vm
:long-float-significand-byte hi
)))
84 (!define-float-dispatching-function float-trapping-nan-p
85 "Return true if the float X is a trapping NaN (Not a Number)."
86 ;; HPPA (and apparently MIPS) have trapping NaNs (SNaNs) with the
87 ;; trapping-nan-bit SET. PPC, SPARC, Alpha, and x86 (and presumably
88 ;; x86-64, ARM, and ARM64) have trapping NaNs (SNaNs) with the
89 ;; trapping-nan-bit CLEAR. Note that the given implementation
90 ;; considers infinities to be FLOAT-TRAPPING-NAN-P on most
93 (zerop (logand (ldb sb
!vm
:single-float-significand-byte bits
)
94 sb
!vm
:single-float-trapping-nan-bit
))
96 (not (zerop (logand (ldb sb
!vm
:single-float-significand-byte bits
)
97 sb
!vm
:single-float-trapping-nan-bit
)))
99 (zerop (logand (ldb sb
!vm
:double-float-significand-byte hi
)
100 sb
!vm
:double-float-trapping-nan-bit
))
102 (not (zerop (logand (ldb sb
!vm
:double-float-significand-byte hi
)
103 sb
!vm
:double-float-trapping-nan-bit
)))
104 #!+(and long-float x86
)
105 (zerop (logand (ldb sb
!vm
:long-float-significand-byte hi
)
106 sb
!vm
:long-float-trapping-nan-bit
)))
108 ;;; If denormalized, use a subfunction from INTEGER-DECODE-FLOAT to find the
109 ;;; actual exponent (and hence how denormalized it is), otherwise we just
110 ;;; return the number of digits or 0.
111 #!-sb-fluid
(declaim (maybe-inline float-precision
))
112 (defun float-precision (f)
113 "Return a non-negative number of significant digits in its float argument.
114 Will be less than FLOAT-DIGITS if denormalized or zero."
115 (declare (explicit-check))
116 (macrolet ((frob (digits bias decode
)
118 ((float-denormalized-p f
)
119 (multiple-value-bind (ignore exp
) (,decode f
)
120 (declare (ignore ignore
))
122 (+ ,digits
(1- ,digits
) ,bias exp
))))
125 (number-dispatch ((f float
))
127 (frob sb
!vm
:single-float-digits sb
!vm
:single-float-bias
128 integer-decode-single-denorm
))
130 (frob sb
!vm
:double-float-digits sb
!vm
:double-float-bias
131 integer-decode-double-denorm
))
134 (frob sb
!vm
:long-float-digits sb
!vm
:long-float-bias
135 integer-decode-long-denorm
)))))
137 (defun float-sign (float1 &optional
(float2 (float 1 float1
)))
138 "Return a floating-point number that has the same sign as
139 FLOAT1 and, if FLOAT2 is given, has the same absolute value
141 (declare (float float1 float2
) (explicit-check))
142 (* (if (etypecase float1
143 (single-float (minusp (single-float-bits float1
)))
144 (double-float (minusp (double-float-high-bits float1
)))
146 (long-float (minusp (long-float-exp-bits float1
))))
151 (defun float-format-digits (format)
153 ((short-float single-float
) sb
!vm
:single-float-digits
)
154 ((double-float #!-long-float long-float
) sb
!vm
:double-float-digits
)
156 (long-float sb
!vm
:long-float-digits
)))
158 #!-sb-fluid
(declaim (inline float-digits float-radix
))
160 (defun float-digits (f)
161 (declare (explicit-check))
162 (number-dispatch ((f float
))
163 ((single-float) sb
!vm
:single-float-digits
)
164 ((double-float) sb
!vm
:double-float-digits
)
166 ((long-float) sb
!vm
:long-float-digits
)))
168 (defun float-radix (x)
169 "Return (as an integer) the radix b of its floating-point argument."
170 (declare (ignore x
) (type float x
))
173 ;;;; INTEGER-DECODE-FLOAT and DECODE-FLOAT
176 (declaim (maybe-inline integer-decode-single-float
177 integer-decode-double-float
))
179 ;;; Handle the denormalized case of INTEGER-DECODE-FLOAT for SINGLE-FLOAT.
180 (defun integer-decode-single-denorm (x)
181 (declare (type single-float x
))
182 (let* ((bits (single-float-bits (abs x
)))
183 (sig (ash (ldb sb
!vm
:single-float-significand-byte bits
) 1))
185 (declare (type (unsigned-byte 24) sig
)
186 (type (integer 0 23) extra-bias
))
188 (unless (zerop (logand sig sb
!vm
:single-float-hidden-bit
))
190 (setq sig
(ash sig
1))
193 (- (- sb
!vm
:single-float-bias
)
194 sb
!vm
:single-float-digits
196 (if (minusp (float-sign x
)) -
1 1))))
198 ;;; Handle the single-float case of INTEGER-DECODE-FLOAT. If an infinity or
199 ;;; NaN, error. If a denorm, call i-d-s-DENORM to handle it.
200 (defun integer-decode-single-float (x)
201 (declare (single-float x
))
202 (let* ((bits (single-float-bits (abs x
)))
203 (exp (ldb sb
!vm
:single-float-exponent-byte bits
))
204 (sig (ldb sb
!vm
:single-float-significand-byte bits
))
205 (sign (if (minusp (float-sign x
)) -
1 1))
206 (biased (- exp sb
!vm
:single-float-bias sb
!vm
:single-float-digits
)))
207 (declare (fixnum biased
))
208 (unless (<= exp sb
!vm
:single-float-normal-exponent-max
)
209 (error "can't decode NaN or infinity: ~S" x
))
210 (cond ((and (zerop exp
) (zerop sig
))
211 (values 0 biased sign
))
212 ((< exp sb
!vm
:single-float-normal-exponent-min
)
213 (integer-decode-single-denorm x
))
215 (values (logior sig sb
!vm
:single-float-hidden-bit
) biased sign
)))))
217 ;;; like INTEGER-DECODE-SINGLE-DENORM, only doubly so
218 (defun integer-decode-double-denorm (x)
219 (declare (type double-float x
))
220 (let* ((high-bits (double-float-high-bits (abs x
)))
221 (sig-high (ldb sb
!vm
:double-float-significand-byte high-bits
))
222 (low-bits (double-float-low-bits x
))
223 (sign (if (minusp (float-sign x
)) -
1 1))
224 (biased (- (- sb
!vm
:double-float-bias
) sb
!vm
:double-float-digits
)))
227 (extra-bias (- sb
!vm
:double-float-digits
33))
229 (declare (type (unsigned-byte 32) sig
) (fixnum extra-bias
))
231 (unless (zerop (logand sig bit
)) (return))
232 (setq sig
(ash sig
1))
234 (values (ash sig
(- sb
!vm
:double-float-digits
32))
235 (truly-the fixnum
(- biased extra-bias
))
237 (let ((sig (ash sig-high
1))
239 (declare (type (unsigned-byte 32) sig
) (fixnum extra-bias
))
241 (unless (zerop (logand sig sb
!vm
:double-float-hidden-bit
))
243 (setq sig
(ash sig
1))
245 (values (logior (ash sig
32) (ash low-bits
(1- extra-bias
)))
246 (truly-the fixnum
(- biased extra-bias
))
249 ;;; like INTEGER-DECODE-SINGLE-FLOAT, only doubly so
250 (defun integer-decode-double-float (x)
251 (declare (double-float x
))
253 (hi (double-float-high-bits abs
))
254 (lo (double-float-low-bits abs
))
255 (exp (ldb sb
!vm
:double-float-exponent-byte hi
))
256 (sig (ldb sb
!vm
:double-float-significand-byte hi
))
257 (sign (if (minusp (float-sign x
)) -
1 1))
258 (biased (- exp sb
!vm
:double-float-bias sb
!vm
:double-float-digits
)))
259 (declare (fixnum biased
))
260 (unless (<= exp sb
!vm
:double-float-normal-exponent-max
)
261 (error "Can't decode NaN or infinity: ~S." x
))
262 (cond ((and (zerop exp
) (zerop sig
) (zerop lo
))
263 (values 0 biased sign
))
264 ((< exp sb
!vm
:double-float-normal-exponent-min
)
265 (integer-decode-double-denorm x
))
268 (logior (ash (logior (ldb sb
!vm
:double-float-significand-byte hi
)
269 sb
!vm
:double-float-hidden-bit
)
274 #!+(and long-float x86
)
275 (defun integer-decode-long-denorm (x)
276 (declare (type long-float x
))
277 (let* ((high-bits (long-float-high-bits (abs x
)))
278 (sig-high (ldb sb
!vm
:long-float-significand-byte high-bits
))
279 (low-bits (long-float-low-bits x
))
280 (sign (if (minusp (float-sign x
)) -
1 1))
281 (biased (- (- sb
!vm
:long-float-bias
) sb
!vm
:long-float-digits
)))
284 (extra-bias (- sb
!vm
:long-float-digits
33))
286 (declare (type (unsigned-byte 32) sig
) (fixnum extra-bias
))
288 (unless (zerop (logand sig bit
)) (return))
289 (setq sig
(ash sig
1))
291 (values (ash sig
(- sb
!vm
:long-float-digits
32))
292 (truly-the fixnum
(- biased extra-bias
))
294 (let ((sig (ash sig-high
1))
296 (declare (type (unsigned-byte 32) sig
) (fixnum extra-bias
))
298 (unless (zerop (logand sig sb
!vm
:long-float-hidden-bit
))
300 (setq sig
(ash sig
1))
302 (values (logior (ash sig
32) (ash low-bits
(1- extra-bias
)))
303 (truly-the fixnum
(- biased extra-bias
))
306 #!+(and long-float x86
)
307 (defun integer-decode-long-float (x)
308 (declare (long-float x
))
309 (let* ((hi (long-float-high-bits x
))
310 (lo (long-float-low-bits x
))
311 (exp-bits (long-float-exp-bits x
))
312 (exp (ldb sb
!vm
:long-float-exponent-byte exp-bits
))
313 (sign (if (minusp exp-bits
) -
1 1))
314 (biased (- exp sb
!vm
:long-float-bias sb
!vm
:long-float-digits
)))
315 (declare (fixnum biased
))
316 (unless (<= exp sb
!vm
:long-float-normal-exponent-max
)
317 (error "can't decode NaN or infinity: ~S" x
))
318 (cond ((and (zerop exp
) (zerop hi
) (zerop lo
))
319 (values 0 biased sign
))
320 ((< exp sb
!vm
:long-float-normal-exponent-min
)
321 (integer-decode-long-denorm x
))
323 (values (logior (ash hi
32) lo
) biased sign
)))))
325 ;;; Dispatch to the correct type-specific i-d-f function.
326 (defun integer-decode-float (x)
327 "Return three values:
328 1) an integer representation of the significand.
329 2) the exponent for the power of 2 that the significand must be multiplied
330 by to get the actual value. This differs from the DECODE-FLOAT exponent
331 by FLOAT-DIGITS, since the significand has been scaled to have all its
332 digits before the radix point.
333 3) -1 or 1 (i.e. the sign of the argument.)"
334 (declare (explicit-check))
335 (number-dispatch ((x float
))
337 (integer-decode-single-float x
))
339 (integer-decode-double-float x
))
342 (integer-decode-long-float x
))))
344 #!-sb-fluid
(declaim (maybe-inline decode-single-float decode-double-float
))
346 ;;; Handle the denormalized case of DECODE-SINGLE-FLOAT. We call
347 ;;; INTEGER-DECODE-SINGLE-DENORM and then make the result into a float.
348 (defun decode-single-denorm (x)
349 (declare (type single-float x
))
350 (multiple-value-bind (sig exp sign
) (integer-decode-single-denorm x
)
351 (values (make-single-float
352 (dpb sig sb
!vm
:single-float-significand-byte
353 (dpb sb
!vm
:single-float-bias
354 sb
!vm
:single-float-exponent-byte
356 (truly-the fixnum
(+ exp sb
!vm
:single-float-digits
))
359 ;;; Handle the single-float case of DECODE-FLOAT. If an infinity or NaN,
360 ;;; error. If a denorm, call d-s-DENORM to handle it.
361 (defun decode-single-float (x)
362 (declare (single-float x
))
363 (let* ((bits (single-float-bits (abs x
)))
364 (exp (ldb sb
!vm
:single-float-exponent-byte bits
))
365 (sign (float-sign x
))
366 (biased (truly-the single-float-exponent
367 (- exp sb
!vm
:single-float-bias
))))
368 (unless (<= exp sb
!vm
:single-float-normal-exponent-max
)
369 (error "can't decode NaN or infinity: ~S" x
))
371 (values 0.0f0 biased sign
))
372 ((< exp sb
!vm
:single-float-normal-exponent-min
)
373 (decode-single-denorm x
))
375 (values (make-single-float
376 (dpb sb
!vm
:single-float-bias
377 sb
!vm
:single-float-exponent-byte
381 ;;; like DECODE-SINGLE-DENORM, only doubly so
382 (defun decode-double-denorm (x)
383 (declare (double-float x
))
384 (multiple-value-bind (sig exp sign
) (integer-decode-double-denorm x
)
385 (values (make-double-float
386 (dpb (logand (ash sig -
32) (lognot sb
!vm
:double-float-hidden-bit
))
387 sb
!vm
:double-float-significand-byte
388 (dpb sb
!vm
:double-float-bias
389 sb
!vm
:double-float-exponent-byte
0))
390 (ldb (byte 32 0) sig
))
391 (truly-the fixnum
(+ exp sb
!vm
:double-float-digits
))
394 ;;; like DECODE-SINGLE-FLOAT, only doubly so
395 (defun decode-double-float (x)
396 (declare (double-float x
))
398 (hi (double-float-high-bits abs
))
399 (lo (double-float-low-bits abs
))
400 (exp (ldb sb
!vm
:double-float-exponent-byte hi
))
401 (sign (float-sign x
))
402 (biased (truly-the double-float-exponent
403 (- exp sb
!vm
:double-float-bias
))))
404 (unless (<= exp sb
!vm
:double-float-normal-exponent-max
)
405 (error "can't decode NaN or infinity: ~S" x
))
407 (values 0.0d0 biased sign
))
408 ((< exp sb
!vm
:double-float-normal-exponent-min
)
409 (decode-double-denorm x
))
411 (values (make-double-float
412 (dpb sb
!vm
:double-float-bias
413 sb
!vm
:double-float-exponent-byte hi
)
417 #!+(and long-float x86
)
418 (defun decode-long-denorm (x)
419 (declare (long-float x
))
420 (multiple-value-bind (sig exp sign
) (integer-decode-long-denorm x
)
421 (values (make-long-float sb
!vm
:long-float-bias
(ash sig -
32)
422 (ldb (byte 32 0) sig
))
423 (truly-the fixnum
(+ exp sb
!vm
:long-float-digits
))
426 #!+(and long-float x86
)
427 (defun decode-long-float (x)
428 (declare (long-float x
))
429 (let* ((hi (long-float-high-bits x
))
430 (lo (long-float-low-bits x
))
431 (exp-bits (long-float-exp-bits x
))
432 (exp (ldb sb
!vm
:long-float-exponent-byte exp-bits
))
433 (sign (if (minusp exp-bits
) -
1l0 1l0))
434 (biased (truly-the long-float-exponent
435 (- exp sb
!vm
:long-float-bias
))))
436 (unless (<= exp sb
!vm
:long-float-normal-exponent-max
)
437 (error "can't decode NaN or infinity: ~S" x
))
439 (values 0.0l0 biased sign
))
440 ((< exp sb
!vm
:long-float-normal-exponent-min
)
441 (decode-long-denorm x
))
443 (values (make-long-float
444 (dpb sb
!vm
:long-float-bias sb
!vm
:long-float-exponent-byte
450 ;;; Dispatch to the appropriate type-specific function.
451 (defun decode-float (f)
452 "Return three values:
453 1) a floating-point number representing the significand. This is always
454 between 0.5 (inclusive) and 1.0 (exclusive).
455 2) an integer representing the exponent.
456 3) -1.0 or 1.0 (i.e. the sign of the argument.)"
457 (declare (explicit-check))
458 (number-dispatch ((f float
))
460 (decode-single-float f
))
462 (decode-double-float f
))
465 (decode-long-float f
))))
469 #!-sb-fluid
(declaim (maybe-inline scale-single-float scale-double-float
))
471 ;;; Handle float scaling where the X is denormalized or the result is
472 ;;; denormalized or underflows to 0.
473 (defun scale-float-maybe-underflow (x exp
)
474 (multiple-value-bind (sig old-exp
) (integer-decode-float x
)
475 (let* ((digits (float-digits x
))
476 (new-exp (+ exp old-exp digits
478 (single-float sb
!vm
:single-float-bias
)
479 (double-float sb
!vm
:double-float-bias
))))
480 (sign (if (minusp (float-sign x
)) 1 0)))
484 (single-float sb
!vm
:single-float-normal-exponent-min
)
485 (double-float sb
!vm
:double-float-normal-exponent-min
)))
486 (when (sb!vm
:current-float-trap
:inexact
)
487 (error 'floating-point-inexact
:operation
'scale-float
488 :operands
(list x exp
)))
489 (when (sb!vm
:current-float-trap
:underflow
)
490 (error 'floating-point-underflow
:operation
'scale-float
491 :operands
(list x exp
)))
492 (let ((shift (1- new-exp
)))
493 (if (< shift
(- (1- digits
)))
496 (single-float (single-from-bits sign
0 (ash sig shift
)))
497 (double-float (double-from-bits sign
0 (ash sig shift
)))))))
500 (single-float (single-from-bits sign new-exp sig
))
501 (double-float (double-from-bits sign new-exp sig
))))))))
503 ;;; Called when scaling a float overflows, or the original float was a
504 ;;; NaN or infinity. If overflow errors are trapped, then error,
505 ;;; otherwise return the appropriate infinity. If a NaN, signal or not
507 (defun scale-float-maybe-overflow (x exp
)
509 ((float-infinity-p x
)
510 ;; Infinity is infinity, no matter how small...
513 (when (and (float-trapping-nan-p x
)
514 (sb!vm
:current-float-trap
:invalid
))
515 (error 'floating-point-invalid-operation
:operation
'scale-float
516 :operands
(list x exp
)))
519 (when (sb!vm
:current-float-trap
:overflow
)
520 (error 'floating-point-overflow
:operation
'scale-float
521 :operands
(list x exp
)))
522 (when (sb!vm
:current-float-trap
:inexact
)
523 (error 'floating-point-inexact
:operation
'scale-float
524 :operands
(list x exp
)))
528 ;; SINGLE-FLOAT-POSITIVE-INFINITY
529 (single-from-bits 0 (1+ sb
!vm
:single-float-normal-exponent-max
) 0))
531 ;; DOUBLE-FLOAT-POSITIVE-INFINITY
532 (double-from-bits 0 (1+ sb
!vm
:double-float-normal-exponent-max
) 0)))))))
534 ;;; Scale a single or double float, calling the correct over/underflow
536 (defun scale-single-float (x exp
)
537 (declare (single-float x
) (integer exp
))
540 (let* ((bits (single-float-bits x
))
541 (old-exp (ldb sb
!vm
:single-float-exponent-byte bits
))
542 (new-exp (+ old-exp exp
)))
545 ((or (< old-exp sb
!vm
:single-float-normal-exponent-min
)
546 (< new-exp sb
!vm
:single-float-normal-exponent-min
))
547 (scale-float-maybe-underflow x exp
))
548 ((or (> old-exp sb
!vm
:single-float-normal-exponent-max
)
549 (> new-exp sb
!vm
:single-float-normal-exponent-max
))
550 (scale-float-maybe-overflow x exp
))
552 (make-single-float (dpb new-exp
553 sb
!vm
:single-float-exponent-byte
555 (unsigned-byte (scale-float-maybe-overflow x exp
))
556 ((integer * 0) (scale-float-maybe-underflow x exp
))))
557 (defun scale-double-float (x exp
)
558 (declare (double-float x
) (integer exp
))
561 (let* ((hi (double-float-high-bits x
))
562 (lo (double-float-low-bits x
))
563 (old-exp (ldb sb
!vm
:double-float-exponent-byte hi
))
564 (new-exp (+ old-exp exp
)))
567 ((or (< old-exp sb
!vm
:double-float-normal-exponent-min
)
568 (< new-exp sb
!vm
:double-float-normal-exponent-min
))
569 (scale-float-maybe-underflow x exp
))
570 ((or (> old-exp sb
!vm
:double-float-normal-exponent-max
)
571 (> new-exp sb
!vm
:double-float-normal-exponent-max
))
572 (scale-float-maybe-overflow x exp
))
574 (make-double-float (dpb new-exp sb
!vm
:double-float-exponent-byte hi
)
576 (unsigned-byte (scale-float-maybe-overflow x exp
))
577 ((integer * 0) (scale-float-maybe-underflow x exp
))))
579 #!+(and x86 long-float
)
580 (defun scale-long-float (x exp
)
581 (declare (long-float x
) (integer exp
))
584 ;;; Dispatch to the correct type-specific scale-float function.
585 (defun scale-float (f ex
)
586 "Return the value (* f (expt (float 2 f) ex)), but with no unnecessary loss
587 of precision or overflow."
588 (declare (explicit-check))
589 (number-dispatch ((f float
))
591 (scale-single-float f ex
))
593 (scale-double-float f ex
))
596 (scale-long-float f ex
))))
598 ;;;; converting to/from floats
600 (defun float (number &optional
(other () otherp
))
601 "Converts any REAL to a float. If OTHER is not provided, it returns a
602 SINGLE-FLOAT if NUMBER is not already a FLOAT. If OTHER is provided, the
603 result is the same float format as OTHER."
604 (declare (explicit-check))
606 (number-dispatch ((number real
) (other float
))
607 (((foreach rational single-float double-float
#!+long-float long-float
)
608 (foreach single-float double-float
#!+long-float long-float
))
609 (coerce number
'(dispatch-type other
))))
612 (coerce number
'single-float
))))
614 (macrolet ((frob (name type
)
616 (number-dispatch ((x real
))
617 (((foreach single-float double-float
#!+long-float long-float
619 ,@(and (sb!c
::template-translates-arg-p
'%double-float
0 'word
)
623 (float-ratio x
',type
))
625 (bignum-to-float x
',type
))))))
626 (frob %single-float single-float
)
627 (frob %double-float double-float
)
629 (frob %long-float long-float
))
631 ;;; Convert a ratio to a float. We avoid any rounding error by doing an
632 ;;; integer division. Accuracy is important to preserve print-read
633 ;;; consistency, since this is ultimately how the reader reads a float. We
634 ;;; scale the numerator by a power of two until the division results in the
635 ;;; desired number of fraction bits, then do round-to-nearest.
636 (defun float-ratio (x format
)
637 (let* ((signed-num (numerator x
))
638 (plusp (plusp signed-num
))
639 (num (if plusp signed-num
(- signed-num
)))
640 (den (denominator x
))
641 (digits (float-format-digits format
))
643 (declare (fixnum digits scale
))
644 ;; Strip any trailing zeros from the denominator and move it into the scale
645 ;; factor (to minimize the size of the operands.)
646 (let ((den-twos (1- (integer-length (logxor den
(1- den
))))))
647 (declare (fixnum den-twos
))
648 (decf scale den-twos
)
649 (setq den
(ash den
(- den-twos
))))
650 ;; Guess how much we need to scale by from the magnitudes of the numerator
651 ;; and denominator. We want one extra bit for a guard bit.
652 (let* ((num-len (integer-length num
))
653 (den-len (integer-length den
))
654 (delta (- den-len num-len
))
655 (shift (1+ (the fixnum
(+ delta digits
))))
656 (shifted-num (ash num shift
)))
657 (declare (fixnum delta shift
))
659 (labels ((float-and-scale (bits)
660 (let* ((bits (ash bits -
1))
661 (len (integer-length bits
)))
662 (cond ((> len digits
)
663 (aver (= len
(the fixnum
(1+ digits
))))
664 (scale-float (floatit (ash bits -
1)) (1+ scale
)))
666 (scale-float (floatit bits
) scale
)))))
668 (let ((sign (if plusp
0 1)))
671 (single-from-bits sign sb
!vm
:single-float-bias bits
))
673 (double-from-bits sign sb
!vm
:double-float-bias bits
))
676 (long-from-bits sign sb
!vm
:long-float-bias bits
))))))
678 (multiple-value-bind (fraction-and-guard rem
)
679 (truncate shifted-num den
)
680 (let ((extra (- (integer-length fraction-and-guard
) digits
)))
681 (declare (fixnum extra
))
684 ((oddp fraction-and-guard
)
688 (if (zerop (logand fraction-and-guard
2))
690 (1+ fraction-and-guard
)))
691 (float-and-scale (1+ fraction-and-guard
)))))
693 (return (float-and-scale fraction-and-guard
)))))
694 (setq shifted-num
(ash shifted-num -
1))
697 ;;; These might be useful if we ever have a machine without float/integer
698 ;;; conversion hardware. For now, we'll use special ops that
699 ;;; uninterruptibly frob the rounding modes & do ieee round-to-integer.
702 ;; The compiler compiles a call to this when we are doing %UNARY-TRUNCATE
703 ;; and the result is known to be a fixnum. We can avoid some generic
704 ;; arithmetic in this case.
705 (defun %unary-truncate-single-float
/fixnum
(x)
706 (declare (single-float x
) (values fixnum
))
707 (locally (declare (optimize (speed 3) (safety 0)))
708 (let* ((bits (single-float-bits x
))
709 (exp (ldb sb
!vm
:single-float-exponent-byte bits
))
710 (frac (logior (ldb sb
!vm
:single-float-significand-byte bits
)
711 sb
!vm
:single-float-hidden-bit
))
712 (shift (- exp sb
!vm
:single-float-digits sb
!vm
:single-float-bias
)))
713 (when (> exp sb
!vm
:single-float-normal-exponent-max
)
714 (error 'floating-point-invalid-operation
:operator
'truncate
716 (if (<= shift
(- sb
!vm
:single-float-digits
))
718 (let ((res (ash frac shift
)))
719 (declare (type (unsigned-byte 31) res
))
723 ;; Double-float version of this operation (see above single op).
724 (defun %unary-truncate-double-float
/fixnum
(x)
725 (declare (double-float x
) (values fixnum
))
726 (locally (declare (optimize (speed 3) (safety 0)))
727 (let* ((hi-bits (double-float-high-bits x
))
728 (exp (ldb sb
!vm
:double-float-exponent-byte hi-bits
))
729 (frac (logior (ldb sb
!vm
:double-float-significand-byte hi-bits
)
730 sb
!vm
:double-float-hidden-bit
))
731 (shift (- exp
(- sb
!vm
:double-float-digits sb
!vm
:n-word-bits
)
732 sb
!vm
:double-float-bias
)))
733 (when (> exp sb
!vm
:double-float-normal-exponent-max
)
734 (error 'floating-point-invalid-operation
:operator
'truncate
736 (if (<= shift
(- sb
!vm
:n-word-bits sb
!vm
:double-float-digits
))
738 (let* ((res-hi (ash frac shift
))
739 (res (if (plusp shift
)
742 (ash (double-float-low-bits x
)
743 (- shift sb
!vm
:n-word-bits
))))
745 (declare (type (unsigned-byte 31) res-hi res
))
750 ;;; This function is called when we are doing a truncate without any funky
751 ;;; divisor, i.e. converting a float or ratio to an integer. Note that we do
752 ;;; *not* return the second value of truncate, so it must be computed by the
753 ;;; caller if needed.
755 ;;; In the float case, we pick off small arguments so that compiler
756 ;;; can use special-case operations. We use an exclusive test, since
757 ;;; (due to round-off error), (float most-positive-fixnum) is likely
758 ;;; to be equal to (1+ most-positive-fixnum). An exclusive test is
759 ;;; good enough, because most-positive-fixnum will be one less than a
760 ;;; power of two, and that power of two will be exactly representable
761 ;;; as a float (at least until we get 128-bit fixnums).
762 (defun %unary-truncate
(number)
763 (number-dispatch ((number real
))
765 ((ratio) (values (truncate (numerator number
) (denominator number
))))
766 (((foreach single-float double-float
#!+long-float long-float
))
767 (if (< (float most-negative-fixnum number
)
769 (float most-positive-fixnum number
))
770 (truly-the fixnum
(%unary-truncate number
))
771 (multiple-value-bind (bits exp
) (integer-decode-float number
)
772 (let ((res (ash bits exp
)))
777 ;;; Specialized versions for floats.
778 (macrolet ((def (type name
)
779 `(defun ,name
(number)
780 (if (< ,(coerce sb
!xc
:most-negative-fixnum type
)
782 ,(coerce sb
!xc
:most-positive-fixnum type
))
783 (truly-the fixnum
(,name number
))
784 ;; General -- slow -- case.
785 (multiple-value-bind (bits exp
) (integer-decode-float number
)
786 (let ((res (ash bits exp
)))
790 (def single-float %unary-truncate
/single-float
)
791 (def double-float %unary-truncate
/double-float
)
793 (def double-float %unary-truncate
/long-float
))
795 ;;; Similar to %UNARY-TRUNCATE, but rounds to the nearest integer. If we
796 ;;; can't use the round primitive, then we do our own round-to-nearest on the
797 ;;; result of i-d-f. [Note that this rounding will really only happen with
798 ;;; double floats, since the whole single-float fraction will fit in a fixnum,
799 ;;; so all single-floats larger than most-positive-fixnum can be precisely
800 ;;; represented by an integer.]
801 (defun %unary-round
(number)
802 (number-dispatch ((number real
))
804 ((ratio) (values (round (numerator number
) (denominator number
))))
805 (((foreach single-float double-float
#!+long-float long-float
))
806 (if (< (float most-negative-fixnum number
)
808 (float most-positive-fixnum number
))
809 (truly-the fixnum
(%unary-round number
))
810 (multiple-value-bind (bits exp
) (integer-decode-float number
)
811 (let* ((shifted (ash bits exp
))
812 (rounded (if (minusp exp
)
813 (let ((fractional-bits (logand bits
(lognot (ash -
1 (- exp
)))))
814 (0.5bits
(ash 1 (- -
1 exp
))))
816 ((> fractional-bits
0.5bits
) (1+ shifted
))
817 ((< fractional-bits
0.5bits
) shifted
)
818 (t (if (oddp shifted
) (1+ shifted
) shifted
))))
824 (defun %unary-ftruncate
(number)
825 (number-dispatch ((number real
))
826 ((integer) (float number
))
827 ((ratio) (float (truncate (numerator number
) (denominator number
))))
828 (((foreach single-float double-float
#!+long-float long-float
))
829 (%unary-ftruncate number
))))
832 "RATIONAL produces a rational number for any real numeric argument. This is
833 more efficient than RATIONALIZE, but it assumes that floating-point is
834 completely accurate, giving a result that isn't as pretty."
835 (declare (explicit-check))
836 (number-dispatch ((x real
))
837 (((foreach single-float double-float
#!+long-float long-float
))
838 (multiple-value-bind (bits exp
) (integer-decode-float x
)
841 (let ((int (if (minusp x
) (- bits
) bits
)))
843 (integer-/-integer int
(ash 1 (- exp
)))
847 ;;; This algorithm for RATIONALIZE, due to Bruno Haible, is included
850 ;;; Algorithm (recursively presented):
851 ;;; If x is a rational number, return x.
852 ;;; If x = 0.0, return 0.
853 ;;; If x < 0.0, return (- (rationalize (- x))).
855 ;;; Call (integer-decode-float x). It returns a m,e,s=1 (mantissa,
857 ;;; If m = 0 or e >= 0: return x = m*2^e.
858 ;;; Search a rational number between a = (m-1/2)*2^e and b = (m+1/2)*2^e
859 ;;; with smallest possible numerator and denominator.
860 ;;; Note 1: If m is a power of 2, we ought to take a = (m-1/4)*2^e.
861 ;;; But in this case the result will be x itself anyway, regardless of
862 ;;; the choice of a. Therefore we can simply ignore this case.
863 ;;; Note 2: At first, we need to consider the closed interval [a,b].
864 ;;; but since a and b have the denominator 2^(|e|+1) whereas x itself
865 ;;; has a denominator <= 2^|e|, we can restrict the seach to the open
867 ;;; So, for given a and b (0 < a < b) we are searching a rational number
868 ;;; y with a <= y <= b.
869 ;;; Recursive algorithm fraction_between(a,b):
872 ;;; then return c ; because a <= c < b, c integer
874 ;;; ; a is not integer (otherwise we would have had c = a < b)
875 ;;; k := c-1 ; k = floor(a), k < a < b <= k+1
876 ;;; return y = k + 1/fraction_between(1/(b-k), 1/(a-k))
877 ;;; ; note 1 <= 1/(b-k) < 1/(a-k)
879 ;;; You can see that we are actually computing a continued fraction expansion.
881 ;;; Algorithm (iterative):
882 ;;; If x is rational, return x.
883 ;;; Call (integer-decode-float x). It returns a m,e,s (mantissa,
885 ;;; If m = 0 or e >= 0, return m*2^e*s. (This includes the case x = 0.0.)
886 ;;; Create rational numbers a := (2*m-1)*2^(e-1) and b := (2*m+1)*2^(e-1)
887 ;;; (positive and already in lowest terms because the denominator is a
888 ;;; power of two and the numerator is odd).
889 ;;; Start a continued fraction expansion
890 ;;; p[-1] := 0, p[0] := 1, q[-1] := 1, q[0] := 0, i := 0.
894 ;;; then k := c-1, partial_quotient(k), (a,b) := (1/(b-k),1/(a-k)),
896 ;;; finally partial_quotient(c).
897 ;;; Here partial_quotient(c) denotes the iteration
898 ;;; i := i+1, p[i] := c*p[i-1]+p[i-2], q[i] := c*q[i-1]+q[i-2].
899 ;;; At the end, return s * (p[i]/q[i]).
900 ;;; This rational number is already in lowest terms because
901 ;;; p[i]*q[i-1]-p[i-1]*q[i] = (-1)^i.
904 ;;; Hardy, Wright: An introduction to number theory
906 ;;; <http://modular.fas.harvard.edu/edu/Fall2001/124/lectures/lecture17/lecture17/>
907 ;;; <http://modular.fas.harvard.edu/edu/Fall2001/124/lectures/lecture17/lecture18/>
909 (defun rationalize (x)
910 "Converts any REAL to a RATIONAL. Floats are converted to a simple rational
911 representation exploiting the assumption that floats are only accurate to
912 their precision. RATIONALIZE (and also RATIONAL) preserve the invariant:
913 (= x (float (rationalize x) x))"
914 (declare (explicit-check))
915 (number-dispatch ((x real
))
916 (((foreach single-float double-float
#!+long-float long-float
))
917 ;; This is a fairly straigtforward implementation of the
918 ;; iterative algorithm above.
919 (multiple-value-bind (frac expo sign
)
920 (integer-decode-float x
)
921 (cond ((or (zerop frac
) (>= expo
0))
926 ;; expo < 0 and (2*m-1) and (2*m+1) are coprime to 2^(1-e),
927 ;; so build the fraction up immediately, without having to do
929 (let ((a (build-ratio (- (* 2 frac
) 1) (ash 1 (- 1 expo
))))
930 (b (build-ratio (+ (* 2 frac
) 1) (ash 1 (- 1 expo
))))
935 (do ((c (ceiling a
) (ceiling a
)))
937 (let ((top (+ (* c p1
) p0
))
938 (bot (+ (* c q1
) q0
)))
939 (build-ratio (if (minusp sign
)
945 (q2 (+ (* k q1
) q0
)))