diff --git a/include/cglm/cglms.h b/include/cglm/cglms.h new file mode 100644 index 0000000..4dd471f --- /dev/null +++ b/include/cglm/cglms.h @@ -0,0 +1,20 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_structs_h +#define cglm_structs_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "cglm.h" +#include "structs/vec4.h" + +#ifdef __cplusplus +} +#endif +#endif /* cglm_structs_h */ diff --git a/include/cglm/structs/vec4-ext.h b/include/cglm/structs/vec4-ext.h new file mode 100644 index 0000000..ea84e82 --- /dev/null +++ b/include/cglm/structs/vec4-ext.h @@ -0,0 +1,114 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_vec4s_ext_h +#define cglm_vec4s_ext_h + +#include "common.h" +#include "vec3-ext.h" +#include +#include +#include + +CGLM_INLINE +vec4s +glms_vec4_broadcast(float val, vec4 d) +{ + vec4s r; + glm_vec4_broadcast(val, r.raw); + return r; +} + +CGLM_INLINE +bool +glms_vec4_eq(vec4s v, float val) +{ + return glm_vec4_eq(v.raw, val); +} + +CGLM_INLINE +bool +glms_vec4_eq_eps(vec4 v, float val) +{ + return glm_vec4_eq_eps(v.raw, val); +} + +CGLM_INLINE +bool +glms_vec4_eq_all(vec4s v) +{ + return glm_vec4_eq_all(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_eqv(vec4s a, vec4s b) +{ + return glm_vec4_eqv(a.raw, b.raw); +} + +CGLM_INLINE +bool +glms_vec4_eqv_eps(vec4s a, vec4s b) +{ + return glm_vec4_eqv_eps(a.raw, b.raw); +} + +CGLM_INLINE +float +glms_vec4_max(vec4s v) +{ + return glm_vec4_max(v.raw); +} + +CGLM_INLINE +float +glms_vec4_min(vec4s v) +{ + return glm_vec4_min(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_isnan(vec4s v) +{ + return glm_vec4_isnan(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_isinf(vec4s v) +{ + return glm_vec4_isinf(v.raw); +} + +CGLM_INLINE +bool +glms_vec4_isvalid(vec4s v) +{ + return glm_vec4_isvalid(v.raw); +} + +CGLM_INLINE +vec4s +glms_vec4_sign(vec4s v) +{ + vec4s r; + glm_vec4_sign(v.raw, r.raw); + return r; +} + +CGLM_INLINE +vec4s +glms_vec4_sqrt(vec4s v) +{ + vec4s r; + glm_vec4_sqrt(v.raw, r.raw); + return r; +} + +#endif /* cglm_vec4s_ext_h */ diff --git a/include/cglm/structs/vec4.h b/include/cglm/structs/vec4.h index 7290c35..b68bb33 100644 --- a/include/cglm/structs/vec4.h +++ b/include/cglm/structs/vec4.h @@ -8,9 +8,11 @@ #ifndef cglm_vec4s_h #define cglm_vec4s_h -#include "common.h" -#include "vec4-ext.h" -#include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" #define GLMS_VEC4_ONE_INIT {1.0f, 1.0f, 1.0f, 1.0f} #define GLMS_VEC4_BLACK_INIT {0.0f, 0.0f, 0.0f, 1.0f} @@ -92,7 +94,7 @@ vec4s glms_vec4_adds(vec4s v, float s) { vec4s r; - glm_vec4_adds(v.raw s, r.raw); + glm_vec4_adds(v.raw, s, r.raw); return r; } @@ -226,7 +228,7 @@ vec4s glms_vec4_inv(vec4s v) { glm_vec4_inv(v.raw); - return r; + return v; } CGLM_INLINE @@ -279,4 +281,7 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) return r; } +#ifdef __cplusplus +} +#endif #endif /* cglm_vec4s_h */ diff --git a/include/cglm/types.h b/include/cglm/types.h index 29703df..69c2885 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -47,17 +47,7 @@ typedef CGLM_ALIGN_IF(16) vec4 mat4[4]; #endif // struct types -typedef union CGLM_ALIGN_IF(8) vec2s { -#ifndef CGLM_NO_ANONYMOUS_STRUCT - struct { - float x; - float y; - }; -#endif - vec2 raw; -} vec2s; - -typedef union CGLM_ALIGN_IF(8) vec3s { +typedef union CGLM_ALIGN_IF(16) vec3s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { float x; @@ -68,17 +58,6 @@ typedef union CGLM_ALIGN_IF(8) vec3s { vec3 raw; } vec3s; -typedef union CGLM_ALIGN_IF(8) ivec3s { -#ifndef CGLM_NO_ANONYMOUS_STRUCT - struct { - int x; - int y; - int z; - }; -#endif - ivec3 raw; -} ivec3s; - typedef union CGLM_ALIGN_IF(16) vec4s { #ifndef CGLM_NO_ANONYMOUS_STRUCT struct { diff --git a/test/src/test_common.c b/test/src/test_common.c index d41d3cb..94331f4 100644 --- a/test/src/test_common.c +++ b/test/src/test_common.c @@ -57,6 +57,14 @@ test_rand_vec4(vec4 dest) { dest[3] = drand48(); } +vec4s +test_rand_vec4s() +{ + vec4s r; + test_rand_vec4(r.raw); + return r; +} + float test_rand(void) { srand((unsigned int)time(NULL)); @@ -127,6 +135,12 @@ test_assert_vec4_eq(vec4 v1, vec4 v2) { assert_true(fabsf(v1[3] - v2[3]) <= 0.000009); } +void +test_assert_vec4s_eq(vec4s v1, vec4s v2) +{ + test_assert_vec4_eq(v1.raw, v2.raw); +} + void test_assert_quat_eq_abs(versor v1, versor v2) { assert_true(fabsf(fabsf(v1[0]) - fabsf(v2[0])) <= 0.0009); /* rounding errors */ diff --git a/test/src/test_common.h b/test/src/test_common.h index 8a16b0f..c6a2dc4 100644 --- a/test/src/test_common.h +++ b/test/src/test_common.h @@ -20,6 +20,7 @@ #include #include +#include #include void @@ -46,6 +47,9 @@ test_assert_vec3_eq(vec3 v1, vec3 v2); void test_assert_vec4_eq(vec4 v1, vec4 v2); +void +test_assert_vec4s_eq(vec4s v1, vec4s v2); + void test_assert_quat_eq(versor v1, versor v2); @@ -56,7 +60,10 @@ void test_rand_vec3(vec3 dest); void -test_rand_vec4(vec4 dest) ; +test_rand_vec4(vec4 dest); + +vec4s +test_rand_vec4s(); float test_rand(void); diff --git a/test/src/test_quat.c b/test/src/test_quat.c index 0d64367..d0cc27c 100644 --- a/test/src/test_quat.c +++ b/test/src/test_quat.c @@ -44,9 +44,11 @@ test_quat(void **state) { test_assert_mat4_eq2(inRot, outRot, 0.000009); /* almost equal */ /* 4. test SSE mul and raw mul */ +#if defined( __SSE__ ) || defined( __SSE2__ ) test_quat_mul_raw(inQuat, outQuat, q3); glm_quat_mul_sse2(inQuat, outQuat, q4); test_assert_quat_eq(q3, q4); +#endif } /* 5. test lookat */ diff --git a/test/src/test_vec4.c b/test/src/test_vec4.c index 8e4fda5..5fe3be8 100644 --- a/test/src/test_vec4.c +++ b/test/src/test_vec4.c @@ -65,10 +65,10 @@ test_vec4_clamp(vec4 v, float minVal, float maxVal) { void test_vec4(void **state) { vec4 v, v1, v2, v3, v4; + vec4s vs1, vs2, vs3, vs4; int i; float d1, d2; - for (i = 0; i < 1000; i++) { /* 1. test SSE/SIMD dot product */ test_rand_vec4(v); @@ -175,4 +175,12 @@ test_vec4(void **state) { assert_true(v3[1] >= 0.0999 && v3[1] <= 0.80001); assert_true(v3[2] >= 0.0999 && v3[2] <= 0.80001); assert_true(v3[3] >= 0.0999 && v3[3] <= 0.80001); + + /* structs */ + vs1 = test_rand_vec4s(); + vs2 = test_rand_vec4s(); + + vs3 = glms_vec4_add(vs1, vs2); + vs4 = glms_vec4_maxv(vs1, vs3); + test_assert_vec4s_eq(vs3, vs4); }