1 ;;;; portable implementations or stubs for nonportable floating point
2 ;;;; things, useful for building Python as a cross-compiler when
3 ;;;; running under an ordinary ANSI Common Lisp implementation
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.
14 (in-package "SB!IMPL")
16 ;;; There seems to be no portable way to mask float traps, but we
17 ;;; shouldn't encounter any float traps when cross-compiling SBCL
18 ;;; itself, anyway, so we just make this a no-op.
19 (defmacro sb!vm::with-float-traps-masked (traps &body body)
20 (declare (ignore traps))
21 ;; FIXME: should become STYLE-WARNING?
22 (format *error-output*
23 "~&(can't portably mask float traps, proceeding anyway)~%")
26 ;;; a helper function for DOUBLE-FLOAT-FOO-BITS functions
28 ;;; Return the low N bits of X as a signed N-bit value.
29 (defun mask-and-sign-extend (x n)
31 (let* ((high-bit (ash 1 (1- n)))
32 (mask (1- (ash high-bit 1)))
33 (uresult (logand mask x)))
34 (if (zerop (logand uresult high-bit))
37 (logand -1 (lognot mask))))))
39 ;;; portable implementations of SINGLE-FLOAT-BITS,
40 ;;; DOUBLE-FLOAT-LOW-BITS, and DOUBLE-FLOAT-HIGH-BITS
42 ;;; KLUDGE: These will fail if the target's floating point isn't IEEE,
43 ;;; and so I'd be more comfortable if there were an assertion
44 ;;; "target's floating point is IEEE" in the code, but I can't see how
47 ;;; KLUDGE: It's sort of weird that these functions return signed
48 ;;; 32-bit values instead of unsigned 32-bit values. This is the way
49 ;;; that the CMU CL machine-dependent functions behaved, and I've
50 ;;; copied that behavior, but it seems to me that it'd be more
51 ;;; idiomatic to return unsigned 32-bit values. Maybe someday the
52 ;;; machine-dependent functions could be tweaked to return unsigned
54 (defun single-float-bits (x)
55 (declare (type single-float x))
56 (assert (= (float-radix x) 2))
58 0 ; known property of IEEE floating point: 0.0 is represented as 0.
59 (multiple-value-bind (lisp-significand lisp-exponent lisp-sign)
60 (integer-decode-float x)
61 (assert (plusp lisp-significand))
62 ;; Calculate IEEE-style fields from Common-Lisp-style fields.
64 ;; KLUDGE: This code was written from my foggy memory of what IEEE
65 ;; format looks like, augmented by some experiments with
66 ;; the existing implementation of SINGLE-FLOAT-BITS, and what
67 ;; I found floating around on the net at
68 ;; <http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html>,
69 ;; <http://rodin.cs.uh.edu/~johnson2/ieee.html>,
71 ;; <http://www.ttu.ee/sidu/cas/IEEE_Floating.htm>.
72 ;; And beyond the probable sheer flakiness of the code, all the bare
73 ;; numbers floating around here are sort of ugly, too. -- WHN 19990711
74 (let* ((significand lisp-significand)
75 (exponent (+ lisp-exponent 23 127))
77 (if (plusp exponent) ; if not obviously denormalized
80 (cond (;; ordinary termination case
81 (>= significand (expt 2 23))
82 (assert (< 0 significand (expt 2 24)))
83 ;; Exponent 0 is reserved for denormalized numbers,
84 ;; and 255 is reserved for specials like NaN.
85 (assert (< 0 exponent 255))
86 (return (logior (ash exponent 23)
89 (;; special termination case, denormalized float number
91 ;; Denormalized numbers have exponent one greater than
92 ;; the exponent field.
93 (return (ash significand -1)))
95 ;; Shift as necessary to set bit 24 of significand.
96 (setf significand (ash significand 1)
97 exponent (1- exponent)))))
100 ;; Denormalized numbers have exponent one greater than the
102 (ash significand -1))
103 (unless (zerop (logand significand 1))
104 (warn "denormalized SINGLE-FLOAT-BITS ~S losing bits" x))
105 (setf significand (ash significand -1)
106 exponent (1+ exponent))))))
109 (-1 (logior unsigned-result (- (expt 2 31)))))))))
110 (defun double-float-bits (x)
111 (declare (type double-float x))
112 (assert (= (float-radix x) 2))
114 0 ; known property of IEEE floating point: 0.0d0 is represented as 0.
115 ;; KLUDGE: As per comments in SINGLE-FLOAT-BITS, above.
116 (multiple-value-bind (lisp-significand lisp-exponent lisp-sign)
117 (integer-decode-float x)
118 (assert (plusp lisp-significand))
119 (let* ((significand lisp-significand)
120 (exponent (+ lisp-exponent 52 1023))
122 (if (plusp exponent) ; if not obviously denormalized
125 (cond (;; ordinary termination case
126 (>= significand (expt 2 52))
127 (assert (< 0 significand (expt 2 53)))
128 ;; Exponent 0 is reserved for denormalized numbers,
129 ;; and 2047 is reserved for specials like NaN.
130 (assert (< 0 exponent 2047))
131 (return (logior (ash exponent 52)
134 (;; special termination case, denormalized float number
136 ;; Denormalized numbers have exponent one greater than
137 ;; the exponent field.
138 (return (ash significand -1)))
140 ;; Shift as necessary to set bit 53 of significand.
141 (setf significand (ash significand 1)
142 exponent (1- exponent)))))
145 ;; Denormalized numbers have exponent one greater than the
147 (ash significand -1))
148 (unless (zerop (logand significand 1))
149 (warn "denormalized SINGLE-FLOAT-BITS ~S losing bits" x))
150 (setf significand (ash significand -1)
151 exponent (1+ exponent))))))
154 (-1 (logior unsigned-result (- (expt 2 63)))))))))
155 (defun double-float-low-bits (x)
156 (declare (type double-float x))
159 ;; Unlike DOUBLE-FLOAT-HIGH-BITS or SINGLE-FLOAT-BITS, the CMU CL
160 ;; DOUBLE-FLOAT-LOW-BITS seems to return a unsigned value, not a signed
162 (logand #xffffffff (double-float-bits x))))
163 (defun double-float-high-bits (x)
164 (declare (type double-float x))
167 (mask-and-sign-extend (ash (double-float-bits x) -32) 32)))
169 ;;; KLUDGE: These functions will blow up on any cross-compilation
170 ;;; host Lisp which has less floating point precision than the target
171 ;;; Lisp. In practice, this may not be a major problem: IEEE
172 ;;; floating point arithmetic is so common these days that most
173 ;;; cross-compilation host Lisps are likely to have exactly the same
174 ;;; floating point precision as the target Lisp. If it turns out to be
175 ;;; a problem, there are possible workarounds involving portable
176 ;;; representations for target floating point numbers, like
177 ;;; (DEFSTRUCT TARGET-SINGLE-FLOAT
178 ;;; (SIGN (REQUIRED-ARGUMENT) :TYPE BIT)
179 ;;; (EXPONENT (REQUIRED-ARGUMENT) :TYPE UNSIGNED-BYTE)
180 ;;; (MANTISSA (REQUIRED-ARGUMENT) :TYPE UNSIGNED-BYTE))
181 ;;; with some sort of MAKE-LOAD-FORM-ish magic to cause them to be
182 ;;; written out in the appropriate target format. (And yes, those
183 ;;; workarounds *do* look messy to me, which is why I just went
184 ;;; with this quick kludge instead.) -- WHN 19990711
185 (defun make-single-float (bits)
186 (if (zerop bits) ; IEEE float special case
188 (let ((sign (ecase (ldb (byte 1 31) bits)
191 (expt (- (ldb (byte 8 23) bits) 127))
192 (mant (* (logior (ldb (byte 23 0) bits)
195 (* sign (expt 2.0 expt) mant))))
196 (defun make-double-float (hi lo)
197 (if (and (zerop hi) (zerop lo)) ; IEEE float special case
199 (let* ((bits (logior (ash hi 32) lo))
200 (sign (ecase (ldb (byte 1 63) bits)
203 (expt (- (ldb (byte 11 52) bits) 1023))
204 (mant (* (logior (ldb (byte 52 0) bits)
207 (* sign (expt 2.0d0 expt) mant))))