cglm icon indicating copy to clipboard operation
cglm copied to clipboard

Use `_mm_sincos_ps` where both sin and cos are calculated for same angle

Open legends2k opened this issue 3 years ago • 6 comments

In //include/cglm/quat.h, we've glm_quatv which calculates a new quaternion from axis and angle; it does

  c = cosf(a);
  s = sinf(a);

This can be optimized by using

  1. _mm_sincos_ps
  2. For non-SIMD cases, some platforms like Linux support sincos

Moving to this would be a nice (micro-)optimization 😉

legends2k avatar May 28 '21 06:05 legends2k

Related discussion on StackOverflow: What is the fastest way to compute sin and cos together?.

legends2k avatar May 28 '21 06:05 legends2k

@legends2k thanks, I like micro optimizations :) we can create a function like glm_sincos() then use _mm_sincos_ps() in for related platforms, I liked the idea

recp avatar May 28 '21 11:05 recp

It seems compilers can do this optimization for many platforms:

Details

arm32 gcc arm64 gcc rv64gc clang armv8-a clang x64 clang x64 gcc x64 icc x64 msvc x86 msvc

myfreeer avatar Apr 22 '23 03:04 myfreeer

@myfreeer thanks for the detailed explanation but I still like glm_sincos() idea which uses sincos internally if available, compilers may optimize this small inline function more easily without doubt rather than complex func I guess:

glm_sincos( ... ) {
  *s = sinf(a);
  *c = cosf(a);
}

let's keep the issue open for a while.

recp avatar Apr 23 '23 07:04 recp

Another benefit to glm_sincos is that most sin/cos approximation functions (e.g., when cross-platform determinism is required) compute both values simultaneously (rather, it is "free" to compute them simultaneously). So it is usually a nice abstraction to have.

gottfriedleibniz avatar Apr 23 '23 16:04 gottfriedleibniz

@gottfriedleibniz thanks for your feedback[s]

recp avatar Apr 23 '23 19:04 recp