*** empty log message ***
[sb-simd.git] / generate-sse-instructions.lisp
blob2bea781fec834d235bbd277fae4b1d5d8699ae85
1 #|
2 Copyright (c) 2005 Risto Laakso
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 instruction reference:
31 http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/26568.pdf
34 TODO:
36 FXRSTOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
37 FXSAVE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
39 LDMXCSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
41 MOVDQ2Q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
43 MOVQ2DQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
45 STMXCSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
50 (declaim (optimize (debug 3)))
52 (defun emit-ops (ops)
53 (loop for op in ops
54 collect `(emit-byte segment ,op) into result
55 finally (return result)))
58 (defun gen-ops (&optional (stream t))
60 ;;; instructions like:
61 ;;; ADDPS xmm1, xmm2/mem128 0F 58 /r
62 (loop for (inst . ops) in
64 ;; single precision float
65 (addps #x0F #x58)
66 (addsubps #xF2 #x0F #xD0)
67 (andnps #x0F #x55)
68 (andps #x0F #x54)
69 (divps #x0F #x5E)
70 (haddps #xF2 #x0F #x7C)
71 (hsubps #xF2 #x0F #x7D)
72 (maxps #x0F #x5F)
73 (minps #x0F #x5D)
74 (mulps #x0F #x59)
75 (orps #x0F #x56)
76 (rcpps #x0F #x53)
77 (rsqrtps #x0F #x52)
78 (sqrtps #x0F #x51)
79 (subps #x0F #x5C)
80 (unpckhps #x0F #x15)
81 (unpcklps #x0F #x14)
82 (xorps #x0F #x57)
84 ;; double precision float
85 (addpd #x66 #x0F #x58)
86 (addsubpd #x66 #x0F #xD0)
87 (andnpd #x66 #x0F #x55)
88 (andpd #x66 #x0F #x54)
89 (divpd #x66 #x0F #x5E)
90 (haddpd #x66 #x0F #x7C)
91 (hsubpd #x66 #x0F #x7D)
92 (maxpd #x66 #x0F #x5F)
93 (minpd #x66 #x0F #x5D)
94 (mulpd #x66 #x0F #x59)
95 (orpd #x66 #x0F #x56)
96 (sqrtpd #x66 #x0F #x51)
97 (subpd #x66 #x0F #x5C)
98 (unpckhpd #x66 #x0F #x15)
99 (unpcklpd #x66 #x0F #x14)
100 (xorpd #x66 #x0F #x57)
102 ;; scalar double precision float
103 (addsd #xF2 #x0F #x58)
104 (comisd #x66 #x0F #x2F)
105 (divsd #xF2 #x0F #x5E)
106 (maxsd #xF2 #x0F #x5F)
107 (minsd #xF2 #x0F #x5D)
108 (mulsd #xF2 #x0F #x59)
109 (sqrtsd #xF2 #x0F #x51)
110 (subsd #xF2 #x0F #x5C)
111 (ucomisd #x66 #x0F #x2E)
113 ;; scalar single precision float
114 (addss #xF3 #x0F #x58)
115 (comiss #x0F #x2F)
116 (divss #xF3 #x0F #x5E)
117 (maxss #xF3 #x0F #x5F)
118 (minss #xF3 #x0F #x5D)
119 (mulss #xF3 #x0F #x59)
120 (rcpss #xF3 #x0F #x53)
121 (rsqrtss #xF3 #x0F #x52)
122 (sqrtss #xF3 #x0F #x51)
123 (subss #xF3 #x0F #x5C)
124 (ucomiss #x0F #x2E)
127 ;; packed integer
128 (packssdw #x66 #x0F #x6B)
129 (packsswb #x66 #x0F #x63)
130 (packuswb #x66 #x0F #x67)
132 (paddb #x66 #x0F #xFC)
133 (paddd #x66 #x0F #xFE)
134 (paddq #x66 #x0F #xD4)
135 (paddsb #x66 #x0F #xEC)
136 (paddsw #x66 #x0F #xED)
137 (paddusb #x66 #x0F #xDC)
138 (paddusw #x66 #x0F #xDD)
139 (paddw #x66 #x0F #xFD)
141 (pand #x66 #x0F #xDB)
142 (pandn #x66 #x0F #xDF)
144 (pavgb #x66 #x0F #xE0)
145 (pavgw #x66 #x0F #xE3)
147 (pcmpeqb #x66 #x0F #x74)
148 (pcmpeqd #x66 #x0F #x76)
149 (pcmpeqw #x66 #x0F #x75)
150 (pcmpgtb #x66 #x0F #x64)
151 (pcmpgtd #x66 #x0F #x66)
152 (pcmpgtw #x66 #x0F #x65)
154 (pmaddwd #x66 #x0F #xF5)
156 (pmaxsw #x66 #x0F #xEE)
157 (pmaxub #x66 #x0F #xDE)
159 (pminsw #x66 #x0F #xEA)
160 (pminub #x66 #x0F #xDA)
162 (pmovmskb #x66 #x0F #xD7)
164 (pmulhuw #x66 #x0F #xE4)
165 (pmulhw #x66 #x0F #xE5)
166 (pmullw #x66 #x0F #xD5)
167 (pmuludq #x66 #x0F #xF4)
169 (por #x66 #x0F #xEB)
171 (psadbw #x66 #x0F #xF6)
172 (pssld #x66 #x0F #xF2)
173 (psllq #x66 #x0F #xF3)
174 (psllw #x66 #x0F #xF1)
175 (psrad #x66 #x0F #xE2)
176 (psraw #x66 #x0F #xE2)
177 (psrld #x66 #x0F #xD2)
178 (psrlq #x66 #x0F #xD3)
179 (psrlw #x66 #x0F #xD1)
181 (psubb #x66 #x0F #xF8)
182 (psubd #x66 #x0F #xFA)
183 (psubq #x66 #x0F #xFB)
184 (psubsb #x66 #x0F #xE8)
185 (psubsw #x66 #x0F #xE9)
186 (psubusb #x66 #x0F #xD8)
187 (psubusw #x66 #x0F #xD9)
188 (psubw #x66 #x0F #xF9)
190 (punpckhbw #x66 #x0F #x68)
191 (punpckhdq #x66 #x0F #x6A)
192 (punpckhqdq #x66 #x0F #x6D)
193 (punpckhwd #x66 #x0F #x69)
194 (punpcklbw #x66 #x0F #x60)
195 (punpckldq #x66 #x0F #x62)
196 (punpcklqdq #x66 #x0F #x6C)
197 (punpcklwd #x66 #x0F #x61)
199 (pxor #x66 #x0F #xEF)
201 ;; convert
202 (cvtdq2pd #xF3 #x0F #xE6)
203 (cvtdq2ps #x0F #x5B)
204 (cvtpd2dq #xF2 #x0F #xE6)
205 (cvtpd2pi #x66 #x0F #x2D)
206 (cvtpd2ps #x66 #x0F #x5A)
207 (cvtpi2pd #x66 #x0F #x2A)
208 (cvtpi2ps #x0F #x2A)
209 (cvtps2dq #x66 #x0F #x5B)
210 (cvtps2pd #x0F #x5A)
211 (cvtps2pi #x0F #x2D)
212 (cvtsd2si #xF2 #x0F #x2D)
213 (cvtsd2ss #xF2 #x0F #x5A)
214 (cvtsi2sd #xF2 #x0F #x2A)
215 (cvtsi2ss #xF3 #x0F #x2A)
216 (cvtss2sd #xF3 #x0F #x5A)
217 (cvtss2si #xF3 #x0F #x2D)
218 (cvttpd2dq #x66 #x0F #xE6)
219 (cvttpd2pi #x66 #x0F #x2C)
220 (cvttps2dq #xF3 #x0F #x5B)
221 (cvttps2pi #x0F #x2C)
222 (cvttsd2si #xF2 #x0F #x2C)
223 (cvttss2si #xF3 #x0F #x2C)
225 ;; misc
226 (lddqu #xF2 #x0F #xF0)
227 (maskmovdqu #x66 #x0F #xF7)
228 (movddup #xF2 #x0F #x12)
229 (movhlps #x0F #x12)
230 (movlhps #x0F #x16)
231 (movmskpd #x66 #x0F #x50)
232 (movmskps #x0F #x50)
233 (movntdq #x66 #x0F #XE7)
234 (movntpd #x66 #x0F #x2B)
235 (movntps #x0F #x2B)
236 (movshdup #xF3 #x0F #x16)
237 (movsldup #xF3 #x0F #x12)
240 (format stream "~S~%~%"
241 `(define-instruction ,(intern (symbol-name inst)) (segment dst src)
242 (:emitter
243 ,@(emit-ops ops)
244 (emit-ea segment src (reg-tn-encoding dst))))))
247 ;; INSTRUCTIONS WITH /r IB8
248 (loop for (inst . ops) in
250 (pextrw #X66 #x0F #xC5)
251 (pinsrw #x66 #x0F #xC4)
253 (pshufd #x66 #x0F #x70)
254 (pshufhw #xF3 #x0F #x70)
255 (pshuflw #xF2 #x0F #x70)
257 (shufpd #x66 #x0F #xC6)
258 (shufps #x0F #xC6)
262 (format stream "~S~%~%"
263 `(define-instruction ,(intern (symbol-name inst)) (segment dst src byte)
264 (:emitter
265 ,@(emit-ops ops)
266 (emit-ea segment src (reg-tn-encoding dst))
267 (emit-sized-immediate segment :byte byte)
268 ))))
270 ;; COMPARE
271 (loop for (inst . ops) in
273 (cmppd #x66 #x0F #xC2)
274 (cmpps #x0F #xC2)
275 (cmpsd #xF2 #x0F #xC2)
276 (cmpss #xF3 #x0F #xC2)
279 (format stream "~S~%~%"
280 `(define-instruction ,(intern (symbol-name inst)) (segment dst src cond)
281 (:emitter
282 ,@(emit-ops ops)
283 (emit-ea segment src (reg-tn-encoding dst))
284 (emit-sized-immediate segment :byte (cdr (assoc cond
285 '((:eq . #b000) (:e . #b000) (:z . #b000)
286 (:l . #b001) (:nge . #b001)
287 (:le . #b010) (:ng . #b010)
288 (:unord . #b011)
289 (:ne . #b100) (:nz . #b100)
290 (:nl . #b101) (:ge . #b101)
291 (:nle . #b110) (:g . #b110)
292 (:ord . #b111)
293 ))))
294 ))))
296 ;; MOVES
297 (loop for (inst ops-m2r ops-r2m) in
299 (movapd (#x66 #x0F #x28) (#x66 #x0F #x29))
300 (movaps (#x0F #x28) (#x0F #x29))
302 (movd (#x66 #x0F #x6E) (#x66 #x0F #x7E))
304 (movdqa (#x66 #x0F #x6F) (#x66 #x0F #x7F))
305 (movdqu (#xF3 #x0F #x6F) (#xF3 #x0F #x7F))
307 (movhpd (#x66 #x0F #x16) (#x66 #x0F #x17))
308 (movhps (#x0F #x16) (#x0F #x17))
310 (movlpd (#x66 #x0F #x12) (#x66 #x0F #x13))
311 (movlps (#x0F #x12) (#x0F #x13))
313 (movq (#xF3 #x0F #x7E) (#x66 #x0F #xD6))
315 (movsd (#xF2 #x0F #x10) (#xF2 #x0F #x11))
317 (movss (#xF3 #x0F #x10) (#xF3 #x0F #x11))
319 (movupd (#x66 #x0F #x10) (#x66 #x0F #x11))
320 (movups (#x0F #x10) (#x0F #x11))
323 (format stream "~S~%~%"
324 `(define-instruction ,(intern (symbol-name inst)) (segment dst src)
325 (:emitter
326 (cond ((sse-register-p dst)
327 ,@(emit-ops ops-m2r)
328 (emit-ea segment src (reg-tn-encoding dst)))
329 (t ,@(emit-ops ops-r2m)
330 (emit-ea segment dst (reg-tn-encoding src))))))))
332 ;; misc
333 (loop for (name mode . opcodes) in
335 (pslld-ib 6 #x66 #x0F #x72)
336 (pslldq-ib 7 #x66 #x0F #x73)
337 (psllq-ib 6 #x66 #x0F #x73)
338 (psllw-ib 6 #x66 #x0F #x71)
339 (psrad-ib 4 #x66 #x0F #x72)
340 (psraw-ib 4 #x66 #x0F #x71)
341 (psrld-ib 2 #x66 #x0F #x72)
342 (psrldq-ib 3 #x66 #x0F #x73)
343 (psrlq-ib 2 #x66 #x0F #x73)
344 (psrlw-ib 2 #x66 #x0F #x71)
347 (format stream "~S~%~%"
348 `(define-instruction ,(intern (symbol-name name)) (segment dst amount)
349 (:emitter
350 ,@(emit-ops opcodes)
351 (emit-ea segment dst ,mode)
352 (emit-byte segment amount)))))
356 (defun gen-ops-to-file (filename)
357 (with-open-file (stream filename :direction :output :if-exists :supersede)
358 (gen-ops stream)))