aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/rounding.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/rounding.h')
-rw-r--r--src/utils/rounding.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/utils/rounding.h b/src/utils/rounding.h
index c77ab9b52..4ddd7a432 100644
--- a/src/utils/rounding.h
+++ b/src/utils/rounding.h
@@ -21,6 +21,9 @@ namespace Botan {
template<typename T>
inline T round_up(T n, T align_to)
{
+ if(align_to == 0)
+ return n;
+
if(n % align_to || n == 0)
n += align_to - (n % align_to);
return n;
@@ -35,9 +38,24 @@ inline T round_up(T n, T align_to)
template<typename T>
inline T round_down(T n, T align_to)
{
+ if(align_to == 0)
+ return n;
+
return (n - (n % align_to));
}
+/**
+* Clamp
+*/
+inline size_t clamp(size_t n, size_t lower_bound, size_t upper_bound)
+ {
+ if(n < lower_bound)
+ return lower_bound;
+ if(n > upper_bound)
+ return upper_bound;
+ return n;
+ }
+
}
#endif