Bitcoin ABC 0.32.4
P2P Digital Currency
assumptions.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2019 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6// Compile-time verification of assumptions we make.
7
8#ifndef BITCOIN_COMPAT_ASSUMPTIONS_H
9#define BITCOIN_COMPAT_ASSUMPTIONS_H
10
11#include <climits>
12#include <cstddef>
13#include <cstdint>
14#include <limits>
15#include <type_traits>
16
17// Assumption: We assume the floating-point types to fulfill the requirements of
18// IEC 559 (IEEE 754) standard.
19// Example(s): Floating-point division by zero in ConnectBlock,
20// CreateTransaction
21// and EstimateMedianVal.
22static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 float assumed");
23static_assert(std::numeric_limits<double>::is_iec559,
24 "IEEE 754 double assumed");
25
26// Assumption: We assume floating-point widths.
27// Example(s): Type punning in serialization code
28// (ser_{float,double}_to_uint{32,64}).
29static_assert(sizeof(float) == 4, "32-bit float assumed");
30static_assert(sizeof(double) == 8, "64-bit double assumed");
31
32// Assumption: We assume integer widths.
33// Example(s): GetSizeOfCompactSize and WriteCompactSize in the serialization
34// code.
35static_assert(sizeof(short) == 2, "16-bit short assumed");
36static_assert(sizeof(int) == 4, "32-bit int assumed");
37static_assert(sizeof(unsigned) == 4, "32-bit unsigned assumed");
38
39// Assumption: We assume 8-bit bytes, because 32-bit int and 16-bit short are
40// assumed.
41static_assert(CHAR_BIT == 8, "8-bit bytes assumed");
42
43// Assumption: We assume uint8_t is an alias of unsigned char.
44// char, unsigned char, and std::byte (C++17) are the only "byte types"
45// according to the C++ Standard. "byte type" means a type that can be used to
46// observe an object's value representation. We use uint8_t everywhere to see
47// bytes, so we have to ensure that uint8_t is an alias to a "byte type".
48// http://eel.is/c++draft/basic.types
49// http://eel.is/c++draft/basic.memobj#def:byte
50// http://eel.is/c++draft/expr.sizeof#1
51// http://eel.is/c++draft/cstdint#syn
52static_assert(std::is_same<uint8_t, unsigned char>::value,
53 "uint8_t is an alias of unsigned char");
54
55// Assumption: We assume size_t to be 32-bit or 64-bit.
56// Example(s): size_t assumed to be at least 32-bit in
57// ecdsa_signature_parse_der_lax(...).
58// size_t assumed to be 32-bit or 64-bit in MallocUsage(...).
59static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8,
60 "size_t assumed to be 32-bit or 64-bit");
61static_assert(sizeof(size_t) == sizeof(void *),
62 "Sizes of size_t and void* assumed to be equal");
63
64// Some important things we are NOT assuming (non-exhaustive list):
65// * We are NOT assuming a specific value for std::endian::native.
66// * We are NOT assuming a specific value for std::locale("").name().
67// * We are NOT assuming a specific value for
68// std::numeric_limits<char>::is_signed.
69
79static_assert((int64_t(-1) >> 1) == int64_t(-1),
80 "Arithmetic right shift assumed");
81
86static_assert((int64_t(-10) & 0xffff) == 0xfff6, "2-complement assumed");
87
91static_assert(std::numeric_limits<long long int>::max() ==
92 std::numeric_limits<int64_t>::max());
93static_assert(std::numeric_limits<long long int>::min() ==
94 std::numeric_limits<int64_t>::min());
95
96#endif // BITCOIN_COMPAT_ASSUMPTIONS_H