Transpose lines.
[sbcl.git] / tests / arith.pure.lisp
blobfc95448c90180b38a5ab385be08ee807fd1b13e6
1 ;;;; arithmetic 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 (enable-test-parallelism)
16 ;;; Once upon a time, in the process of porting CMUCL's SPARC backend
17 ;;; to SBCL, multiplications were excitingly broken. While it's
18 ;;; unlikely that anything with such fundamental arithmetic errors as
19 ;;; these are going to get this far, it's probably worth checking.
20 (with-test (:name (:fundamental-arithmetic :smoke))
21 (macrolet ((test (op res1 res2)
22 `(progn
23 (assert (= (,op 4 2) ,res1))
24 (assert (= (,op 2 4) ,res2))
25 (assert (= (funcall (checked-compile '(lambda (x y) (,op x y)))
26 4 2)
27 ,res1))
28 (assert (= (funcall (checked-compile '(lambda (x y) (,op x y)))
29 2 4)
30 ,res2)))))
31 (test + 6 6)
32 (test - 2 -2)
33 (test * 8 8)
34 (test / 2 1/2)
35 (test expt 16 16)))
37 ;;; In a bug reported by Wolfhard Buss on cmucl-imp 2002-06-18 (BUG
38 ;;; 184), sbcl didn't catch all divisions by zero, notably divisions
39 ;;; of bignums and ratios by 0. Fixed in sbcl-0.7.6.13.
40 (with-test (:name (/ :division-by-zero ratio))
41 (checked-compile-and-assert (:allow-style-warnings t)
42 '(lambda () (/ 2/3 0))
43 (() (condition 'division-by-zero))))
45 (with-test (:name (/ :division-by-zero bignum))
46 (checked-compile-and-assert (:allow-style-warnings t)
47 '(lambda () (/ (1+ most-positive-fixnum) 0))
48 (() (condition 'division-by-zero))))
50 ;;; In a bug reported by Raymond Toy on cmucl-imp 2002-07-18, (COERCE
51 ;;; <RATIONAL> '(COMPLEX FLOAT)) was failing to return a complex
52 ;;; float; a patch was given by Wolfhard Buss cmucl-imp 2002-07-19.
53 (with-test (:name (coerce complex float 1))
54 (assert (= (coerce 1 '(complex float)) #c(1.0 0.0)))
55 (assert (= (coerce 1/2 '(complex float)) #c(0.5 0.0)))
56 (assert (= (coerce 1.0d0 '(complex float)) #c(1.0d0 0.0d0))))
58 ;;; (COERCE #c(<RATIONAL> <RATIONAL>) '(complex float)) resulted in
59 ;;; an error up to 0.8.17.31
60 (with-test (:name (coerce complex float 2))
61 (assert (= (coerce #c(1 2) '(complex float)) #c(1.0 2.0))))
63 ;;; COERCE also sometimes failed to verify that a particular coercion
64 ;;; was possible (in particular coercing rationals to bounded float
65 ;;; types.
66 (with-test (:name (coerce :to float :outside-bounds))
67 (checked-compile-and-assert (:allow-style-warnings t)
68 '(lambda () (coerce 1 '(float 2.0 3.0)))
69 (() (condition 'type-error)))
70 (checked-compile-and-assert (:allow-style-warnings t)
71 '(lambda () (coerce 1 '(single-float -1.0 0.0)))
72 (() (condition 'type-error)))
73 (assert (eql (coerce 1 '(single-float -1.0 2.0)) 1.0)))
75 ;;; ANSI says MIN and MAX should signal TYPE-ERROR if any argument
76 ;;; isn't REAL. SBCL 0.7.7 didn't in the 1-arg case. (reported as a
77 ;;; bug in CMU CL on #lisp IRC by lrasinen 2002-09-01)
78 (with-test (:name (min max type-error))
79 (checked-compile-and-assert (:allow-warnings t :allow-style-warnings t)
80 '(lambda () (min '(1 2 3)))
81 (() (condition 'type-error)))
82 (assert (= (min -1) -1))
83 (checked-compile-and-assert (:allow-warnings t :allow-style-warnings t)
84 '(lambda () (min 1 #(1 2 3)))
85 (() (condition 'type-error)))
86 (assert (= (min 10 11) 10))
87 (checked-compile-and-assert (:allow-warnings t :allow-style-warnings t)
88 '(lambda () (min (find-package "CL") -5.0))
89 (() (condition 'type-error)))
90 (assert (= (min 5.0 -3) -3))
91 (checked-compile-and-assert (:allow-warnings t :allow-style-warnings t)
92 '(lambda () (max #c(4 3)))
93 (() (condition 'type-error)))
94 (assert (= (max 0) 0))
95 (checked-compile-and-assert (:allow-warnings t :allow-style-warnings t)
96 '(lambda () (max "MIX" 3))
97 (() (condition 'type-error)))
98 (assert (= (max -1 10.0) 10.0))
99 (checked-compile-and-assert (:allow-warnings t :allow-style-warnings t)
100 '(lambda () (max 3 #'max))
101 (() (condition 'type-error)))
102 (assert (= (max -3 0) 0)))
104 (with-test (:name :numeric-inequality-&rest-arguments)
105 (dolist (f '(= < <= > >=))
106 ;; 1 arg
107 (assert-error (funcall f 'feep) type-error)
108 (unless (eq f '=)
109 ;; = accepts complex numbers
110 (assert-error (funcall f #c(0s0 1s0)) type-error))
111 ;; 2 arg
112 (assert-error (funcall f 3 'feep) type-error)
113 (assert-error (funcall f 'feep 3) type-error)
114 ;; 3 arg
115 (assert-error (funcall f 0 0 'feep) type-error)
116 (assert-error (funcall f 0 1 'feep) type-error)
117 (assert-error (funcall f 1 0 'feep) type-error)
118 ;; 4 arg
119 (assert-error (funcall f 0 0 0 'feep) type-error))
120 ;; Also MIN,MAX operate only on REAL
121 (dolist (f '(min max))
122 (assert-error (funcall f #c(1s0 -2s0)) type-error)))
124 ;;; (CEILING x 2^k) was optimized incorrectly
125 (with-test (:name (ceiling :power-of-two))
126 (loop for divisor in '(-4 4)
127 for ceiler = (checked-compile `(lambda (x)
128 (declare (fixnum x))
129 (declare (optimize (speed 3)))
130 (ceiling x ,divisor)))
131 do (loop for i from -5 to 5
132 for exact-q = (/ i divisor)
133 do (multiple-value-bind (q r)
134 (funcall ceiler i)
135 (assert (= (+ (* q divisor) r) i))
136 (assert (<= exact-q q))
137 (assert (< q (1+ exact-q)))))))
139 ;;; (TRUNCATE x 2^k) was optimized incorrectly
140 (with-test (:name (truncate :power-of-two))
141 (loop for divisor in '(-4 4)
142 for truncater = (checked-compile `(lambda (x)
143 (declare (fixnum x))
144 (declare (optimize (speed 3)))
145 (truncate x ,divisor)))
146 do (loop for i from -9 to 9
147 for exact-q = (/ i divisor)
148 do (multiple-value-bind (q r)
149 (funcall truncater i)
150 (assert (= (+ (* q divisor) r) i))
151 (assert (<= (abs q) (abs exact-q)))
152 (assert (< (abs exact-q) (1+ (abs q))))))))
154 ;;; CEILING had a corner case, spotted by Paul Dietz
155 (with-test (:name (ceiling :corner-case))
156 (assert (= (ceiling most-negative-fixnum (1+ most-positive-fixnum)) -1)))
158 ;;; give any optimizers of constant multiplication a light testing.
159 ;;; 100 may seem low, but (a) it caught CSR's initial errors, and (b)
160 ;;; before checking in, CSR tested with 10000. So one hundred
161 ;;; checkins later, we'll have doubled the coverage.
162 (with-test (:name (* :multiplication :constant :optimization))
163 (dotimes (i 100)
164 (let* ((x (random most-positive-fixnum))
165 (x2 (* x 2))
166 (x3 (* x 3))
167 (fn (checked-compile
168 `(lambda (y)
169 (declare (optimize speed) (type (integer 0 3) y))
170 (* y ,x))
171 :allow-notes (> x3 most-positive-fixnum))))
172 (assert (= (funcall fn 0) 0))
173 (assert (= (funcall fn 1) x))
174 (assert (= (funcall fn 2) x2))
175 (assert (= (funcall fn 3) x3)))))
177 ;;; Bugs reported by Paul Dietz:
179 ;;; (GCD 0 x) must return (abs x)
180 (with-test (:name (gcd 0))
181 (dolist (x (list -10 (* 3 most-negative-fixnum)))
182 (assert (= (gcd 0 x) (abs x)))))
184 ;;; LCM returns a non-negative number
185 (with-test (:name (lcm :non-negative))
186 (assert (= (lcm 4 -10) 20))
187 (assert (= (lcm 0 0) 0)))
189 ;;; PPC bignum arithmetic bug:
190 (with-test (:name (truncate bignum :ppc))
191 (multiple-value-bind (quo rem)
192 (truncate 291351647815394962053040658028983955 10000000000000000000000000)
193 (assert (= quo 29135164781))
194 (assert (= rem 5394962053040658028983955))))
196 ;;; x86 LEA bug:
197 (with-test (:name (:x86 :lea))
198 (checked-compile-and-assert ()
199 '(lambda (x) (declare (bit x)) (+ x #xf0000000))
200 ((1) #xf0000001)))
202 (with-test (:name (logbitp bignum))
203 (dolist (x '(((1+ most-positive-fixnum) 1 nil)
204 ((1+ most-positive-fixnum) -1 t)
205 ((1+ most-positive-fixnum) (1+ most-positive-fixnum) nil)
206 ((1+ most-positive-fixnum) (1- most-negative-fixnum) t)
207 (1 (ash most-negative-fixnum 1) nil)
208 (#.(- sb-vm:n-word-bits sb-vm:n-fixnum-tag-bits 1) most-negative-fixnum t)
209 (#.(1+ (- sb-vm:n-word-bits sb-vm:n-fixnum-tag-bits 1)) (ash most-negative-fixnum 1) t)
210 (#.(+ 2 (- sb-vm:n-word-bits sb-vm:n-fixnum-tag-bits 1)) (ash most-negative-fixnum 1) t)
211 (#.(+ sb-vm:n-word-bits 32) (ash most-negative-fixnum #.(+ 32 sb-vm:n-fixnum-tag-bits 2)) nil)
212 (#.(+ sb-vm:n-word-bits 33) (ash most-negative-fixnum #.(+ 32 sb-vm:n-fixnum-tag-bits 2)) t)))
213 (destructuring-bind (index int result) x
214 (assert (eq (eval `(logbitp ,index ,int)) result)))))
216 ;;; off-by-1 type inference error for %DPB and %DEPOSIT-FIELD:
217 (with-test (:name (dpb deposit-field :off-by-one))
218 (checked-compile-and-assert ()
219 '(lambda (b)
220 (integer-length (dpb b (byte 4 28) -1005)))
221 ((1230070) 32))
222 (checked-compile-and-assert ()
223 '(lambda (b)
224 (integer-length (deposit-field b (byte 4 28) -1005)))
225 ((1230070) 32)))
227 ;;; type inference leading to an internal compiler error:
228 (with-test (:name (ldb byte 0 :type-inference))
229 (checked-compile-and-assert ()
230 '(lambda (x)
231 (declare (type fixnum x))
232 (ldb (byte 0 0) x))
233 ((1) 0)
234 ((most-positive-fixnum) 0)
235 ((-1) 0)))
237 ;;; Alpha bignum arithmetic bug:
238 (with-test (:name (bignum :arithmetic :alpha))
239 (assert (= (* 966082078641 419216044685) 404997107848943140073085)))
241 ;;; Alpha smallnum arithmetic bug:
242 (with-test (:name (fixnum :arithmetc :alpha))
243 (assert (= (ash -129876 -1026) -1)))
245 ;;; Alpha middlenum (yes, really! Affecting numbers between 2^32 and
246 ;;; 2^64 :) arithmetic bug
247 (with-test (:name (truncate :middlenum))
248 (checked-compile-and-assert ()
249 '(lambda (a b c d)
250 (declare (type (integer -1621 -513) a)
251 (type (integer -3 34163) b)
252 (type (integer -9485132993 81272960) c)
253 (type (integer -255340814 519943) d)
254 (ignorable a b c d))
255 (truncate c (min -100 4149605)))
256 ((-1332 5864 -6963328729 -43789079) (values 69633287 -29))))
258 ;;; Here's another fantastic Alpha backend bug: the code to load
259 ;;; immediate 64-bit constants into a register was wrong.
260 (with-test (:name (dpb :constants))
261 (checked-compile-and-assert ()
262 '(lambda (a b c d)
263 (declare (type (integer -3563 2733564) a)
264 (type (integer -548947 7159) b)
265 (type (integer -19 0) c)
266 (type (integer -2546009 0) d)
267 (ignorable a b c d))
268 (case a
269 ((89 125 16) (ash a (min 18 -706)))
270 (t (dpb -3 (byte 30 30) -1))))
271 ((1227072 -529823 -18 -792831) -2147483649)))
273 ;;; ASH of a negative bignum by a bignum count would erroneously
274 ;;; return 0 prior to sbcl-0.8.4.4
275 (with-test (:name (ash :negative bignum))
276 (assert (= (ash (1- most-negative-fixnum) (1- most-negative-fixnum)) -1)))
278 ;;; Whoops. Too much optimization in division operators for 0
279 ;;; divisor.
280 (with-test (:name (mod truncate rem / floor ceiling :division-by-zero fixnum))
281 (flet ((frob (name)
282 (let ((fn (checked-compile
283 `(lambda (x)
284 (declare (optimize speed) (fixnum x))
285 (,name x 0)))))
286 (assert-error (funcall fn 1) division-by-zero))))
287 (mapc #'frob '(mod truncate rem / floor ceiling))))
289 ;; Check that the logic in SB-KERNEL::BASIC-COMPARE for doing fixnum/float
290 ;; comparisons without rationalizing the floats still gives the right anwers
291 ;; in the edge cases (had a fencepost error).
292 (macrolet ((test (range type sign)
293 `(let (ints
294 floats
295 (start (- ,(find-symbol (format nil
296 "MOST-~A-EXACTLY-~A-INTEGER"
297 sign type)
298 :sb-kernel)
299 ,range)))
300 (dotimes (i (1+ (* ,range 2)))
301 (let* ((x (+ start i))
302 (y (coerce x ',type)))
303 (push x ints)
304 (push y floats)))
305 (dolist (i ints)
306 (dolist (f floats)
307 (dolist (op '(< <= = >= >))
308 (unless (eq (funcall op i f)
309 (funcall op i (rationalize f)))
310 (error "(not (eq (~a ~a ~f) (~a ~a ~a)))~%"
311 op i f
312 op i (rationalize f)))
313 (unless (eq (funcall op f i)
314 (funcall op (rationalize f) i))
315 (error "(not (eq (~a ~f ~a) (~a ~a ~a)))~%"
316 op f i
317 op (rationalize f) i))))))))
318 (test 32 double-float negative)
319 (test 32 double-float positive)
320 (test 32 single-float negative)
321 (test 32 single-float positive))
323 ;; x86-64 sign-extension bug found using pfdietz's random tester.
324 (with-test (:name (:x86-64 :sign-extension))
325 (assert (= 286142502
326 (funcall (lambda ()
327 (declare (notinline logxor))
328 (min (logxor 0 0 0 286142502)))))))
330 ;; Small bugs in LOGCOUNT can still allow SBCL to be built and thus go
331 ;; unnoticed, so check more thoroughly here.
332 (with-test (:name logcount)
333 (flet ((test (x n)
334 (unless (= (logcount x) n)
335 (error "logcount failure for ~a" x))))
336 ;; Test with some patterns with well known number of ones/zeroes ...
337 (dotimes (i 128)
338 (let ((x (ash 1 i)))
339 (test x 1)
340 (test (- x) i)
341 (test (1- x) i)))
342 ;; ... and with some random integers of varying length.
343 (flet ((test-logcount (x)
344 (declare (type integer x))
345 (do ((result 0 (1+ result))
346 (x (if (minusp x)
347 (lognot x)
349 (logand x (1- x))))
350 ((zerop x) result))))
351 (dotimes (i 200)
352 (let ((x (random (ash 1 i))))
353 (test x (test-logcount x))
354 (test (- x) (test-logcount (- x))))))))
356 ;; 1.0 had a broken ATANH on win32
357 (with-test (:name atanh)
358 (assert (= (atanh 0.9d0) 1.4722194895832204d0)))
360 ;; Test some cases of integer operations with constant arguments
361 (with-test (:name :constant-integers)
362 (labels ((test-forms (op x y header &rest forms)
363 (let ((val (funcall op x y)))
364 (dolist (form forms)
365 (let ((new-val (funcall (checked-compile (append header form)) x y)))
366 (unless (eql val new-val)
367 (error "~S /= ~S: ~S ~S ~S~%" val new-val (append header form) x y))))))
368 (test-case (op x y type)
369 (test-forms op x y `(lambda (x y &aux z)
370 (declare (type ,type x y)
371 (ignorable x y z)
372 (notinline identity)
373 (optimize speed (safety 0))))
374 `((,op x ,y))
375 `((setf z (,op x ,y))
376 (identity x)
378 `((values (,op x ,y) x))
379 `((,op ,x y))
380 `((setf z (,op ,x y))
381 (identity y)
383 `((values (,op ,x y) y))
385 `((identity x)
386 (,op x ,y))
387 `((identity x)
388 (setf z (,op x ,y))
389 (identity x)
391 `((identity x)
392 (values (,op x ,y) x))
393 `((identity y)
394 (,op ,x y))
395 `((identity y)
396 (setf z (,op ,x y))
397 (identity y)
399 `((identity y)
400 (values (,op ,x y) y))))
401 (test-op (op)
402 (let ((ub `(unsigned-byte ,sb-vm:n-word-bits))
403 (sb `(signed-byte ,sb-vm:n-word-bits)))
404 (loop for (x y type)
405 in `((2 1 fixnum)
406 (2 1 ,ub)
407 (2 1 ,sb)
408 (,(1+ (ash 1 28)) ,(1- (ash 1 28)) fixnum)
409 (,(+ 3 (ash 1 30)) ,(+ 2 (ash 1 30)) ,ub)
410 (,(- -2 (ash 1 29)) ,(- 3 (ash 1 29)) ,sb)
411 ,@(when (> sb-vm:n-word-bits 32)
412 `((,(1+ (ash 1 29)) ,(1- (ash 1 29)) fixnum)
413 (,(1+ (ash 1 31)) ,(1- (ash 1 31)) ,ub)
414 (,(- -2 (ash 1 31)) ,(- 3 (ash 1 30)) ,sb)
415 (,(ash 1 40) ,(ash 1 39) fixnum)
416 (,(ash 1 40) ,(ash 1 39) ,ub)
417 (,(ash 1 40) ,(ash 1 39) ,sb)))
418 ;; fixnums that can be represented as 32-bit
419 ;; sign-extended immediates on x86-64
420 ,@(when (and (> sb-vm:n-word-bits 32)
421 (< sb-vm:n-fixnum-tag-bits 3))
422 `((,(1+ (ash 1 (- 31 sb-vm:n-fixnum-tag-bits)))
423 ,(1- (ash 1 (- 32 sb-vm:n-fixnum-tag-bits)))
424 fixnum))))
426 (test-case op x y type)
427 (test-case op x x type)))))
428 (mapc #'test-op '(+ - * truncate
429 < <= = >= >
431 eq))))
433 ;; GCD used to sometimes return negative values. The following did, on 32 bit
434 ;; builds.
435 (with-test (:name gcd)
436 ;; from lp#413680
437 (assert (plusp (gcd 20286123923750474264166990598656
438 680564733841876926926749214863536422912)))
439 ;; from lp#516750
440 (assert (plusp (gcd 2596102012663483082521318626691873
441 2596148429267413814265248164610048))))
443 (with-test (:name (expt 0 0))
444 ;; Check that (expt 0.0 0.0) and (expt 0 0.0) signal error, but
445 ;; (expt 0.0 0) returns 1.0
446 (flet ((error-case (expr)
447 (checked-compile-and-assert (:allow-style-warnings t)
448 `(lambda () ,expr)
449 (() (condition 'sb-int:arguments-out-of-domain-error)))))
450 (error-case '(expt 0.0 0.0))
451 (error-case '(expt 0 0.0)))
452 (checked-compile-and-assert (:allow-style-warnings t)
453 `(lambda () (expt 0.0 0))
454 (() 1.0)))
456 (with-test (:name :multiple-constant-folding)
457 (let ((*random-state* (make-random-state t)))
458 (flet ((make-args ()
459 (let (args vars)
460 (loop repeat (1+ (random 12))
461 do (if (zerop (random 2))
462 (let ((var (gensym)))
463 (push var args)
464 (push var vars))
465 (push (- (random 21) 10) args)))
466 (values args vars))))
467 (dolist (op '(+ * logior logxor logand logeqv gcd lcm - /))
468 (loop repeat 10
469 do (multiple-value-bind (args vars) (make-args)
470 (let ((fast (checked-compile
471 `(lambda ,vars
472 (,op ,@args))
473 :allow-style-warnings (eq op '/)))
474 (slow (checked-compile
475 `(lambda ,vars
476 (declare (notinline ,op))
477 (,op ,@args))
478 :allow-style-warnings (eq op '/))))
479 (loop repeat 3
480 do (let* ((call-args (loop repeat (length vars)
481 collect (- (random 21) 10)))
482 (fast-result (handler-case
483 (apply fast call-args)
484 (division-by-zero () :div0)))
485 (slow-result (handler-case
486 (apply slow call-args)
487 (division-by-zero () :div0))))
488 (if (not (eql fast-result slow-result))
489 (error "oops: ~S, ~S" args call-args)
490 #+nil (print (list :ok `(,op ,@args) :=> fast-result))
491 ))))))))))
493 ;;; (TRUNCATE <unsigned-word> <constant unsigned-word>) is optimized
494 ;;; to use multiplication instead of division. This propagates to FLOOR,
495 ;;; MOD and REM. Test that the transform is indeed triggered and test
496 ;;; several cases for correct results.
497 (with-test (:name (:integer-division-using-multiplication :used)
498 :skipped-on (not (or :x86-64 :x86 :arm64)))
499 (dolist (fun '(truncate floor ceiling mod rem))
500 (let* ((foo (checked-compile
501 `(lambda (x)
502 (declare (optimize (speed 3)
503 (space 1)
504 (compilation-speed 0))
505 (type (unsigned-byte ,sb-vm:n-word-bits) x))
506 (,fun x 9))))
507 (disassembly (with-output-to-string (s)
508 (disassemble foo :stream s))))
509 ;; KLUDGE copied from test :float-division-using-exact-reciprocal
510 ;; in compiler.pure.lisp.
511 (assert (and (not (search "DIV" disassembly))
512 (search "MUL" disassembly))))))
514 (with-test (:name (:integer-division-using-multiplication :correctness))
515 (let ((*random-state* (make-random-state t)))
516 (dolist (dividend-type `((unsigned-byte ,sb-vm:n-word-bits)
517 (signed-byte ,sb-vm:n-word-bits)
518 (and fixnum unsigned-byte)
519 (integer 10000 10100)
520 fixnum))
521 (dolist (divisor `(;; Some special cases from the paper
522 7 10 14 641 274177
523 ;; Range extremes
525 ,most-positive-fixnum
526 ,(1- (expt 2 sb-vm:n-word-bits))
527 ;; Some random values
528 ,@(loop for i from 8 to sb-vm:n-word-bits
529 for r = (random (expt 2 i))
530 ;; We don't want 0, 1 and powers of 2.
531 when (not (zerop (logand r (1- r))))
532 collect r
534 collect (- r))))
535 (dolist (fun '(truncate ceiling floor mod rem))
536 (let ((foo (checked-compile
537 `(lambda (x)
538 (declare (optimize (speed 3)
539 (space 1)
540 (compilation-speed 0))
541 (type ,dividend-type x))
542 (,fun x ,divisor)))))
543 (dolist (dividend `(0 1 ,most-positive-fixnum
544 ,(1- divisor) ,divisor
545 ,(1- (* divisor 2)) ,(* divisor 2)
546 ,@(loop repeat 4
547 collect (+ 10000 (random 101)))
548 ,@(loop for i from 4 to sb-vm:n-word-bits
549 for pow = (expt 2 (1- i))
550 for r = (+ pow (random pow))
551 collect r
552 collect (- r))))
553 (when (typep dividend dividend-type)
554 (multiple-value-bind (q1 r1)
555 (funcall foo dividend)
556 (multiple-value-bind (q2 r2)
557 (funcall fun dividend divisor)
558 (unless (and (= q1 q2)
559 (eql r1 r2))
560 (error "bad results for ~s with dividend type ~s"
561 (list fun dividend divisor)
562 dividend-type))))))))))))
564 ;; The fast path for logbitp underestimated sb-vm:n-positive-fixnum-bits
565 ;; for > 61 bit fixnums.
566 (with-test (:name (logbitp :wide fixnum))
567 (assert (not (logbitp (1- (integer-length most-positive-fixnum))
568 most-negative-fixnum))))
570 ;; EXPT dispatches in a complicated way on the types of its arguments.
571 ;; Check that all possible combinations are covered.
572 (with-test (:name (expt :argument-type-combinations))
573 (let ((numbers '(2 ; fixnum
574 3/5 ; ratio
575 1.2f0 ; single-float
576 2.0d0 ; double-float
577 #c(3/5 1/7) ; complex rational
578 #c(1.2f0 1.3f0) ; complex single-float
579 #c(2.0d0 3.0d0))) ; complex double-float
580 (bignum (expt 2 64))
581 results)
582 (dolist (base (cons bignum numbers))
583 (dolist (power numbers)
584 #+nil (format t "(expt ~s ~s) => " base power)
585 (let ((result (expt base power)))
586 #+nil (format t "~s~%" result)
587 (push result results))))
588 (assert (every #'numberp results))))
590 (with-test (:name :bug-741564)
591 ;; The bug was that in (expt <fixnum> <(complex double-float)>) the
592 ;; calculation was partially done only to single-float precision,
593 ;; making the complex double-float result too unprecise. Some other
594 ;; combinations of argument types were affected, too; test that all
595 ;; of them are good to double-float precision.
596 (labels ((nearly-equal-p (x y)
597 "Are the arguments equal to nearly double-float precision?"
598 (declare (type double-float x y))
599 (< (/ (abs (- x y)) (abs y))
600 (* double-float-epsilon 4))) ; Differences in the two least
601 ; significant mantissa bits
602 ; are OK.
603 (test-complex (x y)
604 (and (nearly-equal-p (realpart x) (realpart y))
605 (nearly-equal-p (imagpart x) (imagpart y))))
606 (print-result (msg base power got expected)
607 (declare (ignorable msg base power got expected))
608 #+nil
609 (format t "~a (expt ~s ~s)~%got ~s~%expected ~s~%"
610 msg base power got expected)))
611 (let ((n-broken 0))
612 (flet ((test (base power coerce-to-type)
613 (let* ((got (expt base power))
614 (expected (expt (coerce base coerce-to-type) power))
615 (result (test-complex got expected)))
616 (print-result (if result "Good:" "Bad:")
617 base power got expected)
618 (unless result
619 (incf n-broken)))))
620 (dolist (base (list 2 ; fixnum
621 (expt 2 64) ; bignum
622 3/5 ; ratio
623 2.0f0)) ; single-float
624 (let ((power #c(-2.5d0 -4.5d0))) ; complex double-float
625 (test base power 'double-float)))
626 (dolist (base (list #c(2.0f0 3.0f0) ; complex single-float
627 #c(2 3) ; complex fixnum
628 (complex (expt 2 64) (expt 2 65))
629 ; complex bignum
630 #c(3/5 1/7))) ; complex ratio
631 (dolist (power (list #c(-2.5d0 -4.5d0) ; complex double-float
632 -2.5d0)) ; double-float
633 (test base power '(complex double-float)))))
634 (when (> n-broken 0)
635 (error "Number of broken combinations: ~a" n-broken)))))
637 (with-test (:name (ldb :rlwinm :ppc))
638 (let ((one (checked-compile '(lambda (a) (ldb (byte 9 27) a))))
639 (two (checked-compile '(lambda (a)
640 (declare (type (integer -3 57216651) a))
641 (ldb (byte 9 27) a)))))
642 (assert (= 0 (- (funcall one 10) (funcall two 10))))))
644 ;; The ISQRT implementation is sufficiently complicated that it should
645 ;; be tested.
646 (with-test (:name isqrt)
647 (labels ((test (x)
648 (let* ((r (isqrt x))
649 (r2 (expt r 2))
650 (s2 (expt (1+ r) 2)))
651 (unless (and (<= r2 x)
652 (> s2 x))
653 (error "isqrt failure for ~a" x))))
654 (tests (x)
655 (test x)
656 (let ((x2 (expt x 2)))
657 (test x2)
658 (test (1+ x2))
659 (test (1- x2)))))
660 (test most-positive-fixnum)
661 (test (1+ most-positive-fixnum))
662 (loop for i from 1 to 200
663 for pow = (expt 2 (1- i))
664 for j = (+ pow (random pow))
666 (tests i)
667 (tests j))
668 (dotimes (i 10)
669 (tests (random (expt 2 (+ 1000 (random 10000))))))))
671 ;; bug 1026634 (reported by Eric Marsden on sbcl-devel)
672 (with-test (:name :recursive-cut-to-width)
673 (checked-compile-and-assert (:optimize '(:speed t :safety t :debug t :space t))
674 '(lambda (x)
675 (declare (type (integer 12417236377505266230
676 12417274239874990070)
678 (logand 8459622733968096971 x))
679 ((12417237222845306758) 2612793697039849090)))
681 ;; Also reported by Eric Marsden on sbcl-devel (2013-06-06)
682 (with-test (:name (:recursive-cut-to-width :more))
683 (checked-compile-and-assert ()
684 '(lambda (a b)
685 (logand (the (eql 16779072918521075607) a)
686 (the (integer 21371810342718833225 21371810343571293860) b)))
687 ((16779072918521075607 21371810342718833263) 2923729245085762055)))
689 (with-test (:name (ldb :negative-index-no-error))
690 (checked-compile-and-assert (:optimize :safe)
691 '(lambda (x y) (ldb (byte x y) 100))
692 ((-1 -2) (condition 'error)))
693 (checked-compile-and-assert (:optimize :safe)
694 '(lambda (x y) (mask-field (byte x y) 100))
695 ((-1 -2) (condition 'error)))
696 (checked-compile-and-assert (:optimize :safe)
697 '(lambda (x y) (dpb 0 (byte x y) 100))
698 ((-1 -2) (condition 'error)))
699 (checked-compile-and-assert (:optimize :safe)
700 '(lambda (x y) (deposit-field 0 (byte x y) 100))
701 ((-1 -2) (condition 'error))))
703 (with-test (:name (mask-field setf))
704 (checked-compile-and-assert ()
705 '(lambda (a)
706 (setf (mask-field (byte 2 0) a) 1) a)
707 ((15) 13)))
709 (with-test (:name :complex-multiply)
710 (checked-compile-and-assert ()
711 '(lambda ()
712 (let (z)
713 (expt (setf z (complex -0.123 -0.789)) 2)))
714 (() #C(-0.60739195 0.194094))))
716 (with-test (:name (sqrt complex))
717 (assert (= (expt (sqrt least-negative-double-float) 2)
718 least-negative-double-float)))
720 (with-test (:name (ldb :sign))
721 (checked-compile-and-assert ()
722 `(lambda (x)
723 (ldb (byte ,(1- sb-vm:n-word-bits) 0) x))
724 ((12) 12)))
726 (with-test (:name :mod-arith-large-constant)
727 (checked-compile-and-assert ()
728 '(lambda (x)
729 (declare (sb-ext:word x))
730 (logand sb-ext:most-positive-word
731 (+ x 2312423423)))
732 ((12) 2312423435)))
734 (with-test (:name (bignum :ash :left fixnum))
735 (assert (= (eval '(ash most-negative-fixnum (1- sb-vm:n-word-bits)))
736 (eval '(* most-negative-fixnum (expt 2 (1- sb-vm:n-word-bits)))))))
738 (with-test (:name (ldb fixnum :sign-bits))
739 (checked-compile-and-assert ()
740 '(lambda (x)
741 (declare (fixnum x))
742 (ldb (byte (/ sb-vm:n-word-bits 2)
743 (/ sb-vm:n-word-bits 2))
745 ((most-positive-fixnum) (ash most-positive-fixnum (- (/ sb-vm:n-word-bits 2))))
746 ((-1) (1- (expt 2 (/ sb-vm:n-word-bits 2))))))
748 (with-test (:name (dpb :sign-bits))
749 (checked-compile-and-assert ()
750 '(lambda (x)
751 (declare (fixnum x))
752 (dpb 1 (byte (/ sb-vm:n-word-bits 2)
753 (/ sb-vm:n-word-bits 2))
755 ((-1)
756 (logior (ash 1 (/ sb-vm:n-word-bits 2))
757 (logandc2 -1
758 (mask-field (byte (/ sb-vm:n-word-bits 2)
759 (/ sb-vm:n-word-bits 2))
760 -1))))
761 ((most-positive-fixnum)
762 (logior (ash 1 (/ sb-vm:n-word-bits 2))
763 (logandc2 most-positive-fixnum
764 (mask-field (byte (/ sb-vm:n-word-bits 2)
765 (/ sb-vm:n-word-bits 2))
766 -1))))))
768 (with-test (:name (dpb :position-zero))
769 (checked-compile-and-assert ()
770 '(lambda (x)
771 (declare (sb-vm:word x))
772 (dpb 0 (byte (/ sb-vm:n-word-bits 2) 0) x))
773 ((1) 0)
774 ((sb-ext:most-positive-word)
775 (logxor sb-ext:most-positive-word
776 (1- (expt 2 (/ sb-vm:n-word-bits 2)))))))
778 (with-test (:name (logand :mask-word))
779 (checked-compile-and-assert ()
780 '(lambda (x)
781 (logand x (ash sb-ext:most-positive-word -1)))
782 ((-1) (ash most-positive-word -1))))
784 (with-test (:name (/ complex real single-float))
785 (checked-compile-and-assert ()
786 '(lambda (b)
787 (declare (type single-float b))
788 (/ #c(1.0 2.0) b))
789 ((1.0) #c(1.0 2.0))))
791 (with-test (:name (ash :unsigned))
792 (checked-compile-and-assert ()
793 '(lambda (x)
794 (declare (sb-vm:signed-word x))
795 (ash x -64))
796 (( 123) 0)
797 ((-321) -1)))
799 (with-test (:name :64-bit-logcount)
800 (checked-compile-and-assert
802 '(lambda (x)
803 (declare (type (unsigned-byte 54) x))
804 (logcount x))
805 (((1- (ash 1 24))) 24)
806 (((1- (ash 1 32))) 32)
807 (((1- (ash 1 48))) 48)
808 (((1- (ash 1 54))) 54)))
810 (with-test (:name :complex-rational-eql)
811 ;; ensure no funny business with constant sharing so that we're forced
812 ;; to do the most general form or EQL on two (complex rational)s
813 (eql (read-from-string "#c(1/2 1/3)") (read-from-string "#c(1/2 1/3)")))
815 (with-test (:name :gcd-derive-type)
816 (checked-compile-and-assert
818 '(lambda (a)
819 (declare (type (integer -142 -29) a))
820 (eql (gcd (setq a -29) a) 1))
821 ((-33) nil)))
823 ;;; Test that LOGIOR and LOGXOR on PPC64 can correctly perform the constant 2nd-arg
824 ;;; vop variant on a 32-bit immediate using op + op-shifted instructions.
825 ;;; (Probably we should refactor large parts of the test suite by the CPU
826 ;;; architecture so that you can really see clearly the areas of concern
827 ;;; for each backend, but I'm not about to embark on that now)
828 (with-test (:name :ppc64-logxor-32-bit-const)
829 (let ((f (compile nil
830 '(lambda (x)
831 (declare (fixnum x))
832 ;; The asm code went wrong only if the result was not in the same
833 ;; register as the source, so make sure that X is live throughout
834 ;; the test so that each result is in a different register.
835 (let ((a (logxor x #x9516A7))
836 (b (logior x #x2531b4))
837 (c (ash x -1)))
838 (list a b c x))))))
839 (let ((result (funcall f 0)))
840 (assert (equal result
841 '(#x9516A7 #x2531b4 0 0))))))
843 (with-test (:name :truncate-by-zero-derivation)
844 (assert
845 (not (equal (cadr
846 (cdaddr (sb-kernel:%simple-fun-type
847 (checked-compile
848 `(lambda ()
849 (truncate 5 0))
850 :allow-style-warnings t))))
851 '(integer 0 0)))))
853 (with-test (:name :truncate-by-zero-derivation.2)
854 (checked-compile
855 `(lambda (x y)
856 (declare (type (unsigned-byte 8) x y)
857 (optimize speed (safety 0)))
858 (mod x y))
859 :allow-notes nil))
861 (with-test (:name :ash-fixnum)
862 (checked-compile-and-assert
864 `(lambda (b)
865 (declare (type (integer -2 2) b))
866 (ash b (min 13 b)))
867 ((-2) -1)
868 ((-1) -1)
869 ((0) 0)
870 ((1) 2)
871 ((2) 8)))
873 (with-test (:name :mod-ash-cut)
874 (checked-compile-and-assert
876 `(lambda (b)
877 (logand #xFF (ash 1 (the (integer -1000 1000) b))))
878 ((1) 2)
879 ((500) 0))
880 (checked-compile-and-assert
882 `(lambda (x b)
883 (logand #xFF (ash (the (unsigned-byte 64) x) (the (integer -1000 1000) b))))
884 (((1- (expt 2 64)) -63) 1)
885 (((1- (expt 2 64)) -64) 0)))
887 (with-test (:name :bogus-modular-fun-widths)
888 (checked-compile-and-assert
890 `(lambda (b)
891 (logorc2 0 (- (isqrt (abs (logand (if b -1 2) 2))))))
892 ((t) 0)
893 ((nil) 0)))
895 (with-test (:name :lognot-type-derive)
896 (assert-type
897 (lambda (b)
898 (lognot (if b -1 2)))
899 (or (integer -3 -3) (integer 0 0))))
901 (with-test (:name :logand-minus-1-type-derive)
902 (assert-type
903 (lambda (b)
904 (logand #xf (if b -1 2)))
905 (or (integer 2 2) (integer 15 15))))
907 (with-test (:name :ash-vop-liftimes)
908 (checked-compile-and-assert
910 `(lambda (A C)
911 (declare ((integer 18512171785636 25543390924355) a)
912 ((integer -20485927966480856 54446023204744213) c))
913 (dpb a (byte 20 25) (ash a (min 2 c))))
914 ((23906959691249 -15632482499364879) 15512918556672)
915 ((23906959691249 1) 50697318833122)))
918 (with-test (:name :ash-modarith-transform-loop)
919 (checked-compile-and-assert
921 `(lambda (p1 p2 p3)
922 (declare (type (integer * 53) p1)
923 (type number p2)
924 (type
925 (member 21006398744832 16437837094852630852 2251799813685252
926 -1597729350241882 466525164)
927 p3))
928 (ldb (byte (the (integer -3642545987372 *) p1) p2) p3))
929 ((53 2 21006398744832) 5251599686208)))
931 (with-test (:name :logcount-negative-fixnum)
932 (checked-compile-and-assert
934 `(lambda (x)
935 (logcount (the fixnum x)))
936 ((54) 4)
937 ((-54) 4)))
939 (with-test (:name :mod-ash64-signed)
940 (checked-compile-and-assert
942 `(lambda (a b)
943 (declare (fixnum a b))
944 (logand (ash a b) (1+ most-positive-fixnum)))
945 ((-1 -3) (1+ most-positive-fixnum))
946 ((1 3) 0)))
948 (with-test (:name :zero-shift-flags)
949 (checked-compile-and-assert
951 `(lambda (a m)
952 (declare ((integer 0 5000000000) a)
953 (bit m))
954 (zerop (ash a m)))
955 ((1 0) nil)
956 ((0 1) t)))
958 (with-test (:name :signum-merged-branch-if)
959 (checked-compile-and-assert
961 `(lambda (a y)
962 (declare (fixnum a y))
963 (when (< y 10)
964 (signum a)))
965 ((1 5) 1)
966 ((0 5) 0)
967 ((-1 5) -1)))
969 (with-test (:name :cmov-merged-branch-if)
970 (checked-compile-and-assert
972 `(lambda (b c)
973 (declare (type (integer 0 7711851432375361987) b))
974 (declare (type (integer -2 0) c))
975 (let ((v9 c))
976 (if (< c v9)
978 (if (= v9 c)
980 b))))
981 ((0 -1) -1)))
983 (with-test (:name :ash-amount-unsigned-comparison)
984 (checked-compile-and-assert
986 `(lambda (a)
987 (declare (type (integer -581 900) a))
988 (ash 3 (ash (ash a -50) (1- sb-vm:n-word-bits))))
989 ((-1) 0)
990 ((1) 3)))
992 (with-test (:name :ash-modfx-constant-folding)
993 (checked-compile-and-assert
995 `(lambda (a)
996 (declare (type (integer -10000 5) a))
997 (ldb (byte 16 28)
998 (ash 4611686018427387913
999 (progn
1000 (multiple-value-setq (a) -10000)
1001 a))))
1002 ((0) 0)))
1004 (with-test (:name :fixnum*-lifetimes)
1005 (checked-compile-and-assert
1007 `(lambda (a b)
1008 (declare ((integer -8394154896 -1950772105) b))
1010 (round (+ b 4611686018427387903)
1011 (let ((divisor (expt b 2)))
1012 divisor)))
1013 ((0 -5000000000) (values 0 4611686013427387903))))
1015 (with-test (:name :ash-*-cycle)
1016 (checked-compile-and-assert
1018 `(lambda (x)
1019 (truly-the fixnum (* (the fixnum x) -4)))
1020 ((4) -16)))
1022 (with-test (:name :-unsigned=>signed)
1023 (checked-compile-and-assert
1025 `(lambda (a b)
1026 (declare ((integer #.(- (expt 2 sb-vm:n-word-bits) 10)
1027 #.(- (expt 2 sb-vm:n-word-bits) 1))
1028 a b))
1029 (- a b))
1030 (((- (expt 2 sb-vm:n-word-bits) 10) (- (expt 2 sb-vm:n-word-bits) 9)) -1)))
1032 (with-test (:name :>=-fixnum-integer)
1033 (checked-compile-and-assert
1035 `(lambda (a b)
1036 (declare (fixnum a))
1037 (and (integerp b)
1038 (>= a b)))
1039 ((1 2) nil)
1040 ((2 2) t)
1041 ((3 2) t))
1042 (checked-compile-and-assert
1044 `(lambda (a b)
1045 (declare (fixnum a))
1046 (and (integerp b)
1047 (<= a b)))
1048 ((1 2) t)
1049 ((2 2) t)
1050 ((3 2) nil)))
1052 (with-test (:name :ash-signed-negation-overflow :fails-on :arm)
1053 (checked-compile-and-assert
1055 `(lambda (a b)
1056 (declare (fixnum a)
1057 (sb-vm:signed-word b))
1058 (truly-the sb-vm:signed-word (ash a b)))
1059 ((-44 (- (expt 2 (1- sb-vm:n-word-bits)))) -1)
1060 ((44 2) 176)))
1062 (with-test (:name :-constant-mnf)
1063 (checked-compile-and-assert
1065 `(lambda (a)
1066 (logand most-positive-fixnum
1067 (- (the fixnum a) most-negative-fixnum)))
1068 ((10) 10)))
1070 (with-test (:name :unary-truncate-discard-second-value)
1071 (checked-compile-and-assert
1073 `(lambda (a)
1074 (truncate (expt (complex a 0) 0))
1076 ((1) 1)))
1078 (with-test (:name :unary-truncate-discard-second-value.2)
1079 (checked-compile-and-assert
1080 (:allow-style-warnings t)
1081 `(lambda (a b)
1082 (boole boole-nand
1083 (dpb (* b (unwind-protect 0 b)) (byte 31 28) a)
1084 (ignore-errors
1085 (truncate
1086 (eval
1087 (values 1 2))))))
1088 ((1 2) -2)))
1090 (with-test (:name :logtest-immediate)
1091 (checked-compile-and-assert
1093 `(lambda (x)
1094 (logtest (- (expt 2 63) 3) (the fixnum x)))
1095 ((1) t)
1096 ((2) nil)))
1098 (with-test (:name :logand-negative-derive)
1099 (assert
1100 (subtypep (second (third (sb-kernel:%simple-fun-type
1101 (checked-compile
1102 `(lambda (a b)
1103 (declare ((not unsigned-byte) a b))
1104 (logand a b))))))
1105 '(integer * -1))))
1107 (with-test (:name :ash-mod64-constant-folding)
1108 (checked-compile-and-assert
1110 `(lambda (a d)
1111 (declare (fixnum a)
1112 ((unsigned-byte 64) d))
1113 (setq a -64)
1114 (logand d (ash -1 a)))
1115 ((0 3) 3)))
1117 (with-test (:name :signed-unsigned-cmp-elision)
1118 (checked-compile-and-assert
1120 `(lambda (a b)
1121 (declare (fixnum a)
1122 ((or (integer -1 0)
1123 (unsigned-byte 64)) b))
1124 (let ((v2 (abs b)))
1125 (if (> a v2)
1127 (>= a v2))))
1128 ((1 2) nil)
1129 ((1 1) t)
1130 ((3 0) 3)))
1132 (with-test (:name :integer-fixnum-<)
1133 (checked-compile-and-assert
1135 `(lambda (f b &optional a c d e g h i j k l m n o p q r s)
1136 (declare (fixnum f))
1137 (eval (list 'list a b c d e f g h i j k l m n o p q r s t))
1138 (and (integerp b)
1139 (< f b)))
1140 ((1 t) nil)
1141 ((1 10) t)))
1143 (with-test (:name :cast-externally-checkable-p-satisfies)
1144 (checked-compile-and-assert
1146 `(lambda (u s)
1147 (the (unsigned-byte 64) (+ (the (or null (unsigned-byte 64)) u) (the fixnum s))))
1148 ((1 2) 3)))
1150 (with-test (:name :rem-derive-type)
1151 (flet ((test (form type)
1152 (assert
1153 (type-specifiers-equal
1154 (caddr
1155 (sb-kernel:%simple-fun-type
1156 (checked-compile
1157 `(lambda (a b)
1158 ,form))))
1159 `(values ,type &optional)))))
1160 (test `(rem (the fixnum a) (the integer b))
1161 'fixnum)
1162 (test `(rem (the fixnum a) (the (integer 0 20) b))
1163 '(integer -19 19))
1164 (test `(rem (the (unsigned-byte 32) a) (the (integer 0 20) b))
1165 '(integer 0 19))
1166 (test `(rem (the (signed-byte 32) a) (the (unsigned-byte 32) b))
1167 '(signed-byte 32))
1168 (test `(rem (the (signed-byte 8) a) (the (unsigned-byte 7) b))
1169 '(integer -126 126))))
1171 (with-test (:name :logand-positive-negative-type-derive)
1172 (flet ((test (form type)
1173 (assert
1174 (type-specifiers-equal
1175 (caddr
1176 (sb-kernel:%simple-fun-type
1177 (checked-compile
1178 `(lambda (a)
1179 ,form))))
1180 `(values ,type &optional)))))
1181 (test `(logand (ash -1 20) (the (integer 0 20) a))
1182 '(eql 0))
1183 (test `(logand #xFF (the (integer -20 -1) a))
1184 '(integer 236 255))))