Skip to content

Commit dedab6d

Browse files
committed
feat: wip c vectormath implementation
Signed-off-by: Michael Pollind <[email protected]>
1 parent 32f0c1e commit dedab6d

File tree

7 files changed

+480
-0
lines changed

7 files changed

+480
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Vector Math library for 3-D linear algebra (vector, matrix, quaternion)
2+
SIMD support for SSE. Also includes generic multi-platform scalar version.
3+
4+
Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms,
8+
with or without modification, are permitted provided that the
9+
following conditions are met:
10+
* Redistributions of source code must retain the above copyright
11+
notice, this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in the
14+
documentation and/or other materials provided with the distribution.
15+
* Neither the name of the Sony Computer Entertainment Inc nor the names
16+
of its contributors may be used to endorse or promote products derived
17+
from this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
POSSIBILITY OF SUCH DAMAGE.
30+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# Vectormath
3+
4+
Modified version of Sony's open sourced header-only vector and matrix math library.
5+
I've uploaded a copy here so it can be easily submoduled on other projects.
6+
7+
The main differences from the original library released by Sony:
8+
9+
- Removed deprecated SPU/PPU implementations that only targeted PS3/PowerPC.
10+
- Removed the C interfaces - the C++ interface is much nicer to use with operator overloads, return by val, etc.
11+
- Massive namespace cleanup. Removed or replaced most macros with functions and constants.
12+
- Better compliance with strict aliasing rules.
13+
- Added portable macros for alignment annotations to remove some `#ifdefs`.
14+
- Internal SSE helper code moved to a separate header - other files also renamed.
15+
- Removed the Aos/Soa sub-namespaces, since the Soa implementations were only available for SPU.
16+
- The library now includes only the generic scalar version and the x86/64 SSE intrinsics version.
17+
- Added an unpadded `Vector2` and `Point2` to also support basic 2D vector maths. These are always scalar mode (size = 2 floats).
18+
- All you need to do is include the public header file `vectormath.hpp`. It will expose the relevant parts of the library for you and try to select the SSE implementation if supported.
19+
20+
### Original copyright notice:
21+
22+
<pre>
23+
Vector Math library for 3-D linear algebra (vector, matrix, quaternion)
24+
SIMD support for SSE. Also includes generic multi-platform scalar version.
25+
26+
Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
27+
All rights reserved.
28+
29+
Redistribution and use in source and binary forms,
30+
with or without modification, are permitted provided that the
31+
following conditions are met:
32+
* Redistributions of source code must retain the above copyright
33+
notice, this list of conditions and the following disclaimer.
34+
* Redistributions in binary form must reproduce the above copyright
35+
notice, this list of conditions and the following disclaimer in the
36+
documentation and/or other materials provided with the distribution.
37+
* Neither the name of the Sony Computer Entertainment Inc nor the names
38+
of its contributors may be used to endorse or promote products derived
39+
from this software without specific prior written permission.
40+
41+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51+
POSSIBILITY OF SUCH DAMAGE.
52+
</pre>
53+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
static const float VECTORMATH_SLERP_TOL = 0.999f;
3+
static const double VECTORMATH_SLERP_TOL_D = 0.999;
4+
5+
6+
#define q_vec_x(X) _Generic((X), \
7+
struct vec3f: __vec3f_x \
8+
)(X)
9+
10+
#define q_vec_y(X) _Generic((X), \
11+
struct vec3f: __vec3f_y \
12+
)(X)
13+
14+
#define q_vec_z(X) _Generic((X), \
15+
struct vec3f: __vec3f_z \
16+
)(X)
17+
18+
#define q_vec_dot(X,Y) _Generic((X), \
19+
struct vec3f: __vec3f_dot \
20+
)(X)
21+
22+
static inline float __vec3f_x(const struct vec3f* v1) {
23+
return v1->x;
24+
}
25+
26+
static inline float __vec3f_y(const struct vec3f* v1) {
27+
return v1->y;
28+
}
29+
30+
static inline float __vec3f_z(const struct vec3f* v1) {
31+
return v1->z;
32+
}
33+
34+
// vec3f
35+
static inline struct q_vec3 vec3f_create(float x, float y, float z) {
36+
struct q_vec3 res;
37+
res.x = x;
38+
res.y = y;
39+
res.z = z;
40+
return res;
41+
}
42+
43+
static inline struct q_vec4 vec4f_create(float x, float y, float z, float w) {
44+
struct q_vec4 res;
45+
res.x = x;
46+
res.y = y;
47+
res.z = z;
48+
res.w = w;
49+
return res;
50+
}
51+
52+
inline const struct q_vec3 q_vec3_slerp(float t, const struct q_vec3* unitVec0, const struct q_vec3* unitVec1)
53+
{
54+
float recipSinAngle, scale0, scale1, cosAngle, angle;
55+
cosAngle = q_vec_dot(unitVec0, unitVec1);
56+
if (cosAngle < VECTORMATH_SLERP_TOL)
57+
{
58+
angle = std::acosf(cosAngle);
59+
recipSinAngle = (1.0f / std::sinf(angle));
60+
scale0 = (std::sinf(((1.0f - t) * angle)) * recipSinAngle);
61+
scale1 = (std::sinf((t * angle)) * recipSinAngle);
62+
}
63+
else
64+
{
65+
scale0 = (1.0f - t);
66+
scale1 = t;
67+
}
68+
return ((unitVec0 * scale0) + (unitVec1 * scale1));
69+
}
70+
71+
static inline struct q_vec3 vect3f_mul( const struct q_vec3 *v1, const struct q_vec3 *v2 ) {
72+
73+
74+
}
75+
76+
static inline float __vec3f_dot(const struct q_vec3* v1, const struct q_vec3* v2) {
77+
78+
float result;
79+
result = (vec_x(v1) * vec_x(v2));
80+
result = (result + (vec_y(v1) * vec_y(v2)));
81+
result = (result + (vec_z(v1) * vec_z(v2)));
82+
return result;
83+
84+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#include "../../q_arch.h"
2+
3+
4+
#ifdef VECTORMATH_DEBUG
5+
#include <cstdio>
6+
#endif // VECTORMATH_DEBUG
7+
8+
#if defined( _MSC_VER )
9+
// Visual Studio (MS compiler)
10+
#define VECTORMATH_ALIGNED( type ) __declspec( align( 16 ) ) type
11+
#define VECTORMATH_ALIGNED_TYPE_PRE __declspec( align( 16 ) )
12+
#define VECTORMATH_ALIGNED_TYPE_POST /* nothing */
13+
#elif defined( __GNUC__ )
14+
// GCC or Clang
15+
#define VECTORMATH_ALIGNED( type ) type __attribute__( ( aligned( 16 ) ) )
16+
#define VECTORMATH_ALIGNED_TYPE_PRE /* nothing */
17+
#define VECTORMATH_ALIGNED_TYPE_POST __attribute__( ( aligned( 16 ) ) )
18+
#else
19+
// Unknown compiler
20+
#error "Define VECTORMATH_ALIGNED for your compiler or platform!"
21+
#endif
22+
23+
VECTORMATH_ALIGNED_TYPE_PRE struct vec4i_s {
24+
int x,y,z,w;
25+
} VECTORMATH_ALIGNED_TYPE_POST;
26+
27+
28+
VECTORMATH_ALIGNED_TYPE_PRE struct vec2_s {
29+
float x, y, z, w;
30+
} VECTORMATH_ALIGNED_TYPE_POST;
31+
32+
inline const float vec2_element( const struct vec2_s *v1, uint_fast8_t index);
33+
34+
inline const struct vec2_s vec2_create( float x, float y, float z );
35+
inline const struct vec2_s vec2_create_single( float x);
36+
inline const struct vec2_s vec2_create_vec2( const struct vec2_s* v);
37+
38+
39+
inline const struct vec2_s vec2_slerp( float t, const struct vec2_s *unitVec0, const struct vec2_s *unitVec1 );
40+
inline const struct vec2_s vec2_add( const struct vec2_s *v1, const struct vec2_s *v2 );
41+
inline const struct vec2_s vec2_sub( const struct vec2_s *v1, const struct vec2_s *v2 );
42+
inline const struct vec2_s vec2_mul( const struct vec2_s *v1, const struct vec2_s *v2 );
43+
44+
inline const struct vec3_s vec2_add_scalar( const struct vec2_s *v1, float value);
45+
inline const struct vec3_s vec2_sub_scalar( const struct vec2_s *v1, float value );
46+
inline const struct vec3_s vec2_mul_scalar( const struct vec2_s *v1, float value );
47+
48+
inline const struct vec2_s vec2_max_per_element( const struct vec2_s *v1, const struct vec2_s *v2);
49+
inline const struct vec2_s vec2_min_per_element( const struct vec2_s *v1, const struct vec2_s *v2);
50+
inline const struct vec2_s vec2_copy_sign_element( const struct vec2_s *v1, const struct vec2_s *v2);
51+
52+
inline const struct vec2_s vec2_max_element( const struct vec2_s *v1);
53+
inline const struct vec2_s vec2_min_element( const struct vec2_s *v1);
54+
55+
inline const float vec2_normalize( const struct vec2_s *v1);
56+
inline const float vec2_length( const struct vec2_s *v1);
57+
inline const float vec2_length_sqr( const struct vec2_s *v1);
58+
59+
inline float sum(const struct vec2_s* vec);
60+
61+
inline const float vec2_outer( const struct vec2_s *v1, const struct vec2_s *v2);
62+
63+
VECTORMATH_ALIGNED_TYPE_PRE struct vec3_s {
64+
float x, y, z, w;
65+
} VECTORMATH_ALIGNED_TYPE_POST;
66+
67+
inline const float vec3_element( const struct vec3_s *v1, uint_fast8_t index);
68+
69+
inline const struct vec3_s vec3_create( float x, float y, float z );
70+
inline const struct vec3_s vec3_create_single_scalar( float x);
71+
inline const struct vec3_s vec3_create_vec3( const struct vec3_s* v);
72+
73+
inline const struct vec3_s vec3_slerp( float t, const struct vec3_s *unitVec0, const struct vec3_s *unitVec1 );
74+
inline const struct vec3_s vec3_add( const struct vec3_s *v1, const struct vec3_s *v2 );
75+
inline const struct vec3_s vec3_sub( const struct vec3_s *v1, const struct vec3_s *v2 );
76+
inline const struct vec3_s vec3_mul( const struct vec3_s *v1, const struct vec3_s *v2 );
77+
78+
inline const struct vec3_s vec3_add_scalar( const struct vec3_s *v1, float value);
79+
inline const struct vec3_s vec3_sub_scalar( const struct vec3_s *v1, float value );
80+
inline const struct vec3_s vec3_mul_scalar( const struct vec3_s *v1, float value );
81+
82+
inline const struct vec3_s vec3_max_per_element( const struct vec3_s *v1, const struct vec3_s *v2);
83+
inline const struct vec3_s vec3_min_per_element( const struct vec3_s *v1, const struct vec3_s *v2);
84+
inline const struct vec3_s vec3_copy_sign_element( const struct vec3_s *v1, const struct vec3_s *v2);
85+
86+
inline const struct vec3_s vec3_max_element( const struct vec3_s *v1);
87+
inline const struct vec3_s vec3_min_element( const struct vec3_s *v1);
88+
89+
inline const float vec3_normalize( const struct vec3_s *v1);
90+
inline const float vec3_length( const struct vec3_s *v1);
91+
inline const float vec3_length_sqr( const struct vec3_s *v1);
92+
93+
inline float vec3_sum(const struct vec3_s* vec);
94+
95+
inline const float vec3_cross( const struct vec3_s *v1, const struct vec3_s *v2);
96+
inline const float vec3_outer( const struct vec3_s *v1, const struct vec3_s *v2);
97+
98+
VECTORMATH_ALIGNED_TYPE_PRE struct vec4_s {
99+
float x, y, z, w;
100+
} VECTORMATH_ALIGNED_TYPE_POST;
101+
102+
inline const float vec4_element( const struct vec4_s *v1, uint_fast8_t index);
103+
104+
inline const struct vec3_s vec4_as_vec3( const struct vec4_s* v );
105+
inline const struct vec2_s vec4_as_vec2( const struct vec4_s* v );
106+
107+
inline const struct vec4_s vec4_create( float x, float y, float z );
108+
109+
inline const struct vec4_s vec4_slerp( float t, const struct vec4_s *unitVec0, const struct vec4_s *unitVec1 );
110+
inline const struct vec4_s vec4_add( const struct vec4_s *v1, const struct vec4_s *v2 );
111+
inline const struct vec4_s vec4_sub( const struct vec4_s *v1, const struct vec4_s *v2 );
112+
inline const struct vec4_s vec4_mul( const struct vec4_s *v1, const struct vec4_s *v2 );
113+
114+
inline const struct vec4_s vec4_add_scalar( const struct vec4_s *v1, float value);
115+
inline const struct vec4_s vec4_sub_scalar( const struct vec4_s *v1, float value );
116+
inline const struct vec4_s vec4_mul_scalar( const struct vec4_s *v1, float value );
117+
118+
inline const struct vec4_s vec4_max_per_element( const struct vec4_s *v1, const struct vec4_s *v2);
119+
inline const struct vec4_s vec4_min_per_element( const struct vec4_s *v1, const struct vec4_s *v2);
120+
inline const struct vec4_s vec4_copy_sign_element( const struct vec4_s *v1, const struct vec4_s *v2);
121+
122+
inline const struct vec4_s vec4_max_element( const struct vec4_s *v1);
123+
inline const struct vec4_s vec4_min_element( const struct vec4_s *v1);
124+
125+
inline const float vec4_normalize( const struct vec4_s *v1);
126+
inline const float vec4_length( const struct vec4_s *v1);
127+
inline const float vec4_length_sqr( const struct vec4_s *v1);
128+
129+
inline float vec4_sum(const struct vec4_s* vec);
130+
131+
inline const float vec4_cross( const struct vec4_s *v1, const struct vec4_s *v2);
132+
inline const float vec4_outer( const struct vec4_s *v1, const struct vec4_s *v2);
133+
134+
VECTORMATH_ALIGNED_TYPE_PRE struct mat4_s {
135+
struct vec4_s mCol0;
136+
struct vec4_s mCol1;
137+
struct vec4_s mCol2;
138+
struct vec4_s mCol3;
139+
} VECTORMATH_ALIGNED_TYPE_POST;
140+
141+
inline const float mat4_element( const struct mat4_s *v1, uint_fast8_t col, uint_fast8_t row );
142+
143+
inline const struct vec4_s mat4_create( const struct vec4_s *col0, const struct vec4_s *col1, const struct vec4_s *col2, const struct vec4_s *col3 );
144+
145+
VECTORMATH_ALIGNED_TYPE_PRE struct mat3_s {
146+
struct vec3_s mCol0;
147+
struct vec3_s mCol1;
148+
struct vec3_s mCol2;
149+
struct vec3_s mCol3;
150+
} VECTORMATH_ALIGNED_TYPE_POST;
151+
152+
inline const float mat3_element( const struct mat3_s *v1, uint_fast8_t col, uint_fast8_t row );
153+
154+
inline const struct vec4_s mat3_create( const struct vec3_s *col0, const struct vec3_s *col1, const struct vec3_s *col2, const struct vec3_s *col3 );
155+
156+
//inline const struct vec3_s vec3_slerp( float t, const struct vec3_s *unitVec0, const struct vec3_s *unitVec1 ) {
157+
// v
158+
//}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
// ================================================================================================
3+
// -*- C++ -*-
4+
// File: vectormath/vectormath.hpp
5+
// Author: Guilherme R. Lampert
6+
// Created on: 30/12/16
7+
// Brief: This header exposes the Sony Vectormath library types and functions into the global scope.
8+
// ================================================================================================
9+
10+
#ifndef VECTORMATH_HPP
11+
#define VECTORMATH_HPP
12+
13+
#include "../q_arch.h"
14+
#include "vectormath_settings.h"
15+
16+
//#elif (VECTORMATH_CPU_HAS_SSE1_OR_BETTER && !VECTORMATH_FORCE_SCALAR_MODE) // SSE
17+
// #include "sse/vectormath.hpp"
18+
// using namespace Vectormath::SSE;
19+
//#elif (VECTORMATH_CPU_HAS_NEON && !VECTORMATH_FORCE_SCALAR_MODE) // NEON
20+
// #include "neon/vectormath.hpp"
21+
// using namespace Vectormath::Neon;
22+
//#else // !SSE
23+
#include "scalar/vectormath.h"
24+
//#endif // Vectormath mode selection
25+
26+
////========================================= #TheForgeMathExtensionsBegin ================================================
27+
////========================================= #TheForgeAnimationMathExtensionsBegin =======================================
28+
//#include "soa/soa.hpp"
29+
//using namespace Vectormath::Soa;
30+
////========================================= #TheForgeAnimationMathExtensionsEnd =======================================
31+
////========================================= #TheForgeMathExtensionsEnd ================================================
32+
//
33+
//#include "vec2d.hpp" // - Extended 2D vector and point classes; not aligned and always in scalar floats mode.
34+
//#include "common.hpp" // - Miscellaneous helper functions.
35+
36+
#include "vectormath_common.h"
37+
38+
#endif // VECTORMATH_HPP

0 commit comments

Comments
 (0)