Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

rounding.h
Go to the documentation of this file.
1 /*
2 * Integer Rounding Functions
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_ROUNDING_H__
9 #define BOTAN_ROUNDING_H__
10
11 #include <botan/types.h>
12 #include <botan/assert.h>
13
14 namespace Botan {
15
16 /**
17 * Round up
18 * @param n a non-negative integer
19 * @param align_to the alignment boundary
20 * @return n rounded up to a multiple of align_to
21 */
22 inline size_t round_up(size_t n, size_t align_to)
23 {
24 BOTAN_ASSERT(align_to != 0, "align_to must not be 0");
25
26 if(n % align_to)
27 n += align_to - (n % align_to);
28 return n;
29 }
30
31 /**
32 * Round down
33 * @param n an integer
34 * @param align_to the alignment boundary
35 * @return n rounded down to a multiple of align_to
36 */
37 template<typename T>
38 inline T round_down(T n, T align_to)
39 {
40 if(align_to == 0)
41 return n;
42
43 return (n - (n % align_to));
44 }
45
46 /**
47 * Clamp
48 */
49 inline size_t clamp(size_t n, size_t lower_bound, size_t upper_bound)
50 {
51 if(n < lower_bound)
52 return lower_bound;
53 if(n > upper_bound)
54 return upper_bound;
55 return n;
56 }
57
58 }
59
60 #endif
Botan::round_down
T round_down(T n, T align_to)
Definition: rounding.h:38
Botan::clamp
size_t clamp(size_t n, size_t lower_bound, size_t upper_bound)
Definition: rounding.h:49
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan
Definition: alg_id.cpp:13
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22