Merge bitcoin/bitcoin#34435: refactor: use _MiB/_GiB consistently for byte conversions

af0ee28eb6 refactor: use _MiB consistently for Mebibyte conversions (Lőrinc)
b3edd30aa2 util: add _GiB for Gibibyte conversions (Lőrinc)

Pull request description:

  ### Problem
  Byte-size conversions in the codebase currently show up in many equivalent formats (multiplication/division chains, shifts, hex/binary literals), which creates a maintenance burden and makes review error-prone - especially considering the architectural differences of `size_t`.
  Inspired by https://github.com/bitcoin/bitcoin/pull/34305#discussion_r2734720002, it seemed appropriate to unify `Mebibyte` usage across the codebase and add `Gibibyte` support with 32/64 bit `size_t` validation.

  ### Fix
  This PR refactors those call sites to use `""_MiB` (existing) and `""_GiB` (new), and adds the encountered value/pattern replacements to unit tests to make review straightforward, and to ensure the conversions remain valid.
  The literals are overflow-checked when converting to `size_t`, and unit tests cover the 32-bit boundary cases.

  Concretely, it replaces patterns such as:
  * `1024*1024`, `1<<20`, `0x100000`, `1048576`, `/ 1024 / 1024`, `* (1.0 / 1024 / 1024)` → `1_MiB` or `double(1_MiB)`
  * `1024*1024*1024`, `1<<30`, `0x40000000`, `1024_MiB`, `>> 30` → `1_GiB`

  (added unit tests for each replacement category to ease review)

  Additionally, declarations whose initializer reads a `_MiB`/`_GiB` literal are switched to braced initialization so a future oversized value is rejected at compile time through the narrowing check instead of silently truncating.

  ### Note
  In the few places where arithmetic involves signed values, the result is identical to the previous code assuming those quantities never become negative.

ACKs for top commit:
  achow101:
    ACK af0ee28eb6
  janb84:
    ACK af0ee28eb6
  maflcko:
    review ACK af0ee28eb6 🖍
  hodlinator:
    re-ACK af0ee28eb6

Tree-SHA512: 55286ce3f833f88335394a74e9e0b95c7d023e5cdc9ded40accbbbcd870101e4dcc05926865d6bef4c1be1ebd648aa3fdf947ef9575633ccfe56691f145d7a2d
This commit is contained in:
Ava Chow
2026-04-22 15:37:59 -07:00
40 changed files with 216 additions and 111 deletions

View File

@@ -7,6 +7,7 @@
#include <crypto/chacha20.h>
#include <crypto/chacha20poly1305.h>
#include <span.h>
#include <util/byte_units.h>
#include <cstddef>
#include <cstdint>
@@ -15,7 +16,7 @@
/* Number of bytes to process per iteration */
static const uint64_t BUFFER_SIZE_TINY = 64;
static const uint64_t BUFFER_SIZE_SMALL = 256;
static const uint64_t BUFFER_SIZE_LARGE = 1024*1024;
static const uint64_t BUFFER_SIZE_LARGE{1_MiB};
static void CHACHA20(benchmark::Bench& bench, size_t buffersize)
{

View File

@@ -7,6 +7,7 @@
#include <cstddef>
#include <cstdint>
#include <util/byte_units.h>
#include <vector>
#define ASIZE 2048
@@ -15,7 +16,7 @@
static void BenchLockedPool(benchmark::Bench& bench)
{
void *synth_base = reinterpret_cast<void*>(0x08000000);
const size_t synth_size = 1024*1024;
const size_t synth_size{1_MiB};
Arena b(synth_base, synth_size, 16);
std::vector<void*> addr{ASIZE, nullptr};

View File

@@ -6,6 +6,7 @@
#include <bench/bench.h>
#include <crypto/poly1305.h>
#include <span.h>
#include <util/byte_units.h>
#include <cstddef>
#include <cstdint>
@@ -14,7 +15,7 @@
/* Number of bytes to process per iteration */
static constexpr uint64_t BUFFER_SIZE_TINY = 64;
static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
static constexpr uint64_t BUFFER_SIZE_LARGE = 1024*1024;
static constexpr uint64_t BUFFER_SIZE_LARGE{1_MiB};
static void POLY1305(benchmark::Bench& bench, size_t buffersize)
{