0.8.3.79
[sbcl/lichteblau.git] / tests / arith.pure.lisp
blobce3cfa45bc9b5699fb4b40f7163549f235e9c155
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 (cl:in-package :cl-user)
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 (macrolet ((test (op res1 res2)
21 `(progn
22 (assert (= (,op 4 2) ,res1))
23 (assert (= (,op 2 4) ,res2))
24 (assert (= (funcall (compile nil (lambda (x y) (,op x y))) 4 2)
25 ,res1))
26 (assert (= (funcall (compile nil (lambda (x y) (,op x y))) 2 4)
27 ,res2)))))
28 (test + 6 6)
29 (test - 2 -2)
30 (test * 8 8)
31 (test / 2 1/2)
32 (test expt 16 16))
34 ;;; In a bug reported by Wolfhard Buss on cmucl-imp 2002-06-18 (BUG
35 ;;; 184), sbcl didn't catch all divisions by zero, notably divisions
36 ;;; of bignums and ratios by 0. Fixed in sbcl-0.7.6.13.
37 (assert (raises-error? (/ 2/3 0) division-by-zero))
38 (assert (raises-error? (/ (1+ most-positive-fixnum) 0) division-by-zero))
40 ;;; In a bug reported by Raymond Toy on cmucl-imp 2002-07-18, (COERCE
41 ;;; <RATIONAL> '(COMPLEX FLOAT)) was failing to return a complex
42 ;;; float; a patch was given by Wolfhard Buss cmucl-imp 2002-07-19.
43 (assert (= (coerce 1 '(complex float)) #c(1.0 0.0)))
44 (assert (= (coerce 1/2 '(complex float)) #c(0.5 0.0)))
45 (assert (= (coerce 1.0d0 '(complex float)) #c(1.0d0 0.0d0)))
47 ;;; COERCE also sometimes failed to verify that a particular coercion
48 ;;; was possible (in particular coercing rationals to bounded float
49 ;;; types.
50 (assert (raises-error? (coerce 1 '(float 2.0 3.0)) type-error))
51 (assert (raises-error? (coerce 1 '(single-float -1.0 0.0)) type-error))
52 (assert (eql (coerce 1 '(single-float -1.0 2.0)) 1.0))
54 ;;; ANSI says MIN and MAX should signal TYPE-ERROR if any argument
55 ;;; isn't REAL. SBCL 0.7.7 didn't in the 1-arg case. (reported as a
56 ;;; bug in CMU CL on #lisp IRC by lrasinen 2002-09-01)
57 (assert (null (ignore-errors (min '(1 2 3)))))
58 (assert (= (min -1) -1))
59 (assert (null (ignore-errors (min 1 #(1 2 3)))))
60 (assert (= (min 10 11) 10))
61 (assert (null (ignore-errors (min (find-package "CL") -5.0))))
62 (assert (= (min 5.0 -3) -3))
63 (assert (null (ignore-errors (max #c(4 3)))))
64 (assert (= (max 0) 0))
65 (assert (null (ignore-errors (max "MIX" 3))))
66 (assert (= (max -1 10.0) 10.0))
67 (assert (null (ignore-errors (max 3 #'max))))
68 (assert (= (max -3 0) 0))
70 ;;; (CEILING x 2^k) was optimized incorrectly
71 (loop for divisor in '(-4 4)
72 for ceiler = (compile nil `(lambda (x)
73 (declare (fixnum x))
74 (declare (optimize (speed 3)))
75 (ceiling x ,divisor)))
76 do (loop for i from -5 to 5
77 for exact-q = (/ i divisor)
78 do (multiple-value-bind (q r)
79 (funcall ceiler i)
80 (assert (= (+ (* q divisor) r) i))
81 (assert (<= exact-q q))
82 (assert (< q (1+ exact-q))))))
84 ;;; CEILING had a corner case, spotted by Paul Dietz
85 (assert (= (ceiling most-negative-fixnum (1+ most-positive-fixnum)) -1))
87 ;;; give any optimizers of constant multiplication a light testing.
88 ;;; 100 may seem low, but (a) it caught CSR's initial errors, and (b)
89 ;;; before checking in, CSR tested with 10000. So one hundred
90 ;;; checkins later, we'll have doubled the coverage.
91 (dotimes (i 100)
92 (let* ((x (random most-positive-fixnum))
93 (x2 (* x 2))
94 (x3 (* x 3)))
95 (let ((fn (handler-bind ((sb-ext:compiler-note #'error))
96 (compile nil
97 `(lambda (y)
98 (declare (optimize speed) (type (integer 0 3) y))
99 (* y ,x))))))
100 (unless (and (= (funcall fn 0) 0)
101 (= (funcall fn 1) x)
102 (= (funcall fn 2) x2)
103 (= (funcall fn 3) x3))
104 (error "bad results for ~D" x)))))
106 ;;; Bugs reported by Paul Dietz:
108 ;;; (GCD 0 x) must return (abs x)
109 (dolist (x (list -10 (* 3 most-negative-fixnum)))
110 (assert (= (gcd 0 x) (abs x))))
111 ;;; LCM returns a non-negative number
112 (assert (= (lcm 4 -10) 20))
113 (assert (= (lcm 0 0) 0))
115 ;;; PPC bignum arithmetic bug:
116 (multiple-value-bind (quo rem)
117 (truncate 291351647815394962053040658028983955 10000000000000000000000000)
118 (assert (= quo 29135164781))
119 (assert (= rem 5394962053040658028983955)))
121 ;;; x86 LEA bug:
122 (assert (= (funcall
123 (compile nil '(lambda (x) (declare (bit x)) (+ x #xf0000000)))
125 #xf0000001))
127 ;;; LOGBITP on bignums:
128 (dolist (x '(((1+ most-positive-fixnum) 1 nil)
129 ((1+ most-positive-fixnum) -1 t)
130 ((1+ most-positive-fixnum) (1+ most-positive-fixnum) nil)
131 ((1+ most-positive-fixnum) (1- most-negative-fixnum) t)
132 (1 (ash most-negative-fixnum 1) nil)
133 (29 most-negative-fixnum t)
134 (30 (ash most-negative-fixnum 1) t)
135 (31 (ash most-negative-fixnum 1) t)
136 (64 (ash most-negative-fixnum 36) nil)
137 (65 (ash most-negative-fixnum 36) t)))
138 (destructuring-bind (index int result) x
139 (assert (eq (eval `(logbitp ,index ,int)) result))))
141 ;;; off-by-1 type inference error for %DPB and %DEPOSIT-FIELD:
142 (let ((f (compile nil '(lambda (b)
143 (integer-length (dpb b (byte 4 28) -1005))))))
144 (assert (= (funcall f 1230070) 32)))
145 (let ((f (compile nil '(lambda (b)
146 (integer-length (deposit-field b (byte 4 28) -1005))))))
147 (assert (= (funcall f 1230070) 32)))
149 ;;; type inference leading to an internal compiler error:
150 (let ((f (compile nil '(lambda (x)
151 (declare (type fixnum x))
152 (ldb (byte 0 0) x)))))
153 (assert (= (funcall f 1) 0))
154 (assert (= (funcall f most-positive-fixnum) 0))
155 (assert (= (funcall f -1) 0)))
157 ;;; Alpha bignum arithmetic bug:
158 (assert (= (* 966082078641 419216044685) 404997107848943140073085))