-
Notifications
You must be signed in to change notification settings - Fork 8
/
vm.lisp
285 lines (241 loc) · 9.12 KB
/
vm.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;;;; by Nikodemus Siivola <[email protected]>, 2009.
;;;;
;;;; Permission is hereby granted, free of charge, to any person
;;;; obtaining a copy of this software and associated documentation files
;;;; (the "Software"), to deal in the Software without restriction,
;;;; including without limitation the rights to use, copy, modify, merge,
;;;; publish, distribute, sublicense, and/or sell copies of the Software,
;;;; and to permit persons to whom the Software is furnished to do so,
;;;; subject to the following conditions:
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(in-package :sb-cga)
(defmacro define-vm-fun (name lambda-list &body generic-body)
(multiple-value-bind (forms declarations doc)
(alexandria:parse-body generic-body :documentation t)
(declare (ignorable forms))
;; Out-of-line wrappers for SSE2 versions, and pure lisp versions
;; for SSE2-less platforms.
;;
;; FIXME: This means other ports are all-or-nothing propositions.
;; ...so this should really be conditionalized per-function somehow.
`(progn
#-sb-cga-sse2
(declaim (inline ,name))
(defun ,name ,lambda-list
,@(when doc (list doc))
,@declarations
(declare (optimize (speed 3) (safety 1) (debug 1)
#+sb-cga-sse2
(sb-c::recognize-self-calls 0)))
#+sb-cga-sse2
(,name ,@lambda-list)
#-sb-cga-sse2
(progn ,@forms)))))
;;;; COMPARISON
(define-vm-fun %vec= (a b)
(macrolet ((dim (n)
`(= (aref a ,n) (aref b ,n))))
(and (dim 0) (dim 1) (dim 2))))
;;;; VECTOR COPYING
(define-vm-fun %copy-vec (result vec)
"Copy contents of VEC into RESULT, return RESULT. Unsafe."
(macrolet ((dim (n)
`(setf (aref result ,n) (aref vec ,n))))
(dim 0)
(dim 1)
(dim 2)
result))
;;;; VECTOR ADDITION
(define-vm-fun %vec+ (result a b)
"Add VEC A and B, store result in VEC RESULT. Return RESULT. Unsafe"
(macrolet ((dim (n)
`(setf (aref result ,n) (+ (aref a ,n) (aref b ,n)))))
(dim 0)
(dim 1)
(dim 2)
result))
(define-vm-fun %%vec+/1 (a b) (%vec+ a a b))
(define-vm-fun %%vec+/2 (a b) (%vec+ b a b))
;;;; VECTOR SUBSTRACTION
(define-vm-fun %vec- (result a b)
"Substract VEC B from VEC A, store result in VEC RESULT. Return RESULT.
Unsafe."
(macrolet ((dim (n)
`(setf (aref result ,n) (- (aref a ,n) (aref b ,n)))))
(dim 0)
(dim 1)
(dim 2)
result))
(define-vm-fun %%vec-/1 (a b) (%vec- a a b))
(define-vm-fun %%vec-/2 (a b) (%vec- b a b))
;;;; VECTOR/SCALAR MULTIPLICATION
(define-vm-fun %vec* (result a f)
"Multiply VEC A with single-float F, store result in VEC RESULT. Return
RESULT. Unsafe."
(macrolet ((dim (n)
`(setf (aref result ,n) (* (aref a ,n) f))))
(dim 0)
(dim 1)
(dim 2)
result))
(define-vm-fun %%vec*/1 (a f) (%vec* a a f))
;;;; VECTOR/SCALAR DIVISION
(define-vm-fun %vec/ (result a f)
"Divide VEC A by single-float F, store result in VEC RESULT. Return RESULT.
Unsafe."
(macrolet ((dim (n)
`(setf (aref result ,n) (/ (aref a ,n) f))))
(dim 0)
(dim 1)
(dim 2)
result))
(define-vm-fun %%vec//1 (a f) (%vec/ a a f))
;;;; DOT PRODUCT
(define-vm-fun %dot-product (a b)
(macrolet ((dim (n)
`(* (aref a ,n) (aref b ,n))))
(+ (dim 0) (dim 1) (dim 2))))
;;;; HADAMARD PRODUCT
(define-vm-fun %hadamard-product (result a b)
"Compute hadamard product (elementwise product) of VEC A and VEC B, store
result in VEC RESULT. Return RESULT. Unsafe."
(macrolet ((dim (n)
`(setf (aref result ,n) (* (aref a ,n) (aref b ,n)))))
(dim 0)
(dim 1)
(dim 2)
result))
(define-vm-fun %%hadamard-product/1 (a b) (%hadamard-product a a b))
(define-vm-fun %%hadamard-product/2 (a b) (%hadamard-product b a b))
;;;; LENGTH
(define-vm-fun %vec-length (a)
(macrolet ((dim (n)
`(let ((d (aref a ,n)))
(* d d))))
(sqrt (+ (dim 0) (dim 1) (dim 2)))))
;;;; NORMALIZATION
(define-vm-fun %normalize (result a)
"Normalize VEC A, store result into VEC RESULT. Return RESULT. Unsafe."
(let* ((va (aref a 0))
(vb (aref a 1))
(vc (aref a 2))
(len (sqrt (+ (* va va) (* vb vb) (* vc vc)))))
(setf (aref result 0) (/ va len)
(aref result 1) (/ vb len)
(aref result 2) (/ vc len))
result))
(define-vm-fun %%normalize/1 (a) (%normalize a a))
(define-vm-fun %%normalized-vec (result x y z)
(let ((len (sqrt (+ (* x x) (* y y) (* z z)))))
(setf (aref result 0) (/ x len)
(aref result 1) (/ y len)
(aref result 2) (/ z len))
result))
;;;; LINEAR INTERPOLATION
(define-vm-fun %vec-lerp (result a b f)
"Linear interpolate VEC A and VEC B using single-float F as the
interpolation factor, store result in VEC RESULT. Return RESULT. Unsafe."
(let ((f2 (- 1.0 f)))
(macrolet ((dim (n)
`(setf (aref result ,n) (+ (* f2 (aref a ,n)) (* f (aref b ,n))))))
(dim 0)
(dim 1)
(dim 2))
result))
(define-vm-fun %%vec-lerp/1 (a b f) (%vec-lerp a a b f))
(define-vm-fun %%vec-lerp/2 (a b f) (%vec-lerp b a b f))
;;;; TRANSFORMING A VECTOR -- either as a point or a direction
(define-vm-fun %transform-point (result vec matrix)
"Apply transformation MATRIX to VEC, store result in RESULT. Return RESULT. Unsafe."
(let ((a (aref vec 0))
(b (aref vec 1))
(c (aref vec 2)))
(macrolet ((dim (n)
`(setf (aref result ,n)
(+ (* a (mref matrix ,n 0))
(* b (mref matrix ,n 1))
(* c (mref matrix ,n 2))
(mref matrix ,n 3)))))
(dim 0)
(dim 1)
(dim 2)
result)))
(define-vm-fun %%transform-point/1 (vec matrix)
(%transform-point vec vec matrix))
(define-vm-fun %transform-direction (result vec matrix)
"Apply transformation MATRIX to VEC, store result in RESULT. Return RESULT. Unsafe."
(let ((a (aref vec 0))
(b (aref vec 1))
(c (aref vec 2)))
(macrolet ((dim (n)
`(setf (aref result ,n)
(+ (* a (mref matrix ,n 0))
(* b (mref matrix ,n 1))
(* c (mref matrix ,n 2))))))
(dim 0)
(dim 1)
(dim 2)
result)))
(define-vm-fun %%transform-direction/1 (vec matrix)
(%transform-direction vec vec matrix))
;;;; ADJUSTING A VECTOR
(define-vm-fun %adjust-vec (result point direction distance)
"Multiply VEC DIRECTION by single-float DISTANCE adding the result to VEC POINT.
Store result in RESULT, and return it."
(macrolet ((dim (n)
`(setf (aref result ,n)
(+ (aref point ,n) (* (aref direction ,n) distance)))))
(dim 0)
(dim 1)
(dim 2)
result))
(define-vm-fun %%adjust-vec/1 (point direction distance)
(%adjust-vec point point direction distance))
(define-vm-fun %%adjust-vec/2 (point direction distance)
(%adjust-vec direction point direction distance))
;;;; Mapping from n-ary consing to non-consing versions where the first
;;;; argument is both an operand an a place to store the result.
(defvar *optimizable-funs* nil)
(defvar *alloc-funs* nil)
(defun optimize-vec-allocation (form)
;; If the first or second argument is known to return a freshly
;; consed value, reuse it by rewriting:
;;
;; (foo (bar ...) ...) -> (%%foo/1 (bar ...) ...)
;; (foo x (bar ...) ...) -> (%%foo/2 x (bar ...) ...)
;;
(flet ((opt-arg-p (arg)
(and (consp arg)
(let ((op (car arg)))
(or (member op *alloc-funs* :test #'eq)
(assoc op *optimizable-funs* :test #'eq))))))
(destructuring-bind (name/1 name/2)
(cdr (assoc (car form) *optimizable-funs* :test #'eq))
(let ((res (if name/2
(destructuring-bind (arg1 arg2 &rest more) (cdr form)
(cond ((opt-arg-p arg1)
`(,name/1 ,arg1 ,arg2 ,@more))
((opt-arg-p arg2)
`(,name/2 ,arg1 ,arg2 ,@more))
(t
form)))
(destructuring-bind (arg1 &rest more) (cdr form)
(if (opt-arg-p arg1)
`(,name/1 ,arg1 ,@more)
form)))))
#+nil
(unless (eq form res) (break "~S -> ~S" form res))
res))))
(defun note-optimizable-fun (name name/1 &optional name/2)
(let ((cell (assoc name *optimizable-funs* :test #'eq)))
(if cell
(setf (cdr cell) (list name/1 name/2))
(push (list name name/1 name/2) *optimizable-funs*))
name))