v29.4-roots.3 · Commit 2: [PATCH 02/30] runtime: preserve shared operator controls
src/util/overflow.h
Before: src/util/overflow.h · After: src/util/overflow.h
File metadata
index 67711af0a5ecfad37921ce5cf8b57bda0eb62fec..18134139315f0a22ffa4a70c283b54837a5dfdf6 100644
Download original patch · Release notes
Diff rows 1–29 of 29. Line numbers refer to the original before/after files.
+ Added− RemovedUnmarked lines are context.
Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.
src/util/overflow.h
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
#ifndef BITCOIN_UTIL_OVERFLOW_H
|
|
6
6
|
#define BITCOIN_UTIL_OVERFLOW_H
|
|
7
7
|
|
|
8
|
+
#include <util/check.h>
|
|
9
|
+
|
|
8
10
|
#include <climits>
|
|
9
11
|
#include <concepts>
|
|
10
12
|
#include <limits>
|
|
@@ -49,6 +51,21 @@ template <class T>
|
|
|
49
51
|
return i + j;
|
|
50
52
|
}
|
|
51
53
|
|
|
54
|
+
/**
|
|
55
|
+
* @brief Integer ceiling division (for non-negative values).
|
|
56
|
+
*
|
|
57
|
+
* Computes the smallest integer q such that q * divisor >= dividend.
|
|
58
|
+
* Both dividend and divisor must be non-negative, and divisor must be non-zero.
|
|
59
|
+
*
|
|
60
|
+
* The implementation avoids overflow that can occur with `(dividend + divisor - 1) / divisor`.
|
|
61
|
+
*/
|
|
62
|
+
template <std::integral Dividend, std::integral Divisor>
|
|
63
|
+
[[nodiscard]] constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
|
|
64
|
+
{
|
|
65
|
+
Assert(dividend >= 0 && divisor > 0);
|
|
66
|
+
return dividend / divisor + (dividend % divisor != 0);
|
|
67
|
+
}
|
|
68
|
+
|
|
52
69
|
/**
|
|
53
70
|
* @brief Left bit shift with overflow checking.
|
|
54
71
|
* @param input The input value to be left shifted.
|