1 ;;;; arithmetic tests with no side effects
3 ;;;; This software is part of the SBCL system. See the README file for
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
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
)
23 (assert (= (,op
4 2) ,res1
))
24 (assert (= (,op
2 4) ,res2
))
25 (assert (= (funcall (checked-compile '(lambda (x y
) (,op x y
)))
28 (assert (= (funcall (checked-compile '(lambda (x y
) (,op x y
)))
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
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 '(= < <= > >=))
107 (assert-error (funcall f
'feep
) type-error
)
109 ;; = accepts complex numbers
110 (assert-error (funcall f
#c
(0s0 1s0
)) type-error
))
112 (assert-error (funcall f
3 'feep
) type-error
)
113 (assert-error (funcall f
'feep
3) type-error
)
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
)
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)
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
)
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)
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
))
164 (let* ((x (random most-positive-fixnum
))
169 (declare (optimize speed
) (type (integer 0 3) y
))
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))))
197 (with-test (:name
(:x86
:lea
))
198 (checked-compile-and-assert ()
199 '(lambda (x) (declare (bit x
)) (+ x
#xf0000000
))
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 ()
220 (integer-length (dpb b
(byte 4 28) -
1005)))
222 (checked-compile-and-assert ()
224 (integer-length (deposit-field b
(byte 4 28) -
1005)))
227 ;;; type inference leading to an internal compiler error:
228 (with-test (:name
(ldb byte
0 :type-inference
))
229 (checked-compile-and-assert ()
231 (declare (type fixnum x
))
234 ((most-positive-fixnum) 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 ()
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
)
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 ()
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
)
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
280 (with-test (:name
(mod truncate rem
/ floor ceiling
:division-by-zero fixnum
))
282 (let ((fn (checked-compile
284 (declare (optimize speed
) (fixnum x
))
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
)
295 (start (- ,(find-symbol (format nil
296 "MOST-~A-EXACTLY-~A-INTEGER"
300 (dotimes (i (1+ (* ,range
2)))
301 (let* ((x (+ start i
))
302 (y (coerce x
',type
)))
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)))~%"
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)))~%"
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
))
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
)
334 (unless (= (logcount x
) n
)
335 (error "logcount failure for ~a" x
))))
336 ;; Test with some patterns with well known number of ones/zeroes ...
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
))
350 ((zerop x
) result
))))
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
)))
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
)
373 (optimize speed
(safety 0))))
375 `((setf z
(,op x
,y
))
378 `((values (,op x
,y
) x
))
380 `((setf z
(,op
,x y
))
383 `((values (,op
,x y
) y
))
392 (values (,op x
,y
) x
))
400 (values (,op
,x y
) y
))))
402 (let ((ub `(unsigned-byte ,sb-vm
:n-word-bits
))
403 (sb `(signed-byte ,sb-vm
:n-word-bits
)))
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
)))
426 (test-case op x y type
)
427 (test-case op x x type
)))))
428 (mapc #'test-op
'(+ -
* truncate
433 ;; GCD used to sometimes return negative values. The following did, on 32 bit
435 (with-test (:name gcd
)
437 (assert (plusp (gcd 20286123923750474264166990598656
438 680564733841876926926749214863536422912)))
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
)
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))
456 (with-test (:name
:multiple-constant-folding
)
457 (let ((*random-state
* (make-random-state t
)))
460 (loop repeat
(1+ (random 12))
461 do
(if (zerop (random 2))
462 (let ((var (gensym)))
465 (push (- (random 21) 10) args
)))
466 (values args vars
))))
467 (dolist (op '(+ * logior logxor logand logeqv gcd lcm -
/))
469 do
(multiple-value-bind (args vars
) (make-args)
470 (let ((fast (checked-compile
473 :allow-style-warnings
(eq op
'/)))
474 (slow (checked-compile
476 (declare (notinline ,op
))
478 :allow-style-warnings
(eq op
'/))))
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
))
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
502 (declare (optimize (speed 3)
504 (compilation-speed 0))
505 (type (unsigned-byte ,sb-vm
:n-word-bits
) x
))
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)
521 (dolist (divisor `(;; Some special cases from the paper
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
))))
535 (dolist (fun '(truncate ceiling floor mod rem
))
536 (let ((foo (checked-compile
538 (declare (optimize (speed 3)
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)
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
))
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
)
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
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
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
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
))
609 (format t
"~a (expt ~s ~s)~%got ~s~%expected ~s~%"
610 msg base power got expected
)))
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
)
620 (dolist (base (list 2 ; fixnum
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))
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
)))))
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
646 (with-test (:name isqrt
)
650 (s2 (expt (1+ r
) 2)))
651 (unless (and (<= r2 x
)
653 (error "isqrt failure for ~a" x
))))
656 (let ((x2 (expt x
2)))
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
))
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
))
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 ()
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 ()
706 (setf (mask-field (byte 2 0) a
) 1) a
)
709 (with-test (:name
:complex-multiply
)
710 (checked-compile-and-assert ()
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 ()
723 (ldb (byte ,(1- sb-vm
:n-word-bits
) 0) x
))
726 (with-test (:name
:mod-arith-large-constant
)
727 (checked-compile-and-assert ()
729 (declare (sb-ext:word x
))
730 (logand sb-ext
:most-positive-word
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 ()
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 ()
752 (dpb 1 (byte (/ sb-vm
:n-word-bits
2)
753 (/ sb-vm
:n-word-bits
2))
756 (logior (ash 1 (/ sb-vm
:n-word-bits
2))
758 (mask-field (byte (/ sb-vm
:n-word-bits
2)
759 (/ sb-vm
:n-word-bits
2))
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))
768 (with-test (:name
(dpb :position-zero
))
769 (checked-compile-and-assert ()
771 (declare (sb-vm:word x
))
772 (dpb 0 (byte (/ sb-vm
:n-word-bits
2) 0) x
))
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 ()
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 ()
787 (declare (type single-float b
))
789 ((1.0
) #c
(1.0
2.0))))
791 (with-test (:name
(ash :unsigned
))
792 (checked-compile-and-assert ()
794 (declare (sb-vm:signed-word x
))
799 (with-test (:name
:64-bit-logcount
)
800 (checked-compile-and-assert
803 (declare (type (unsigned-byte 54) 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
819 (declare (type (integer -
142 -
29) a
))
820 (eql (gcd (setq a -
29) a
) 1))
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
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
))
839 (let ((result (funcall f
0)))
840 (assert (equal result
841 '(#x9516A7
#x2531b4
0 0))))))
843 (with-test (:name
:truncate-by-zero-derivation
)
846 (cdaddr (sb-kernel:%simple-fun-type
850 :allow-style-warnings t
))))
853 (with-test (:name
:truncate-by-zero-derivation
.2)
856 (declare (type (unsigned-byte 8) x y
)
857 (optimize speed
(safety 0)))
861 (with-test (:name
:ash-fixnum
)
862 (checked-compile-and-assert
865 (declare (type (integer -
2 2) b
))
873 (with-test (:name
:mod-ash-cut
)
874 (checked-compile-and-assert
877 (logand #xFF
(ash 1 (the (integer -
1000 1000) b
))))
880 (checked-compile-and-assert
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
891 (logorc2 0 (- (isqrt (abs (logand (if b -
1 2) 2))))))
895 (with-test (:name
:lognot-type-derive
)
898 (lognot (if b -
1 2)))
899 (or (integer -
3 -
3) (integer 0 0))))
901 (with-test (:name
:logand-minus-1-type-derive
)
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
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
922 (declare (type (integer * 53) p1
)
925 (member 21006398744832 16437837094852630852 2251799813685252
926 -
1597729350241882 466525164)
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
935 (logcount (the fixnum x
)))
939 (with-test (:name
:mod-ash64-signed
)
940 (checked-compile-and-assert
943 (declare (fixnum a b
))
944 (logand (ash a b
) (1+ most-positive-fixnum
)))
945 ((-1 -
3) (1+ most-positive-fixnum
))
948 (with-test (:name
:zero-shift-flags
)
949 (checked-compile-and-assert
952 (declare ((integer 0 5000000000) a
)
958 (with-test (:name
:signum-merged-branch-if
)
959 (checked-compile-and-assert
962 (declare (fixnum a y
))
969 (with-test (:name
:cmov-merged-branch-if
)
970 (checked-compile-and-assert
973 (declare (type (integer 0 7711851432375361987) b
))
974 (declare (type (integer -
2 0) c
))
983 (with-test (:name
:ash-amount-unsigned-comparison
)
984 (checked-compile-and-assert
987 (declare (type (integer -
581 900) a
))
988 (ash 3 (ash (ash a -
50) (1- sb-vm
:n-word-bits
))))
992 (with-test (:name
:ash-modfx-constant-folding
)
993 (checked-compile-and-assert
996 (declare (type (integer -
10000 5) a
))
998 (ash 4611686018427387913
1000 (multiple-value-setq (a) -
10000)
1004 (with-test (:name
:fixnum
*-lifetimes
)
1005 (checked-compile-and-assert
1008 (declare ((integer -
8394154896 -
1950772105) b
))
1010 (round (+ b
4611686018427387903)
1011 (let ((divisor (expt b
2)))
1013 ((0 -
5000000000) (values 0 4611686013427387903))))
1015 (with-test (:name
:ash-
*-cycle
)
1016 (checked-compile-and-assert
1019 (truly-the fixnum
(* (the fixnum x
) -
4)))
1022 (with-test (:name
:-unsigned
=>signed
)
1023 (checked-compile-and-assert
1026 (declare ((integer #.
(- (expt 2 sb-vm
:n-word-bits
) 10)
1027 #.
(- (expt 2 sb-vm
:n-word-bits
) 1))
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
1036 (declare (fixnum a
))
1042 (checked-compile-and-assert
1045 (declare (fixnum a
))
1052 (with-test (:name
:ash-signed-negation-overflow
:fails-on
:arm
)
1053 (checked-compile-and-assert
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)
1062 (with-test (:name
:-constant-mnf
)
1063 (checked-compile-and-assert
1066 (logand most-positive-fixnum
1067 (- (the fixnum a
) most-negative-fixnum
)))
1070 (with-test (:name
:unary-truncate-discard-second-value
)
1071 (checked-compile-and-assert
1074 (truncate (expt (complex a
0) 0))
1078 (with-test (:name
:unary-truncate-discard-second-value
.2)
1079 (checked-compile-and-assert
1080 (:allow-style-warnings t
)
1083 (dpb (* b
(unwind-protect 0 b
)) (byte 31 28) a
)
1090 (with-test (:name
:logtest-immediate
)
1091 (checked-compile-and-assert
1094 (logtest (- (expt 2 63) 3) (the fixnum x
)))
1098 (with-test (:name
:logand-negative-derive
)
1100 (subtypep (second (third (sb-kernel:%simple-fun-type
1103 (declare ((not unsigned-byte
) a b
))
1107 (with-test (:name
:ash-mod64-constant-folding
)
1108 (checked-compile-and-assert
1112 ((unsigned-byte 64) d
))
1114 (logand d
(ash -
1 a
)))
1117 (with-test (:name
:signed-unsigned-cmp-elision
)
1118 (checked-compile-and-assert
1123 (unsigned-byte 64)) b
))
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
))
1143 (with-test (:name
:cast-externally-checkable-p-satisfies
)
1144 (checked-compile-and-assert
1147 (the (unsigned-byte 64) (+ (the (or null
(unsigned-byte 64)) u
) (the fixnum s
))))
1150 (with-test (:name
:rem-derive-type
)
1151 (flet ((test (form type
)
1153 (type-specifiers-equal
1155 (sb-kernel:%simple-fun-type
1159 `(values ,type
&optional
)))))
1160 (test `(rem (the fixnum a
) (the integer b
))
1162 (test `(rem (the fixnum a
) (the (integer 0 20) b
))
1164 (test `(rem (the (unsigned-byte 32) a
) (the (integer 0 20) b
))
1166 (test `(rem (the (signed-byte 32) a
) (the (unsigned-byte 32) b
))
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
)
1174 (type-specifiers-equal
1176 (sb-kernel:%simple-fun-type
1180 `(values ,type
&optional
)))))
1181 (test `(logand (ash -
1 20) (the (integer 0 20) a
))
1183 (test `(logand #xFF
(the (integer -
20 -
1) a
))
1184 '(integer 236 255))))