x86-64: put vector widetag and maybe length w/byte-sized store
[sbcl.git] / tests / float.pure.lisp
blob2516867f61b5cdb6e31d43f4c7c19290162c14b5
1 ;;;; floating-point-related tests with no side effects
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (cl:in-package :cl-user)
16 (dolist (ifnis (list (cons single-float-positive-infinity
17 single-float-negative-infinity)
18 (cons double-float-positive-infinity
19 double-float-negative-infinity)))
20 (destructuring-bind (+ifni . -ifni) ifnis
21 (assert (= (* +ifni 1) +ifni))
22 (assert (= (* +ifni -0.1) -ifni))
23 (assert (= (+ +ifni -0.1) +ifni))
24 (assert (= (- +ifni -0.1) +ifni))
25 (assert (= (sqrt +ifni) +ifni))
26 (assert (= (* -ifni -14) +ifni))
27 (assert (= (/ -ifni 0.1) -ifni))
28 (assert (= (/ -ifni 100/3) -ifni))
29 (assert (not (= +ifni -ifni)))
30 (assert (= -ifni -ifni))
31 (assert (not (= +ifni 100/3)))
32 (assert (not (= -ifni -1.0 -ifni)))
33 (assert (not (= -ifni -17/02 -ifni)))
34 (assert (< -ifni +ifni))
35 (assert (not (< +ifni 100)))
36 (assert (not (< +ifni 100.0)))
37 (assert (not (< +ifni -ifni)))
38 (assert (< 100 +ifni))
39 (assert (< 100.0 +ifni))
40 (assert (>= 100 -ifni))
41 (assert (not (<= 6/7 (* 3 -ifni))))
42 (assert (not (> +ifni +ifni)))))
44 ;;; ANSI: FLOAT-RADIX should signal an error if its argument is not a
45 ;;; float.
46 ;;;
47 ;;; (Peter Van Eynde's ansi-test suite caught this, and Eric Marsden
48 ;;; reported a fix for CMU CL, which was ported to sbcl-0.6.12.35.)
49 (assert (typep (nth-value 1 (ignore-errors (float-radix "notfloat")))
50 'type-error))
52 (assert (typep (nth-value 1 (ignore-errors
53 (funcall (fdefinition 'float-radix) "notfloat")))
54 'type-error))
56 ;;; Before 0.8.2.14 the cross compiler failed to work with
57 ;;; denormalized numbers
58 (when (subtypep 'single-float 'short-float)
59 (assert (eql least-positive-single-float least-positive-short-float)))
61 ;;; bug found by Paul Dietz: FFLOOR and similar did not work for integers
62 (let ((tests '(((ffloor -8 3) (-3.0 1))
63 ((fround -8 3) (-3.0 1))
64 ((ftruncate -8 3) (-2.0 -2))
65 ((fceiling -8 3) (-2.0 -2)))))
66 (loop for (exp res) in tests
67 for real-res = (multiple-value-list (eval exp))
68 do (assert (equal real-res res))))
70 ;;; bug 45b reported by PVE
71 (dolist (type '(short single double long))
72 (dolist (sign '(positive negative))
73 (let* ((name (find-symbol (format nil "LEAST-~A-~A-FLOAT"
74 sign type)
75 :cl))
76 (value (symbol-value name)))
77 (assert (zerop (/ value 2))))))
79 ;;; bug found by Paul Dietz: bad rounding on small floats
80 (assert (= (fround least-positive-short-float least-positive-short-float) 1.0))
82 ;;; bug found by Peter Seibel: scale-float was only accepting float
83 ;;; exponents, when it should accept all integers. (also bug #269)
84 (assert (= (multiple-value-bind (significand expt sign)
85 (integer-decode-float least-positive-double-float)
86 (* (scale-float (float significand 0.0d0) expt) sign))
87 least-positive-double-float))
88 (assert (= (multiple-value-bind (significand expt sign)
89 (decode-float least-positive-double-float)
90 (* (scale-float significand expt) sign))
91 least-positive-double-float))
92 (assert (= 0.0 (scale-float 1.0 most-negative-fixnum)))
93 (assert (= 0.0d0 (scale-float 1.0d0 (1- most-negative-fixnum))))
95 (with-test (:name (:scale-float-overflow :bug-372)
96 :fails-on '(and :darwin :ppc)) ;; bug 372
97 (assert-error (scale-float 1.0 most-positive-fixnum)
98 floating-point-overflow)
99 (assert-error (scale-float 1.0d0 (1+ most-positive-fixnum))
100 floating-point-overflow))
102 ;;; bug found by jsnell when nfroyd tried to implement better LOGAND
103 ;;; type derivation.
104 (assert (= (integer-decode-float (coerce -1756510900000000000
105 'single-float))
106 12780299))
108 ;;; MISC.564: no out-of-line %ATAN2 for constant folding
109 (assert (typep
110 (funcall
111 (compile
113 '(lambda (p1)
114 (declare (optimize (speed 3) (safety 2) (debug 3) (space 0))
115 (type complex p1))
116 (phase (the (eql #c(1.0d0 2.0d0)) p1))))
117 #c(1.0d0 2.0d0))
118 'double-float))
120 ;;; More out of line functions (%COS, %SIN, %TAN) for constant folding,
121 ;;; reported by Mika Pihlajamäki
122 (funcall (compile nil '(lambda () (cos (tan (round 0))))))
123 (funcall (compile nil '(lambda () (sin (tan (round 0))))))
124 (funcall (compile nil '(lambda () (tan (tan (round 0))))))
126 (with-test (:name (:addition-overflow :bug-372)
127 :fails-on '(or (and :ppc :openbsd)
128 (and :ppc :darwin)
129 (and :x86 :netbsd)))
130 (assert (typep (nth-value
132 (ignore-errors
133 (sb-sys:without-interrupts
134 (sb-int:set-floating-point-modes :current-exceptions nil
135 :accrued-exceptions nil)
136 (loop repeat 2 summing most-positive-double-float)
137 (sleep 2))))
138 'floating-point-overflow)))
140 ;; This is the same test as above. Even if the above copy passes,
141 ;; this copy will fail if SIGFPE handling ends up clearing the FPU
142 ;; control word, which can happen if the kernel clears the FPU control
143 ;; (a reasonable thing for it to do) and the runtime fails to
144 ;; compensate for this (see RESTORE_FP_CONTROL_WORD in interrupt.c).
145 ;; Note that this only works when running float.pure.lisp alone, as
146 ;; the preceeding "pure" test files aren't as free of side effects as
147 ;; we might like.
148 (with-test (:name (:addition-overflow :bug-372 :take-2)
149 :fails-on '(or (and :ppc :openbsd)
150 (and :ppc :darwin)
151 (and :x86 :netbsd)))
152 (assert (typep (nth-value
154 (ignore-errors
155 (sb-sys:without-interrupts
156 (sb-int:set-floating-point-modes :current-exceptions nil
157 :accrued-exceptions nil)
158 (loop repeat 2 summing most-positive-double-float)
159 (sleep 2))))
160 'floating-point-overflow)))
162 ;;; On x86-64 generating complex floats on the stack failed an aver in
163 ;;; the compiler if the stack slot was the same as the one containing
164 ;;; the real part of the complex. The following expression was able to
165 ;;; trigger this in 0.9.5.62.
166 (with-test (:name :complex-float-stack)
167 (dolist (type '((complex double-float)
168 (complex single-float)))
169 (compile nil
170 `(lambda (x0 x1 x2 x3 x4 x5 x6 x7)
171 (declare (type ,type x0 x1 x2 x3 x4 x5 x6 x7))
172 (let ((x0 (+ x0 x0))
173 (x1 (+ x1 x1))
174 (x2 (+ x2 x2))
175 (x3 (+ x3 x3))
176 (x4 (+ x4 x4))
177 (x5 (+ x5 x5))
178 (x6 (+ x6 x6))
179 (x7 (+ x7 x7)))
180 (* (+ x0 x1 x2 x3) (+ x4 x5 x6 x7)
181 (+ x0 x2 x4 x6) (+ x1 x3 x5 x7)
182 (+ x0 x3 x4 x7) (+ x1 x2 x5 x6)
183 (+ x0 x1 x6 x7) (+ x2 x3 x4 x5)))))))
186 (with-test (:name :nan-comparisons
187 :fails-on '(or :sparc))
188 (sb-int:with-float-traps-masked (:invalid)
189 (macrolet ((test (form)
190 (let ((nform (subst '(/ 0.0 0.0) 'nan form)))
191 `(progn
192 (assert (eval ',nform))
193 (assert (eval `(let ((nan (/ 0.0 0.0)))
194 ,',form)))
195 (assert (funcall
196 (compile nil `(lambda () ,',nform))))
197 (assert (funcall
198 (compile nil `(lambda (nan) ,',form))
199 (/ 0.0 0.0)))))))
200 (test (/= nan nan))
201 (test (/= nan nan nan))
202 (test (/= 1.0 nan 2.0 nan))
203 (test (/= nan 1.0 2.0 nan))
204 (test (not (= nan 1.0)))
205 (test (not (= nan nan)))
206 (test (not (= nan nan nan)))
207 (test (not (= 1.0 nan)))
208 (test (not (= nan 1.0)))
209 (test (not (= 1.0 1.0 nan)))
210 (test (not (= 1.0 nan 1.0)))
211 (test (not (= nan 1.0 1.0)))
212 (test (not (>= nan nan)))
213 (test (not (>= nan 1.0)))
214 (test (not (>= 1.0 nan)))
215 (test (not (>= 1.0 nan 0.0)))
216 (test (not (>= 1.0 0.0 nan)))
217 (test (not (>= nan 1.0 0.0)))
218 (test (not (<= nan nan)))
219 (test (not (<= nan 1.0)))
220 (test (not (<= 1.0 nan)))
221 (test (not (<= 1.0 nan 2.0)))
222 (test (not (<= 1.0 2.0 nan)))
223 (test (not (<= nan 1.0 2.0)))
224 (test (not (< nan nan)))
225 (test (not (< -1.0 nan)))
226 (test (not (< nan 1.0)))
227 (test (not (> nan nan)))
228 (test (not (> -1.0 nan)))
229 (test (not (> nan 1.0))))))
231 (with-test (:name :log-int/double-accuracy)
232 ;; we used to use single precision for intermediate results
233 (assert (eql 2567.6046442221327d0
234 (log (loop for n from 1 to 1000 for f = 1 then (* f n)
235 finally (return f))
236 10d0)))
237 ;; both ways
238 (assert (eql (log 123123123.0d0 10) (log 123123123 10.0d0))))
240 (with-test (:name :log-base-zero-return-type)
241 (assert (eql 0.0f0 (log 123 (eval 0))))
242 (assert (eql 0.0d0 (log 123.0d0 (eval 0))))
243 (assert (eql 0.0d0 (log 123 (eval 0.0d0))))
244 (let ((f (compile nil '(lambda (x y)
245 (declare (optimize speed))
246 (etypecase x
247 (single-float
248 (etypecase y
249 (single-float (log x y))
250 (double-float (log x y))))
251 (double-float
252 (etypecase y
253 (single-float (log x y))
254 (double-float (log x y)))))))))
255 (assert (eql 0.0f0 (funcall f 123.0 0.0)))
256 (assert (eql 0.0d0 (funcall f 123.0d0 0.0)))
257 (assert (eql 0.0d0 (funcall f 123.0d0 0.0d0)))
258 (assert (eql 0.0d0 (funcall f 123.0 0.0d0)))))
260 ;; Bug reported by Eric Marsden on July 15 2009. The compiler
261 ;; used not to constant fold calls with arguments of type
262 ;; (EQL foo).
263 (with-test (:name :eql-type-constant-fold)
264 (assert (equal '(FUNCTION (T) (VALUES (MEMBER T) &OPTIONAL))
265 (sb-kernel:%simple-fun-type
266 (compile nil `(lambda (x)
267 (eql #c(1.0 2.0)
268 (the (eql #c(1.0 2.0))
269 x))))))))
271 ;; Leakage from the host could result in wrong values for truncation.
272 (with-test (:name :truncate)
273 (assert (plusp (sb-kernel:%unary-truncate/single-float (expt 2f0 33))))
274 (assert (plusp (sb-kernel:%unary-truncate/double-float (expt 2d0 33))))
275 ;; That'd be one strange host, but just in case
276 (assert (plusp (sb-kernel:%unary-truncate/single-float (expt 2f0 65))))
277 (assert (plusp (sb-kernel:%unary-truncate/double-float (expt 2d0 65)))))
279 ;; On x86-64, we sometimes forgot to clear the higher order bits of the
280 ;; destination register before using it with an instruction that doesn't
281 ;; clear the (unused) high order bits. Suspect instructions are operations
282 ;; with only one operand: for everything else, the destination has already
283 ;; been loaded with a value, making it safe (by induction).
285 ;; The tests are extremely brittle and could be broken by any number of
286 ;; back- or front-end optimisations. We should just keep the issue above
287 ;; in mind at all times when working with SSE or similar instruction sets.
289 ;; Run only on x86/x86-64m as no other platforms have SB-VM::TOUCH-OBJECT.
290 #-interpreter
291 (macrolet ((with-pinned-floats ((count type &rest names) &body body)
292 "Force COUNT float values to be kept live (and hopefully in registers),
293 fill a temporary register with noise, and execute BODY."
294 ;; KLUDGE: SB-VM is locked, and non-x86oids don't have
295 ;; SB-VM::TOUCH-OBJECT. Don't even READ this body on
296 ;; other platforms.
297 #-(or x86 x86-64)
298 (declare (ignore count type names body))
299 #+(or x86 x86-64)
300 (let ((dummy (loop repeat count
301 collect (or (pop names)
302 (gensym "TEMP")))))
303 `(let ,(loop for i downfrom -1
304 for var in dummy
305 for j = (coerce i type)
306 collect
307 `(,var ,(complex j j))) ; we don't actually need that, but
308 (declare (type (complex ,type) ,@dummy)) ; future-proofing can't hurt
309 ,@(loop for var in dummy
310 for i upfrom 0
311 collect `(setf ,var ,(complex i (coerce i type))))
312 (multiple-value-prog1
313 (progn
314 (let ((x ,(complex 1d0 1d0)))
315 (declare (type (complex double-float) x))
316 (setf x ,(complex most-positive-fixnum (float most-positive-fixnum 1d0)))
317 (sb-vm::touch-object x))
318 (locally ,@body))
319 ,@(loop for var in dummy
320 collect `(sb-vm::touch-object ,var)))))))
321 (with-test (:name :clear-sqrtsd :skipped-on '(not (or :x86 :x86-64)))
322 (flet ((test-sqrtsd (float)
323 (declare (optimize speed (safety 1))
324 (type (double-float (0d0)) float))
325 (with-pinned-floats (14 double-float x0)
326 (let ((x (sqrt float)))
327 (values (+ x x0) float)))))
328 (declare (notinline test-sqrtsd))
329 (assert (zerop (imagpart (test-sqrtsd 4d0))))))
331 (with-test (:name :clear-sqrtsd-single :skipped-on '(not (or :x86 :x86-64)))
332 (flet ((test-sqrtsd-float (float)
333 (declare (optimize speed (safety 1))
334 (type (single-float (0f0)) float))
335 (with-pinned-floats (14 single-float x0)
336 (let ((x (sqrt float)))
337 (values (+ x x0) float)))))
338 (declare (notinline test-sqrtsd-float))
339 (assert (zerop (imagpart (test-sqrtsd-float 4f0))))))
341 (with-test (:name :clear-cvtss2sd :skipped-on '(not (or :x86 :x86-64)))
342 (flet ((test-cvtss2sd (float)
343 (declare (optimize speed (safety 1))
344 (type single-float float))
345 (with-pinned-floats (14 double-float x0)
346 (let ((x (float float 0d0)))
347 (values (+ x x0) (+ 1e0 float))))))
348 (declare (notinline test-cvtss2sd))
349 (assert (zerop (imagpart (test-cvtss2sd 1f0))))))
351 (with-test (:name :clear-cvtsd2ss :skipped-on '(not (or :x86 :x86-64)))
352 (flet ((test-cvtsd2ss (float)
353 (declare (optimize speed (safety 1))
354 (type double-float float))
355 (with-pinned-floats (14 single-float x0)
356 (let ((x (float float 1e0)))
357 (values (+ x x0) (+ 1d0 float))))))
358 (declare (notinline test-cvtsd2ss))
359 (assert (zerop (imagpart (test-cvtsd2ss 4d0))))))
361 (with-test (:name :clear-cvtsi2sd :skipped-on '(not (or :x86 :x86-64)))
362 (flet ((test-cvtsi2sd (int)
363 (declare (optimize speed (safety 0))
364 (type (unsigned-byte 10) int))
365 (with-pinned-floats (15 double-float x0)
366 (+ (float int 0d0) x0))))
367 (declare (notinline test-cvtsi2sd))
368 (assert (zerop (imagpart (test-cvtsi2sd 4))))))
370 (with-test (:name :clear-cvtsi2ss :skipped-on '(not (or :x86 :x86-64)))
371 (flet ((test-cvtsi2ss (int)
372 (declare (optimize speed (safety 0))
373 (type (unsigned-byte 10) int))
374 (with-pinned-floats (15 single-float x0)
375 (+ (float int 0e0) x0))))
376 (declare (notinline test-cvtsi2ss))
377 (assert (zerop (imagpart (test-cvtsi2ss 4)))))))
379 (with-test (:name :round-to-bignum)
380 (assert (= (round 1073741822.3d0) 1073741822))
381 (assert (= (round 1073741822.5d0) 1073741822))
382 (assert (= (round 1073741822.7d0) 1073741823))
383 (assert (= (round 1073741823.3d0) 1073741823))
384 (assert (= (round 1073741823.5d0) 1073741824))
385 (assert (= (round 1073741823.7d0) 1073741824)))
387 (with-test (:name :round-single-to-bignum)
388 (assert (= (round 1e14) 100000000376832))
389 (assert (= (round 1e19) 9999999980506447872)))
391 (with-test (:name :scaled-%hypot)
392 (assert (<= (abs (complex most-positive-double-float 1d0))
393 (1+ most-positive-double-float))))
395 ;; On x86-64, MAKE-SINGLE-FLOAT with a negative argument used to set
396 ;; bits 32-63 of the XMM register to 1, breaking the invariant that
397 ;; unused parts of XMM registers are always zero. This could become
398 ;; visible as a QNaN in the imaginary part when next using the register
399 ;; in a (COMPLEX SINGLE-FLOAT) operation.
400 (with-test (:name :make-single-float-clear-imagpart)
401 (let ((f (checked-compile
402 '(lambda (x)
403 (declare (optimize speed))
404 (= #c(1.0f0 2.0f0)
405 (+ #c(3.0f0 2.0f0)
406 (sb-kernel:make-single-float x))))))
407 (bits (sb-kernel:single-float-bits -2.0f0)))
408 (assert (< bits 0)) ; Make sure the test is fit for purpose.
409 (assert (funcall f bits))))
411 (with-test (:name :negative-zero-derivation)
412 (assert (not
413 (funcall (checked-compile
414 '(lambda (exponent)
415 (declare ((integer 0 1) exponent))
416 (eql 0d0 (scale-float -0.0d0 exponent))))
417 0))))