-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsbcl.lisp
executable file
·792 lines (739 loc) · 26.4 KB
/
sbcl.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
;;;; 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)
#-sbcl
(error "This file is SBCL only!")
(eval-when (:compile-toplevel :load-toplevel)
(sb-int:set-floating-point-modes :traps nil))
;;;; Misc bits.
(macrolet ((def (name cname ctype ltype one)
`(progn
(declaim (inline ,name))
,(if (sb-sys:find-foreign-symbol-address cname)
`(sb-alien:define-alien-routine (,cname ,name)
,ctype (,ctype ,ctype))
;; In absence of nice cbrt implementation (Windows...)
;; let's use the stupid one. Note: this fails
;; cubic-roots-above.1 test by finding more roots than
;; expected. Haven't checked if this is because this
;; implementation is more or less precise and libc cbrt.
;;
;; FIXME: Could just implement Kahan's cbrt in Lisp.
`(defun ,name (n)
(declare (,ltype n))
(if (minusp n)
(- (expt (- n) ,(/ one 3)))
(expt n ,(/ one 3))))))))
(def cbrt/single "cbrtf" sb-alien:float single-float 1f0)
(def cbrt/double "cbrt" sb-alien:double double-float 1d0))
(declaim (inline cbrt))
(defun cbrt (float)
"Cube root of FLOAT."
(etypecase float
(single-float
(cbrt/single float))
(double-float
(cbrt/double float))))
(declaim (inline single-float-quiet-nan))
(defun single-float-quiet-nan ()
(sb-kernel:make-single-float #x-400000))
(declaim (inline double-float-quiet-nan))
(defun double-float-quiet-nan ()
(sb-kernel:make-double-float #x-80000 0))
(declaim (inline float-nan-p))
(defun float-nan-p (x)
(sb-ext:float-nan-p x))
;;;; :SB-CGA-SSE2 tells vm.lisp if it should use the Lisp versions of various
;;;; functions.
#+x86-64
(eval-when (:compile-toplevel :load-toplevel :execute)
(pushnew :sb-cga-sse2 *features*))
;;;; Aliases for this file
(eval-when (:compile-toplevel)
(defmacro inst (instruction &rest args)
`(sb-assem:inst ,instruction ,@args))
(defmacro move (dst src)
`(sb-c:move ,dst ,src))
(defmacro define-vop (name/inherits &body body)
`(sb-c:define-vop ,name/inherits
,@(subst 'sb-vm::descriptor-reg 'descriptor-reg
(subst 'sb-vm::single-reg 'single-reg body)))))
;;;; Utilities for writing VOPs
#+sb-cga-sse2
(eval-when (:compile-toplevel :load-toplevel :execute)
;; check if we should use sb-vm::ea or make-ea
;; ea was added in 1.4.11
;; make-ea was removed after 2.0.11
(when (fboundp (find-symbol "MAKE-EA" :sb-vm))
(pushnew :sb-cga-make-ea *features*)))
#+sb-cga-sse2
(progn
#-sb-cga-make-ea
(defmacro ea-for-data (vector index)
`(sb-vm::ea (- (+ (* sb-vm:vector-data-offset sb-vm:n-word-bytes)
;; 4 bytes per single-float
(* ,index 4))
sb-vm:other-pointer-lowtag)
,vector))
#+sb-cga-make-ea
(defmacro ea-for-data (vector index)
`(sb-vm::make-ea :dword
:base ,vector
:disp (- (+ (* sb-vm:vector-data-offset sb-vm:n-word-bytes)
;; 4 bytes per single-float
(* ,index 4))
sb-vm:other-pointer-lowtag)))
#-sb-cga-make-ea
(defmacro ea-for-slice (vector &optional (index 0))
`(sb-vm::ea (- (+ (* sb-vm:vector-data-offset sb-vm:n-word-bytes)
;; 4 bytes per single-float, 16 per slice.
(* ,index 16))
sb-vm:other-pointer-lowtag)
,vector))
#+sb-cga-make-ea
(defmacro ea-for-slice (vector &optional (index 0))
`(sb-vm::make-ea :dword
:base ,vector
:disp (- (+ (* sb-vm:vector-data-offset sb-vm:n-word-bytes)
;; 4 bytes per single-float, 16 per slice.
(* ,index 16))
sb-vm:other-pointer-lowtag)))
(defmacro load-slice (xmm vector &optional (index 0))
`(inst movaps ,xmm (ea-for-slice ,vector ,index)))
(defmacro store-slice (xmm vector &optional (index 0))
`(inst movaps (ea-for-slice ,vector ,index) ,xmm))
(defmacro fill-xmm (target source &optional (index 0))
`(progn
,(if (consp source)
`(inst movss ,target ,source)
`(unless (sb-c:location= ,target ,source)
(inst movaps ,target ,source)))
,@(ecase index
(0 `((inst unpcklps ,target ,target)
(inst unpcklps ,target ,target)))
(1 `((inst unpcklps ,target ,target)
(inst unpckhps ,target ,target)))
(2 `((inst unpckhps ,target ,target)
(inst unpcklps ,target ,target)))
(3 `((inst unpckhps ,target ,target)
(inst unpckhps ,target ,target)))))))
;;;; VECTOR COMPARISON
#+sb-cga-sse2
(define-vop (%vec=)
(:translate %vec=)
(:policy :fast-safe)
(:args (vector1 :scs (descriptor-reg))
(vector2 :scs (descriptor-reg)))
(:conditional :e)
;; FIXME: Because there is no plain XMM-REG SC, we abuse SINGLE-REG pretty
;; horribly -- same for all the VOPs that follow.
(:temporary (:sc single-reg) tmp)
(:temporary (:sc descriptor-reg) mask)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector1)
;; Compare
(inst cmpps :neq tmp (ea-for-slice vector2))
;; Grab sign bits, mask 4th elt & check for zero.
(inst movmskps mask tmp)
(inst and mask #b0111)
(inst test mask mask)))
;;;; VECTOR COPYING
#+sb-cga-sse2
(define-vop (%copy-vec)
(:translate %copy-vec)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector)
;; Save copy to source vector
(store-slice tmp result-vector)
(move result result-vector)))
;;;; VECTOR ADDITION
#+sb-cga-sse2
(define-vop (%vec+)
(:translate %vec+)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector1 :scs (descriptor-reg))
(vector2 :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector1)
;; Add
(inst addps tmp (ea-for-slice vector2))
;; Save result to source vector
(store-slice tmp result-vector)
(move result result-vector)))
#+sb-cga-sse2
(macrolet ((def (name target1 target2 result)
`(define-vop (,name)
(:translate ,name)
(:policy :fast-safe)
(:args (vector1 :scs (descriptor-reg) ,@target1)
(vector2 :scs (descriptor-reg) ,@target2))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector1)
;; Add
(inst addps tmp (ea-for-slice vector2))
(store-slice tmp ,result)
(move result ,result)))))
(def %%vec+/1 (:target result) () vector1)
(def %%vec+/2 () (:target result) vector2))
;;;; VECTOR SUBSTRACTION
#+sb-cga-sse2
(define-vop (%vec-)
(:translate %vec-)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector1 :scs (descriptor-reg))
(vector2 :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector1)
;; Sub
(inst subps tmp (ea-for-slice vector2))
;; Save result to source vector
(store-slice tmp result-vector)
(move result result-vector)))
#+sb-cga-sse2
(macrolet ((def (name target1 target2 result)
`(define-vop (,name)
(:translate ,name)
(:policy :fast-safe)
(:args (vector1 :scs (descriptor-reg) ,@target1)
(vector2 :scs (descriptor-reg) ,@target2))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector1)
;; Sub
(inst subps tmp (ea-for-slice vector2))
(store-slice tmp ,result)
(move result ,result)))))
(def %%vec-/1 (:target result) () vector1)
(def %%vec-/2 () (:target result) vector2))
;;;; VECTOR/SCALAR MULTIPLICATION
#+sb-cga-sse2
(define-vop (%vec*)
(:translate %vec*)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector :scs (descriptor-reg))
(f :scs (single-reg) :target tmp))
(:arg-types * * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
(fill-xmm tmp f)
;; Multiply
(inst mulps tmp (ea-for-slice vector))
;; Save result to result vector
(store-slice tmp result-vector)
(move result result-vector)))
#+sb-cga-sse2
(define-vop (%%vec*/1)
(:translate %%vec*/1)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg) :target result)
(f :scs (single-reg) :target result))
(:arg-types * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
(fill-xmm tmp f)
;; Multiply
(inst mulps tmp (ea-for-slice vector))
(store-slice tmp vector)
(move result vector)))
;;;; VECTOR/SCALAR DIVISION
#+sb-cga-sse2
(define-vop (%vec/)
(:translate %vec/)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector :scs (descriptor-reg))
(f :scs (single-reg) :target floats))
(:arg-types * * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:temporary (:sc single-reg) floats)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector)
(fill-xmm floats f)
;; Divide
(inst divps tmp floats)
;; Save result to source vector
(store-slice tmp result-vector)
(move result result-vector)))
#+sb-cga-sse2
(define-vop (%%vec//1)
(:translate %%vec//1)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg) :target result)
(f :scs (single-reg) :target floats))
(:arg-types * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:temporary (:sc single-reg) floats)
(:generator 10
(fill-xmm floats f)
;; Divide
(load-slice tmp vector)
(inst divps tmp floats)
(store-slice tmp vector)
(move result vector)))
;;;; DOT PRODUCT
#+sb-cga-sse2
(define-vop (%dot-product)
(:translate %dot-product)
(:policy :fast-safe)
(:args (vector1 :scs (descriptor-reg))
(vector2 :scs (descriptor-reg)))
(:arg-types * *)
(:results (result :scs (single-reg)))
(:result-types single-float)
(:temporary (:sc single-reg) tmp2)
(:temporary (:sc single-reg) tmp3)
(:generator 10
;; Load elements of VECTOR1
(inst movss result (ea-for-data vector1 0))
(inst movss tmp2 (ea-for-data vector1 1))
(inst movss tmp3 (ea-for-data vector1 2))
;; Multiply by elements of VECTOR2
(inst mulss result (ea-for-data vector2 0))
(inst mulss tmp2 (ea-for-data vector2 1))
(inst mulss tmp3 (ea-for-data vector2 2))
;; Add
(inst addss result tmp2)
(inst addss result tmp3)))
;;;; HADAMARD PRODUCT
#+sb-cga-sse2
(define-vop (%hadamard-product)
(:translate %hadamard-product)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector1 :scs (descriptor-reg))
(vector2 :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector1)
;; Multiply elementwise
(inst mulps tmp (ea-for-slice vector2))
;; Result
(store-slice tmp result-vector)
(move result result-vector)))
#+sb-cga-sse2
(macrolet ((def (name target1 target2 result)
`(define-vop (,name)
(:translate ,name)
(:policy :fast-safe)
(:args (vector1 :scs (descriptor-reg) ,@target1)
(vector2 :scs (descriptor-reg) ,@target2))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
;; Load vector into TMP
(load-slice tmp vector2)
;; Multiply elementwise
(inst mulps tmp (ea-for-slice vector1))
(store-slice tmp ,result)
(move result ,result)))))
(def %%hadamard-product/1 (:target result) () vector1)
(def %%hadamard-product/2 () (:target result) vector2))
;;; VECTOR LENGTH
#+sb-cga-sse2
(define-vop (%vec-length)
(:translate %vec-length)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg)))
(:results (result :scs (single-reg)))
(:result-types single-float)
(:temporary (:sc single-reg) tmp1)
(:temporary (:sc single-reg) tmp2)
(:generator 10
;; Again, vector ops not so hot -- so just hand-optimize.
(inst movss result (ea-for-data vector 0))
(inst movss tmp1 (ea-for-data vector 1))
(inst movss tmp2 (ea-for-data vector 2))
(inst mulss result result)
(inst mulss tmp1 tmp1)
(inst mulss tmp2 tmp2)
(inst addss result tmp1)
(inst addss result tmp2)
(inst sqrtss result result)))
;;;; NORMALIZATION
#+sb-cga-sse2
(define-vop (%normalize)
(:translate %normalize)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp1)
(:temporary (:sc single-reg) tmp2)
(:temporary (:sc single-reg) tmp3)
(:temporary (:sc single-reg) tmp4)
(:temporary (:sc single-reg) tmp5)
(:temporary (:sc single-reg) tmp6)
(:generator 10
(inst movss tmp1 (ea-for-data vector 0))
(inst movss tmp2 (ea-for-data vector 1))
(inst movss tmp3 (ea-for-data vector 2))
(inst movss tmp4 tmp1)
(inst movss tmp5 tmp2)
(inst movss tmp6 tmp3)
;; Compute 1/length into tmp1
(inst mulss tmp1 tmp1)
(inst mulss tmp2 tmp2)
(inst mulss tmp3 tmp3)
(inst addss tmp1 tmp2)
(inst addss tmp1 tmp3)
(inst rsqrtss tmp1 tmp1)
;; Multiply original -- is this faster then loading again and
;; using MULPS?
(inst mulss tmp4 tmp1)
(inst mulss tmp5 tmp1)
(inst mulss tmp6 tmp1)
;; Store result
(inst movss (ea-for-data result-vector 0) tmp4)
(inst movss (ea-for-data result-vector 1) tmp5)
(inst movss (ea-for-data result-vector 2) tmp6)
(move result result-vector)))
#+sb-cga-sse2
(define-vop (%%normalize/1)
(:translate %%normalize/1)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg) :target result))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp1)
(:temporary (:sc single-reg) tmp2)
(:temporary (:sc single-reg) tmp3)
(:temporary (:sc single-reg) tmp4)
(:temporary (:sc single-reg) tmp5)
(:temporary (:sc single-reg) tmp6)
(:generator 10
(inst movss tmp1 (ea-for-data vector 0))
(inst movss tmp2 (ea-for-data vector 1))
(inst movss tmp3 (ea-for-data vector 2))
(inst movss tmp4 tmp1)
(inst movss tmp5 tmp2)
(inst movss tmp6 tmp3)
;; Compute the length into tmp1
(inst mulss tmp1 tmp1)
(inst mulss tmp2 tmp2)
(inst mulss tmp3 tmp3)
(inst addss tmp1 tmp2)
(inst addss tmp1 tmp3)
(inst rsqrtss tmp1 tmp1)
;; Divide original -- is this faster then loading again and
;; using MULPS?
(inst mulss tmp4 tmp1)
(inst mulss tmp5 tmp1)
(inst mulss tmp6 tmp1)
;; Store result
(inst movss (ea-for-data vector 0) tmp4)
(inst movss (ea-for-data vector 1) tmp5)
(inst movss (ea-for-data vector 2) tmp6)
(move result vector)))
#+sb-cga-sse2
(define-vop (%%normalized-vec)
(:translate %%normalized-vec)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg) :target result)
(x :scs (single-reg))
(y :scs (single-reg))
(z :scs (single-reg)))
(:arg-types t single-float single-float single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) a)
(:temporary (:sc single-reg) b)
(:temporary (:sc single-reg) c)
(:generator 10
(inst movss a x)
(inst movss b y)
(inst movss c z)
;; Compute the length into X
(inst mulss x x)
(inst mulss y y)
(inst mulss z z)
(inst addss x y)
(inst addss x z)
(inst rsqrtss x x)
;; Divide original -- is this faster then loading again and
;; using MULPS?
(inst mulss a x)
(inst mulss b x)
(inst mulss c x)
;; Store result
(inst movss (ea-for-data vector 0) a)
(inst movss (ea-for-data vector 1) b)
(inst movss (ea-for-data vector 2) c)
(move result vector)))
;;;; LINEAR INTERPOLATION
#+sb-cga-sse2
(define-vop (%vec-lerp)
(:translate %vec-lerp)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector1 :scs (descriptor-reg))
(vector2 :scs (descriptor-reg))
(f :scs (single-reg) :target floats))
(:arg-types * * * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) floats)
(:temporary (:sc single-reg) 1-floats)
(:generator 10
(fill-xmm floats f)
(fill-xmm 1-floats (sb-c:register-inline-constant 1.0))
(inst subps 1-floats floats)
;; Multiply VECTOR1 by 1-FLOATS, and VECTOR2 by FLOATS
(inst mulps 1-floats (ea-for-slice vector1))
(inst mulps floats (ea-for-slice vector2))
;; Add
(inst addps floats 1-floats)
;; Save result and return
(store-slice floats result-vector)
(move result result-vector)))
#+sb-cga-sse2
(macrolet ((def (name target1 target2 result)
`(define-vop (,name)
(:translate ,name)
(:policy :fast-safe)
(:args (vector1 :scs (descriptor-reg) ,@target1)
(vector2 :scs (descriptor-reg) ,@target2)
(f :scs (single-reg) :target floats))
(:arg-types * * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) floats)
(:temporary (:sc single-reg) 1-floats)
(:generator 10
(fill-xmm floats f)
(fill-xmm 1-floats (sb-c:register-inline-constant 1.0))
(inst subps 1-floats floats)
;; Multiply VECTOR1 by 1-FLOATS, and VECTOR2 by FLOATS
(inst mulps 1-floats (ea-for-slice vector1))
(inst mulps floats (ea-for-slice vector2))
;; Add
(inst addps floats 1-floats)
;; Save result and return
(store-slice floats ,result)
(move result ,result)))))
(def %%vec-lerp/1 (:target result) () vector1)
(def %%vec-lerp/2 () (:target result) vector2))
;;;; TRANSFORMING A POINT
#+sb-cga-sse2
(define-vop (%transform-point)
(:translate %transform-point)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector :scs (descriptor-reg))
(matrix :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) vec)
(:temporary (:sc single-reg) col1)
(:temporary (:sc single-reg) col2)
(:temporary (:sc single-reg) col3)
(:generator 10
(load-slice vec vector)
;; Distribute vec[2] and multiply
(inst movaps col3 vec)
(inst unpckhps col3 col3)
(inst unpcklps col3 col3)
(inst mulps col3 (ea-for-slice matrix 2))
;; Distribute vec[1] and multiply
(inst movaps col2 vec)
(inst unpcklps col2 col2)
(inst unpckhps col2 col2)
(inst mulps col2 (ea-for-slice matrix 1))
;; Distribute vec[0] and multiply
(inst movaps col1 vec)
(inst unpcklps col1 col1)
(inst unpcklps col1 col1)
(inst mulps col1 (ea-for-slice matrix 0))
;; Add rows -- including the translation column.
(inst addps col1 (ea-for-slice matrix 3))
(inst addps col3 col2)
(inst addps col1 col3)
;; Store result
(store-slice col1 result-vector)
(move result result-vector)))
#+sb-cga-sse2
(define-vop (%%transform-point/1)
(:translate %%transform-point/1)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg) :target result)
(matrix :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) vec)
(:temporary (:sc single-reg) col1)
(:temporary (:sc single-reg) col2)
(:temporary (:sc single-reg) col3)
(:generator 10
(load-slice vec vector)
;; Distribute vec[2] and multiply
(inst movaps col3 vec)
(inst unpckhps col3 col3)
(inst unpcklps col3 col3)
(inst mulps col3 (ea-for-slice matrix 2))
;; Distribute vec[1] and multiply
(inst movaps col2 vec)
(inst unpcklps col2 col2)
(inst unpckhps col2 col2)
(inst mulps col2 (ea-for-slice matrix 1))
;; Distribute vec[0] and multiply
(inst movaps col1 vec)
(inst unpcklps col1 col1)
(inst unpcklps col1 col1)
(inst mulps col1 (ea-for-slice matrix 0))
;; Add rows -- including the translation column.
(inst addps col1 (ea-for-slice matrix 3))
(inst addps col3 col2)
(inst addps col1 col3)
;; Store result
(store-slice col1 vector)
(move result vector)))
;;;; TRANSFORMING A DIRECTION
#+sb-cga-sse2
(define-vop (%transform-direction)
(:translate %transform-direction)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(vector :scs (descriptor-reg))
(matrix :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) vec)
(:temporary (:sc single-reg) col1)
(:temporary (:sc single-reg) col2)
(:temporary (:sc single-reg) col3)
(:generator 10
;; Load stuff
(load-slice vec vector)
;; Distribute vec[2] and multiply
(inst movaps col3 vec)
(inst unpckhps col3 col3)
(inst unpcklps col3 col3)
(inst mulps col3 (ea-for-slice matrix 2))
;; Distribute vec[1] and multiply
(inst movaps col2 vec)
(inst unpcklps col2 col2)
(inst unpckhps col2 col2)
(inst mulps col2 (ea-for-slice matrix 1))
;; Distribute vec[0] and multiply
(inst movaps col1 vec)
(inst unpcklps col1 col1)
(inst unpcklps col1 col1)
(inst mulps col1 (ea-for-slice matrix 0))
;; Add rows -- except for translation.
(inst addps col1 col2)
(inst addps col1 col3)
;; Store result
(store-slice col1 result-vector)
(move result result-vector)))
#+sb-cga-sse2
(define-vop (%%transform-direction/1)
(:translate %%transform-direction/1)
(:policy :fast-safe)
(:args (vector :scs (descriptor-reg) :target result)
(matrix :scs (descriptor-reg)))
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) vec)
(:temporary (:sc single-reg) col1)
(:temporary (:sc single-reg) col2)
(:temporary (:sc single-reg) col3)
(:generator 10
;; Load stuff
(load-slice vec vector)
;; Distribute vec[2] and multiply
(inst movaps col3 vec)
(inst unpckhps col3 col3)
(inst unpcklps col3 col3)
(inst mulps col3 (ea-for-slice matrix 2))
;; Distribute vec[1] and multiply
(inst movaps col2 vec)
(inst unpcklps col2 col2)
(inst unpckhps col2 col2)
(inst mulps col2 (ea-for-slice matrix 1))
;; Distribute vec[0] and multiply
(inst movaps col1 vec)
(inst unpcklps col1 col1)
(inst unpcklps col1 col1)
(inst mulps col1 (ea-for-slice matrix 0))
;; Add rows -- except for translation.
(inst addps col1 col2)
(inst addps col1 col3)
;; Store result
(store-slice col1 vector)
(move result vector)))
;;;; ADJUSTING A VECTOR
#+sb-cga-sse2
(define-vop (%adjust-vec)
(:translate %adjust-vec)
(:policy :fast-safe)
(:args (result-vector :scs (descriptor-reg) :target result)
(point :scs (descriptor-reg))
(direction :scs (descriptor-reg))
(distance :scs (single-reg) :target tmp))
(:arg-types * * * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
(fill-xmm tmp distance)
;; Multiply by DIRECTION
(inst mulps tmp (ea-for-slice direction))
;; Add POINT
(inst addps tmp (ea-for-slice point))
;; Result
(store-slice tmp result-vector)
(move result result-vector)))
#+sb-cga-sse2
(macrolet ((def (name target1 target2 result)
`(define-vop (,name)
(:translate ,name)
(:policy :fast-safe)
(:args (point :scs (descriptor-reg) ,@target1)
(direction :scs (descriptor-reg) ,@target2)
(distance :scs (single-reg) :target tmp))
(:arg-types * * single-float)
(:results (result :scs (descriptor-reg)))
(:temporary (:sc single-reg) tmp)
(:generator 10
(fill-xmm tmp distance)
;; Multiply by DIRECTION
(inst mulps tmp (ea-for-slice direction))
;; Add POINT
(inst addps tmp (ea-for-slice point))
(store-slice tmp ,result)
(move result ,result)))))
(def %%adjust-vec/1 (:target result) () point)
(def %%adjust-vec/2 () (:target result) direction))