1.0.18.3: more conservative arithmetic optimizations
[sbcl/pkhuong.git] / tests / bit-vector.impure-cload.lisp
blob8eab49a2e2d081d81a858f37142d259f107612c3
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 ;;; the bitvector transforms were buggy prior to sbcl-0.7.3.4 under
13 ;;; speed-optimizing regimes; in particular, they would fail if the
14 ;;; vector length were near ARRAY-DIMENSION-LIMIT. Testing this takes
15 ;;; up a certain amount of time...
17 (declaim (optimize (speed 3) (safety 1) (space 0) (compilation-speed 0)))
19 (defun test-small-bit-vectors ()
20 ;; deal with the potential length 0 special case
21 (let ((a (make-array 0 :element-type 'bit))
22 (b (make-array 0 :element-type 'bit)))
23 (assert (equal (bit-not a) #*))
24 (assert (equal (bit-xor a b a) #*))
25 (assert (equal (bit-and a a b) #*)))
26 ;; also test some return values for sanity
27 (let ((a (make-array 33 :element-type 'bit :initial-element 0))
28 (b (make-array 33 :element-type 'bit :initial-element 0)))
29 (assert (equal (bit-not a a) #*111111111111111111111111111111111))
30 (setf (aref a 0) 0) ; a = #*011..1
31 (setf (aref b 1) 1) ; b = #*010..0
32 (assert (equal (bit-xor a b) #*001111111111111111111111111111111))
33 (assert (equal (bit-and a b) #*010000000000000000000000000000000)))
34 ;; a special COUNT transform on bitvectors; triggers on (>= SPEED SPACE)
35 (locally
36 (declare (optimize (speed 3) (space 1)))
37 (let ((bv1 (make-array 5 :element-type 'bit))
38 (bv2 (make-array 0 :element-type 'bit))
39 (bv3 (make-array 68 :element-type 'bit)))
40 (declare (type simple-bit-vector bv1 bv2 bv3))
41 (setf (sbit bv3 42) 1)
42 ;; bitvector smaller than the word size
43 (assert (= 0 (count 1 bv1)))
44 (assert (= 5 (count 0 bv1)))
45 ;; special case of 0-length bitvectors
46 (assert (= 0 (count 1 bv2)))
47 (assert (= 0 (count 0 bv2)))
48 ;; bitvector larger than the word size
49 (assert (= 1 (count 1 bv3)))
50 (assert (= 67 (count 0 bv3))))))
52 (defun inform (msg)
53 (print msg)
54 (force-output))
56 (defun test-big-bit-vectors ()
57 ;; now test the biggy, mostly that it works...
58 (let ((a (progn
59 (inform :make-array-1)
60 (make-array (1- array-dimension-limit)
61 :element-type 'bit :initial-element 0)))
62 (b (progn
63 (inform :make-array-2)
64 (make-array (1- array-dimension-limit)
65 :element-type 'bit :initial-element 0))))
66 (inform :bit-not)
67 (bit-not a a)
68 (inform :aref-1)
69 (assert (= (aref a 0) 1))
70 (inform :aref-2)
71 (assert (= (aref a (- array-dimension-limit 2)) 1))
72 (inform :bit-and)
73 (bit-and a b a)
74 (inform :aref-3)
75 (assert (= (aref a 0) 0))
76 (inform :aref-4)
77 (assert (= (aref a (- array-dimension-limit 2)) 0))))
79 (test-small-bit-vectors)
81 (eval-when (:compile-toplevel :load-toplevel :execute)
82 (defun dynamic-space-size ()
83 #+gencgc
84 (- sb-vm:dynamic-space-end sb-vm:dynamic-space-start)
85 #-gencgc
86 (- sb-vm:dynamic-0-space-end sb-vm:dynamic-0-space-start)))
88 ;; except on machines where the arrays won't fit into the dynamic space.
89 #+#.(cl:if (cl:> (cl-user::dynamic-space-size)
90 (cl:truncate (cl:1- cl:array-dimension-limit)
91 sb-vm:n-word-bits))
92 '(and)
93 '(or))
94 (test-big-bit-vectors)