mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-05-02 09:07:12 +02:00
Implement a helper function to clamp a value between the ranges of another type.
This commit is contained in:
parent
b486ecc2fb
commit
886e525119
@ -19,7 +19,6 @@
|
|||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <cfloat>
|
#include <cfloat>
|
||||||
#include <limits>
|
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
#include <vcmp.h>
|
#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);
|
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.
|
* Compute the next power of two for the specified number.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user