Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / dynamics / basic_demo / Stubs / Adlfloat4.inl
1 /*
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose, 
7 including commercial applications, and to alter it and redistribute it freely, 
8 subject to the following restrictions:
9
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14 //Originally written by Takahiro Harada
15
16
17 //#define CHECK_ALIGNMENT(a) CLASSERT((u32(&(a)) & 0xf) == 0);
18 #define CHECK_ALIGNMENT(a) a;
19
20
21 __inline
22 float4 make_float4(float x, float y, float z, float w = 0.f)
23 {
24         float4 v;
25         v.x = x; v.y = y; v.z = z; v.w = w;
26         return v;
27 }
28
29 __inline
30 float4 make_float4(float x)
31 {
32         return make_float4(x,x,x,x);
33 }
34
35 __inline
36 float4 make_float4(const int4& x)
37 {
38         return make_float4((float)x.s[0], (float)x.s[1], (float)x.s[2], (float)x.s[3]);
39 }
40
41 __inline
42 float2 make_float2(float x, float y)
43 {
44         float2 v;
45         v.s[0] = x; v.s[1] = y;
46         return v;
47 }
48
49 __inline
50 float2 make_float2(float x)
51 {
52         return make_float2(x,x);
53 }
54
55 __inline
56 float2 make_float2(const int2& x)
57 {
58         return make_float2((float)x.s[0], (float)x.s[1]);
59 }
60
61 __inline
62 int4 make_int4(int x, int y, int z, int w = 0)
63 {
64         int4 v;
65         v.s[0] = x; v.s[1] = y; v.s[2] = z; v.s[3] = w;
66         return v;
67 }
68
69 __inline
70 int4 make_int4(int x)
71 {
72         return make_int4(x,x,x,x);
73 }
74
75 __inline
76 int4 make_int4(const float4& x)
77 {
78         return make_int4((int)x.x, (int)x.y, (int)x.z, (int)x.w);
79 }
80
81 __inline
82 int2 make_int2(int a, int b)
83 {
84         int2 ans; ans.x = a; ans.y = b;
85         return ans;
86 }
87
88 __inline
89 float4 operator-(const float4& a)
90 {
91         return make_float4(-a.x, -a.y, -a.z, -a.w);
92 }
93
94 __inline
95 float4 operator*(const float4& a, const float4& b)
96 {
97         CLASSERT((u32(&a) & 0xf) == 0);
98
99         float4 out;
100         out.s[0] = a.s[0]*b.s[0];
101         out.s[1] = a.s[1]*b.s[1];
102         out.s[2] = a.s[2]*b.s[2];
103         out.s[3] = a.s[3]*b.s[3];
104         return out;
105 }
106
107 __inline
108 float4 operator*(float a, const float4& b)
109 {
110         return make_float4(a*b.s[0], a*b.s[1], a*b.s[2], a*b.s[3]);
111 }
112
113 __inline
114 float4 operator*(const float4& b, float a)
115 {
116         CHECK_ALIGNMENT(b);
117
118         return make_float4(a*b.s[0], a*b.s[1], a*b.s[2], a*b.s[3]);
119 }
120
121 __inline
122 void operator*=(float4& a, const float4& b)
123 {
124         CHECK_ALIGNMENT(a);
125
126         a.s[0]*=b.s[0];
127         a.s[1]*=b.s[1];
128         a.s[2]*=b.s[2];
129         a.s[3]*=b.s[3];
130 }
131
132 __inline
133 void operator*=(float4& a, float b)
134 {
135         CHECK_ALIGNMENT(a);
136
137         a.s[0]*=b;
138         a.s[1]*=b;
139         a.s[2]*=b;
140         a.s[3]*=b;
141 }
142
143 //
144 __inline
145 float4 operator/(const float4& a, const float4& b)
146 {
147         CHECK_ALIGNMENT(a);
148
149         float4 out;
150         out.s[0] = a.s[0]/b.s[0];
151         out.s[1] = a.s[1]/b.s[1];
152         out.s[2] = a.s[2]/b.s[2];
153         out.s[3] = a.s[3]/b.s[3];
154         return out;
155 }
156
157 __inline
158 float4 operator/(const float4& b, float a)
159 {
160         CHECK_ALIGNMENT(b);
161
162         return make_float4(b.s[0]/a, b.s[1]/a, b.s[2]/a, b.s[3]/a);
163 }
164
165 __inline
166 void operator/=(float4& a, const float4& b)
167 {
168         a.s[0]/=b.s[0];
169         a.s[1]/=b.s[1];
170         a.s[2]/=b.s[2];
171         a.s[3]/=b.s[3];
172 }
173
174 __inline
175 void operator/=(float4& a, float b)
176 {
177         CLASSERT((u32(&a) & 0xf) == 0);
178
179         a.s[0]/=b;
180         a.s[1]/=b;
181         a.s[2]/=b;
182         a.s[3]/=b;
183 }
184 //
185
186 __inline
187 float4 operator+(const float4& a, const float4& b)
188 {
189         CHECK_ALIGNMENT(a);
190
191         float4 out;
192         out.s[0] = a.s[0]+b.s[0];
193         out.s[1] = a.s[1]+b.s[1];
194         out.s[2] = a.s[2]+b.s[2];
195         out.s[3] = a.s[3]+b.s[3];
196         return out;
197 }
198
199 __inline
200 float4 operator+(const float4& a, float b)
201 {
202         CHECK_ALIGNMENT(a);
203
204         float4 out;
205         out.s[0] = a.s[0]+b;
206         out.s[1] = a.s[1]+b;
207         out.s[2] = a.s[2]+b;
208         out.s[3] = a.s[3]+b;
209         return out;
210 }
211
212 __inline
213 float4 operator-(const float4& a, const float4& b)
214 {
215         CHECK_ALIGNMENT(a);
216
217         float4 out;
218         out.s[0] = a.s[0]-b.s[0];
219         out.s[1] = a.s[1]-b.s[1];
220         out.s[2] = a.s[2]-b.s[2];
221         out.s[3] = a.s[3]-b.s[3];
222         return out;
223 }
224
225 __inline
226 float4 operator-(const float4& a, float b)
227 {
228         CHECK_ALIGNMENT(a);
229
230         float4 out;
231         out.s[0] = a.s[0]-b;
232         out.s[1] = a.s[1]-b;
233         out.s[2] = a.s[2]-b;
234         out.s[3] = a.s[3]-b;
235         return out;
236 }
237
238 __inline
239 void operator+=(float4& a, const float4& b)
240 {
241         CHECK_ALIGNMENT(a);
242
243         a.s[0]+=b.s[0];
244         a.s[1]+=b.s[1];
245         a.s[2]+=b.s[2];
246         a.s[3]+=b.s[3];
247 }
248
249 __inline
250 void operator+=(float4& a, float b)
251 {
252         CHECK_ALIGNMENT(a);
253
254         a.s[0]+=b;
255         a.s[1]+=b;
256         a.s[2]+=b;
257         a.s[3]+=b;
258 }
259
260 __inline
261 void operator-=(float4& a, const float4& b)
262 {
263         CHECK_ALIGNMENT(a);
264
265         a.s[0]-=b.s[0];
266         a.s[1]-=b.s[1];
267         a.s[2]-=b.s[2];
268         a.s[3]-=b.s[3];
269 }
270
271 __inline
272 void operator-=(float4& a, float b)
273 {
274         CHECK_ALIGNMENT(a);
275
276         a.s[0]-=b;
277         a.s[1]-=b;
278         a.s[2]-=b;
279         a.s[3]-=b;
280 }
281
282
283
284
285
286 __inline
287 float4 cross3(const float4& a, const float4& b)
288 {
289         return make_float4(a.s[1]*b.s[2]-a.s[2]*b.s[1], 
290                 a.s[2]*b.s[0]-a.s[0]*b.s[2], 
291                 a.s[0]*b.s[1]-a.s[1]*b.s[0], 
292                 0);
293 }
294
295 __inline
296 float dot3F4(const float4& a, const float4& b)
297 {
298         return a.x*b.x+a.y*b.y+a.z*b.z;
299 }
300
301 __inline
302 float length3(const float4& a)
303 {
304         return sqrtf(dot3F4(a,a));
305 }
306
307 __inline
308 float dot4(const float4& a, const float4& b)
309 {
310         return a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w;
311 }
312
313 //      for height
314 __inline
315 float dot3w1(const float4& point, const float4& eqn)
316 {
317         return point.x*eqn.x+point.y*eqn.y+point.z*eqn.z+eqn.w;
318 }
319
320 __inline
321 float4 normalize3(const float4& a)
322 {
323         float length = sqrtf(dot3F4(a, a));
324         return 1.f/length * a;
325 }
326
327 __inline
328 float4 normalize4(const float4& a)
329 {
330         float length = sqrtf(dot4(a, a));
331         return 1.f/length * a;
332 }
333
334 __inline
335 float4 createEquation(const float4& a, const float4& b, const float4& c)
336 {
337         float4 eqn;
338         float4 ab = b-a;
339         float4 ac = c-a;
340         eqn = normalize3( cross3(ab, ac) );
341         eqn.w = -dot3F4(eqn,a);
342         return eqn;
343 }
344
345
346 template<typename T>
347 __inline
348 T max2(const T& a, const T& b)
349 {
350         return (a>b)? a:b;
351 }
352
353 template<typename T>
354 __inline
355 T min2(const T& a, const T& b)
356 {
357         return (a<b)? a:b;
358 }
359
360 template<>
361 __inline
362 float4 max2(const float4& a, const float4& b)
363 {
364         return make_float4( max2(a.x,b.x), max2(a.y,b.y), max2(a.z,b.z), max2(a.w,b.w) );
365 }
366
367 template<>
368 __inline
369 float4 min2(const float4& a, const float4& b)
370 {
371         return make_float4( min2(a.x,b.x), min2(a.y,b.y), min2(a.z,b.z), min2(a.w,b.w) );
372 }
373