1 ;;;; arithmetic tests without 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 (defmacro define-compiled-fun
(fun name
)
16 (declaim (notinline ,name
))
17 (defun ,name
(&rest args
)
18 (declare (optimize safety
))
21 (2 (,fun
(car args
) (cadr args
)))
22 (t (apply #',fun args
))))))
24 (define-compiled-fun min compiled-min
)
25 (define-compiled-fun max compiled-max
)
26 (define-compiled-fun + compiled-
+)
27 (define-compiled-fun * compiled-
*)
28 (define-compiled-fun logand compiled-logand
)
29 (define-compiled-fun logior compiled-logior
)
30 (define-compiled-fun logxor compiled-logxor
)
32 (assert (null (ignore-errors (compiled-min '(1 2 3)))))
33 (assert (= (compiled-min -
1) -
1))
34 (assert (null (ignore-errors (compiled-min 1 #(1 2 3)))))
35 (assert (= (compiled-min 10 11) 10))
36 (assert (null (ignore-errors (compiled-min (find-package "CL") -
5.0))))
37 (assert (= (compiled-min 5.0 -
3) -
3))
38 (assert (null (ignore-errors (compiled-max #c
(4 3)))))
39 (assert (= (compiled-max 0) 0))
40 (assert (null (ignore-errors (compiled-max "MIX" 3))))
41 (assert (= (compiled-max -
1 10.0) 10.0))
42 (assert (null (ignore-errors (compiled-max 3 #'max
))))
43 (assert (= (compiled-max -
3 0) 0))
45 (assert (null (ignore-errors (compiled-+ "foo"))))
46 (assert (= (compiled-+ 3f0
) 3f0
))
47 (assert (null (ignore-errors (compiled-+ 1 #p
"tmp"))))
48 (assert (= (compiled-+ 1 2) 3))
49 (assert (null (ignore-errors (compiled-+ '(1 2 3) 3))))
50 (assert (= (compiled-+ 3f0
4f0
) 7f0
))
51 (assert (null (ignore-errors (compiled-* "foo"))))
52 (assert (= (compiled-* 3f0
) 3f0
))
53 (assert (null (ignore-errors (compiled-* 1 #p
"tmp"))))
54 (assert (= (compiled-* 1 2) 2))
55 (assert (null (ignore-errors (compiled-* '(1 2 3) 3))))
56 (assert (= (compiled-* 3f0
4f0
) 12f0
))
58 (assert (null (ignore-errors (compiled-logand #(1)))))
59 (assert (= (compiled-logand 1) 1))
60 (assert (null (ignore-errors (compiled-logior 3f0
))))
61 (assert (= (compiled-logior 4) 4))
62 (assert (null (ignore-errors (compiled-logxor #c
(2 3)))))
63 (assert (= (compiled-logxor -
6) -
6))
65 (with-test (:name
(coerce :overflow
))
66 (checked-compile-and-assert
68 '(lambda (n) (coerce n
'single-float
))
69 (((expt 10 1000)) (condition 'floating-point-overflow
))))
71 (defun are-we-getting-ash-right (x y
)
72 (declare (optimize speed
)
73 (type (unsigned-byte 32) x
)
74 (type (integer -
40 0) y
))
76 (defun what-about-with-constants (x)
77 (declare (optimize speed
) (type (unsigned-byte 32) x
))
81 (assert (= (are-we-getting-ash-right (1- (ash 1 32)) (- i
))
85 (assert (= (what-about-with-constants (1- (ash 1 32))) 0))
87 (defun one-more-test-case-to-catch-sparc (x y
)
88 (declare (optimize speed
(safety 0))
89 (type (unsigned-byte 32) x
) (type (integer -
40 2) y
))
90 (the (unsigned-byte 32) (ash x y
)))
91 (assert (= (one-more-test-case-to-catch-sparc (1- (ash 1 32)) -
40) 0))
94 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
95 (defvar *n-fixnum-bits
* (- sb-vm
:n-word-bits sb-vm
::n-fixnum-tag-bits
))
96 (defvar *shifts
* (let ((list (list 0
98 (1- sb-vm
:n-word-bits
)
100 (1+ sb-vm
:n-word-bits
))))
101 (append list
(mapcar #'- list
)))))
103 (macrolet ((nc-list ()
104 `(list ,@(loop for i from
0 below
(length *shifts
*)
105 collect
`(frob (nth ,i
*shifts
*)))))
107 `(list ,@(loop for i from
0 below
(length *shifts
*)
108 collect
`(frob ,(nth i
*shifts
*))))))
111 `(list x
,y
(ash x
,y
))))
115 `(list x
,y
(ash x
,y
))))
117 (defun nc-modular-ash-ub (x)
119 `(list x
,y
(logand most-positive-fixnum
(ash x
,y
)))))
121 (defun c-modular-ash-ub (x)
122 (declare (type (and fixnum unsigned-byte
) x
)
125 `(list x
,y
(logand most-positive-fixnum
(ash x
,y
)))))
128 (let* ((values (list 0 1 most-positive-fixnum
))
129 (neg-values (cons most-negative-fixnum
130 (mapcar #'- values
))))
131 (labels ((test (value fun1 fun2
)
132 (let ((res1 (funcall fun1 value
))
133 (res2 (funcall fun2 value
)))
134 (mapcar (lambda (a b
)
136 (error "ash failure for ~A vs ~A: ~A not EQUALP ~A"
140 (loop for x in values do
141 (test x
'nc-ash
'c-ash
)
142 (test x
'nc-modular-ash-ub
'c-modular-ash-ub
))
143 (loop for x in neg-values do
144 (test x
'nc-ash
'c-ash
))))
147 (declaim (inline ppc-ldb-2
))
149 (defun ppc-ldb-2 (fun value
)
150 (declare (type (signed-byte 32) value
)
151 (optimize (speed 3) (safety 0) (space 1) (debug 1)
152 (compilation-speed 0)))
153 (funcall fun
(ldb (byte 8 24) value
))
154 (funcall fun
(ldb (byte 8 16) value
))
155 (funcall fun
(ldb (byte 8 8) value
))
156 (funcall fun
(ldb (byte 8 0) value
))
159 (defun ppc-ldb-1 (fun)
160 (declare (optimize (speed 3) (safety 0) (space 1) (debug 1)
161 (compilation-speed 0)))
163 for param
:across
(make-array 1 :initial-element nil
)
164 for size
:across
(make-array 1 :element-type
'fixnum
:initial-element
3)
165 do
(ppc-ldb-2 fun
(if param size -
1))))
167 (with-test (:name
:ppc-ldb
)
169 (ppc-ldb-1 (lambda (x)
171 (assert (equal acc
'(#xff
#xff
#xff
#xff
)))))
173 (with-test (:name
:ldb-word-cast
)
174 (checked-compile-and-assert
177 (truly-the fixnum
(ldb (byte x y
) 100)))
180 (with-test (:name
:logbitp-negative-error
)
181 (checked-compile-and-assert
185 ((-1 0) (condition 'type-error
))
186 ((-2 (1+ most-positive-fixnum
)) (condition 'type-error
))
187 (((1- most-negative-fixnum
) 1) (condition 'type-error
))))
189 (with-test (:name
:*-overflow-ratio
)
190 (checked-compile-and-assert
193 (the fixnum
(* 8 a
)))
197 (with-test (:name
:bignum-float
)
198 (checked-compile-and-assert
201 (sb-sys:without-gcing
202 (let ((res (sb-bignum:%allocate-bignum
2)))
203 (setf (sb-bignum:%bignum-ref res
1) 529
204 (sb-bignum:%bignum-ref res
0) 9223372036854775807)
205 (sb-bignum:%bignum-set-length res
1)
208 (sb-bignum:%bignum-set-length res
2)))))
209 ((-9.223372036854776d18
) nil
)
210 ((9.223372036854776d18
) t
)))
212 (with-test (:name
:overflow-transform-nil
)
213 (checked-compile-and-assert
219 (svref v
(+ i
26387449082611642302))))
224 (checked-compile-and-assert
225 (:allow-style-warnings t
)
228 (- (length s
) 12129535698721845515))))))
230 (with-test (:name
:integer-length-union-derivation
)
231 (checked-compile-and-assert
242 (with-test (:name
:isqrt-union
)
245 (declare ((or (integer 1 5) (integer 9 10)) x
))
249 (with-test (:name
:integer-length-union
)
252 (declare ((or (integer 1 5) (integer 9 10)) x
))
256 (with-test (:name
:rem-transform-erase-types
)
257 (checked-compile-and-assert
260 (declare ((integer * 0) a
))
264 (checked-compile-and-assert
267 (declare ((member 7 -
9) a
))
272 (with-test (:name
:unexpected-immediates-in-vops
)
277 do
(print (logbitp i n
))
278 (the (satisfies minusp
) i
))))
282 do
(print (lognot i
))
283 (the (satisfies minusp
) i
))))
288 (the (satisfies minusp
) i
))))
293 (the (satisfies minusp
) i
))))
298 (the (satisfies minusp
) i
))))
301 (loop for i of-type fixnum below
2
302 do
(print (logand most-positive-word
(* i
4)))
303 (the (satisfies minusp
) i
)))))
305 (with-test (:name
:/-by-integer-type
)
308 (declare ((integer 1 9) x
)
311 (or (rational -
9 (0)) (rational (0) 9)))
314 (declare ((integer 1 9) x
)
320 (declare ((rational 0 9) x
)
325 (with-test (:name
:truncate-unused-q
)
326 (checked-compile-and-assert
336 (with-test (:name
:*-by-integer-type
)
339 (declare (integer x
))
341 (or (integer 5) (integer * -
5) (integer 0 0))))
343 (with-test (:name
:truncate-transform-unused-result
)
346 (declare ((integer -
1000 0) c
)
349 (truncate (truncate (rem c -
89) -
16) 20)))
352 (with-test (:name
:rem^
2)
353 (checked-compile-and-assert
363 (with-test (:name
:deposit-field-derive-type
)
366 (declare ((member 8 10) s
))
367 (deposit-field -
21031455 (byte s
9) 1565832649825))
368 (or (integer 1565832320097 1565832320097) (integer 1565832713313 1565832713313))))
370 (with-test (:name
:logior-negative-bound
)
371 (checked-compile-and-assert
374 (declare ((integer 7703 1903468060) c
))
375 (logandc1 (/ (logorc2 c b
) -
1) c
))
378 (with-test (:name
:set-numeric-contagion
)
382 sum
(coerce n
'single-float
)))
383 (or (integer 0 0) single-float
)))
385 (with-test (:name
:overflow-transform-order
)
386 (checked-compile-and-assert
393 ((most-positive-fixnum nil
) nil
)
394 ((most-positive-fixnum t
) (condition 'type-error
))))
396 (with-test (:name
:logtest-memref-boxed
)
397 (checked-compile-and-assert
400 (declare (sb-vm:word b
))
406 (((expt 2 (1- sb-vm
:n-word-bits
))) nil
)
407 (((1+ (expt 2 (1- sb-vm
:n-word-bits
)))) t
:test
(lambda (x y
)
409 (functionp (car x
))))))
411 (with-test (:name
:range-unsigned
)
414 (declare (type (integer 1 109) d
))
415 (typep (- d
) '(integer -
47727025476642942 -
2593702250735)))
418 (with-test (:name
:signed-byte-8-p-unsigned
)
421 (declare (type (simple-array sb-vm
:word
(*)) a
)
423 (the (signed-byte 8) (aref a
0)))
426 (with-test (:name
:or-chain
)
427 (checked-compile-and-assert
431 (case b
((0 -
3) 1) (t 2)))
437 (with-test (:name
:or-chain-types
)
438 (checked-compile-and-assert
441 (declare ((integer -
1 1) b
))
449 (with-test (:name
:or-chain-tagging
)
450 (checked-compile-and-assert
461 (checked-compile-and-assert
470 (checked-compile-and-assert
475 ((-4611686018427387807) nil
)
478 (checked-compile-and-assert
485 (checked-compile-and-assert
488 (case x
((-3 -
2 17) t
)))
489 ((4611686018427387902) nil
)
494 (with-test (:name
:range
<=-same
)
495 (checked-compile-and-assert
498 (declare (type fixnum a
))
513 (with-test (:name
:/-folding
)
514 (checked-compile-and-assert
520 ((0) (condition 'division-by-zero
)))
521 (checked-compile-and-assert
527 ((0) (condition 'division-by-zero
))))
529 (with-test (:name
:dpb-size-overflow
)
530 (checked-compile-and-assert
533 (declare ((unsigned-byte 8) a
))
538 (with-test (:name
:mask-field-size-overflow
)
539 (checked-compile-and-assert
543 (mask-field (byte 78 0) a
)))