Compare commits

...

8 Commits

Author SHA1 Message Date
strangezak
6c7bffdccb Quaternion add and sub ops are not SSEd 2018-06-15 17:34:39 -07:00
strangezak
a22d411267 The start of making Quaternions first class citizens 2018-06-15 17:28:04 -07:00
strangezak
2a050bc124 Merge branch 'master' of https://github.com/HandmadeMath/Handmade-Math into array-operators 2018-06-15 17:18:11 -07:00
Ben Visness
e095aefaf7 Bump file version 2018-06-10 15:32:12 -04:00
Ben Visness
4e2f47db55 Add array subscript operators for all types (#88)
* Add array subscript operators for all types

* Taking the parameter for the operator[] as a reference. This should allow it to be inlined

* I guess you can't do that.

* Update version and readme
2018-06-10 15:26:48 -04:00
strangezak
0e2bc9be4f I guess you can't do that. 2018-06-09 10:35:38 -07:00
strangezak
ec5bc9c0d3 Taking the parameter for the operator[] as a reference. This should allow it to be inlined 2018-06-09 10:27:40 -07:00
Ben Visness
baf1a70fc6 Add array subscript operators for all types 2018-06-09 11:50:41 -04:00
3 changed files with 112 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
HandmadeMath.h v1.5.1
HandmadeMath.h v1.6.0
This is a single header file with a bunch of useful functions for game and
graphics math operations.
@@ -168,8 +168,12 @@
longer has any effect.
1.5.1
(*) Fixed a bug with uninitialized elements in HMM_LookAt.
1.6.0
(*) Added array subscript operators for vector and matrix types in
C++. This is provided as a convenience, but be aware that it may
incur an extra function call in unoptimized builds.
LICENSE
This software is in the public domain. Where that dedication is not
@@ -314,6 +318,13 @@ typedef union hmm_vec2
};
float Elements[2];
#ifdef __cplusplus
inline float &operator[](int Index)
{
return Elements[Index];
}
#endif
} hmm_vec2;
typedef union hmm_vec3
@@ -358,6 +369,13 @@ typedef union hmm_vec3
};
float Elements[3];
#ifdef __cplusplus
inline float &operator[](int Index)
{
return Elements[Index];
}
#endif
} hmm_vec3;
typedef union hmm_vec4
@@ -415,6 +433,13 @@ typedef union hmm_vec4
#ifdef HANDMADE_MATH__USE_SSE
__m128 InternalElementsSSE;
#endif
#ifdef __cplusplus
inline float &operator[](int Index)
{
return Elements[Index];
}
#endif
} hmm_vec4;
typedef union hmm_mat4
@@ -424,6 +449,21 @@ typedef union hmm_mat4
#ifdef HANDMADE_MATH__USE_SSE
__m128 Rows[4];
#endif
#ifdef __cplusplus
inline hmm_vec4 operator[](const int Index)
{
float* col = Elements[Index];
hmm_vec4 result;
result.Elements[0] = col[0];
result.Elements[1] = col[1];
result.Elements[2] = col[2];
result.Elements[3] = col[3];
return result;
}
#endif
} hmm_mat4;
typedef union hmm_quaternion
@@ -441,8 +481,17 @@ typedef union hmm_quaternion
float W;
};
struct
{
hmm_vec4 XYZW;
};
float Elements[4];
#if HANDMADE_MATH__USE_SSE
_m128 InternalElementsSSE;
#endif
} hmm_quaternion;
typedef int32_t hmm_bool;
@@ -1277,11 +1326,14 @@ HMM_INLINE hmm_quaternion HMM_Quaternion(float X, float Y, float Z, float W)
{
hmm_quaternion Result;
#if HANDMADE_MATH__USE_SSE
Result.InternalElementsSSE = _mm_setr_ps(X, Y, Z, W);
#else
Result.X = X;
Result.Y = Y;
Result.Z = Z;
Result.W = W;
#endif
return (Result);
}
@@ -1289,11 +1341,15 @@ HMM_INLINE hmm_quaternion HMM_QuaternionV4(hmm_vec4 Vector)
{
hmm_quaternion Result;
#if HANDMADE_MATH__USE_SSE
Result.InternalElementsSSE = _mm_setr_ps(Vector.X, Vector.Y, Vector.Z, Vector.W);
#else
Result.X = Vector.X;
Result.Y = Vector.Y;
Result.Z = Vector.Z;
Result.W = Vector.W;
Result.W = Vector.W;
#endif
return (Result);
}
@@ -1301,11 +1357,14 @@ HMM_INLINE hmm_quaternion HMM_AddQuaternion(hmm_quaternion Left, hmm_quaternion
{
hmm_quaternion Result;
#if HANDMADE_MATH__USE_SSE
Result.InternalElementsSSE = _mm_add_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
#else
Result.X = Left.X + Right.X;
Result.Y = Left.Y + Right.Y;
Result.Z = Left.Z + Right.Z;
Result.W = Left.W + Right.W;
#endif
return (Result);
}
@@ -1313,11 +1372,15 @@ HMM_INLINE hmm_quaternion HMM_SubtractQuaternion(hmm_quaternion Left, hmm_quater
{
hmm_quaternion Result;
#if HANDMADE_MATH__USE_SSE
Result.InternalElementsSSE = _mm_sub_ps(Left.InternalElementsSSE, Right.InternalElementsSSE);
#else
Result.X = Left.X - Right.X;
Result.Y = Left.Y - Right.Y;
Result.Z = Left.Z - Right.Z;
Result.W = Left.W - Right.W;
#endif
return (Result);
}

View File

@@ -8,8 +8,9 @@ To get started, go download [the latest release](https://github.com/HandmadeMath
-----
Version | Changes |
Version | Changes |
----------------|----------------|
**1.6.0** | Added array subscript operators for vector and matrix types in C++. This is provided as a convenience, but be aware that it may incur an extra function call in unoptimized builds.
**1.5.1** | Fixed a bug with uninitialized elements in HMM_LookAt.
**1.5.0** | Changed internal structure for better performance and inlining. As a result, `HANDMADE_MATH_NO_INLINE` has been removed and no longer has any effect.
**1.4.0** | Fixed bug when using C mode. SSE'd all vec4 operations. Removed zeroing for better performance.

View File

@@ -18,6 +18,10 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v2.Height, 2.0f);
EXPECT_FLOAT_EQ(v2.Elements[0], 1.0f);
EXPECT_FLOAT_EQ(v2.Elements[1], 2.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v2[0], 1.0f);
EXPECT_FLOAT_EQ(v2[1], 2.0f);
#endif
EXPECT_FLOAT_EQ(v2i.X, 1.0f);
EXPECT_FLOAT_EQ(v2i.Y, 2.0f);
@@ -29,6 +33,10 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v2i.Height, 2.0f);
EXPECT_FLOAT_EQ(v2i.Elements[0], 1.0f);
EXPECT_FLOAT_EQ(v2i.Elements[1], 2.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v2i[0], 1.0f);
EXPECT_FLOAT_EQ(v2i[1], 2.0f);
#endif
//
// Test vec3
@@ -56,6 +64,11 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v3.UV.Elements[1], 2.0f);
EXPECT_FLOAT_EQ(v3.VW.Elements[0], 2.0f);
EXPECT_FLOAT_EQ(v3.VW.Elements[1], 3.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v3[0], 1.0f);
EXPECT_FLOAT_EQ(v3[1], 2.0f);
EXPECT_FLOAT_EQ(v3[2], 3.0f);
#endif
EXPECT_FLOAT_EQ(v3i.X, 1.0f);
EXPECT_FLOAT_EQ(v3i.Y, 2.0f);
@@ -77,6 +90,11 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v3i.UV.Elements[1], 2.0f);
EXPECT_FLOAT_EQ(v3i.VW.Elements[0], 2.0f);
EXPECT_FLOAT_EQ(v3i.VW.Elements[1], 3.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v3i[0], 1.0f);
EXPECT_FLOAT_EQ(v3i[1], 2.0f);
EXPECT_FLOAT_EQ(v3i[2], 3.0f);
#endif
//
// Test vec4
@@ -107,6 +125,12 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v4.RGB.Elements[0], 1.0f);
EXPECT_FLOAT_EQ(v4.RGB.Elements[1], 2.0f);
EXPECT_FLOAT_EQ(v4.RGB.Elements[2], 3.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v4[0], 1.0f);
EXPECT_FLOAT_EQ(v4[1], 2.0f);
EXPECT_FLOAT_EQ(v4[2], 3.0f);
EXPECT_FLOAT_EQ(v4[3], 4.0f);
#endif
EXPECT_FLOAT_EQ(v4i.X, 1.0f);
EXPECT_FLOAT_EQ(v4i.Y, 2.0f);
@@ -130,6 +154,12 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v4i.RGB.Elements[0], 1.0f);
EXPECT_FLOAT_EQ(v4i.RGB.Elements[1], 2.0f);
EXPECT_FLOAT_EQ(v4i.RGB.Elements[2], 3.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v4i[0], 1.0f);
EXPECT_FLOAT_EQ(v4i[1], 2.0f);
EXPECT_FLOAT_EQ(v4i[2], 3.0f);
EXPECT_FLOAT_EQ(v4i[3], 4.0f);
#endif
EXPECT_FLOAT_EQ(v4v.X, 1.0f);
EXPECT_FLOAT_EQ(v4v.Y, 2.0f);
@@ -153,6 +183,12 @@ TEST(Initialization, Vectors)
EXPECT_FLOAT_EQ(v4v.RGB.Elements[0], 1.0f);
EXPECT_FLOAT_EQ(v4v.RGB.Elements[1], 2.0f);
EXPECT_FLOAT_EQ(v4v.RGB.Elements[2], 3.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(v4v[0], 1.0f);
EXPECT_FLOAT_EQ(v4v[1], 2.0f);
EXPECT_FLOAT_EQ(v4v[2], 3.0f);
EXPECT_FLOAT_EQ(v4v[3], 4.0f);
#endif
}
TEST(Initialization, MatrixEmpty)
@@ -163,6 +199,9 @@ TEST(Initialization, MatrixEmpty)
for (int Row = 0; Row < 4; ++Row)
{
EXPECT_FLOAT_EQ(m4.Elements[Column][Row], 0.0f);
#ifdef __cplusplus
EXPECT_FLOAT_EQ(m4[Column][Row], 0.0f);
#endif
}
}
}