1 #ifndef vector_header
2 #define vector_header
3
4 #include <math.h>
5 #include <stdio.h>
6
7 #define VEC_FUNC __forceinline
8
9 #include <xmmintrin.h>
10 #include <mmintrin.h>
11
12 //--------------------------------------
13 // Struct: Vector3
14 // Desc: represents a 3D vector
15 //--------------------------------------
16 struct vector3
17 {
18 __declspec( align(16) ) float data[4];
19 };
20
21 typedef struct vector3 Vector3;
22
23
24 //--------------------------------------
25 // Macro: x_com, y_com, z_com
26 // Desc: accessors for different
27 // elements i.e. vec.x_com = vec.data[0]
28 //--------------------------------------
29 #define x_com data[0]
30 #define y_com data[1]
31 #define z_com data[2]
32
33
34 //--------------------------------------
35 // Macro: VECTOR_INIT
36 // Desc: used as initializer list to
37 // initialize vectors i.e.
38 // Vector3 v = VECTOR_INIT( 1, 1, 1 );
39 //--------------------------------------
40 #define VECTOR_INIT( x, y, z ) {{ x, y, z, 0}}
41 //#define VECTOR_INIT( x, y, z ) {{ x, y, z }}
42
43
44
45
46
47 //--------------------------------------
48 // Macro: VECTOR_SET
49 // Desc: sets the values of a vector
50 //--------------------------------------
51 #define VECTOR_SET( vector, x, y, z ) \
52 vector.x_com = x; \
53 vector.y_com = y; \
54 vector.z_com = z; \
55
56 //--------------------------------------
57 // Macro: VECTOR_COPY
58 // Desc: copies the values of one
59 // vector to another
60 //--------------------------------------
61 #define VECTOR_COPY( source, destination ) \
62 memcpy( (destination).data, (source).data, sizeof( float ) * 4 )\
63
64 //--------------------------------------
65 // Function: vector3_add
66 // Desc: adds vec01 and vec02 and stores
67 // results in out
68 //--------------------------------------
69 void vector3_add( Vector3* vec01, Vector3* vec02, Vector3* out );
70
71 //--------------------------------------
72 // Function: vector3_add
73 // Desc: subtracts vec01 and vec02
74 // and stores results in out
75 //--------------------------------------
76 void vector3_sub( Vector3* vec01, Vector3* vec02, Vector3* out );
77
78 //--------------------------------------
79 // Function: vector3_add
80 // Desc: performs the cross product of
81 // vec01 and vec02 and stores the
82 // results in out
83 //--------------------------------------
84 void vector3_crs( Vector3* vec01, Vector3* vec02, Vector3* out );
85
86 //--------------------------------------
87 // Macro: SSE2_DOT
88 // Desc: macro for the dot product of
89 // two vectors.
90 //--------------------------------------
91 #define SSE2_DOT( vec01, vec02, r_out ) { \
92 __m128 r_vec01 = _mm_load_ps( vec01->data ); \
93 __m128 r_vec02 = _mm_load_ps( vec02->data ); \
94 __m128 r_mul = _mm_mul_ps( r_vec01, r_vec02 ); \
95 __m128 r_mul_s = _mm_shuffle_ps( r_mul, r_mul, _MM_SHUFFLE( 2, 2, 2, 2 ) ); \
96 r_mul_s = _mm_add_ps( r_mul, r_mul_s ); \
97 r_mul = _mm_shuffle_ps( r_mul, r_mul, _MM_SHUFFLE( 1, 1, 1, 1 ) ); \
98 r_out = _mm_add_ss( r_mul, r_mul_s ); }
99
100
101 //--------------------------------------
102 // Function: vector3_dot
103 // Desc: performs the dot product of
104 // vec01 and vec02 and stores the
105 // results in out
106 //--------------------------------------
107 float vector3_dot( Vector3* vec01, Vector3* vec02 );
108
109 //--------------------------------------
110 // Function: vector3_mul_vfv
111 // Desc: multiplies a vector by a
112 // constant and stores the result in out
113 //--------------------------------------
114 void vector3_mul_vfv( Vector3* vec, float constant, Vector3* out );
115
116 //--------------------------------------
117 // Function: vector3_mul
118 // Desc: multiplies a vector by a
119 // constant.
120 //--------------------------------------
121 void vector3_mul( Vector3* vec_inout, float constant );
122
123
124 //--------------------------------------
125 // Function: vector3_length
126 // Desc: calculates the length of
127 // vector
128 //--------------------------------------
129 float vector3_length( Vector3* vec );
130
131 //--------------------------------------
132 // Function: vector3_length_sqr
133 // Desc: calculates the length of
134 // vector squared
135 //--------------------------------------
136 float vector3_length_sqr( Vector3* vec );
137
138 //--------------------------------------
139 // Function: vector3_normalize_vv
140 // Desc: normalizes a vector and stores
141 // the results in out.
142 //--------------------------------------
143 void vector3_normalize_vv( Vector3* vec, Vector3* out );
144
145 //--------------------------------------
146 // Function: vector3_normalize
147 // Desc: normalizes a vector.
148 //--------------------------------------
149 void vector3_normalize( Vector3* vec_inout );
150
151 //--------------------------------------
152 // Function: vector3_print
153 // Desc: converts a vector a string
154 // and outputs it to the file
155 //--------------------------------------
156 void vector3_print( FILE* file, Vector3* vec, const char* name );
157
158 //--------------------------------------
159 // Function: vector3_test
160 // Desc: a vector test script
161 //--------------------------------------
162 void vector3_test();
163
164 #endif
syntax highlighted by Code2HTML,
v. 0.9.1