Replies: 2 comments
-
Hi @enadream, Sorry for the delay,
In general creating an intermediate object is not a drawback but beneficial for some circumstances:
CGLM_INLINE
void
glm_vec3_cross(vec3 a, vec3 b, vec3 dest) {
vec3 c;
/* (u2.v3 - u3.v2, u3.v1 - u1.v3, u1.v2 - u2.v1) */
c[0] = a[1] * b[2] - a[2] * b[1];
c[1] = a[2] * b[0] - a[0] * b[2];
c[2] = a[0] * b[1] - a[1] * b[0];
glm_vec3_copy(c, dest);
} can indeed be (without extra glm_vec3_copy()): CGLM_INLINE
void
glm_vec3_cross(vec3 a, vec3 b, vec3 dest) {
/* (u2.v3 - u3.v2, u3.v1 - u1.v3, u1.v2 - u2.v1) */
dest[0] = a[1] * b[2] - a[2] * b[1];
dest[1] = a[2] * b[0] - a[0] * b[2];
dest[2] = a[0] * b[1] - a[1] * b[0];
} bu if you call dest[0] = a[1] * dest[2] - a[2] * dest[1];
dest[1] = a[2] * dest[0] - a[0] * dest[2];
dest[2] = a[0] * dest[1] - a[1] * dest[0]; as you can see
vec3 can be optimized with simd in the future if there will be enough room for efficiency Struct APIcglm also provides structure api ( see documentation ). Struct api returns intermediate value directly and compilers may produce faster code by return value optimization and reduce extra copy/move operations. Both Array and Struct Api ( including CALL / GLMM APIs too ) can be used together. |
Beta Was this translation helpful? Give feedback.
-
@recp I hadn't thought about it this way. Thanks for the clarification. |
Beta Was this translation helpful? Give feedback.
-
Hi, I decided to use cglm instead of glm because I thought glm doesn't make operations in place, I mean it creates an intermediate object to make operations and then copy the result into the target object. I thought if I use cglm that problem wouldn't be in case, however when I read your vec3.h file I saw that you used a lot of intermediate vec3 inorder to do operations and then copy that vector to the dest parameter. Why did you do that ? Why instead of copying results directly into dest vector you created a vec t and then copied it into dest using glm_vec3_copy ? What's the point of that ?
Beta Was this translation helpful? Give feedback.
All reactions