Debian Codegraph @

contrib/cl-simd/sbcl-functions.lisp

raw
1 ;;; -*- mode: Lisp; indent-tabs-mode: nil; -*-
2 ;;;
3 ;;; Copyright (c) 2010, Alexander Gavrilov ([email protected])
4 ;;;
5 ;;; This file implements VOP-wrapping functions and non-primitive
6 ;;; extensions to the core intrinsic set.
7 ;;;
8
9 (in-package #:SSE)
10
11 ;;; Materialize the intrinsic functions.
12
13 ;; Since VOPs are activated only on load, actual functions that
14 ;; wrap them have to be defined in a different file. This is a
15 ;; hack to generate the functions from the same macro invocations
16 ;; as the VOPS.
17
18 (macrolet ((def-float-set-intrinsic (pubname fname atype aregtype rtype move)
19 (declare (ignore aregtype move))
20 `(progn
21 (defun ,fname (arg)
22 (declare (type ,atype arg))
23 (truly-the ,rtype (%primitive ,fname arg)))
24 ;; Public function - includes coercion
25 (export ',pubname)
26 (declaim (ftype (function (real) ,rtype) ,pubname)
27 (inline ,pubname))
28 (defun ,pubname (arg) (,fname (coerce arg ',atype)))))
29 (def-unary-intrinsic (name rtype insn cost c-name &key immediate-arg &allow-other-keys)
30 (declare (ignore insn cost c-name))
31 (unless immediate-arg
32 `(defun ,name (x)
33 (declare (type sse-pack x))
34 (truly-the ,rtype (%primitive ,name x)))))
35 (def-binary-intrinsic (name rtype insn cost c-name &key immediate-arg &allow-other-keys)
36 (declare (ignore insn cost c-name))
37 (unless immediate-arg
38 `(defun ,name (x y ,@(if immediate-arg '(imm)))
39 (declare (type sse-pack x y))
40 (truly-the ,rtype (%primitive ,name x y)))))
41 (def-sse-int-intrinsic (name itype rtype insn cost c-name &key immediate-arg &allow-other-keys)
42 (declare (ignore insn cost c-name))
43 (unless immediate-arg
44 `(defun ,name (x iv)
45 (declare (type sse-pack x)
46 (type ,itype iv))
47 (truly-the ,rtype (%primitive ,name x iv)))))
48 (def-comparison-intrinsic (name arg-type insn cost c-name &key &allow-other-keys)
49 (declare (ignore insn cost c-name arg-type))
50 `(defun ,name (x y)
51 (declare (type sse-pack x y))
52 (truly-the boolean (,name x y))))
53 (def-load-intrinsic (name rtype insn c-name &key register-arg &allow-other-keys)
54 (declare (ignore insn c-name))
55 (let* ((vop (symbolicate "%" name))
56 (valarg (if register-arg '(value))))
57 `(progn
58 (declaim (inline ,name))
59 (defun ,name (,@valarg pointer &optional (offset 0))
60 (declare ,@(if register-arg '((type sse-pack value)))
61 (type system-area-pointer pointer)
62 (type signed-word offset))
63 ,(if rtype
64 `(truly-the ,rtype (,vop ,@valarg pointer offset 1 0))
65 `(,vop ,@valarg pointer offset 1 0))))))
66 (def-store-intrinsic (name rtype insn c-name &key setf-name &allow-other-keys)
67 (declare (ignore insn c-name))
68 (let* ((vop (symbolicate "%" name)))
69 `(progn
70 (declaim (inline ,name))
71 (defun ,name (pointer value &optional (offset 0))
72 (declare (type system-area-pointer pointer)
73 (type sse-pack value)
74 (type signed-word offset))
75 (,vop pointer offset 1 0 value)
76 (truly-the ,rtype value))
77 ,(if setf-name
78 `(defsetf ,setf-name (pointer &optional (offset 0)) (value)
79 `(,',name ,pointer ,value ,offset)))))))
80 ;; Load the definition list
81 #.(loop for name being each present-symbol
82 when (get name 'intrinsic-spec)
83 collect it into specs
84 finally (return `(progn ,@specs))))
85
86 ;;; Helper functions and macros
87
88 (defmacro def-utility (name args rtype &body code)
89 `(progn
90 (export ',name)
91 (declaim (ftype (function ,(mapcar (constantly 'sse-pack) args) ,rtype) ,name)
92 (inline ,name))
93 (defun ,name ,args ,@code)))
94
95 (defmacro def-if-function (name rtype postfix)
96 (let* ((not-x (symbolicate "NOT-" postfix))
97 (or-x (symbolicate "OR-" postfix))
98 (and-x (symbolicate "AND-" postfix))
99 (andn-x (symbolicate "ANDNOT-" postfix))
100 (xor-x (symbolicate "XOR-" postfix))
101 (true (%make-sse-pack #xFFFFFFFFFFFFFFFF #xFFFFFFFFFFFFFFFF))
102 (false (%make-sse-pack 0 0)))
103 `(progn
104 (export ',name)
105 (defknown ,name (sse-pack sse-pack sse-pack) ,rtype (foldable flushable))
106 (defun ,name (condition true-val false-val)
107 (,or-x (,and-x condition true-val)
108 (,andn-x condition false-val)))
109 ;; Instead of inlining, use a transform so that the splice
110 ;; rule has a chance to apply. This depends on transform
111 ;; definitions behaving like a LIFO:
112 (deftransform ,name ((condition true-val false-val) * *)
113 "Expand the conditional."
114 '(,or-x (,and-x condition true-val) (,andn-x condition false-val)))
115 (def-splice-transform ,name ((,not-x cond) tv fv) (,name cond fv tv))
116 ;; NOT elimination and partial constant folding for bitwise ops:
117 (def-splice-transform ,not-x ((,not-x arg1)) arg1)
118 (def-splice-transform ,and-x (arg1 (,not-x arg2)) (,andn-x arg2 arg1))
119 (def-splice-transform ,and-x ((,not-x arg1) arg2) (,andn-x arg1 arg2))
120 (def-splice-transform ,andn-x ((,not-x arg1) arg2) (,and-x arg1 arg2))
121 (%deftransform ',or-x '(function * *) #'commutative-arg-swap "place constant arg last")
122 (%deftransform ',and-x '(function * *) #'commutative-arg-swap "place constant arg last")
123 (%deftransform ',xor-x '(function * *) #'commutative-arg-swap "place constant arg last")
124 (deftransform ,or-x ((arg1 arg2) (* (constant-arg (member ,true))) *) ,true)
125 (deftransform ,or-x ((arg1 arg2) (* (constant-arg (member ,false))) *) 'arg1)
126 (deftransform ,and-x ((arg1 arg2) (* (constant-arg (member ,true))) *) 'arg1)
127 (deftransform ,and-x ((arg1 arg2) (* (constant-arg (member ,false))) *) ,false)
128 (deftransform ,xor-x ((arg1 arg2) (* (constant-arg (member ,false))) *) 'arg1)
129 (deftransform ,andn-x ((arg1 arg2) (* (constant-arg (member ,true))) *) 'arg1)
130 (deftransform ,andn-x ((arg1 arg2) (* (constant-arg (member ,false))) *) ,false)
131 (deftransform ,andn-x ((arg1 arg2) ((constant-arg (member ,true)) *) *) ,false)
132 (deftransform ,andn-x ((arg1 arg2) ((constant-arg (member ,false)) *) *) 'arg2))))
133
134 (defmacro def-not-cmp-pairs (not-fun &rest pairs)
135 `(progn
136 ,@(loop for (a b) on pairs by #'cddr
137 collect `(def-splice-transform ,not-fun ((,a arg1 arg2)) (,b arg1 arg2))
138 collect `(def-splice-transform ,not-fun ((,b arg1 arg2)) (,a arg1 arg2)))))
139
140 ;;; CPU control
141
142 (defun cpu-mxcsr ()
143 (cpu-mxcsr))
144
145 (defun %set-cpu-mxcsr (x)
146 (declare (type (unsigned-byte 32) x))
147 (%set-cpu-mxcsr x))
148
149 (defsetf cpu-mxcsr %set-cpu-mxcsr)
150
151 (defun cpu-load-fence () (cpu-load-fence))
152 (defun cpu-store-fence () (cpu-store-fence))
153 (defun cpu-memory-fence () (cpu-memory-fence))
154
155 (defun cpu-pause () (cpu-pause))
156
157 ;;; Single-float
158
159 ;; Constants
160
161 (define-symbol-macro 0.0-ps (truly-the float-sse-pack #.(%make-sse-pack 0 0)))
162
163 (define-symbol-macro true-ss (truly-the float-sse-pack #.(%make-sse-pack #xFFFFFFFF 0)))
164 (define-symbol-macro true-ps (truly-the float-sse-pack #.(%make-sse-pack #xFFFFFFFFFFFFFFFF #xFFFFFFFFFFFFFFFF)))
165
166 (define-symbol-macro false-ss (truly-the float-sse-pack #.(%make-sse-pack 0 0)))
167 (define-symbol-macro false-ps (truly-the float-sse-pack #.(%make-sse-pack 0 0)))
168
169 ;; Initialization
170
171 (declaim (inline set1-ps set-ps setr-ps setzero-ps))
172
173 (defun set1-ps (val)
174 (let ((valv (set-ss val)))
175 (shuffle-ps valv valv 0)))
176
177 (defun set-ps (x3 x2 x1 x0)
178 (movelh-ps (unpacklo-ps (set-ss x0) (set-ss x1))
179 (unpacklo-ps (set-ss x2) (set-ss x3))))
180
181 (defun setr-ps (x0 x1 x2 x3)
182 (movelh-ps (unpacklo-ps (set-ss x0) (set-ss x1))
183 (unpacklo-ps (set-ss x2) (set-ss x3))))
184
185 (defun setzero-ps () 0.0-ps)
186
187 ;; Arithmetic negation
188
189 (def-utility neg-ss (arg) float-sse-pack
190 (xor-ps arg #.(%make-sse-pack #x80000000 0)))
191
192 (def-utility neg-ps (arg) float-sse-pack
193 (xor-ps arg #.(%make-sse-pack #x8000000080000000 #x8000000080000000)))
194
195 ;; Bitwise operations
196
197 (def-if-function if-ps float-sse-pack #:ps)
198
199 ;; Comparisons
200
201 (def-utility >-ss (x y) float-sse-pack (<-ss y x))
202 (def-utility >-ps (x y) float-sse-pack (<-ps y x))
203 (def-utility >=-ss (x y) float-sse-pack (<=-ss y x))
204 (def-utility >=-ps (x y) float-sse-pack (<=-ps y x))
205 (def-utility />-ss (x y) float-sse-pack (/<-ss y x))
206 (def-utility />-ps (x y) float-sse-pack (/<-ps y x))
207 (def-utility />=-ss (x y) float-sse-pack (/<=-ss y x))
208 (def-utility />=-ps (x y) float-sse-pack (/<=-ps y x))
209
210 (def-not-cmp-pairs not-ps
211 =-ps /=-ps <-ps /<-ps <=-ps /<=-ps >-ps />-ps >=-ps />=-ps cmpord-ps cmpunord-ps)
212
213 ;; Shuffle
214
215 (declaim (inline %sse-pack-to-int %int-to-sse-pack %shuffle-subints))
216
217 (defun %sse-pack-to-int (pack)
218 (logior (%sse-pack-low pack) (ash (%sse-pack-high pack) 64)))
219
220 (defun %int-to-sse-pack (val &aux (mask #xFFFFFFFFFFFFFFFF))
221 (%make-sse-pack (logand val mask) (logand (ash val -64) mask)))
222
223 (defun %shuffle-subints (xval yval imm bit-cnt &aux (mask (1- (ash 1 bit-cnt))))
224 (flet ((bits (idx)
225 (logand 3 (ash imm (* -2 idx))))
226 (val (src idx)
227 (logand mask (ash src (* (- bit-cnt) idx)))))
228 (logior (val xval (bits 0))
229 (ash (val xval (bits 1)) bit-cnt)
230 (ash (val yval (bits 2)) (* 2 bit-cnt))
231 (ash (val yval (bits 3)) (* 3 bit-cnt)))))
232
233 (defun shuffle-ps (x y imm)
234 (declare (type sse-pack x y))
235 (let* ((xval (%sse-pack-to-int x))
236 (yval (%sse-pack-to-int y)))
237 (truly-the float-sse-pack (%int-to-sse-pack (%shuffle-subints xval yval imm 32)))))
238
239 ;;; Double-float
240
241 ;; Constants
242
243 (define-symbol-macro 0.0-pd (truly-the double-sse-pack #.(%make-sse-pack 0 0)))
244
245 (define-symbol-macro true-sd (truly-the double-sse-pack #.(%make-sse-pack #xFFFFFFFFFFFFFFFF 0)))
246 (define-symbol-macro true-pd (truly-the double-sse-pack #.(%make-sse-pack #xFFFFFFFFFFFFFFFF #xFFFFFFFFFFFFFFFF)))
247
248 (define-symbol-macro false-sd (truly-the double-sse-pack #.(%make-sse-pack 0 0)))
249 (define-symbol-macro false-pd (truly-the double-sse-pack #.(%make-sse-pack 0 0)))
250
251 ;; Initialization
252
253 (declaim (inline set1-pd set-pd setr-pd setzero-pd))
254
255 (defun set1-pd (val)
256 (let ((valv (set-sd val)))
257 (shuffle-pd valv valv 0)))
258
259 (defun set-pd (x1 x0)
260 (unpacklo-pd (set-sd x0) (set-sd x1)))
261
262 (defun setr-pd (x0 x1)
263 (unpacklo-pd (set-sd x0) (set-sd x1)))
264
265 (defun setzero-pd () 0.0-pd)
266
267 ;; Arithmetic negation
268
269 (def-utility neg-sd (arg) double-sse-pack
270 (xor-pd arg #.(%make-sse-pack #x8000000000000000 0)))
271
272 (def-utility neg-pd (arg) double-sse-pack
273 (xor-pd arg #.(%make-sse-pack #x8000000000000000 #x8000000000000000)))
274
275 ;; Bitwise operations
276
277 (def-if-function if-pd double-sse-pack #:pd)
278
279 ;; Comparisons
280
281 (def-utility >-sd (x y) double-sse-pack (<-sd y x))
282 (def-utility >-pd (x y) double-sse-pack (<-pd y x))
283 (def-utility >=-sd (x y) double-sse-pack (<=-sd y x))
284 (def-utility >=-pd (x y) double-sse-pack (<=-pd y x))
285 (def-utility />-sd (x y) double-sse-pack (/<-sd y x))
286 (def-utility />-pd (x y) double-sse-pack (/<-pd y x))
287 (def-utility />=-sd (x y) double-sse-pack (/<=-sd y x))
288 (def-utility />=-pd (x y) double-sse-pack (/<=-pd y x))
289
290 (def-not-cmp-pairs not-pd
291 =-pd /=-pd <-pd /<-pd <=-pd /<=-pd >-pd />-pd >=-pd />=-pd cmpord-pd cmpunord-pd)
292
293 ;; Shuffle
294
295 (defun shuffle-pd (x y imm)
296 (declare (type sse-pack x y))
297 (truly-the double-sse-pack
298 (%make-sse-pack (if (logtest imm 1) (%sse-pack-high x) (%sse-pack-low x))
299 (if (logtest imm 2) (%sse-pack-high y) (%sse-pack-low y)))))
300
301 ;;; Integer
302
303 ;; Constants
304
305 (define-symbol-macro 0-pi (truly-the int-sse-pack #.(%make-sse-pack 0 0)))
306
307 (define-symbol-macro true-pi (truly-the int-sse-pack #.(%make-sse-pack #xFFFFFFFFFFFFFFFF #xFFFFFFFFFFFFFFFF)))
308
309 (define-symbol-macro false-pi (truly-the int-sse-pack #.(%make-sse-pack 0 0)))
310
311 ;; Initialization
312
313 (macrolet ((defset (name type)
314 `(defun ,name (x)
315 (declare (type ,type x))
316 (,name x))))
317 (defset %set-int (signed-byte 64))
318 (defset %set-uint (unsigned-byte 64))
319 (defset convert-si32-to-pi (signed-byte 32))
320 (defset convert-su32-to-pi (unsigned-byte 32))
321 (defset convert-si64-to-pi (signed-byte 64))
322 (defset convert-su64-to-pi (unsigned-byte 64)))
323
324 (macrolet ((defset1 (name setter type shuffle &rest expands)
325 `(progn
326 (export ',name)
327 (declaim (inline ,name))
328 (defun ,name (arg)
329 (let ((val (,setter (the ,type arg))))
330 (declare (type int-sse-pack val))
331 ,@(loop for x in expands collect `(setq val (,x val val)))
332 (shuffle-pi32 val ,shuffle))))))
333 (defset1 set1-pi8 %set-int fixnum #4r0000 unpacklo-pi8 unpacklo-pi16)
334 (defset1 set1-pi16 %set-int fixnum #4r0000 unpacklo-pi16)
335 (defset1 set1-pi32 %set-int (signed-byte 32) #4r0000)
336 (defset1 set1-pu32 %set-uint (unsigned-byte 32) #4r0000)
337 (defset1 set1-pi64 %set-int (signed-byte 64) #4r1010)
338 (defset1 set1-pu64 %set-uint (unsigned-byte 64) #4r1010))
339
340 (macrolet ((defset (name rname setter type depth)
341 (let* ((names (loop for i from 0 below (ash 1 depth)
342 collect (symbolicate (format nil "X~A" i))))
343 (funcs #(unpacklo-pi64 unpacklo-pi32 unpacklo-pi16 unpacklo-pi8))
344 (body (loop for i downfrom depth to 0
345 for bv = (mapcar (lambda (x) `(,setter (the ,type ,x))) names)
346 then (loop for (a b) on bv by #'cddr
347 collect `(,(svref funcs i) ,a ,b))
348 finally (return (first bv)))))
349 `(progn
350 (export ',name)
351 (export ',rname)
352 (declaim (inline ,name ,rname))
353 (defun ,name (,@(reverse names)) ,body)
354 (defun ,rname (,@names) ,body)))))
355 (defset set-pi8 setr-pi8 %set-int fixnum 4)
356 (defset set-pi16 setr-pi16 %set-int fixnum 3)
357 (defset set-pi32 setr-pi32 %set-int (signed-byte 32) 2)
358 (defset set-pu32 setr-pu32 %set-uint (unsigned-byte 32) 2)
359 (defset set-pi64 setr-pi64 %set-int (signed-byte 64) 1)
360 (defset set-pu64 setr-pu64 %set-uint (unsigned-byte 64) 1))
361
362 (declaim (inline setzero-pi))
363 (defun setzero-pi () 0-pi)
364
365 ;; Masked move
366
367 (export 'maskmoveu-pi)
368
369 (declaim (inline maskmoveu-pi))
370
371 (defun maskmoveu-pi (value mask pointer &optional (offset 0))
372 (declare (type sse-pack value mask)
373 (type system-area-pointer pointer)
374 (type fixnum offset))
375 (%maskmoveu-pi value mask pointer offset))
376
377 ;; Arithmetic negation (subtract from 0)
378
379 (macrolet ((frob (name subf)
380 `(def-utility ,name (arg) int-sse-pack (,subf 0-pi arg))))
381 (frob neg-pi8 sub-pi8)
382 (frob neg-pi16 sub-pi16)
383 (frob neg-pi32 sub-pi32)
384 (frob neg-pi64 sub-pi64))
385
386 ;; Bitwise operations
387
388 (def-if-function if-pi int-sse-pack #:pi)
389
390 ;; Comparisons
391
392 (def-utility <-pi8 (x y) int-sse-pack (>-pi8 y x))
393 (def-utility <-pi16 (x y) int-sse-pack (>-pi16 y x))
394 (def-utility <-pi32 (x y) int-sse-pack (>-pi32 y x))
395
396 (def-utility <=-pi8 (x y) int-sse-pack (not-pi (>-pi8 x y)))
397 (def-utility <=-pi16 (x y) int-sse-pack (not-pi (>-pi16 x y)))
398 (def-utility <=-pi32 (x y) int-sse-pack (not-pi (>-pi32 x y)))
399
400 (def-utility >=-pi8 (x y) int-sse-pack (not-pi (>-pi8 y x)))
401 (def-utility >=-pi16 (x y) int-sse-pack (not-pi (>-pi16 y x)))
402 (def-utility >=-pi32 (x y) int-sse-pack (not-pi (>-pi32 y x)))
403
404 (def-utility /=-pi8 (x y) int-sse-pack (not-pi (=-pi8 x y)))
405 (def-utility /=-pi16 (x y) int-sse-pack (not-pi (=-pi16 x y)))
406 (def-utility /=-pi32 (x y) int-sse-pack (not-pi (=-pi32 x y)))
407
408 ;; Shifts
409
410 (defun slli-pi (x imm)
411 (declare (type sse-pack x))
412 (truly-the int-sse-pack
413 (if (> imm 15)
414 0-pi
415 (%int-to-sse-pack (ash (%sse-pack-to-int x) (* 8 imm))))))
416
417 (defun srli-pi (x imm)
418 (declare (type sse-pack x))
419 (truly-the int-sse-pack
420 (if (> imm 15)
421 0-pi
422 (%int-to-sse-pack (ash (%sse-pack-to-int x) (* -8 imm))))))
423
424 ;; Extract & insert
425
426 (defun extract-pi16 (x imm)
427 (declare (type sse-pack x))
428 (logand #xFFFF
429 (ash (%sse-pack-to-int x)
430 (- (* 16 (logand imm 7))))))
431
432 (defun insert-pi16 (x intv imm)
433 (declare (type sse-pack x))
434 (let ((shift (* 16 (logand imm 7))))
435 (truly-the int-sse-pack
436 (%int-to-sse-pack
437 (logior (logand (%sse-pack-to-int x)
438 (lognot (ash #xFFFF shift)))
439 (ash (logand intv #xFFFF) shift))))))
440
441 ;; Shuffle
442
443 (defun shuffle-pi32 (x imm)
444 (declare (type sse-pack x))
445 (let* ((xval (%sse-pack-to-int x)))
446 (truly-the int-sse-pack (%int-to-sse-pack (%shuffle-subints xval xval imm 32)))))
447
448 (defun shufflelo-pi16 (x imm)
449 (declare (type sse-pack x))
450 (let* ((xval (%sse-pack-low x)))
451 (truly-the int-sse-pack (%make-sse-pack (%shuffle-subints xval xval imm 16)
452 (%sse-pack-high x)))))
453
454 (defun shufflehi-pi16 (x imm)
455 (declare (type sse-pack x))
456 (let* ((xval (%sse-pack-high x)))
457 (truly-the int-sse-pack (%make-sse-pack (%sse-pack-low x)
458 (%shuffle-subints xval xval imm 16)))))
459