Faster EQL for bignums on x86-64
[sbcl.git] / src / code / pred.lisp
blob54ecfb3c13b2009b17e5b20ef752e9d900e960fd
1 ;;;; predicate functions (EQUAL and friends, and type predicates)
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; miscellaneous non-primitive predicates
16 #!-sb-fluid (declaim (inline streamp))
17 (defun streamp (stream)
18 (typep stream 'stream))
20 ;;; various (VECTOR FOO) type predicates, not implemented as simple
21 ;;; widetag tests
22 (macrolet
23 ((def ()
24 `(progn
25 ,@(loop for (name spec) in *vector-without-complex-typecode-infos*
26 collect `(defun ,name (x)
27 (or (typep x '(simple-array ,spec (*)))
28 (and (complex-vector-p x)
29 (do ((data (%array-data-vector x) (%array-data-vector data)))
30 ((not (array-header-p data)) (typep data '(simple-array ,spec (*))))))))))))
31 (def))
33 ;;; Is X an extended sequence?
34 ;; This is like the "hierarchical layout depths for other things"
35 ;; case of the TYPEP transform (cf 'typetran'). Ideally it would
36 ;; be preferable to share TYPEP's code rather than repeat it here.
37 (declaim (maybe-inline extended-sequence-p))
38 (defun extended-sequence-p (x)
39 (let* ((slayout #.(info :type :compiler-layout 'sequence))
40 (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
41 (layout
42 ;; It is not an error to define a class that is both SEQUENCE and
43 ;; FUNCALLABLE-INSTANCE with metaclass FUNCALLABLE-STANDARD-CLASS
44 (cond ((%instancep x) (%instance-layout x))
45 ((funcallable-instance-p x) (%funcallable-instance-layout x))
46 (t (return-from extended-sequence-p nil)))))
47 (when (layout-invalid layout)
48 (setq layout (update-object-layout-or-invalid x slayout)))
49 ;; It's _nearly_ impossible to create an instance which is exactly
50 ;; of type SEQUENCE. To wit: (make-instance 'sequence) =>
51 ;; "Cannot allocate an instance of #<BUILT-IN-CLASS SEQUENCE>."
52 ;; We should not need to check for that, just the 'inherits' vector.
53 ;; However, bootstrap code does a sleazy thing, making an instance of
54 ;; the abstract base type which is impossible for user code to do.
55 ;; Preferably the prototype instance for SEQUENCE would be one that could
56 ;; exist, so it would be a STANDARD-OBJECT and SEQUENCE. But it's not.
57 ;; Hence we have to check for a layout that no code using the documented
58 ;; sequence API would ever see, just to get the boundary case right.
59 ;; Note also:
60 ;; - Some builtins use a prototype object that is strictly deeper than
61 ;; layout of the named class because it is indeed the case that no
62 ;; object's layout can ever be EQ to that of the ancestor.
63 ;; e.g. a fixnum as representative of class REAL
64 ;; - Some builtins actually fail (TYPEP (CLASS-PROTOTYPE X) X)
65 ;; but that's not an excuse for getting SEQUENCE wrong:
66 ;; (CLASS-PROTOTYPE (FIND-CLASS 'FLOAT)) => 42
67 ;; (CLASS-PROTOTYPE (FIND-CLASS 'VECTOR)) => 42
68 ;; (CLASS-PROTOTYPE (FIND-CLASS 'LIST)) => 42
69 ;; (CLASS-PROTOTYPE (FIND-CLASS 'STRING)) => 42
70 (let ((inherits (layout-inherits (truly-the layout layout))))
71 (declare (optimize (safety 0)))
72 (if (and (> (length inherits) depthoid)
73 (eq (svref inherits depthoid) slayout))
75 (eq layout slayout)))))
77 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
78 (defun sequencep (x)
79 (declare (inline extended-sequence-p))
80 (or (listp x) (vectorp x) (extended-sequence-p x)))
82 ;;;; primitive predicates. These must be supported directly by the
83 ;;;; compiler.
85 (defun not (object)
86 #!+sb-doc
87 "Return T if X is NIL, otherwise return NIL."
88 (not object))
90 ;;; All the primitive type predicate wrappers share a parallel form..
91 (macrolet ((def-type-predicate-wrapper (pred)
92 (let* ((name (symbol-name pred))
93 (stem (string-left-trim "%" (string-right-trim "P-" name)))
94 (article (if (position (schar name 0) "AEIOU") "an" "a")))
95 `(defun ,pred (object)
96 #!+sb-doc
97 ,(format nil
98 "Return true if OBJECT is ~A ~A, and NIL otherwise."
99 article
100 stem)
101 ;; (falling through to low-level implementation)
102 (,pred object)))))
103 (def-type-predicate-wrapper array-header-p)
104 (def-type-predicate-wrapper arrayp)
105 (def-type-predicate-wrapper atom)
106 ;; Testing for BASE-CHAR-P is usually redundant on #-sb-unicode,
107 ;; remove it there completely so that #-sb-unicode build will
108 ;; break when it's used.
109 #!+sb-unicode (def-type-predicate-wrapper base-char-p)
110 (def-type-predicate-wrapper base-string-p)
111 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
112 (def-type-predicate-wrapper bignump)
113 (def-type-predicate-wrapper bit-vector-p)
114 (def-type-predicate-wrapper characterp)
115 (def-type-predicate-wrapper code-component-p)
116 (def-type-predicate-wrapper consp)
117 (def-type-predicate-wrapper compiled-function-p)
118 (def-type-predicate-wrapper complexp)
119 (def-type-predicate-wrapper complex-double-float-p)
120 (def-type-predicate-wrapper complex-float-p)
121 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
122 (def-type-predicate-wrapper complex-rational-p)
123 (def-type-predicate-wrapper complex-single-float-p)
124 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
125 ;; the type it tests for in the Common Lisp type system, and since it's
126 ;; only used in the implementation of a few specialized things.)
127 (def-type-predicate-wrapper double-float-p)
128 (def-type-predicate-wrapper extended-char-p)
129 (def-type-predicate-wrapper fdefn-p)
130 (def-type-predicate-wrapper fixnump)
131 (def-type-predicate-wrapper floatp)
132 (def-type-predicate-wrapper functionp)
133 (def-type-predicate-wrapper integerp)
134 (def-type-predicate-wrapper listp)
135 (def-type-predicate-wrapper long-float-p)
136 (def-type-predicate-wrapper lra-p)
137 (def-type-predicate-wrapper null)
138 (def-type-predicate-wrapper numberp)
139 (def-type-predicate-wrapper rationalp)
140 (def-type-predicate-wrapper ratiop)
141 (def-type-predicate-wrapper realp)
142 (def-type-predicate-wrapper short-float-p)
143 (def-type-predicate-wrapper single-float-p)
144 #!+sb-simd-pack (def-type-predicate-wrapper simd-pack-p)
145 (def-type-predicate-wrapper %instancep)
146 (def-type-predicate-wrapper symbolp)
147 (def-type-predicate-wrapper %other-pointer-p)
148 (def-type-predicate-wrapper system-area-pointer-p)
149 (def-type-predicate-wrapper weak-pointer-p)
150 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
151 (progn
152 (def-type-predicate-wrapper unsigned-byte-32-p)
153 (def-type-predicate-wrapper signed-byte-32-p))
154 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
155 (progn
156 (def-type-predicate-wrapper unsigned-byte-64-p)
157 (def-type-predicate-wrapper signed-byte-64-p))
158 ;; Specialized array types
159 (macrolet ((saetp-defs ()
160 `(progn
161 ,@(map 'list
162 (lambda (saetp)
163 `(def-type-predicate-wrapper
164 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
165 sb!vm:*specialized-array-element-type-properties*))))
166 (saetp-defs))
167 ;; Other array types
168 (def-type-predicate-wrapper simple-array-p)
169 (def-type-predicate-wrapper simple-rank-1-array-*-p)
170 (def-type-predicate-wrapper simple-string-p)
171 (def-type-predicate-wrapper stringp)
172 (def-type-predicate-wrapper vectorp)
173 (def-type-predicate-wrapper vector-nil-p))
175 #!+(or x86 x86-64 arm)
176 (defun fixnum-mod-p (x limit)
177 (and (fixnump x)
178 (<= 0 x limit)))
180 #!+(or x86 x86-64 ppc)
181 (defun %other-pointer-subtype-p (x choices)
182 (and (%other-pointer-p x)
183 (member (%other-pointer-widetag x) choices)
186 ;;; Return the specifier for the type of object. This is not simply
187 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
188 ;;; goals than TYPE-OF. In particular, speed is more important than
189 ;;; precision here, and it is not permitted to return member types,
190 ;;; negation, union, or intersection types.
191 (defun type-of (object)
192 #!+sb-doc
193 "Return the type of OBJECT."
194 (typecase object
195 (fixnum
196 (cond
197 ((<= 0 object 1) 'bit)
198 ((< object 0) 'fixnum)
199 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
200 (integer
201 (if (>= object 0)
202 '(integer #.(1+ sb!xc:most-positive-fixnum))
203 'bignum))
204 (standard-char 'standard-char)
205 (base-char 'base-char)
206 (extended-char 'extended-char)
207 ((member t) 'boolean)
208 (keyword 'keyword)
209 ((or array complex #!+sb-simd-pack simd-pack)
210 (let ((sb!kernel::*unparse-allow-negation* nil))
211 (declare (special sb!kernel::*unparse-allow-negation*)) ; forward ref
212 (type-specifier (ctype-of object))))
214 (let* ((classoid (layout-classoid (layout-of object)))
215 (name (classoid-name classoid)))
216 (if (%instancep object)
217 (case name
218 (sb!alien-internals:alien-value
219 `(alien
220 ,(sb!alien-internals:unparse-alien-type
221 (sb!alien-internals:alien-value-type object))))
223 (let ((pname (classoid-proper-name classoid)))
224 (if (classoid-p pname)
225 (classoid-pcl-class pname)
226 pname))))
227 name)))))
229 ;;;; equality predicates
231 ;;; This is real simple, 'cause the compiler takes care of it.
232 (defun eq (obj1 obj2)
233 #!+sb-doc
234 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
235 (eq obj1 obj2))
237 (declaim (inline %eql))
238 (defun %eql (obj1 obj2)
239 #!+sb-doc
240 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
241 (or (eq obj1 obj2)
242 (if (or (typep obj2 'fixnum)
243 (not (typep obj2 'number)))
245 ;; I would think that we could do slightly better here by testing that
246 ;; both objs are OTHER-POINTER-P with equal %OTHER-POINTER-WIDETAGs.
247 ;; Then dispatch on obj2 and elide the TYPEP on obj1 using TRULY-THE.
248 ;; Also would need to deal with immediate single-float for 64-bit.
249 (macrolet ((foo (&rest stuff)
250 `(typecase obj2
251 ,@(mapcar (lambda (foo)
252 (let ((type (car foo))
253 (fn (cadr foo)))
254 `(,type
255 (and (typep obj1 ',type)
256 (,fn obj1 obj2)))))
257 stuff))))
258 (foo
259 (single-float eql)
260 (double-float eql)
261 #!+long-float
262 (long-float eql)
263 (bignum
264 #!-integer-eql-vop (lambda (x y) (zerop (bignum-compare x y)))
265 #!+integer-eql-vop eql) ; will become %eql/integer
266 (ratio
267 (lambda (x y)
268 (and (eql (numerator x) (numerator y))
269 (eql (denominator x) (denominator y)))))
270 (complex
271 (lambda (x y)
272 (and (eql (realpart x) (realpart y))
273 (eql (imagpart x) (imagpart y))))))))))
275 (defun eql (x y)
276 (%eql x y))
278 (defun bit-vector-= (x y)
279 (declare (type bit-vector x y))
280 (cond ((eq x y))
281 ((and (simple-bit-vector-p x)
282 (simple-bit-vector-p y))
283 (bit-vector-= x y)) ; DEFTRANSFORM
285 (and (= (length x) (length y))
286 (do ((i 0 (1+ i))
287 (length (length x)))
288 ((= i length) t)
289 (declare (fixnum i))
290 (unless (= (bit x i) (bit y i))
291 (return nil)))))))
293 (defun equal (x y)
294 #!+sb-doc
295 "Return T if X and Y are EQL or if they are structured components whose
296 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
297 length and have identical components. Other arrays must be EQ to be EQUAL."
298 ;; Non-tail self-recursion implemented with a local auxiliary function
299 ;; is a lot faster than doing it the straightforward way (at least
300 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
301 (labels ((equal-aux (x y)
302 (cond ((%eql x y)
304 ((consp x)
305 (and (consp y)
306 (equal-aux (car x) (car y))
307 (equal-aux (cdr x) (cdr y))))
308 ((stringp x)
309 (and (stringp y) (string= x y)))
310 ((pathnamep x)
311 (and (pathnamep y) (pathname= x y)))
312 ((bit-vector-p x)
313 (and (bit-vector-p y)
314 (bit-vector-= x y)))
315 (t nil))))
316 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
317 ;; of 200 times with INLINE). -- JES, 2005-12-30
318 (declare (maybe-inline equal-aux))
319 (equal-aux x y)))
321 ;;; EQUALP comparison of HASH-TABLE values
322 (defun hash-table-equalp (x y)
323 (declare (type hash-table x y))
324 (or (eq x y)
325 (and (hash-table-p y)
326 (eql (hash-table-count x) (hash-table-count y))
327 (eql (hash-table-test x) (hash-table-test y))
328 (block comparison-of-entries
329 (maphash (lambda (key x-value)
330 (multiple-value-bind (y-value y-value-p)
331 (gethash key y)
332 (unless (and y-value-p (equalp x-value y-value))
333 (return-from comparison-of-entries nil))))
335 t))))
337 (defun equalp (x y)
338 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
339 ; and HASH-TABLEs.
340 "This is like EQUAL, except more liberal in several respects.
341 Numbers may be of different types, as long as the values are identical
342 after coercion. Characters may differ in alphabetic case. Vectors and
343 arrays must have identical dimensions and EQUALP elements, but may differ
344 in their type restriction."
345 (cond ((eq x y) t)
346 ((characterp x) (and (characterp y) (char-equal x y)))
347 ((numberp x) (and (numberp y) (= x y)))
348 ((consp x)
349 (and (consp y)
350 (equalp (car x) (car y))
351 (equalp (cdr x) (cdr y))))
352 ((pathnamep x)
353 (and (pathnamep y) (pathname= x y)))
354 ((hash-table-p x)
355 (and (hash-table-p y)
356 (hash-table-equalp x y)))
357 ((%instancep x)
358 (let ((layout-x (%instance-layout x)))
359 (and (%instancep y)
360 (eq layout-x (%instance-layout y))
361 ;; TODO: store one bit in the layout indicating whether EQUALP
362 ;; should scan slots. (basically a STRUCTURE-CLASSOID-P bit)
363 (structure-classoid-p (layout-classoid layout-x))
364 (macrolet ((slot-ref-equalp ()
365 `(let ((x-el (%instance-ref x i))
366 (y-el (%instance-ref y i)))
367 (or (eq x-el y-el) (equalp x-el y-el)))))
368 #!-interleaved-raw-slots
369 (let ((raw-len (layout-n-untagged-slots layout-x))
370 (total-len (layout-length layout-x)))
371 (and (dotimes (i (- total-len raw-len) t)
372 (unless (slot-ref-equalp)
373 (return nil)))
374 (or (zerop raw-len)
375 (raw-instance-slots-equalp layout-x x y))))
376 #!+interleaved-raw-slots
377 (let ((metadata (layout-untagged-bitmap layout-x)))
378 (if (zerop metadata)
379 (loop for i of-type index from sb!vm:instance-data-start
380 below (layout-length layout-x)
381 always (slot-ref-equalp))
382 (let ((comparators (layout-equalp-tests layout-x)))
383 (unless (= (length comparators)
384 (- (layout-length layout-x) sb!vm:instance-data-start))
385 (bug "EQUALP got incomplete instance layout"))
386 ;; See remark at the source code for %TARGET-DEFSTRUCT
387 ;; explaining how to use the vector of comparators.
388 (loop for i of-type index from sb!vm:instance-data-start
389 below (layout-length layout-x)
390 for test = (data-vector-ref
391 comparators (- i sb!vm:instance-data-start))
392 always (cond ((eql test 0) (slot-ref-equalp))
393 ((functionp test)
394 (funcall test i x y))
395 (t))))))))))
396 ((vectorp x)
397 (let ((length (length x)))
398 (and (vectorp y)
399 (= length (length y))
400 (dotimes (i length t)
401 (let ((x-el (aref x i))
402 (y-el (aref y i)))
403 (unless (or (eq x-el y-el)
404 (equalp x-el y-el))
405 (return nil)))))))
406 ((arrayp x)
407 (and (arrayp y)
408 (= (array-rank x) (array-rank y))
409 (dotimes (axis (array-rank x) t)
410 (unless (= (array-dimension x axis)
411 (array-dimension y axis))
412 (return nil)))
413 (dotimes (index (array-total-size x) t)
414 (let ((x-el (row-major-aref x index))
415 (y-el (row-major-aref y index)))
416 (unless (or (eq x-el y-el)
417 (equalp x-el y-el))
418 (return nil))))))
419 (t nil)))
421 (/show0 "about to do test cases in pred.lisp")
422 #!+sb-test
423 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
424 (0.0 1.0 nil)
425 (#c(1 0) #c(1.0 0.0) t)
426 (#c(0 1) #c(0.0 1.0) t)
427 (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
428 ("Hello" "hello" t)
429 ("Hello" #(#\h #\E #\l #\l #\o) t)
430 ("Hello" "goodbye" nil))))
431 (/show0 "TEST-CASES bound in pred.lisp")
432 (dolist (test-case test-cases)
433 (/show0 "about to do a TEST-CASE in pred.lisp")
434 (destructuring-bind (x y expected-result) test-case
435 (let* ((result (equalp x y))
436 (bresult (if result 1 0))
437 (expected-bresult (if expected-result 1 0)))
438 (unless (= bresult expected-bresult)
439 (/show0 "failing test in pred.lisp")
440 (error "failed test (EQUALP ~S ~S)" x y))))))
441 (/show0 "done with test cases in pred.lisp")