// Copyright (c) 2025-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_UTIL_BYTE_UNITS_H #define BITCOIN_UTIL_BYTE_UNITS_H #include #include #include namespace util::detail { template consteval uint64_t ByteUnitsToBytes(unsigned long long units) { const auto bytes{CheckedLeftShift(units, SHIFT)}; if (!bytes || *bytes > std::numeric_limits::max()) { throw std::overflow_error("Too large"); } return *bytes; } } // namespace util::detail /// Conversion of MiB to bytes. consteval uint64_t operator""_MiB(unsigned long long mebibytes) { return util::detail::ByteUnitsToBytes<20>(mebibytes); } /// Conversion of GiB to bytes. consteval uint64_t operator""_GiB(unsigned long long gibibytes) { return util::detail::ByteUnitsToBytes<30>(gibibytes); } #endif // BITCOIN_UTIL_BYTE_UNITS_H