g-truc / glm

OpenGL Mathematics (GLM)
https://glm.g-truc.net
Other
9.35k stars 2.14k forks source link

Using constexpr for normalize #1314

Open cmd05 opened 1 month ago

cmd05 commented 1 month ago

I am trying to write a config file using glm and wanted to use glm::normalize with constexpr. If I am understanding correctly it is due to inversesqrt relying on std::sqrt, which is not constexpr.

struct compute_normalize
{
    GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v)
    {
        GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'normalize' accepts only floating-point inputs");

        return v * inversesqrt(dot(v, v));
    }
};

This document (part III-D) could be relevant for the decision for not using sqrt as constexpr in cmath.

Could we have a feature to use a constexpr version of glm::normalize which would internally rely on something other than std::sqrt as I feel normalize is a very commonly used function and could benefit from being computed at compile time.

Edit: I am using C++17 which has a compile time sqrt in cmath

#ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
  inline _GLIBCXX_CONSTEXPR float
  sqrt(float __x)
  { return __builtin_sqrtf(__x); }

  inline _GLIBCXX_CONSTEXPR long double
  sqrt(long double __x)
  { return __builtin_sqrtl(__x); }
#endif