1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Implement a helper function to clamp a value between the ranges of another type.

This commit is contained in:
Sandu Liviu Catalin 2016-03-26 18:14:47 +02:00
parent b486ecc2fb
commit 886e525119
2 changed files with 20 additions and 1 deletions

View File

@ -19,7 +19,6 @@
// ------------------------------------------------------------------------------------------------
#include <ctime>
#include <cfloat>
#include <limits>
#include <cstdarg>
#include <cstring>
#include <algorithm>

View File

@ -6,6 +6,7 @@
// ------------------------------------------------------------------------------------------------
#include <cmath>
#include <limits>
// ------------------------------------------------------------------------------------------------
#include <vcmp.h>
@ -225,6 +226,25 @@ template< typename T > inline T Clamp(T val, T min, T max)
return val < min ? min : (val > max ? max : val);
}
/* ------------------------------------------------------------------------------------------------
* Force a value to be the boundaries of the specified type.
*/
template< typename T, typename U > inline U ClampL(T val)
{
// Is the specified value bellow the minimum?
if (val < std::numeric_limits< U >::min())
{
return std::numeric_limits< U >::min();
}
// Is the specified value above the maximum?
else if (val > std::numeric_limits< U >::max())
{
return std::numeric_limits< U >::max();
}
// Return the value as is
return static_cast< U >(val);
}
/* ------------------------------------------------------------------------------------------------
* Compute the next power of two for the specified number.
*/