Skip to content

Using constexpr for normalize #1314

@cmd05

Description

@cmd05

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions