/src/botan/build/include/internal/botan/internal/rounding.h
Line | Count | Source |
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/assert.h> |
12 | | #include <botan/types.h> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | /** |
17 | | * Integer rounding |
18 | | * |
19 | | * Returns an integer z such that n <= z <= n + align_to |
20 | | * and z % align_to == 0 |
21 | | * |
22 | | * @param n an integer |
23 | | * @param align_to the alignment boundary |
24 | | * @return n rounded up to a multiple of align_to |
25 | | */ |
26 | 9.80k | constexpr inline size_t round_up(size_t n, size_t align_to) { |
27 | | // Arguably returning n in this case would also be sensible |
28 | 9.80k | BOTAN_ARG_CHECK(align_to != 0, "align_to must not be 0"); |
29 | | |
30 | 9.80k | if(n % align_to > 0) { |
31 | 6.95k | const size_t adj = align_to - (n % align_to); |
32 | 6.95k | BOTAN_ARG_CHECK(n + adj >= n, "Integer overflow during rounding"); |
33 | 6.95k | n += adj; |
34 | 6.95k | } |
35 | 9.80k | return n; |
36 | 9.80k | } |
37 | | |
38 | | } // namespace Botan |
39 | | |
40 | | #endif |