Skip to content
Bitcoin Roots

v29.4-roots.1 · Commit 2: [PATCH 02/13] 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. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/util/overflow.h CHANGED
@@ -5,6 +5,8 @@
5
  #ifndef BITCOIN_UTIL_OVERFLOW_H
6
  #define BITCOIN_UTIL_OVERFLOW_H
7
 
 
 
8
  #include <climits>
9
  #include <concepts>
10
  #include <limits>
@@ -49,6 +51,21 @@ template <class T>
49
  return i + j;
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  /**
53
  * @brief Left bit shift with overflow checking.
54
  * @param input The input value to be left shifted.
 
5
  #ifndef BITCOIN_UTIL_OVERFLOW_H
6
  #define BITCOIN_UTIL_OVERFLOW_H
7
 
8
+ #include <util/check.h>
9
+
10
  #include <climits>
11
  #include <concepts>
12
  #include <limits>
 
51
  return i + j;
52
  }
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
+
69
  /**
70
  * @brief Left bit shift with overflow checking.
71
  * @param input The input value to be left shifted.