Coverage Report

Created: 2024-11-29 06:10

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