aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/rounding.h
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-10 03:41:59 +0000
committerlloyd <[email protected]>2014-01-10 03:41:59 +0000
commit6894dca64c04936d07048c0e8cbf7e25858548c3 (patch)
tree5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/utils/rounding.h
parent9efa3be92442afb3d0b69890a36c7f122df18eda (diff)
Move lib into src
Diffstat (limited to 'src/lib/utils/rounding.h')
-rw-r--r--src/lib/utils/rounding.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lib/utils/rounding.h b/src/lib/utils/rounding.h
new file mode 100644
index 000000000..4ddd7a432
--- /dev/null
+++ b/src/lib/utils/rounding.h
@@ -0,0 +1,61 @@
+/*
+* Integer Rounding Functions
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_ROUNDING_H__
+#define BOTAN_ROUNDING_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/**
+* Round up
+* @param n an integer
+* @param align_to the alignment boundary
+* @return n rounded up to a multiple of align_to
+*/
+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;
+ }
+
+/**
+* Round down
+* @param n an integer
+* @param align_to the alignment boundary
+* @return n rounded down to a multiple of 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