Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using constexpr for normalize #1314

Open
cmd05 opened this issue Oct 7, 2024 · 0 comments
Open

Using constexpr for normalize #1314

cmd05 opened this issue Oct 7, 2024 · 0 comments

Comments

@cmd05
Copy link

cmd05 commented Oct 7, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant