Skip to content

Instantly share code, notes, and snippets.

@smx-smx
Last active August 21, 2017 21:38
Show Gist options
  • Save smx-smx/7fbf06b2b4e3a364c5e6916fbf928ae4 to your computer and use it in GitHub Desktop.
Save smx-smx/7fbf06b2b4e3a364c5e6916fbf928ae4 to your computer and use it in GitHub Desktop.
Vala Structs by value
[CCode(cheader_filename = "struct.h")]
namespace CGlm {
[SimpleType, CCode(cname = "mat4", cprefix = "glm_mat4_", default_value = "GLM_MAT4_IDENTITY_INIT")]
public struct Matrix4 {
[CCode(cname = "GLM_MAT4_IDENTITY_INIT")]
public const Matrix4 IDENTITY;
public Matrix4(){}
public float det();
public void identity();
}
}
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
// SMX: Trimmed down cglm headers for this test
#ifndef cglm_types_h
#define cglm_types_h
#if defined(_WIN32)
# define CGLM_ALIGN(X) /* __declspec(align(X)) */
#else
# define CGLM_ALIGN(X) __attribute((aligned(X)))
#endif
typedef float vec3[3];
typedef int ivec3[3];
typedef CGLM_ALIGN(16) float vec4[4];
typedef vec3 mat3[3];
typedef vec4 mat4[4];
typedef vec4 versor;
#define CGLM_PI (float)M_PI
#define CGLM_PI_2 (float)M_PI_2
#define CGLM_PI_4 (float)M_PI_4
// from mat4.h
#define GLM_MAT4_IDENTITY_INIT {{1.0f, 0.0f, 0.0f, 0.0f}, \
{0.0f, 1.0f, 0.0f, 0.0f}, \
{0.0f, 0.0f, 1.0f, 0.0f}, \
{0.0f, 0.0f, 0.0f, 1.0f}}
#define glm__memcpy(type, dest, src, size) \
do { \
type *srci; \
type *srci_end; \
type *desti; \
\
srci = (type *)src; \
srci_end = (type *)((char *)srci + size); \
desti = (type *)dest; \
\
while (srci != srci_end) \
*desti++ = *srci++; \
} while (0)
void
glm_mat4_ucopy(mat4 mat, mat4 dest) {
glm__memcpy(float, dest, mat, sizeof(mat4));
}
void
glm_mat4_copy(mat4 mat, mat4 dest) {
#ifdef __AVX__
_mm256_store_ps(dest[0], _mm256_load_ps(mat[0]));
_mm256_store_ps(dest[2], _mm256_load_ps(mat[2]));
#elif defined( __SSE__ ) || defined( __SSE2__ )
_mm_store_ps(dest[0], _mm_load_ps(mat[0]));
_mm_store_ps(dest[1], _mm_load_ps(mat[1]));
_mm_store_ps(dest[2], _mm_load_ps(mat[2]));
_mm_store_ps(dest[3], _mm_load_ps(mat[3]));
#else
glm_mat4_ucopy(mat, dest);
#endif
}
void
glm_mat4_identity(mat4 mat) {
mat4 t = GLM_MAT4_IDENTITY_INIT;
glm_mat4_copy(t, mat);
}
#endif /* cglm_types_h */
using CGlm;
void main() {
Matrix4 mat = {};
// We do something with mat
float det = mat.det();
stdout.printf("Determinant: %.2f\n", det);
// We want a copy of mat to mat2
Matrix4 mat2 = {};
mat2 = mat;
// We do something with mat2
float det2 = mat2.det();
stdout.printf("Determinant: %.2f\n", det2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment