Debian Codegraph @

src/cmp/cmpopt-num.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;; Copyright (c) 2010, Juan Jose Garcia Ripoll
5 ;;;;
6 ;;;; See the file 'LICENSE' for the copyright details.
7 ;;;;
8
9 ;;;; Optimizer for numerical expressions.
10
11 (in-package "COMPILER")
12
13 ;;;
14 ;;; We transform BOOLE into the individual operations, which have inliners
15 ;;;
16
17 (define-compiler-macro boole (&whole form op-code op1 op2)
18 (or (and (constantp op-code *cmp-env*)
19 (case (ext:constant-form-value op-code *cmp-env*)
20 (#. boole-clr `(progn (ext:checked-value integer ,op1) (ext:checked-value integer ,op2) 0))
21 (#. boole-set `(progn (ext:checked-value integer ,op1) (ext:checked-value integer ,op2) -1))
22 (#. boole-1 `(prog1 (ext:checked-value integer ,op1) (ext:checked-value integer ,op2)))
23 (#. boole-2 `(progn (ext:checked-value integer ,op1) (ext:checked-value integer ,op2)))
24 (#. boole-c1 `(prog1 (lognot ,op1) (ext:checked-value integer ,op2)))
25 (#. boole-c2 `(progn (ext:checked-value integer ,op1) (lognot ,op2)))
26 (#. boole-and `(logand ,op1 ,op2))
27 (#. boole-ior `(logior ,op1 ,op2))
28 (#. boole-xor `(logxor ,op1 ,op2))
29 (#. boole-eqv `(logeqv ,op1 ,op2))
30 (#. boole-nand `(lognand ,op1 ,op2))
31 (#. boole-nor `(lognor ,op1 ,op2))
32 (#. boole-andc1 `(logandc1 ,op1 ,op2))
33 (#. boole-andc2 `(logandc2 ,op1 ,op2))
34 (#. boole-orc1 `(logorc1 ,op1 ,op2))
35 (#. boole-orc2 `(logorc2 ,op1 ,op2))))
36 form))
37
38 ;;;
39 ;;; LDB
40 ;;; Look for inline expansion of LDB1 in sysfun.lsp
41 ;;;
42
43 (defun inline-bytespec (bytespec)
44 (declare (si::c-local))
45 (and (consp bytespec)
46 (eq 'BYTE (car bytespec))
47 (= (length bytespec) 3)
48 (policy-inline-bit-operations)))
49
50 (define-compiler-macro ldb (&whole whole bytespec integer)
51 (if (inline-bytespec bytespec)
52 (ext:with-clean-symbols (%pos %size)
53 `(with-let*-type-check ((%size ,(second bytespec) unsigned-byte)
54 (%pos ,(third bytespec) unsigned-byte))
55 (logand (lognot (ash -1 %size)) (ash ,integer (- %pos)))))
56 whole))
57
58 (define-compiler-macro ldb-test (&whole whole bytespec integer)
59 (if (inline-bytespec bytespec)
60 `(not (zerop (mask-field ,bytespec ,integer)))
61 whole))
62
63 (define-compiler-macro mask-field (&whole whole bytespec integer)
64 (if (inline-bytespec bytespec)
65 (ext:with-clean-symbols (%pos %size)
66 `(with-let*-type-check ((%size ,(second bytespec) unsigned-byte)
67 (%pos ,(third bytespec) unsigned-byte))
68 (logand (ash (lognot (ash -1 %size)) %pos)
69 ,integer)))
70 whole))
71
72 (define-compiler-macro dpb (&whole whole newbyte bytespec integer)
73 (if (inline-bytespec bytespec)
74 (ext:with-clean-symbols (%pos %size %mask)
75 `(with-let*-type-check ((%size ,(second bytespec) unsigned-byte)
76 (%pos ,(third bytespec) unsigned-byte)
77 (%mask (ash (lognot (ash -1 %size)) %pos) t))
78 (logior (logand (ash ,newbyte %pos) %mask)
79 (logandc2 ,integer %mask))))
80 whole))
81
82 (define-compiler-macro deposit-field (&whole whole newbyte bytespec integer)
83 (if (inline-bytespec bytespec)
84 (ext:with-clean-symbols (%pos %size %mask)
85 `(with-let*-type-check ((%size ,(second bytespec) unsigned-byte)
86 (%pos ,(third bytespec) unsigned-byte)
87 (%mask (ash (lognot (ash -1 %size)) %pos) t))
88 (logior (logand ,newbyte %mask)
89 (logandc2 ,integer %mask)
90 )))
91 whole))
92
93 (define-setf-expander ldb (&environment env bytespec int)
94 (multiple-value-bind (temps vals stores store-form access-form)
95 (get-setf-expansion int env)
96 (if (inline-bytespec bytespec)
97 (let* ((bpos (gensym))
98 (bsize (gensym))
99 (store (gensym))
100 (btemp `(byte ,bpos ,bsize))
101 (stemp (first stores)))
102 (values `(,bpos ,bsize ,@temps)
103 `(,(second bytespec) ,(third bytespec) ,@vals)
104 `(,store)
105 `(let ((,stemp (dpb ,store ,btemp ,access-form)))
106 ,store-form ,store)
107 `(ldb ,btemp ,access-form)))
108 (let* ((btemp (gensym))
109 (store (gensym))
110 (stemp (first stores)))
111 (values `(,btemp ,@temps)
112 `(,bytespec ,@vals)
113 `(,store)
114 `(let ((,stemp (dpb ,store ,btemp ,access-form)))
115 ,store-form ,store)
116 `(ldb ,btemp ,access-form))))))
117
118 (define-setf-expander mask-field (&environment env bytespec int)
119 (multiple-value-bind (temps vals stores store-form access-form)
120 (get-setf-expansion int env)
121 (if (inline-bytespec bytespec)
122 (let* ((bpos (gensym))
123 (bsize (gensym))
124 (store (gensym))
125 (btemp `(byte ,bpos ,bsize))
126 (stemp (first stores)))
127 (values `(,bpos ,bsize ,@temps)
128 `(,(second bytespec) ,(third bytespec) ,@vals)
129 `(,store)
130 `(let ((,stemp (deposit-field ,store ,btemp ,access-form)))
131 ,store-form ,store)
132 `(mask-field ,btemp ,access-form)))
133 (let* ((btemp (gensym))
134 (store (gensym))
135 (stemp (first stores)))
136 (values `(,btemp ,@temps)
137 `(,bytespec ,@vals)
138 `(,store)
139 `(let ((,stemp (deposit-field ,store ,btemp ,access-form)))
140 ,store-form ,store)
141 `(mask-field ,btemp ,access-form))))))
142
143 ;;;
144 ;;; ASH
145 ;;;
146
147 (define-compiler-macro ash (&whole whole argument shift)
148 (cond ((and (integerp argument)
149 (integerp shift))
150 (ash argument shift))
151 ((and (policy-assume-right-type)
152 (integerp shift))
153 (if (zerop shift)
154 argument
155 `(shift ,argument ,shift)))
156 (t
157 whole)))
158
159 (defun simplify-arithmetic (operator args whole)
160 (if (every #'numberp args)
161 (apply operator args)
162 (let ((l (length args)))
163 (cond ((> l 2)
164 (simplify-arithmetic
165 operator
166 (list* (simplify-arithmetic operator
167 (list (first args) (second args))
168 nil)
169 (cddr args))
170 nil))
171 ((= l 2)
172 (or whole (list* operator args)))
173 ((= l 1)
174 (if (or (eq operator '*) (eq operator '+))
175 (first args)
176 (or whole (list* operator args))))
177 ((eq operator '*)
178 1)
179 ((eq operator '+)
180 0)
181 (t
182 (error 'simple-program-error
183 :format-error "Wrong number of arguments for operator ~a in ~a"
184 :format-arguments (list operator (or whole
185 (list* operator args)))))))))
186
187 ;;; INV These compiler macros are expected to be in effect by C inliners
188 ;;; defined for corresponding operators. -- jd 2023-12-04
189
190 (define-compiler-macro * (&whole all &rest args)
191 (simplify-arithmetic '* args all))
192
193 (define-compiler-macro + (&whole all &rest args)
194 (simplify-arithmetic '+ args all))
195
196 (define-compiler-macro / (&whole all &rest args)
197 (simplify-arithmetic '/ args all))
198
199 (define-compiler-macro - (&whole all &rest args)
200 (simplify-arithmetic '- args all))