Botan  2.7.0
Crypto and TLS for C++11
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 
13 namespace Botan {
14 
15 /**
16 * Round up
17 * @param n a non-negative integer
18 * @param align_to the alignment boundary
19 * @return n rounded up to a multiple of align_to
20 */
21 inline size_t round_up(size_t n, size_t align_to)
22  {
23  BOTAN_ARG_CHECK(align_to != 0, "align_to must not be 0");
24 
25  if(n % align_to)
26  n += align_to - (n % align_to);
27  return n;
28  }
29 
30 /**
31 * Round down
32 * @param n an integer
33 * @param align_to the alignment boundary
34 * @return n rounded down to a multiple of align_to
35 */
36 template<typename T>
37 inline T round_down(T n, T align_to)
38  {
39  if(align_to == 0)
40  return n;
41 
42  return (n - (n % align_to));
43  }
44 
45 /**
46 * Clamp
47 */
48 inline size_t clamp(size_t n, size_t lower_bound, size_t upper_bound)
49  {
50  if(n < lower_bound)
51  return lower_bound;
52  if(n > upper_bound)
53  return upper_bound;
54  return n;
55  }
56 
57 }
58 
59 #endif
T round_down(T n, T align_to)
Definition: rounding.h:37
size_t clamp(size_t n, size_t lower_bound, size_t upper_bound)
Definition: rounding.h:48
Definition: alg_id.cpp:13
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
fe T
Definition: ge.cpp:37
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21