HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VM_BasicFunc.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: VM_BasicFunc.h ( VM Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __VM_BasicFunc__
12 #define __VM_BasicFunc__
13 
14 #include "VM_API.h"
15 #include <SYS/SYS_Types.h>
16 
17 // Uncomment this define to "test" with the basic instructions. These are
18 // brain-dead slow, so they shouldn't be used in actual code.
19 //#define CPU_HAS_SIMD_INSTR 1
20 
21 class v4si;
22 
23 class v4sf {
24 public:
25  v4sf() {}
26  v4sf(float a) { f[0] = f[1] = f[2] = f[3] = a; }
27  v4sf(float a, float b, float c, float d)
28  {
29  f[0] = a;
30  f[1] = b;
31  f[2] = c;
32  f[3] = d;
33  }
34 
35  operator v4si() const;
36  fpreal32 f[4];
37 };
38 
39 class v4si {
40 public:
41  v4si() {}
42  v4si(int32 a) { i[0] = i[1] = i[2] = i[3] = a; }
44  {
45  i[0] = a;
46  i[1] = b;
47  i[2] = c;
48  i[3] = d;
49  }
50 
51  operator v4sf() const;
52  int32 i[4];
53 };
54 
55 inline
56 v4sf::operator v4si() const
57 {
58  return *(v4si *)this;
59 }
60 
61 inline
62 v4si::operator v4sf() const
63 {
64  return *(v4sf *)this;
65 }
66 
67 #define V4SF(A) (v4sf)A
68 #define V4SI(A) (v4si)A
69 
70 #define vm_BASIC_IFF(OP) \
71  v4si r; \
72  r.i[0] = a.f[0] OP b.f[0]; \
73  r.i[1] = a.f[1] OP b.f[1]; \
74  r.i[2] = a.f[2] OP b.f[2]; \
75  r.i[3] = a.f[3] OP b.f[3]; \
76  return r;
77 
78 #define vm_BASIC_CF(OP) \
79  v4si r; \
80  r.i[0] = a.f[0] OP b.f[0] ? 0xFFFFFFFF : 0; \
81  r.i[1] = a.f[1] OP b.f[1] ? 0xFFFFFFFF : 0; \
82  r.i[2] = a.f[2] OP b.f[2] ? 0xFFFFFFFF : 0; \
83  r.i[3] = a.f[3] OP b.f[3] ? 0xFFFFFFFF : 0; \
84  return r;
85 
86 #define vm_BASIC_CI(OP) \
87  v4si r; \
88  r.i[0] = a.i[0] OP b.i[0] ? 0xFFFFFFFF : 0; \
89  r.i[1] = a.i[1] OP b.i[1] ? 0xFFFFFFFF : 0; \
90  r.i[2] = a.i[2] OP b.i[2] ? 0xFFFFFFFF : 0; \
91  r.i[3] = a.i[3] OP b.i[3] ? 0xFFFFFFFF : 0; \
92  return r;
93 
94 #define vm_BASIC_III(OP) \
95  v4si r; \
96  r.i[0] = a.i[0] OP b.i[0]; \
97  r.i[1] = a.i[1] OP b.i[1]; \
98  r.i[2] = a.i[2] OP b.i[2]; \
99  r.i[3] = a.i[3] OP b.i[3]; \
100  return r;
101 
102 #define vm_BASIC_FFF(OP) \
103  v4sf r; \
104  r.f[0] = a.f[0] OP b.f[0]; \
105  r.f[1] = a.f[1] OP b.f[1]; \
106  r.f[2] = a.f[2] OP b.f[2]; \
107  r.f[3] = a.f[3] OP b.f[3]; \
108  return r;
109 
110 #define vm_BASIC_FFFF(OP1, OP2) \
111  v4sf r; \
112  r.f[0] = a.f[0] OP1 b.f[0] OP2 c.f[0]; \
113  r.f[1] = a.f[1] OP1 b.f[1] OP2 c.f[1]; \
114  r.f[2] = a.f[2] OP1 b.f[2] OP2 c.f[2]; \
115  r.f[3] = a.f[3] OP1 b.f[3] OP2 c.f[3]; \
116  return r;
117 
118 #define vm_BASIC_UFuncF(FUNC) \
119  v4sf r; \
120  r.f[0] = FUNC(a.f[0]); \
121  r.f[1] = FUNC(a.f[1]); \
122  r.f[2] = FUNC(a.f[2]); \
123  r.f[3] = FUNC(a.f[3]); \
124  return r;
125 
126 #define vm_BASIC_UFuncFF(FUNC) \
127  v4sf r; \
128  r.f[0] = FUNC(a.f[0], b.f[0]); \
129  r.f[1] = FUNC(a.f[1], b.f[1]); \
130  r.f[2] = FUNC(a.f[2], b.f[2]); \
131  r.f[3] = FUNC(a.f[3], b.f[3]); \
132  return r;
133 
134 static inline v4si vm_clt(const v4sf &a, const v4sf &b) { vm_BASIC_CF(<) }
135 static inline v4si vm_cle(const v4sf &a, const v4sf &b) { vm_BASIC_CF(<=) }
136 static inline v4si vm_cgt(const v4sf &a, const v4sf &b) { vm_BASIC_CF(>) }
137 static inline v4si vm_cge(const v4sf &a, const v4sf &b) { vm_BASIC_CF(>=) }
138 static inline v4si vm_ceq(const v4sf &a, const v4sf &b) { vm_BASIC_CF(==) }
139 static inline v4si vm_cne(const v4sf &a, const v4sf &b) { vm_BASIC_CF(!=) }
140 
141 static inline v4si vm_clt(const v4si &a, const v4si &b) { vm_BASIC_CI(<) }
142 static inline v4si vm_cle(const v4si &a, const v4si &b) { vm_BASIC_CI(<=) }
143 static inline v4si vm_cgt(const v4si &a, const v4si &b) { vm_BASIC_CI(>) }
144 static inline v4si vm_cge(const v4si &a, const v4si &b) { vm_BASIC_CI(>=) }
145 static inline v4si vm_ceq(const v4si &a, const v4si &b) { vm_BASIC_CI(==) }
146 static inline v4si vm_cne(const v4si &a, const v4si &b) { vm_BASIC_CI(!=) }
147 
148 static inline v4si vm_add(const v4si &a, const v4si &b) { vm_BASIC_III(+) }
149 static inline v4si vm_sub(const v4si &a, const v4si &b) { vm_BASIC_III(-) }
150 
151 static inline v4sf vm_add(const v4sf &a, const v4sf &b) { vm_BASIC_FFF(+) }
152 static inline v4sf vm_sub(const v4sf &a, const v4sf &b) { vm_BASIC_FFF(-) }
153 static inline v4sf vm_mul(const v4sf &a, const v4sf &b) { vm_BASIC_FFF(*) }
154 static inline v4sf vm_div(const v4sf &a, const v4sf &b) { vm_BASIC_FFF(/) }
155 
156 static inline v4si vm_and(const v4si &a, const v4si &b) { vm_BASIC_III(&) }
157 static inline v4si vm_or(const v4si &a, const v4si &b) { vm_BASIC_III(|) }
158 static inline v4si vm_xor(const v4si &a, const v4si &b) { vm_BASIC_III(^) }
159 
160 static inline v4si vm_andnot(const v4si &a, const v4si &b)
161 {
162  v4si r;
163  r.i[0] = ~a.i[0] & b.i[0];
164  r.i[1] = ~a.i[1] & b.i[1];
165  r.i[2] = ~a.i[2] & b.i[2];
166  r.i[3] = ~a.i[3] & b.i[3];
167  return r;
168 }
169 
170 static inline v4sf
171 vm_min(const v4sf &a, const v4sf &b) { vm_BASIC_UFuncFF(SYSmin) }
172 static inline v4sf
173 vm_max(const v4sf &a, const v4sf &b) { vm_BASIC_UFuncFF(SYSmax) }
174 
175 static inline v4sf
176 vm_madd(const v4sf &a, const v4sf &b, const v4sf &c) { vm_BASIC_FFFF(*, +) }
177 
178 static inline v4sf vm_sqrt(const v4sf &a) { vm_BASIC_UFuncF(SYSsqrt) }
179 static inline v4sf vm_isqrt(const v4sf &a) { vm_BASIC_UFuncF(1/SYSsqrt) }
180 static inline v4sf vm_negate(const v4sf &a) { vm_BASIC_UFuncF(-) }
181 static inline v4sf vm_reciprocal(const v4sf &a) { vm_BASIC_UFuncF(1/) }
182 static inline v4sf vm_abs(const v4sf &a) { vm_BASIC_UFuncF(SYSabs) }
183 
184 static inline v4si vm_floor(const v4sf &a)
185 {
186  v4si r;
187  r.i[0] = (int)SYSfastFloor(a.f[0]);
188  r.i[1] = (int)SYSfastFloor(a.f[1]);
189  r.i[2] = (int)SYSfastFloor(a.f[2]);
190  r.i[3] = (int)SYSfastFloor(a.f[3]);
191  return r;
192 }
193 
194 static inline v4si vm_intcast(const v4sf &a)
195 {
196  v4si r;
197  r.i[0] = (int)a.f[0];
198  r.i[1] = (int)a.f[1];
199  r.i[2] = (int)a.f[2];
200  r.i[3] = (int)a.f[3];
201  return r;
202 }
203 
204 static inline v4sf vm_floatcast(const v4si &a)
205 {
206  v4sf r;
207  r.f[0] = (float)a.i[0];
208  r.f[1] = (float)a.i[1];
209  r.f[2] = (float)a.i[2];
210  r.f[3] = (float)a.i[3];
211  return r;
212 }
213 
214 static inline v4si
215 vm_splats(uint32 a)
216 {
217  return v4si(a);
218 }
219 
220 static inline v4si
221 vm_splats(int32 a)
222 {
223  return v4si(a);
224 }
225 
226 static inline v4sf
227 vm_splats(float a)
228 {
229  return v4sf(a);
230 }
231 
232 static inline v4si
233 vm_splats(uint32 a, uint32 b, uint32 c, uint32 d)
234 {
235  return v4si(a, b, c, d);
236 }
237 
238 static inline v4si
239 vm_splats(int32 a, int32 b, int32 c, int32 d)
240 {
241  return v4si(a, b, c, d);
242 }
243 
244 static inline v4sf
245 vm_splats(float a, float b, float c, float d)
246 {
247  return v4sf(a, b, c, d);
248 }
249 
250 static inline bool
251 vm_allbits(const v4si &a)
252 {
253  return (a.i[0] & a.i[1] & a.i[2] & a.i[3]) == 0xFFFFFFFF;
254 }
255 
256 template <int A, int B, int C, int D>
257 static inline v4sf
258 vm_shuffle(const v4sf &v)
259 {
260  v4sf vec;
261 
262  vec.f[0] = v.f[A];
263  vec.f[1] = v.f[B];
264  vec.f[2] = v.f[C];
265  vec.f[3] = v.f[D];
266 
267  return vec;
268 }
269 
270 static inline v4si
271 vm_load(const int32 v[4])
272 {
273  return v4si(v[0], v[1], v[2], v[3]);
274 }
275 
276 static inline v4sf
277 vm_load(const float v[4])
278 {
279  return v4sf(v[0], v[1], v[2], v[3]);
280 }
281 
282 static inline void
283 vm_store(float dst[4], v4sf value)
284 {
285  dst[0] = value.f[0];
286  dst[1] = value.f[1];
287  dst[2] = value.f[2];
288  dst[3] = value.f[3];
289 }
290 
291 
292 static inline v4si
293 vm_insert(const v4si v, int32 a, int n)
294 {
295  v4si vec;
296 
297  vec = v;
298  vec.i[n] = a;
299  return vec;
300 }
301 
302 static inline v4sf
303 vm_insert(const v4sf v, float a, int n)
304 {
305  v4sf vec;
306 
307  vec = v;
308  vec.f[n] = a;
309  return vec;
310 }
311 
312 static inline int
313 vm_extract(const v4si v, int n)
314 {
315  return v.i[n];
316 }
317 
318 static inline float
319 vm_extract(const v4sf v, int n)
320 {
321  return v.f[n];
322 }
323 
324 #define VM_EXTRACT vm_extract
325 #define VM_INSERT vm_insert
326 #define VM_SPLATS vm_splats
327 #define VM_LOAD vm_load
328 #define VM_STORE vm_store
329 
330 #define VM_CMPLT vm_clt
331 #define VM_CMPLE vm_cle
332 #define VM_CMPGT vm_cgt
333 #define VM_CMPGE vm_cge
334 #define VM_CMPEQ vm_ceq
335 #define VM_CMPNE vm_cne
336 
337 #define VM_ICMPLT vm_clt
338 #define VM_ICMPGT vm_cgt
339 #define VM_ICMPEQ vm_ceq
340 
341 #define VM_IADD vm_add
342 #define VM_ISUB vm_sub
343 
344 #define VM_ADD vm_add
345 #define VM_SUB vm_sub
346 #define VM_MUL vm_mul
347 #define VM_DIV vm_div
348 #define VM_FDIV vm_div
349 #define VM_NEG vm_negate
350 #define VM_SQRT vm_sqrt
351 #define VM_FSQRT vm_sqrt
352 #define VM_ISQRT vm_isqrt
353 #define VM_ABS vm_abs
354 
355 #define VM_MADD vm_madd
356 #define VM_INVERT vm_reciprocal
357 
358 #define VM_MIN vm_min
359 #define VM_MAX vm_max
360 
361 #define VM_AND vm_and
362 #define VM_ANDNOT vm_andnot
363 #define VM_OR vm_or
364 #define VM_XOR vm_xor
365 
366 #define VM_ALLBITS vm_allbits
367 
368 #define VM_SHUFFLE vm_shuffle
369 
370 #define VM_P_FLOOR()
371 #define VM_FLOOR vm_floor
372 #define VM_E_FLOOR()
373 
374 #define VM_INT vm_intcast
375 
376 #define VM_IFLOAT vm_floatcast
377 
378 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1365
const GLdouble * v
Definition: glcorearb.h:836
vector int v4si
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
#define SYSabs(a)
Definition: SYS_Math.h:1367
#define vm_BASIC_III(OP)
Definition: VM_BasicFunc.h:94
v4sf(float a)
Definition: VM_BasicFunc.h:26
fpreal32 f[4]
Definition: VM_BasicFunc.h:36
GLdouble n
Definition: glcorearb.h:2007
GLfloat f
Definition: glcorearb.h:1925
v4sf()
Definition: VM_BasicFunc.h:25
v4sf(float a, float b, float c, float d)
Definition: VM_BasicFunc.h:27
#define vm_BASIC_CF(OP)
Definition: VM_BasicFunc.h:78
#define vm_BASIC_UFuncF(FUNC)
Definition: VM_BasicFunc.h:118
int int32
Definition: SYS_Types.h:34
#define vm_BASIC_FFF(OP)
Definition: VM_BasicFunc.h:102
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
#define vm_BASIC_FFFF(OP1, OP2)
Definition: VM_BasicFunc.h:110
int32 i[4]
Definition: VM_BasicFunc.h:52
GLenum GLenum dst
Definition: glcorearb.h:1792
GLsizei const GLfloat * value
Definition: glcorearb.h:823
v4si(int32 a)
Definition: VM_BasicFunc.h:42
typedef int
Definition: png.h:1175
v4si()
Definition: VM_BasicFunc.h:41
v4si(int32 a, int32 b, int32 c, int32 d)
Definition: VM_BasicFunc.h:43
#define vm_BASIC_UFuncFF(FUNC)
Definition: VM_BasicFunc.h:126
GLboolean r
Definition: glcorearb.h:1221
#define SYSmin(a, b)
Definition: SYS_Math.h:1366
vector float v4sf
float fpreal32
Definition: SYS_Types.h:190
#define vm_BASIC_CI(OP)
Definition: VM_BasicFunc.h:86
unsigned int uint32
Definition: SYS_Types.h:35