Botan 3.5.0
Crypto and TLS for C&
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
13namespace Botan {
14
15/**
16* Integer rounding
17*
18* Returns an integer z such that n <= z <= n + align_to
19* and z % align_to == 0
20*
21* @param n an integer
22* @param align_to the alignment boundary
23* @return n rounded up to a multiple of align_to
24*/
25inline size_t round_up(size_t n, size_t align_to) {
26 // Arguably returning n in this case would also be sensible
27 BOTAN_ARG_CHECK(align_to != 0, "align_to must not be 0");
28
29 if(n % align_to > 0) {
30 const size_t adj = align_to - (n % align_to);
31 BOTAN_ARG_CHECK(n + adj >= n, "Integer overflow during rounding");
32 n += adj;
33 }
34 return n;
35}
36
37} // namespace Botan
38
39#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
size_t round_up(size_t n, size_t align_to)
Definition rounding.h:25