Debian Codegraph @

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

raw
1 ;;; -*- mode: Lisp; indent-tabs-mode: nil; -*-
2 ;;;
3 ;;; Copyright (c) 2010, Alexander Gavrilov ([email protected])
4 ;;;
5 ;;; This file defines macros for wrapping C-level SSE intrinsics.
6 ;;;
7
8 (in-package #:SSE)
9
10 ;;; The compound SSE pack type
11
12 (deftype sse-pack (&optional item)
13 (ecase item
14 (* 'ext:sse-pack)
15 ((single-float float) 'float-sse-pack)
16 (double-float 'double-sse-pack)
17 (integer 'int-sse-pack)))
18
19 ;;; Helper macros and functions
20
21 (defmacro typename-case (value &body clauses)
22 "Syntax: (case value &body clauses)"
23 `(cond ,@(mapcar (lambda (clause)
24 `((subtypep ,value ',(first clause))
25 ,@(rest clause)))
26 clauses)
27 (t (error "Unsupported type name: ~S" ,value))))
28
29 (defun foreign-type-of (lt)
30 (typename-case lt
31 (nil :object)
32 (int-sse-pack :int-sse-pack)
33 (float-sse-pack :float-sse-pack)
34 (double-sse-pack :double-sse-pack)
35 (boolean :bool)
36 (single-float :float)
37 (double-float :double)
38 (fixnum :fixnum)
39 #+uint32-t
40 (ext:integer32 :int32-t)
41 #+uint32-t
42 (ext:byte32 :uint32-t)
43 #+uint64-t
44 (ext:integer64 :int64-t)
45 #+uint64-t
46 (ext:byte64 :uint64-t)
47 (integer :fixnum)))
48
49 (defun pointer-c-type-of (lt)
50 (typename-case lt
51 (nil "void")
52 (int-sse-pack "__m128i")
53 (float-sse-pack "float")
54 (double-sse-pack "double")))
55
56 ;; Accept any real values for floating-point arguments:
57 (defun declaim-arg-type-of (lt)
58 (typename-case lt
59 ((or single-float double-float) 'real)
60 (ext:sse-pack 'ext:sse-pack)
61 (fixnum 'fixnum)
62 (t lt)))
63
64 (defun inline-arg-type-of (lt)
65 (typename-case lt
66 ((or single-float double-float) 'c::fixnum-float)
67 (fixnum 'fixnum)
68 (t lt)))
69
70 ;; Constant expansion
71 (defun expand-constant (form env &optional chgp)
72 (let* ((mform (macroexpand form env))
73 (cform (cond ((and (symbolp mform) (constantp mform))
74 (symbol-value mform))
75 (t mform))))
76 (values cform (or chgp (not (eql cform form))))))
77
78 ;; Macro helpers
79 (defun make-arg-name (index)
80 (intern (format nil "ARG~A" index)))
81
82 (defun make-arg-nums (lst)
83 (loop for i from 0 below (length lst) collect i))
84
85 (defun wrap-ret-arg (core ret-type &optional ret-arg)
86 (cond ((eq ret-type nil)
87 (format nil "(~A,ECL_NIL)" core))
88 (ret-arg
89 (format nil "@~36R;(~A,#~36R)" ret-arg core ret-arg))
90 (t core)))
91
92 ;; Constant generation
93 (defun make-pack-of-bin (bin-value &key (as 'int-sse-pack))
94 (let* ((all (loop for i from 0 to 15
95 for v = bin-value then (ash v -8)
96 collect (logand v 255)))
97 (pack (ext:vector-to-sse-pack
98 (make-array 16 :element-type '(unsigned-byte 8) :initial-contents all))))
99 (if (eq as 'int-sse-pack)
100 pack
101 `(the ,as ,(ext:sse-pack-as-elt-type
102 pack (ecase as
103 (EXT:FLOAT-SSE-PACK 'single-float)
104 (EXT:DOUBLE-SSE-PACK 'double-float)))))))
105
106 (defmacro def-inline (name mode arg-types ret-type call-str &rest flags)
107 `(eval-when (:compile-toplevel :load-toplevel)
108 ;; FIXME: We should think of a way to achieve this without using
109 ;; internal compiler functions
110 (let ((c::*inline-information* (c::machine-inline-information c::*default-machine*)))
111 (c::def-inline ,name ,mode ,arg-types ,ret-type ,call-str ,@flags)
112 (setf (c::machine-inline-information c::*default-machine*)
113 c::*inline-information*))))
114
115 (defmacro def-intrinsic (name arg-types ret-type c-name
116 &key (export t) ret-arg reorder-args immediate-args defun-body)
117 "Defines and exports an SSE intrinsic function with matching open-coding rules."
118 (let* ((anums (make-arg-nums arg-types))
119 (asyms (mapcar #'make-arg-name anums))
120 (aftypes (mapcar #'foreign-type-of arg-types))
121 (rftype (foreign-type-of ret-type))
122 (call-anums (if reorder-args (reverse anums) anums))
123 (call-str (wrap-ret-arg (format nil "~A(~{#~36R~^,~})" c-name call-anums) ret-type ret-arg)))
124 `(progn
125 ,(if export `(export ',name))
126 ,@(if immediate-args ; Generate a constantness verifier macro
127 `((define-compiler-macro ,name (&whole whole &environment env ,@asyms &aux chgp)
128 ,@(loop for (arg type) in immediate-args
129 collect `(let ((oldv ,arg))
130 (multiple-value-setq (,arg chgp) (expand-constant oldv env chgp))
131 (unless (typep ,arg ',type)
132 (c::cmperr "In call to ~A: Argument ~S~@[ = ~S~] is not a constant of type ~A"
133 ',name oldv (unless (eql oldv ,arg) ,arg) ',type))))
134 (if chgp (list ',name ,@asyms) whole))))
135 (proclaim '(ftype (function ,(mapcar #'declaim-arg-type-of arg-types) ,(or ret-type 'null)) ,name))
136 ,@(if (null immediate-args)
137 `((defun ,name ,asyms
138 (declare (optimize (speed 0) (debug 0) (safety 1)))
139 (ffi:c-inline ,asyms ,aftypes ,rftype ,(or defun-body call-str) :one-liner t))))
140 (def-inline ,name :always ,(mapcar #'inline-arg-type-of arg-types) ,rftype ,call-str))))
141
142 (defmacro def-unary-intrinsic (name ret-type insn cost c-name
143 &key (arg-type ret-type) partial result-size immediate-arg)
144 (declare (ignore insn cost partial result-size))
145 `(def-intrinsic ,name (,arg-type ,@(if immediate-arg (list immediate-arg)))
146 ,ret-type ,c-name :immediate-args ,(if immediate-arg `((arg1 ,immediate-arg)))))
147
148 (defmacro def-cvt-to-int32-intrinsic (name ret-type insn cost c-name
149 &key (arg-type ret-type) partial immediate-arg)
150 (declare (ignore insn cost partial))
151 (assert (subtypep ret-type '(signed-byte 32)))
152 `(def-intrinsic ,name (,arg-type ,@(if immediate-arg (list immediate-arg)))
153 ,ret-type ,c-name :immediate-args ,(if immediate-arg `((arg1 ,immediate-arg)))))
154
155 (defmacro def-binary-intrinsic (name ret-type insn cost c-name
156 &key (x-type ret-type) (y-type ret-type)
157 commutative tags immediate-arg)
158 (declare (ignore insn cost commutative tags))
159 `(def-intrinsic ,name (,x-type ,y-type ,@(if immediate-arg (list immediate-arg)))
160 ,ret-type ,c-name :immediate-args ,(if immediate-arg `((arg2 ,immediate-arg)))))
161
162 (defmacro def-sse-int-intrinsic (name int-type ret-type insn cost c-name
163 &key (arg-type ret-type) immediate-arg make-temporary defun-body)
164 (declare (ignore insn cost make-temporary))
165 `(def-intrinsic ,name (,arg-type ,int-type ,@(if immediate-arg (list immediate-arg)))
166 ,ret-type ,c-name :immediate-args ,(if immediate-arg `((arg2 ,immediate-arg)))
167 :defun-body ,defun-body))
168
169 (defmacro def-comparison-intrinsic (name arg-type insn cost c-name &key commutative tags)
170 (declare (ignore insn cost commutative tags))
171 `(def-intrinsic ,name (,arg-type ,arg-type) boolean ,c-name))
172
173 (defmacro %def-aref-intrinsic (tag val-type c-type reader writer &key (aux-args "") (bsize 16))
174 "Defines and exports macros and functios that implement vectorized array access."
175 (let* ((rftype (foreign-type-of val-type))
176 (aref-name (intern (format nil "AREF-~A" tag) *package*))
177 (rm-aref-name (intern (format nil "ROW-MAJOR-AREF-~A" tag) *package*))
178 (rm-aset-name (intern (format nil "ROW-MAJOR-ASET-~A" tag) *package*))
179 (known-elt-types '((single-float "sf")
180 (double-float "df")
181 (ext:byte8 "b8")
182 (ext:integer8 "i8")
183 #+uint16-t (ext:byte16 "b16")
184 #+uint16-t (ext:integer16 "i16")
185 #+uint32-t (ext:byte32 "b32")
186 #+uint32-t (ext:integer32 "i32")
187 #+uint64-t (ext:byte64 "b64")
188 #+uint64-t (ext:integer64 "i64"))))
189 (flet ((fmtr (ptr-fmt &rest ptr-args)
190 (wrap-ret-arg (format nil "~A((~A*)~?~A)"
191 reader c-type ptr-fmt ptr-args aux-args)
192 val-type))
193 (fmtw (ptr-fmt &rest ptr-args)
194 (wrap-ret-arg (format nil "~A((~A*)~?,#2)"
195 writer c-type ptr-fmt ptr-args)
196 val-type 2)))
197 `(progn
198 (export ',aref-name)
199 (export ',rm-aref-name)
200 (defmacro ,aref-name (array &rest indexes)
201 (let ((varr (gensym "ARR")))
202 `(let ((,varr ,array))
203 (declare (:read-only ,varr))
204 (,',rm-aref-name ,varr (array-row-major-index ,varr ,@indexes)))))
205 (proclaim '(ftype (function (array fixnum) ,(or val-type 'null)) ,rm-aref-name))
206 (defun ,rm-aref-name (array index)
207 (declare (optimize (speed 0) (debug 0) (safety 2)))
208 (ffi:c-inline (array index) (:object :int) ,rftype
209 ,(fmtr "ecl_row_major_ptr(#0,#1,~A)" bsize)
210 :one-liner t))
211 ;; AREF
212 (def-inline ,rm-aref-name :always (t t) ,rftype
213 ,(fmtr "ecl_row_major_ptr(#0,fixint(#1),~A)" bsize))
214 (def-inline ,rm-aref-name :always (t fixnum) ,rftype
215 ,(fmtr "ecl_row_major_ptr(#0,#1,~A)" bsize))
216 ;; AREF unsafe
217 ,@(mapcar (lambda (spec)
218 `(def-inline ,rm-aref-name :unsafe ((array ,(first spec)) fixnum) ,rftype
219 ,(fmtr "(&(#0)->array.self.~A[#1])" (second spec))))
220 known-elt-types)
221 ,@(if writer
222 `((define-setf-expander ,aref-name (array &rest indexes)
223 (let ((varr (gensym)) (vidx (gensym)) (vval (gensym)))
224 (values (list varr vidx)
225 (list array `(array-row-major-index ,varr ,@indexes))
226 (list vval)
227 `(,',rm-aset-name ,varr ,vidx ,vval) `(,',rm-aref-name ,varr ,vidx))))
228 (proclaim '(ftype (function (array fixnum ,(declaim-arg-type-of val-type)) ,val-type) ,rm-aset-name))
229 (defun ,rm-aset-name (array index value)
230 (declare (optimize (speed 0) (debug 0) (safety 2)))
231 (prog1 value
232 (ffi:c-inline (array index value) (:object :int ,rftype) :void
233 ,(fmtw "ecl_row_major_ptr(#0,#1,~A)" bsize)
234 :one-liner t)))
235 (defsetf ,rm-aref-name ,rm-aset-name)
236 ;; ASET
237 (def-inline ,rm-aset-name :always (t t ,val-type) ,rftype
238 ,(fmtw "ecl_row_major_ptr(#0,fixint(#1),~A)" bsize))
239 (def-inline ,rm-aset-name :always (t fixnum ,val-type) ,rftype
240 ,(fmtw "ecl_row_major_ptr(#0,#1,~A)" bsize))
241 ;; ASET unsafe
242 ,@(mapcar (lambda (spec)
243 `(def-inline ,rm-aset-name :unsafe ((array ,(first spec)) fixnum ,val-type) ,rftype
244 ,(fmtw "(&(#0)->array.self.~A[#1])" (second spec))))
245 known-elt-types)))))))
246
247 (defmacro def-aref-intrinsic (tag val-type reader-fun writer-fun &key (ref-size 16))
248 `(%def-aref-intrinsic ,tag ,val-type ,(pointer-c-type-of val-type)
249 ,(get reader-fun 'c-function-name) ,(get writer-fun 'c-function-name)
250 :bsize ,ref-size
251 :aux-args ,(get reader-fun 'c-call-aux-args)))
252
253 (defmacro def-mem-intrinsic (name c-type ret-type c-name &key (public t)
254 prefix-args (prefix-fmt "~@{#~36R,~}")
255 postfix-args (postfix-fmt "~@{,#~36R~}" pf-p) ret-arg)
256 "Defines and exports an SSE memory access intrinsic function with matching open-coding rules."
257 (let* ((anums (make-arg-nums (append prefix-args postfix-args)))
258 (asyms (mapcar #'make-arg-name anums))
259 (prefix-nums (subseq anums 0 (length prefix-args)))
260 (postfix-nums (mapcar #'1+ (subseq anums (length prefix-args))))
261 (prefix-syms (subseq asyms 0 (length prefix-args)))
262 (postfix-syms (subseq asyms (length prefix-args)))
263 (prefix-itypes (mapcar #'inline-arg-type-of prefix-args))
264 (postfix-itypes (mapcar #'inline-arg-type-of postfix-args))
265 (rftype (foreign-type-of ret-type))
266 (ptr-idx (length prefix-args))
267 (offset-idx (+ ptr-idx 1 (length postfix-args))))
268 (flet ((fmt (ptr-text)
269 (wrap-ret-arg (format nil "~A(~?(~A*)~?~?)"
270 c-name prefix-fmt prefix-nums
271 c-type ptr-text (list ptr-idx offset-idx)
272 postfix-fmt postfix-nums)
273 ret-type ret-arg)))
274 `(progn
275 ,(when public `(export ',name))
276 (eval-when (:compile-toplevel :load-toplevel :execute)
277 (setf (get ',name 'c-function-name) ,c-name)
278 ,(if (and pf-p (null postfix-args))
279 `(setf (get ',name 'c-call-aux-args) ,postfix-fmt)))
280 (proclaim '(ftype (function (,@(mapcar #'declaim-arg-type-of prefix-args) si:foreign-data
281 ,@(mapcar #'declaim-arg-type-of postfix-args) &optional fixnum) ,ret-type) ,name))
282 (defun ,name (,@prefix-syms ptr ,@postfix-syms &optional (offset 0))
283 (declare (optimize (speed 0) (debug 0) (safety 1)))
284 (ffi:c-inline (,@prefix-syms ptr ,@postfix-syms offset)
285 (,@(mapcar #'foreign-type-of prefix-args) :pointer-void
286 ,@(mapcar #'foreign-type-of postfix-args) :int) ,rftype
287 ,(fmt "(((char*)#~A) + #~A)")
288 :one-liner t))
289 (def-inline ,name :always (,@prefix-itypes t ,@postfix-itypes) ,rftype
290 ,(fmt "ecl_to_pointer(#~A)"))
291 (def-inline ,name :always (,@prefix-itypes t ,@postfix-itypes t) ,rftype
292 ,(fmt "(((char*)ecl_to_pointer(#~A)) + fixint(#~A))"))
293 (def-inline ,name :always (,@prefix-itypes t ,@postfix-itypes fixnum) ,rftype
294 ,(fmt "(((char*)ecl_to_pointer(#~A)) + #~A)"))
295 (def-inline ,name :unsafe (,@prefix-itypes si:foreign-data ,@postfix-itypes) ,rftype
296 ,(fmt "(#~A)->foreign.data"))
297 (def-inline ,name :unsafe (,@prefix-itypes si:foreign-data ,@postfix-itypes t) ,rftype
298 ,(fmt "(((char*)(#~A)->foreign.data) + ecl_fixnum(#~A))"))
299 (def-inline ,name :unsafe (,@prefix-itypes si:foreign-data ,@postfix-itypes fixnum) ,rftype
300 ,(fmt "(((char*)(#~A)->foreign.data) + #~A)"))))))
301
302 (defmacro def-load-intrinsic (name ret-type insn c-name &key register-arg tags size postfix-fmt)
303 (declare (ignore insn tags size))
304 `(def-mem-intrinsic ,name ,(pointer-c-type-of ret-type) ,ret-type ,c-name
305 :prefix-args ,(if register-arg (list ret-type))
306 :postfix-fmt ,(or postfix-fmt "")))
307
308 (defmacro def-store-intrinsic (name ret-type insn c-name &key setf-name)
309 (declare (ignore insn))
310 `(progn
311 (def-mem-intrinsic ,name ,(pointer-c-type-of ret-type) ,ret-type ,c-name
312 :public ,(not setf-name) :postfix-args (,ret-type) :ret-arg 1)
313 ,(if setf-name
314 `(defsetf ,setf-name (pointer &optional (offset 0)) (value)
315 `(,',name ,pointer ,value ,offset)))))
316