mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-09-05 17:58:14 +00:00
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
This commit is contained in:
@@ -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
|
||||
|
@@ -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.
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user