Fix test failure as a result of #+immobile-code.
[sbcl.git] / contrib / sb-rotate-byte / compiler.lisp
blob705b56be46d68af9d2ef4c084be2247094ebd603
1 (in-package "SB-ROTATE-BYTE")
3 (defknown rotate-byte (integer byte-specifier integer) integer
4 (foldable flushable)
5 :overwrite-fndb-silently t)
6 (defknown %rotate-byte (integer bit-index bit-index integer) integer
7 (foldable flushable)
8 :overwrite-fndb-silently t)
9 (defknown %unsigned-32-rotate-byte ((integer -31 31) (unsigned-byte 32))
10 (unsigned-byte 32)
11 (foldable flushable)
12 :overwrite-fndb-silently t)
13 #+64-bit
14 (defknown %unsigned-64-rotate-byte ((integer -63 63) (unsigned-byte 64))
15 (unsigned-byte 64)
16 (foldable flushable)
17 :overwrite-fndb-silently t)
19 (macrolet (;; see src/compiler/srctran.lisp
20 (with-byte-specifier ((size-var pos-var spec) &body body)
21 (once-only ((spec `(macroexpand ,spec))
22 (temp '(gensym)))
23 `(if (and (consp ,spec)
24 (eq (car ,spec) 'byte)
25 (= (length ,spec) 3))
26 (let ((,size-var (second ,spec))
27 (,pos-var (third ,spec)))
28 ,@body)
29 (let ((,size-var `(byte-size ,,temp))
30 (,pos-var `(byte-position ,,temp)))
31 `(let ((,,temp ,,spec))
32 ,,@body))))))
33 (define-source-transform rotate-byte (count spec num)
34 (with-byte-specifier (size pos spec)
35 `(%rotate-byte ,count ,size ,pos ,num))))
37 (defoptimizer (%rotate-byte derive-type) ((count size posn num))
38 ;; FIXME: this looks fairly unwieldy. I'm sure it can be made
39 ;; simpler, and also be made to deal with negative integers too.
40 (declare (ignore count posn))
41 (let ((size (sb-c::lvar-type size)))
42 (if (numeric-type-p size)
43 (let ((size-high (numeric-type-high size))
44 (num-type (sb-c::lvar-type num)))
45 (if (and size-high
46 num-type
47 (<= size-high sb-vm:n-word-bits)
48 (csubtypep num-type
49 (specifier-type `(unsigned-byte ,size-high))))
50 (specifier-type `(unsigned-byte ,size-high))
51 *universal-type*))
52 *universal-type*)))
54 (deftransform %rotate-byte ((count size pos integer)
55 ((integer -31 31)
56 (constant-arg (member 32))
57 (constant-arg (member 0))
58 (unsigned-byte 32)) *)
59 "inline 32-bit rotation"
60 '(%unsigned-32-rotate-byte count integer))
62 ;; Generic implementation for platforms that don't supply VOPs for 32-bit
63 ;; rotate.
64 #-(or x86 x86-64 ppc arm arm64)
65 (deftransform %unsigned-32-rotate-byte ((.count. .integer.)
66 ((integer -31 31)
67 (unsigned-byte 32)) *)
68 '(if (< .count. 0)
69 (logior (ldb (byte 32 0) (ash .integer. (+ .count. 32)))
70 (ash .integer. .count.))
71 (logior (ldb (byte 32 0) (ash .integer. .count.))
72 (ash .integer. (- .count. 32)))))
74 #+64-bit
75 (deftransform %rotate-byte ((count size pos integer)
76 ((integer -63 63)
77 (constant-arg (member 64))
78 (constant-arg (member 0))
79 (unsigned-byte 64)) *)
80 "inline 64-bit rotation"
81 '(%unsigned-64-rotate-byte count integer))
83 ;;; This transform needs to come after the others to ensure it gets
84 ;;; first crack at a zero COUNT, since transforms are currently run
85 ;;; latest-defined first.
86 (deftransform %rotate-byte ((count size pos integer)
87 ((constant-arg (member 0)) * * *) *)
88 "fold identity operation"
89 'integer)