Skip to content

Commit b1fed40

Browse files
0xf00ff00fchristophe-lunarg
authored andcommitted
Make min, max, mix and clamp constexpr
These functions were already marked constexpr in the header, but their implementations relied on non-constexpr functions. Mark those as constexpr.
1 parent 2436fe7 commit b1fed40

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

glm/detail/func_common.inl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ namespace detail
4747
{
4848
template<typename T>
4949
struct TMin {
50-
GLM_FUNC_QUALIFIER T operator()(const T& a, const T& b) { return min(a, b); }
50+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T operator()(const T& a, const T& b) { return min(a, b); }
5151
};
5252

5353
template<typename T>
5454
struct TMax {
55-
GLM_FUNC_QUALIFIER T operator()(const T& a, const T& b) { return max(a, b); }
55+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T operator()(const T& a, const T& b) { return max(a, b); }
5656
};
5757

5858
template<typename T>
@@ -87,7 +87,7 @@ namespace detail
8787
template<length_t L, typename T, typename U, qualifier Q, bool Aligned>
8888
struct compute_mix_vector
8989
{
90-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, U, Q> const& a)
90+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, U, Q> const& a)
9191
{
9292
static_assert(std::numeric_limits<U>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a");
9393

@@ -98,7 +98,7 @@ namespace detail
9898
template<length_t L, typename T, qualifier Q, bool Aligned>
9999
struct compute_mix_vector<L, T, bool, Q, Aligned>
100100
{
101-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, bool, Q> const& a)
101+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, bool, Q> const& a)
102102
{
103103
vec<L, T, Q> Result(0);
104104
for(length_t i = 0; i < x.length(); ++i)
@@ -110,7 +110,7 @@ namespace detail
110110
template<length_t L, typename T, typename U, qualifier Q, bool Aligned>
111111
struct compute_mix_scalar
112112
{
113-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, U const& a)
113+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, U const& a)
114114
{
115115
static_assert(std::numeric_limits<U>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a");
116116

@@ -121,7 +121,7 @@ namespace detail
121121
template<length_t L, typename T, qualifier Q, bool Aligned>
122122
struct compute_mix_scalar<L, T, bool, Q, Aligned>
123123
{
124-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, bool const& a)
124+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y, bool const& a)
125125
{
126126
return a ? y : x;
127127
}
@@ -130,7 +130,7 @@ namespace detail
130130
template<typename T, typename U>
131131
struct compute_mix
132132
{
133-
GLM_FUNC_QUALIFIER static T call(T const& x, T const& y, U const& a)
133+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static T call(T const& x, T const& y, U const& a)
134134
{
135135
static_assert(std::numeric_limits<U>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT || GLM_CONFIG_UNRESTRICTED_GENTYPE, "'mix' only accept floating-point inputs for the interpolator a");
136136

@@ -141,7 +141,7 @@ namespace detail
141141
template<typename T>
142142
struct compute_mix<T, bool>
143143
{
144-
GLM_FUNC_QUALIFIER static T call(T const& x, T const& y, bool const& a)
144+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static T call(T const& x, T const& y, bool const& a)
145145
{
146146
return a ? y : x;
147147
}
@@ -237,7 +237,7 @@ namespace detail
237237
template<length_t L, typename T, qualifier Q, bool Aligned>
238238
struct compute_min_vector
239239
{
240-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
240+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
241241
{
242242
return detail::functor2<vec, L, T, Q>::call(TMin<T>(), x, y);
243243
}
@@ -246,7 +246,7 @@ namespace detail
246246
template<length_t L, typename T, qualifier Q, bool Aligned>
247247
struct compute_max_vector
248248
{
249-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
249+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
250250
{
251251
return detail::functor2<vec, L, T, Q>::call(TMax<T>(), x, y);
252252
}
@@ -255,7 +255,7 @@ namespace detail
255255
template<length_t L, typename T, qualifier Q, bool Aligned>
256256
struct compute_clamp_vector
257257
{
258-
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& minVal, vec<L, T, Q> const& maxVal)
258+
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x, vec<L, T, Q> const& minVal, vec<L, T, Q> const& maxVal)
259259
{
260260
return min(max(x, minVal), maxVal);
261261
}

test/core/core_func_common.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,28 @@ namespace min_
298298
return Error;
299299
}
300300

301+
#if GLM_HAS_CONSTEXPR
302+
static int test_constexpr()
303+
{
304+
constexpr glm::vec1 A0 = glm::min(glm::vec1(1), glm::vec1(1));
305+
static_assert(glm::all(glm::equal(A0, glm::vec1(1), glm::epsilon<float>())), "GLM: Failed constexpr");
306+
307+
constexpr glm::vec2 B0 = glm::min(glm::vec2(1), glm::vec2(1));
308+
constexpr glm::vec2 B1 = glm::min(glm::vec2(1), 1.0f);
309+
static_assert(glm::all(glm::equal(B0, B1, glm::epsilon<float>())), "GLM: Failed constexpr");
310+
311+
constexpr glm::vec3 C0 = glm::min(glm::vec3(1), glm::vec3(1));
312+
constexpr glm::vec3 C1 = glm::min(glm::vec3(1), 1.0f);
313+
static_assert(glm::all(glm::equal(C0, C1, glm::epsilon<float>())), "GLM: Failed constexpr");
314+
315+
constexpr glm::vec4 D0 = glm::min(glm::vec4(1), glm::vec4(1));
316+
constexpr glm::vec4 D1 = glm::min(glm::vec4(1), 1.0f);
317+
static_assert(glm::all(glm::equal(D0, D1, glm::epsilon<float>())), "GLM: Failed constexpr");
318+
319+
return 0;
320+
}
321+
#endif
322+
301323
static int min_tern(int a, int b)
302324
{
303325
return a < b ? a : b;
@@ -383,6 +405,28 @@ namespace max_
383405

384406
return Error;
385407
}
408+
409+
#if GLM_HAS_CONSTEXPR
410+
static int test_constexpr()
411+
{
412+
constexpr glm::vec1 A0 = glm::max(glm::vec1(1), glm::vec1(1));
413+
static_assert(glm::all(glm::equal(A0, glm::vec1(1), glm::epsilon<float>())), "GLM: Failed constexpr");
414+
415+
constexpr glm::vec2 B0 = glm::max(glm::vec2(1), glm::vec2(1));
416+
constexpr glm::vec2 B1 = glm::max(glm::vec2(1), 1.0f);
417+
static_assert(glm::all(glm::equal(B0, B1, glm::epsilon<float>())), "GLM: Failed constexpr");
418+
419+
constexpr glm::vec3 C0 = glm::max(glm::vec3(1), glm::vec3(1));
420+
constexpr glm::vec3 C1 = glm::max(glm::vec3(1), 1.0f);
421+
static_assert(glm::all(glm::equal(C0, C1, glm::epsilon<float>())), "GLM: Failed constexpr");
422+
423+
constexpr glm::vec4 D0 = glm::max(glm::vec4(1), glm::vec4(1));
424+
constexpr glm::vec4 D1 = glm::max(glm::vec4(1), 1.0f);
425+
static_assert(glm::all(glm::equal(D0, D1, glm::epsilon<float>())), "GLM: Failed constexpr");
426+
427+
return 0;
428+
}
429+
#endif
386430
}//namespace max_
387431

388432
namespace clamp_
@@ -1386,7 +1430,13 @@ int main()
13861430
Error += step_::test();
13871431
Error += smoothstep_::test();
13881432
Error += max_::test();
1433+
#if GLM_HAS_CONSTEXPR
1434+
Error += max_::test_constexpr();
1435+
#endif
13891436
Error += min_::test();
1437+
#if GLM_HAS_CONSTEXPR
1438+
Error += min_::test_constexpr();
1439+
#endif
13901440
Error += clamp_::test();
13911441
Error += round_::test();
13921442
Error += roundEven::test();

0 commit comments

Comments
 (0)