Botan 3.9.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/assert.h>
12#include <botan/types.h>
13
14namespace 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*/
26constexpr inline size_t round_up(size_t n, size_t align_to) {
27 // Arguably returning n in this case would also be sensible
28 BOTAN_ARG_CHECK(align_to != 0, "align_to must not be 0");
29
30 if(n % align_to > 0) {
31 const size_t adj = align_to - (n % align_to);
32 BOTAN_ARG_CHECK(n + adj >= n, "Integer overflow during rounding");
33 n += adj;
34 }
35 return n;
36}
37
38} // namespace Botan
39
40#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
constexpr size_t round_up(size_t n, size_t align_to)
Definition rounding.h:26