1.0.38.12: Fix FP traps on PPC/Linux.
[sbcl.git] / tests / float.pure.lisp
1 ;;;; floating-point-related tests with no side effects
2
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.
13
14 (cl:in-package :cl-user)
15
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)))))
43
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))
51
52 (assert (typep (nth-value 1 (ignore-errors
53                               (funcall (fdefinition 'float-radix) "notfloat")))
54                'type-error))
55
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)))
60
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))))
69
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))))))
78
79 ;;; bug found by Paul Dietz: bad rounding on small floats
80 (assert (= (fround least-positive-short-float least-positive-short-float) 1.0))
81
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))))
94
95 (with-test (:name (:scale-float-overflow :bug-372)
96             :fails-on :darwin) ;; bug 372
97   (progn
98     (assert (raises-error? (scale-float 1.0 most-positive-fixnum)
99                            floating-point-overflow))
100     (assert (raises-error? (scale-float 1.0d0 (1+ most-positive-fixnum))
101                            floating-point-overflow))))
102
103 ;;; bug found by jsnell when nfroyd tried to implement better LOGAND
104 ;;; type derivation.
105 (assert (= (integer-decode-float (coerce -1756510900000000000
106                                          'single-float))
107            12780299))
108
109 ;;; MISC.564: no out-of-line %ATAN2 for constant folding
110 (assert (typep
111   (funcall
112    (compile
113     nil
114     '(lambda (p1)
115       (declare (optimize (speed 3) (safety 2) (debug 3) (space 0))
116        (type complex p1))
117       (phase (the (eql #c(1.0d0 2.0d0)) p1))))
118    #c(1.0d0 2.0d0))
119     'double-float))
120
121 ;;; More out of line functions (%COS, %SIN, %TAN) for constant folding,
122 ;;; reported by Mika Pihlajamäki
123 (funcall (compile nil '(lambda () (cos (tan (round 0))))))
124 (funcall (compile nil '(lambda () (sin (tan (round 0))))))
125 (funcall (compile nil '(lambda () (tan (tan (round 0))))))
126
127 (with-test (:name (:addition-overflow :bug-372)
128             :fails-on '(or (and :ppc :openbsd) :darwin (and :x86 :netbsd)))
129   (assert (typep (nth-value
130                   1
131                   (ignore-errors
132                     (sb-sys:without-interrupts
133                      (sb-int:set-floating-point-modes :current-exceptions nil
134                                                       :accrued-exceptions nil)
135                      (loop repeat 2 summing most-positive-double-float)
136                      (sleep 2))))
137                  'floating-point-overflow)))
138
139 ;;; On x86-64 generating complex floats on the stack failed an aver in
140 ;;; the compiler if the stack slot was the same as the one containing
141 ;;; the real part of the complex. The following expression was able to
142 ;;; trigger this in 0.9.5.62.
143 (with-test (:name :complex-float-stack)
144   (dolist (type '((complex double-float)
145                   (complex single-float)))
146     (compile nil
147              `(lambda (x0 x1 x2 x3 x4 x5 x6 x7)
148                 (declare (type ,type x0 x1 x2 x3 x4 x5 x6 x7))
149                 (let ((x0 (+ x0 x0))
150                       (x1 (+ x1 x1))
151                       (x2 (+ x2 x2))
152                       (x3 (+ x3 x3))
153                       (x4 (+ x4 x4))
154                       (x5 (+ x5 x5))
155                       (x6 (+ x6 x6))
156                       (x7 (+ x7 x7)))
157                   (* (+ x0 x1 x2 x3) (+ x4 x5 x6 x7)
158                      (+ x0 x2 x4 x6) (+ x1 x3 x5 x7)
159                      (+ x0 x3 x4 x7) (+ x1 x2 x5 x6)
160                      (+ x0 x1 x6 x7) (+ x2 x3 x4 x5)))))))
161
162
163 (with-test (:name :nan-comparisons
164             :fails-on '(or :sparc :mips))
165   (sb-int:with-float-traps-masked (:invalid)
166     (macrolet ((test (form)
167                  (let ((nform (subst '(/ 0.0 0.0) 'nan form)))
168                    `(progn
169                       (assert (eval ',nform))
170                       (assert (eval `(let ((nan (/ 0.0 0.0)))
171                                        ,',form)))
172                       (assert (funcall
173                                (compile nil `(lambda () ,',nform))))
174                       (assert (funcall
175                                (compile nil `(lambda (nan) ,',form))
176                                (/ 0.0 0.0)))))))
177       (test (/= nan nan))
178       (test (/= nan nan nan))
179       (test (/= 1.0 nan 2.0 nan))
180       (test (/= nan 1.0 2.0 nan))
181       (test (not (= nan 1.0)))
182       (test (not (= nan nan)))
183       (test (not (= nan nan nan)))
184       (test (not (= 1.0 nan)))
185       (test (not (= nan 1.0)))
186       (test (not (= 1.0 1.0 nan)))
187       (test (not (= 1.0 nan 1.0)))
188       (test (not (= nan 1.0 1.0)))
189       (test (not (>= nan nan)))
190       (test (not (>= nan 1.0)))
191       (test (not (>= 1.0 nan)))
192       (test (not (>= 1.0 nan 0.0)))
193       (test (not (>= 1.0 0.0 nan)))
194       (test (not (>= nan 1.0 0.0)))
195       (test (not (<= nan nan)))
196       (test (not (<= nan 1.0)))
197       (test (not (<= 1.0 nan)))
198       (test (not (<= 1.0 nan 2.0)))
199       (test (not (<= 1.0 2.0 nan)))
200       (test (not (<= nan 1.0 2.0)))
201       (test (not (< nan nan)))
202       (test (not (< -1.0 nan)))
203       (test (not (< nan 1.0)))
204       (test (not (> nan nan)))
205       (test (not (> -1.0 nan)))
206       (test (not (> nan 1.0))))))
207
208 (with-test (:name :log-int/double-accuracy)
209   ;; we used to use single precision for intermediate results
210   (assert (eql 2567.6046442221327d0
211                (log (loop for n from 1 to 1000 for f = 1 then (* f n)
212                           finally (return f))
213                     10d0)))
214   ;; both ways
215   (assert (eql (log 123123123.0d0 10) (log 123123123 10.0d0))))
216
217 (with-test (:name :log-base-zero-return-type)
218   (assert (eql 0.0f0 (log 123 (eval 0))))
219   (assert (eql 0.0d0 (log 123.0d0 (eval 0))))
220   (assert (eql 0.0d0 (log 123 (eval 0.0d0))))
221   (let ((f (compile nil '(lambda (x y)
222                           (declare (optimize speed))
223                           (etypecase x
224                             (single-float
225                              (etypecase y
226                                (single-float (log x y))
227                                (double-float (log x y))))
228                             (double-float
229                              (etypecase y
230                                (single-float (log x y))
231                                (double-float (log x y)))))))))
232     (assert (eql 0.0f0 (funcall f 123.0 0.0)))
233     (assert (eql 0.0d0 (funcall f 123.0d0 0.0)))
234     (assert (eql 0.0d0 (funcall f 123.0d0 0.0d0)))
235     (assert (eql 0.0d0 (funcall f 123.0 0.0d0)))))
236
237 ;; Bug reported by Eric Marsden on July 15 2009. The compiler
238 ;; used not to constant fold calls with arguments of type
239 ;; (EQL foo).
240 (with-test (:name :eql-type-constant-fold)
241   (assert (equal '(FUNCTION (T) (VALUES (MEMBER T) &OPTIONAL))
242                  (sb-kernel:%simple-fun-type
243                   (compile nil `(lambda (x)
244                                   (eql #c(1.0 2.0)
245                                        (the (eql #c(1.0 2.0))
246                                          x))))))))
247
248 ;; The x86 port used not to reduce the arguments of transcendentals
249 ;; correctly. On other platforms, we trust libm to DTRT.
250 #+x86
251 (with-test (:name :range-reduction)
252   (flet ((almost= (x y)
253            (< (abs (- x y)) 1d-5)))
254     (macrolet ((foo (op value)
255                  `(assert (almost= (,op (mod ,value (* 2 pi)))
256                                    (,op ,value)))))
257       (let ((big (* pi (expt 2d0 70)))
258             (mid (coerce most-positive-fixnum 'double-float))
259             (odd (* pi most-positive-fixnum)))
260         (foo sin big)
261         (foo sin mid)
262         (foo sin odd)
263         (foo sin (/ odd 2d0))
264
265         (foo cos big)
266         (foo cos mid)
267         (foo cos odd)
268         (foo cos (/ odd 2d0))
269
270         (foo tan big)
271         (foo tan mid)
272         (foo tan odd)))))
273
274 ;; Leakage from the host could result in wrong values for truncation.
275 (with-test (:name :truncate)
276   (assert (plusp (sb-kernel:%unary-truncate/single-float (expt 2f0 33))))
277   (assert (plusp (sb-kernel:%unary-truncate/double-float (expt 2d0 33))))
278   ;; That'd be one strange host, but just in case
279   (assert (plusp (sb-kernel:%unary-truncate/single-float (expt 2f0 65))))
280   (assert (plusp (sb-kernel:%unary-truncate/double-float (expt 2d0 65)))))
281
282 ;; On x86-64, we sometimes forgot to clear the higher order bits of the
283 ;; destination register before using it with an instruction that doesn't
284 ;; clear the (unused) high order bits. Suspect instructions are operations
285 ;; with only one operand: for everything else, the destination has already
286 ;; been loaded with a value, making it safe (by induction).
287 ;;
288 ;; The tests are extremely brittle and could be broken by any number of
289 ;; back- or front-end optimisations. We should just keep the issue above
290 ;; in mind at all times when working with SSE or similar instruction sets.
291 #+(or x86 x86-64) ;; No other platforms have SB-VM::TOUCH-OBJECT.
292 (macrolet ((with-pinned-floats ((count type &rest names) &body body)
293              "Force COUNT float values to be kept live (and hopefully in registers),
294               fill a temporary register with noise, and execute BODY."
295              (let ((dummy (loop repeat count
296                                 collect (or (pop names)
297                                             (gensym "TEMP")))))
298                `(let ,(loop for i downfrom -1
299                             for var in dummy
300                             for j = (coerce i type)
301                             collect
302                             `(,var ,(complex j j))) ; we don't actually need that, but
303                   (declare (type (complex ,type) ,@dummy)) ; future-proofing can't hurt
304                   ,@(loop for var in dummy
305                           for i upfrom 0
306                           collect `(setf ,var ,(complex i (coerce i type))))
307                   (multiple-value-prog1
308                       (progn
309                         (let ((x ,(complex 1d0 1d0)))
310                           (declare (type (complex double-float) x))
311                           (setf x ,(complex most-positive-fixnum (float most-positive-fixnum 1d0)))
312                           (sb-vm::touch-object x))
313                         (locally ,@body))
314                     ,@(loop for var in dummy
315                             collect `(sb-vm::touch-object ,var)))))))
316   (with-test (:name :clear-sqrtsd)
317     (flet ((test-sqrtsd (float)
318              (declare (optimize speed (safety 1))
319                       (type (double-float (0d0)) float))
320              (with-pinned-floats (14 double-float x0)
321                (let ((x (sqrt float)))
322                  (values (+ x x0) float)))))
323       (declare (notinline test-sqrtsd))
324       (assert (zerop (imagpart (test-sqrtsd 4d0))))))
325
326   (with-test (:name :clear-sqrtsd-single)
327     (flet ((test-sqrtsd-float (float)
328              (declare (optimize speed (safety 1))
329                       (type (single-float (0f0)) float))
330              (with-pinned-floats (14 single-float x0)
331                (let ((x (sqrt float)))
332                  (values (+ x x0) float)))))
333       (declare (notinline test-sqrtsd-float))
334       (assert (zerop (imagpart (test-sqrtsd-float 4f0))))))
335
336   (with-test (:name :clear-cvtss2sd)
337     (flet ((test-cvtss2sd (float)
338              (declare (optimize speed (safety 1))
339                       (type single-float float))
340              (with-pinned-floats (14 double-float x0)
341                (let ((x (float float 0d0)))
342                  (values (+ x x0) (+ 1e0 float))))))
343       (declare (notinline test-cvtss2sd))
344       (assert (zerop (imagpart (test-cvtss2sd 1f0))))))
345
346   (with-test (:name :clear-cvtsd2ss)
347     (flet ((test-cvtsd2ss (float)
348              (declare (optimize speed (safety 1))
349                       (type double-float float))
350              (with-pinned-floats (14 single-float x0)
351                (let ((x (float float 1e0)))
352                  (values (+ x x0) (+ 1d0 float))))))
353       (declare (notinline test-cvtsd2ss))
354       (assert (zerop (imagpart (test-cvtsd2ss 4d0))))))
355
356   (with-test (:name :clear-cvtsi2sd)
357     (flet ((test-cvtsi2sd (int)
358              (declare (optimize speed (safety 0))
359                       (type (unsigned-byte 10) int))
360              (with-pinned-floats (15 double-float x0)
361                (+ (float int 0d0) x0))))
362       (declare (notinline test-cvtsi2sd))
363       (assert (zerop (imagpart (test-cvtsi2sd 4))))))
364
365   (with-test (:name :clear-cvtsi2ss)
366     (flet ((test-cvtsi2ss (int)
367              (declare (optimize speed (safety 0))
368                       (type (unsigned-byte 10) int))
369              (with-pinned-floats (15 single-float x0)
370                (+ (float int 0e0) x0))))
371       (declare (notinline test-cvtsi2ss))
372       (assert (zerop (imagpart (test-cvtsi2ss 4)))))))