]> gitweb.factorcode.org Git - factor.git/blob - extra/math/matrices/simd/simd.factor
Merge branch 'reentrantvm' of git://github.com/phildawes/factor
[factor.git] / extra / math / matrices / simd / simd.factor
1 ! (c)Joe Groff bsd license
2 USING: accessors classes.struct generalizations kernel locals
3 math math.combinatorics math.functions math.matrices.simd math.vectors
4 math.vectors.simd sequences sequences.private specialized-arrays
5 typed ;
6 QUALIFIED-WITH: alien.c-types c
7 SIMD: c:float
8 SPECIALIZED-ARRAY: float-4
9 IN: math.matrices.simd
10
11 STRUCT: matrix4
12     { rows float-4[4] } ;
13
14 INSTANCE: matrix4 immutable-sequence
15
16 M: matrix4 length drop 4 ; inline
17 M: matrix4 nth-unsafe rows>> nth-unsafe ; inline
18 M: matrix4 new-sequence 2drop matrix4 (struct) ; inline
19
20 <PRIVATE
21
22 : rows ( a -- a1 a2 a3 a4 )
23     rows>> 4 firstn ; inline
24
25 :: set-rows ( c1 c2 c3 c4 c -- c )
26     c rows>> :> rows
27     c1 rows set-first
28     c2 rows set-second
29     c3 rows set-third
30     c4 rows set-fourth
31     c ; inline
32
33 :: 2map-rows ( a b quot -- c )
34     matrix4 (struct) :> c
35
36     a rows :> a4 :> a3 :> a2 :> a1
37     b rows :> b4 :> b3 :> b2 :> b1
38
39     a1 b1 quot call
40     a2 b2 quot call
41     a3 b3 quot call
42     a4 b4 quot call
43
44     c set-rows ; inline
45
46 :: map-rows ( a quot -- c )
47     matrix4 (struct) :> c
48
49     a rows :> a4 :> a3 :> a2 :> a1
50
51     a1 quot call
52     a2 quot call
53     a3 quot call
54     a4 quot call
55
56     c set-rows ; inline
57     
58 PRIVATE>
59
60 TYPED: m4+ ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v+ ] 2map-rows ;
61 TYPED: m4- ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v- ] 2map-rows ;
62 TYPED: m4* ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v* ] 2map-rows ;
63 TYPED: m4/ ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v/ ] 2map-rows ;
64
65 TYPED: m4*n ( a: matrix4 b: float -- c: matrix4 ) [ v*n ] curry map-rows ;
66 TYPED: m4/n ( a: matrix4 b: float -- c: matrix4 ) [ v/n ] curry map-rows ;
67 TYPED: n*m4 ( a: float b: matrix4 -- c: matrix4 ) [ n*v ] with map-rows ;
68 TYPED: n/m4 ( a: float b: matrix4 -- c: matrix4 ) [ n/v ] with map-rows ;
69
70 TYPED:: m4. ( a: matrix4 b: matrix4 -- c: matrix4 )
71     matrix4 (struct) :> c
72
73     a rows :> a4 :> a3 :> a2 :> a1
74     b rows :> b4 :> b3 :> b2 :> b1
75
76     a1 first  b1 n*v :> c1a
77     a2 first  b1 n*v :> c2a
78     a3 first  b1 n*v :> c3a
79     a4 first  b1 n*v :> c4a
80
81     a1 second b2 n*v c1a v+ :> c1b 
82     a2 second b2 n*v c2a v+ :> c2b
83     a3 second b2 n*v c3a v+ :> c3b
84     a4 second b2 n*v c4a v+ :> c4b
85
86     a1 third  b3 n*v c1b v+ :> c1c 
87     a2 third  b3 n*v c2b v+ :> c2c
88     a3 third  b3 n*v c3b v+ :> c3c
89     a4 third  b3 n*v c4b v+ :> c4c
90
91     a1 fourth b4 n*v c1c v+
92     a2 fourth b4 n*v c2c v+
93     a3 fourth b4 n*v c3c v+
94     a4 fourth b4 n*v c4c v+
95
96     c set-rows ;
97
98 TYPED:: v.m4 ( a: float-4 b: matrix4 -- c: float-4 )
99     b rows :> b4 :> b3 :> b2 :> b1
100     
101     a first  b1 n*v
102     a second b2 n*v v+
103     a third  b3 n*v v+
104     a fourth b4 n*v v+ ;
105
106 TYPED:: m4.v ( a: matrix4 b: float-4 -- c: float-4 )
107     a rows [ b v. ] 4 napply float-4-boa ;
108
109 CONSTANT: identity-matrix4
110     S{ matrix4 f
111         float-4-array{
112             float-4{ 1.0 0.0 0.0 0.0 }
113             float-4{ 0.0 1.0 0.0 0.0 }
114             float-4{ 0.0 0.0 1.0 0.0 }
115             float-4{ 0.0 0.0 0.0 1.0 }
116         }
117     }
118
119 CONSTANT: zero-matrix4
120     S{ matrix4 f
121         float-4-array{
122             float-4{ 0.0 0.0 0.0 0.0 }
123             float-4{ 0.0 0.0 0.0 0.0 }
124             float-4{ 0.0 0.0 0.0 0.0 }
125             float-4{ 0.0 0.0 0.0 0.0 }
126         }
127     }
128
129 TYPED:: m4^n ( m: matrix4 n: fixnum -- m^n: matrix4 )
130     identity-matrix4 n [ m m4. ] times ;
131
132 TYPED:: scale-matrix4 ( factors: float-4 -- matrix: matrix4 )
133     matrix4 (struct) :> c
134
135     factors float-4{ t t t f } vbitand :> factors'
136
137     factors' { 0 3 3 3 } vshuffle
138     factors' { 3 1 3 3 } vshuffle
139     factors' { 3 3 2 3 } vshuffle
140     float-4{ 0.0 0.0 0.0 1.0 }
141
142     c set-rows ;
143
144 : ortho-matrix4 ( factors -- matrix )
145     float-4{ 1.0 1.0 1.0 1.0 } swap v/ scale-matrix4 ; inline
146
147 TYPED:: translation-matrix4 ( offset: float-4 -- matrix: matrix4 )
148     matrix4 (struct) :> c
149
150     float-4{ 0.0 0.0 0.0 1.0 } :> c4
151     float-4{ t t t f } offset c4 v? :> offset'
152
153     offset' { 3 3 3 0 } vshuffle float-4{ t f f t } vbitand
154     offset' { 3 3 3 1 } vshuffle float-4{ f t f t } vbitand
155     offset' { 3 3 3 2 } vshuffle float-4{ f f t t } vbitand
156     c4
157
158     c set-rows ;
159
160 TYPED:: rotation-matrix4 ( axis: float-4 theta: float -- matrix: matrix4 )
161     !   x*x + c*(1.0 - x*x)   x*y*(1.0 - c) - s*z   x*z*(1.0 - c) + s*y   0
162     !   x*y*(1.0 - c) + s*z   y*y + c*(1.0 - y*y)   y*z*(1.0 - c) - s*x   0
163     !   x*z*(1.0 - c) - s*y   y*z*(1.0 - c) + s*x   z*z + c*(1.0 - z*z)   0
164     !   0                     0                     0                     1
165     matrix4 (struct) :> triangle-m
166     theta cos :> c
167     theta sin :> s
168
169     float-4{  1.0 -1.0  1.0 0.0 } :> triangle-sign
170
171     c float-4-with :> cc
172     s float-4-with :> ss
173     1.0 float-4-with :> ones
174     ones cc v- :> 1-c
175     axis axis v* :> axis2
176
177     axis2 cc ones axis2 v- v* v+ :> diagonal
178
179     axis { 1 0 0 3 } vshuffle axis { 2 2 1 3 } vshuffle v* 1-c v*
180     float-4{ t t t f } vbitand :> triangle-a
181     ss axis v* triangle-sign v* :> triangle-b
182     triangle-a triangle-b v+ :> triangle-lo
183     triangle-a triangle-b v- :> triangle-hi
184
185     diagonal scale-matrix4 :> diagonal-m
186
187     triangle-hi { 3 2 1 3 } vshuffle
188     triangle-hi { 3 3 0 3 } vshuffle triangle-lo { 2 3 3 3 } vshuffle v+
189     triangle-lo { 1 0 3 3 } vshuffle
190     float-4 new
191
192     triangle-m set-rows drop
193
194     diagonal-m triangle-m m4+ ;
195
196 TYPED:: frustum-matrix4 ( xy: float-4 near: float far: float -- matrix: matrix4 )
197     matrix4 (struct) :> c
198
199     near near near far + 2 near far * * float-4-boa :> num
200     float-4{ t t f f } xy near far - float-4-with v? :> denom
201     num denom v/ :> fov
202
203     fov { 0 0 0 0 } vshuffle float-4{ t f f f } vbitand
204     fov { 1 1 1 1 } vshuffle float-4{ f t f f } vbitand
205     fov { 2 2 2 3 } vshuffle float-4{ f f t t } vbitand
206     float-4{ 0.0 0.0 -1.0 0.0 }
207
208     c set-rows ;
209