Debian Codegraph @

contrib/cl-simd/ecl-sse-utils.lisp

raw
1 ;;; -*- mode: Lisp; indent-tabs-mode: nil; -*-
2 ;;;
3 ;;; Copyright (c) 2010, Alexander Gavrilov ([email protected])
4 ;;;
5 ;;; This file defines some extensions to the base intrinsic set,
6 ;;; and other utility functions.
7 ;;;
8
9 (in-package #:SSE)
10
11 ;;; Helper macros and functions
12 (eval-when (:compile-toplevel :load-toplevel :execute)
13 ;; Try using a matching inverse function name
14 (defun lookup-flip (arg pairs &key no-reverse)
15 (and (consp arg)
16 (let ((fix (or (cdr (assoc (first arg) pairs))
17 (unless no-reverse
18 (car (rassoc (first arg) pairs))))))
19 (cond ((eq fix :identity)
20 (assert (null (cddr arg)))
21 (second arg))
22 (fix
23 `(,fix ,@(rest arg)))
24 (t nil)))))
25 ;; Macroexpand, plus compiler expand some specific names
26 (defun expand-condition (form env)
27 (setq form (macroexpand form env))
28 (loop while (and (consp form)
29 (symbolp (first form))
30 (get (first form) 'expand-in-condition))
31 do (setq form (c::cmp-expand-macro (compiler-macro-function (first form))
32 form env)))
33 form)
34 ;; Checks if the form is an unary call
35 (defun is-unary? (form op)
36 (and (consp form)
37 (eq (first form) op)
38 (null (cddr form))))
39 ;; IF-style function expander
40 (defun expand-if-macro (condition then-value else-value env if-f not-f or-f and-f andnot-f type-name zero-val &key flip)
41 (let* ((condition (expand-condition condition env))
42 (then-value (macroexpand then-value env))
43 (else-value (macroexpand else-value env))
44 (then-zero? (equal then-value zero-val))
45 (else-zero? (equal else-value zero-val)))
46 (cond ((is-unary? condition not-f)
47 (expand-if-macro (second condition) else-value then-value
48 env if-f not-f or-f and-f andnot-f type-name zero-val
49 :flip (not flip)))
50 ((and then-zero? else-zero?)
51 zero-val)
52 (then-zero?
53 `(,andnot-f ,condition ,else-value))
54 (else-zero?
55 `(,and-f ,condition ,then-value))
56 (t
57 (let* ((csym (gensym))
58 (args `((,and-f ,csym ,then-value)
59 (,andnot-f ,csym ,else-value))))
60 `(let ((,csym ,condition))
61 (declare (type ,type-name ,csym)
62 (:read-only ,csym))
63 (,or-f ,@(if flip (reverse args) args)))))))))
64
65 (defmacro def-utility (name arg-types ret-type expansion &key expand-args expand-in-condition)
66 "Defines and exports a function & compiler macro with the specified expansion."
67 (let* ((anames (mapcar #'make-arg-name (make-arg-nums arg-types))))
68 `(progn
69 (export ',name)
70 (eval-when (:compile-toplevel :load-toplevel)
71 ,@(if expand-in-condition
72 `((setf (get ',name 'expand-in-condition) t)))
73 (define-compiler-macro ,name (&environment env ,@anames)
74 (declare (ignorable env))
75 ,@(loop for arg in (if (eq expand-args t) anames expand-args)
76 collect `(setq ,arg (macroexpand ,arg env)))
77 ,expansion))
78 (proclaim '(ftype (function ,(mapcar #'declaim-arg-type-of arg-types) ,ret-type) ,name))
79 (defun ,name ,anames
80 (declare (optimize (speed 0) (debug 0) (safety 1)))
81 (let ,(mapcar #'list anames anames)
82 (declare ,@(loop for an in anames and at in arg-types
83 collect `(type ,at ,an)))
84 ;; Depends on the compiler macro being expanded:
85 (,name ,@anames))))))
86
87 (defmacro def-if-function (name type-name postfix)
88 `(def-utility ,name (,type-name ,type-name ,type-name) ,type-name
89 (expand-if-macro arg0 arg1 arg2 env
90 ',name
91 ',(intern (format nil "NOT-~A" postfix))
92 ',(intern (format nil "OR-~A" postfix))
93 ',(intern (format nil "AND-~A" postfix))
94 ',(intern (format nil "ANDNOT-~A" postfix))
95 ',type-name
96 '(,(intern (format nil "SETZERO-~A" postfix))))))
97
98 ;;; Aligned array allocation
99
100 (deftype sse-array (elt-type &optional dims)
101 "Type of arrays efficiently accessed by SSE aref intrinsics and returned by make-sse-array.
102 Should be assumed to be SIMPLE-ARRAY, except that displacing with MAKE-SSE-ARRAY is allowed."
103 (when (eq elt-type '*)
104 (c::cmperr "SSE-ARRAY must have a specific element type."))
105 (let ((upgraded (upgraded-array-element-type elt-type)))
106 (when (member upgraded '(t bit))
107 (c::cmperr "Invalid SSE-ARRAY element type: ~S" elt-type))
108 (unless (subtypep upgraded elt-type)
109 (c::cmpwarn "SSE-ARRAY element type ~S has been upgraded to ~S" elt-type upgraded))
110 `(array ,upgraded ,dims)))
111
112 (defun make-sse-array (dimensions &rest args &key (element-type '(unsigned-byte 8)) displaced-to &allow-other-keys)
113 "Allocates an SSE-ARRAY aligned to the 16-byte boundary. May flatten displacement chains for performance reasons."
114 (if displaced-to
115 (apply #'make-array dimensions args)
116 (multiple-value-bind (elt-size adj-type)
117 (array-element-type-byte-size element-type)
118 (when (eq adj-type t)
119 (error "Cannot use element type T with SSE."))
120 (sys::remf args :element-type)
121 (let* ((full-size (if (numberp dimensions)
122 dimensions
123 (reduce #'* dimensions)))
124 (padded-size (+ full-size (ceiling 15 elt-size)))
125 (array (apply #'make-array padded-size :element-type adj-type args))
126 (misalign (ffi:c-inline (array) (:object) :int
127 "(((unsigned long)(#0)->array.self.b8) & 15)"
128 :one-liner t))
129 (offset (/ (if (> misalign 0) (- 16 misalign) 0) elt-size)))
130 (make-array dimensions :element-type element-type
131 :displaced-to array :displaced-index-offset offset)))))
132
133 ;;; Single-float tools
134
135 ;; Constants
136
137 (defmacro set-true-ss ()
138 (load-time-value (make-pack-of-bin #xFFFFFFFF :as 'float-sse-pack)))
139
140 (defmacro set-true-ps ()
141 (load-time-value (make-pack-of-bin -1 :as 'float-sse-pack)))
142
143 (eval-when (:compile-toplevel :load-toplevel)
144 (define-symbol-macro 0.0-ps (setzero-ps))
145
146 (define-symbol-macro true-ss (set-true-ss))
147 (define-symbol-macro false-ss (setzero-ps))
148
149 (define-symbol-macro true-ps (set-true-ps))
150 (define-symbol-macro false-ps (setzero-ps)))
151
152 ;; Bitwise if
153
154 (def-if-function if-ps float-sse-pack #:ps)
155
156 ;; Arithmetic negation (xor with negative zero)
157
158 (def-utility neg-ss (float-sse-pack) float-sse-pack
159 `(xor-ps ,arg0 ,(load-time-value (make-pack-of-bin #x80000000 :as 'float-sse-pack))))
160
161 (def-utility neg-ps (float-sse-pack) float-sse-pack
162 `(xor-ps ,arg0 ,(load-time-value
163 (make-pack-of-bin #x80000000800000008000000080000000 :as 'float-sse-pack))))
164
165 ;; Logical inversion
166
167 (def-utility not-ps (float-sse-pack) float-sse-pack
168 (or (lookup-flip arg0 '((=-ps . /=-ps)
169 (<-ps . /<-ps)
170 (<=-ps . /<=-ps)
171 (>-ps . />-ps)
172 (>=-ps . />=-ps)
173 (cmpord-ps . cmpunord-ps)
174 (not-ps . :identity)))
175 `(xor-ps ,arg0 true-ps))
176 :expand-args t)
177
178 ;; Shuffle
179
180 (defun shuffle-ps (x y mask)
181 (declare (optimize (speed 0) (debug 0) (safety 1))
182 (type t x y mask))
183 (check-type x sse-pack)
184 (check-type y sse-pack)
185 (check-type mask (unsigned-byte 8))
186 (ffi:c-inline (x y mask) (:object :object :int) :float-sse-pack
187 "_mm_setr_ps(
188 (#0)->sse.data.sf[(#2)&3],
189 (#0)->sse.data.sf[((#2)>>2)&3],
190 (#1)->sse.data.sf[((#2)>>4)&3],
191 (#1)->sse.data.sf[((#2)>>6)&3]
192 )" :one-liner t))
193
194 ;;; Double-float tools
195
196 ;; Constants
197
198 (defmacro set-true-sd ()
199 (load-time-value (make-pack-of-bin #xFFFFFFFFFFFFFFFF :as 'double-sse-pack)))
200
201 (defmacro set-true-pd ()
202 (load-time-value (make-pack-of-bin -1 :as 'double-sse-pack)))
203
204 (eval-when (:compile-toplevel :load-toplevel)
205 (define-symbol-macro 0.0-pd (setzero-pd))
206
207 (define-symbol-macro true-sd (set-true-sd))
208 (define-symbol-macro false-sd (setzero-pd))
209
210 (define-symbol-macro true-pd (set-true-pd))
211 (define-symbol-macro false-pd (setzero-pd)))
212
213 ;; Bitwise if
214
215 (def-if-function if-pd double-sse-pack #:pd)
216
217 ;; Arithmetic negation (xor with negative zero)
218
219 (def-utility neg-sd (double-sse-pack) double-sse-pack
220 `(xor-pd ,arg0
221 ,(load-time-value
222 (make-pack-of-bin #x8000000000000000 :as 'double-sse-pack))))
223
224 (def-utility neg-pd (double-sse-pack) double-sse-pack
225 `(xor-pd ,arg0
226 ,(load-time-value
227 (make-pack-of-bin #x80000000000000008000000000000000 :as 'double-sse-pack))))
228
229 ;; Logical inversion
230
231 (def-utility not-pd (double-sse-pack) double-sse-pack
232 (or (lookup-flip arg0 '((=-pd . /=-pd)
233 (<-pd . /<-pd)
234 (<=-pd . /<=-pd)
235 (>-pd . />-pd)
236 (>=-pd . />=-pd)
237 (cmpord-pd . cmpunord-pd)
238 (not-pd . :identity)))
239 `(xor-pd ,arg0 true-pd))
240 :expand-args t)
241
242 ;; Shuffle
243
244 (defun shuffle-pd (x y mask)
245 (declare (optimize (speed 0) (debug 0) (safety 1))
246 (type t x y mask))
247 (check-type x sse-pack)
248 (check-type y sse-pack)
249 (check-type mask (unsigned-byte 2))
250 (ffi:c-inline (x y mask) (:object :object :int) :double-sse-pack
251 "_mm_setr_pd(
252 (#0)->sse.data.df[(#2)&1],
253 (#1)->sse.data.df[((#2)>>1)&1]
254 )" :one-liner t))
255
256 ;;; Integer tools
257
258 ;; Constants
259
260 (defmacro set-true-pi ()
261 (load-time-value (make-pack-of-bin -1 :as 'int-sse-pack)))
262
263 (eval-when (:compile-toplevel :load-toplevel)
264 (define-symbol-macro 0-pi (setzero-pi))
265
266 (define-symbol-macro true-pi (set-true-pi))
267 (define-symbol-macro false-pi (setzero-pi)))
268
269 ;; Bitwise if
270
271 (def-if-function if-pi float-sse-pack #:pi)
272
273 ;; Arithmetic negation (subtract from 0)
274
275 (macrolet ((frob (name subf)
276 `(def-utility ,name (int-sse-pack) int-sse-pack
277 `(,',subf (setzero-pi) ,arg0))))
278 (frob neg-pi8 sub-pi8)
279 (frob neg-pi16 sub-pi16)
280 (frob neg-pi32 sub-pi32)
281 (frob neg-pi64 sub-pi64))
282
283 ;; Logical inversion
284
285 (def-utility not-pi (int-sse-pack) int-sse-pack
286 (or (lookup-flip arg0 '((<=-pi8 . >-pi8)
287 (<=-pi16 . >-pi16)
288 (<=-pi32 . >-pi32)
289 (>=-pi8 . <-pi8)
290 (>=-pi16 . <-pi16)
291 (>=-pi32 . <-pi32)
292 (/=-pi8 . =-pi8)
293 (/=-pi16 . =-pi16)
294 (/=-pi32 . =-pi32)
295 (not-pi . :identity))
296 :no-reverse t)
297 `(xor-pi ,arg0 true-pi))
298 :expand-args t)
299
300 (macrolet ((frob (name code)
301 `(def-utility ,name (int-sse-pack int-sse-pack) int-sse-pack
302 ,code
303 :expand-in-condition t)))
304
305 (frob <=-pi8 `(not-pi (>-pi8 ,arg0 ,arg1)))
306 (frob <=-pi16 `(not-pi (>-pi16 ,arg0 ,arg1)))
307 (frob <=-pi32 `(not-pi (>-pi32 ,arg0 ,arg1)))
308
309 (frob >=-pi8 `(not-pi (<-pi8 ,arg0 ,arg1)))
310 (frob >=-pi16 `(not-pi (<-pi16 ,arg0 ,arg1)))
311 (frob >=-pi32 `(not-pi (<-pi32 ,arg0 ,arg1)))
312
313 (frob /=-pi8 `(not-pi (=-pi8 ,arg0 ,arg1)))
314 (frob /=-pi16 `(not-pi (=-pi16 ,arg0 ,arg1)))
315 (frob /=-pi32 `(not-pi (=-pi32 ,arg0 ,arg1))))
316
317 ;; Shifts
318
319 (defun slli-pi (x shift)
320 (declare (optimize (speed 0) (debug 0) (safety 1))
321 (type t x shift))
322 (check-type x sse-pack)
323 (check-type shift (unsigned-byte 8))
324 (ffi:c-inline (x shift) (:object :int) :object
325 "cl_object rv = ecl_make_int_sse_pack(_mm_setzero_si128());
326 unsigned bshift=(#1), i;
327 for (i = 0; i + bshift < 16; i++)
328 rv->sse.data.b8[i+bshift] = (#0)->sse.data.b8[i];
329 @(return) = rv;"))
330
331 (defun srli-pi (x shift)
332 (declare (optimize (speed 0) (debug 0) (safety 1))
333 (type t x shift))
334 (check-type x sse-pack)
335 (check-type shift (unsigned-byte 8))
336 (ffi:c-inline (x shift) (:object :int) :object
337 "cl_object rv = ecl_make_int_sse_pack(_mm_setzero_si128());
338 int bshift=(#1), i;
339 for (i = 16 - bshift - 1; i >= 0; i--)
340 rv->sse.data.b8[i] = (#0)->sse.data.b8[i+bshift];
341 @(return) = rv;"))
342
343 ;; Extract & insert
344
345 (defun extract-pi16 (x index)
346 (declare (optimize (speed 0) (debug 0) (safety 1))
347 (type t x index))
348 (check-type x sse-pack)
349 (check-type index (unsigned-byte 8))
350 (ffi:c-inline (x index) (:object :int) :fixnum
351 "*((unsigned short*)&(#0)->sse.data.b8[((#1)&3)*2])"
352 :one-liner t))
353
354 (defun insert-pi16 (x ival index)
355 (declare (optimize (speed 0) (debug 0) (safety 1))
356 (type t x ival index))
357 (check-type x sse-pack)
358 (check-type index (unsigned-byte 8))
359 (ffi:c-inline (x ival index) (:int-sse-pack :int :int) :object
360 "cl_object rv = ecl_make_int_sse_pack(#0);
361 *((unsigned short*)&rv->sse.data.b8[((#2)&3)*2]) = (unsigned short)(#1);
362 @(return) = rv;"))
363
364 ;; Shuffles
365
366 (defun shuffle-pi32 (x mask)
367 (declare (optimize (speed 0) (debug 0) (safety 1))
368 (type t x mask))
369 (check-type x sse-pack)
370 (check-type mask (unsigned-byte 8))
371 (ffi:c-inline (x mask) (:object :int) :int-sse-pack
372 "unsigned *pd = (unsigned*)(#0)->sse.data.b8;
373 @(return) = _mm_setr_epi32(pd[(#1)&3],pd[((#1)>>2)&3],pd[((#1)>>4)&3],pd[((#1)>>6)&3]);"))
374
375 (defun shufflelo-pi16 (x mask)
376 (declare (optimize (speed 0) (debug 0) (safety 1))
377 (type t x mask))
378 (check-type x sse-pack)
379 (check-type mask (unsigned-byte 8))
380 (ffi:c-inline (x mask) (:object :int) :int-sse-pack
381 "unsigned short *pd = (unsigned short*)(#0)->sse.data.b8;
382 @(return) = _mm_setr_epi16(
383 pd[(#1)&3],pd[((#1)>>2)&3],pd[((#1)>>4)&3],pd[(((#1)>>6)&3)],
384 pd[4], pd[5], pd[6], pd[7]
385 );"))
386
387 (defun shufflehi-pi16 (x mask)
388 (declare (optimize (speed 0) (debug 0) (safety 1))
389 (type t x mask))
390 (check-type x sse-pack)
391 (check-type mask (unsigned-byte 8))
392 (ffi:c-inline (x mask) (:object :int) :int-sse-pack
393 "unsigned short *pb = (unsigned short*)(#0)->sse.data.b8, *pd = pb+4;
394 @(return) = _mm_setr_epi16(
395 pb[0], pb[1], pb[2], pb[3],
396 pd[(#1)&3],pd[((#1)>>2)&3],pd[((#1)>>4)&3],pd[(((#1)>>6)&3)]
397 );"))
398