Bitcoin ABC  0.24.10
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 <cstdint>
13 #include <limits>
14 #include <type_traits>
15 
16 // Assumption: We assume that the macro NDEBUG is not defined.
17 // Example(s): We use assert(...) extensively with the assumption of it never
18 // being a noop at runtime.
19 #if defined(NDEBUG)
20 #error "Bitcoin cannot be compiled without assertions."
21 #endif
22 
23 // Assumption: We assume a C++17 (ISO/IEC 14882:2017) compiler (minimum
24 // requirement).
25 // Example(s): We assume the presence of C++17 features everywhere :-)
26 // ISO Standard C++17 [cpp.predefined]p1:
27 // "The name __cplusplus is defined to the value 201703L when compiling a C++
28 // translation unit."
29 static_assert(__cplusplus >= 201703L, "C++17 standard assumed");
30 
31 // Assumption: We assume the floating-point types to fulfill the requirements of
32 // IEC 559 (IEEE 754) standard.
33 // Example(s): Floating-point division by zero in ConnectBlock,
34 // CreateTransaction
35 // and EstimateMedianVal.
36 static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 float assumed");
37 static_assert(std::numeric_limits<double>::is_iec559,
38  "IEEE 754 double assumed");
39 
40 // Assumption: We assume floating-point widths.
41 // Example(s): Type punning in serialization code
42 // (ser_{float,double}_to_uint{32,64}).
43 static_assert(sizeof(float) == 4, "32-bit float assumed");
44 static_assert(sizeof(double) == 8, "64-bit double assumed");
45 
46 // Assumption: We assume integer widths.
47 // Example(s): GetSizeOfCompactSize and WriteCompactSize in the serialization
48 // code.
49 static_assert(sizeof(short) == 2, "16-bit short assumed");
50 static_assert(sizeof(int) == 4, "32-bit int assumed");
51 static_assert(sizeof(unsigned) == 4, "32-bit unsigned assumed");
52 
53 // Assumption: We assume 8-bit bytes, because 32-bit int and 16-bit short are
54 // assumed.
55 static_assert(CHAR_BIT == 8, "8-bit bytes assumed");
56 
57 // Assumption: We assume uint8_t is an alias of unsigned char.
58 // char, unsigned char, and std::byte (C++17) are the only "byte types"
59 // according to the C++ Standard. "byte type" means a type that can be used to
60 // observe an object's value representation. We use uint8_t everywhere to see
61 // bytes, so we have to ensure that uint8_t is an alias to a "byte type".
62 // http://eel.is/c++draft/basic.types
63 // http://eel.is/c++draft/basic.memobj#def:byte
64 // http://eel.is/c++draft/expr.sizeof#1
65 // http://eel.is/c++draft/cstdint#syn
66 static_assert(std::is_same<uint8_t, unsigned char>::value,
67  "uint8_t is an alias of unsigned char");
68 
69 // Assumption: We assume size_t to be 32-bit or 64-bit.
70 // Example(s): size_t assumed to be at least 32-bit in
71 // ecdsa_signature_parse_der_lax(...).
72 // size_t assumed to be 32-bit or 64-bit in MallocUsage(...).
73 static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8,
74  "size_t assumed to be 32-bit or 64-bit");
75 static_assert(sizeof(size_t) == sizeof(void *),
76  "Sizes of size_t and void* assumed to be equal");
77 
78 // Some important things we are NOT assuming (non-exhaustive list):
79 // * We are NOT assuming a specific value for std::endian::native.
80 // * We are NOT assuming a specific value for std::locale("").name().
81 // * We are NOT assuming a specific value for
82 // std::numeric_limits<char>::is_signed.
83 
93 static_assert((int64_t(-1) >> 1) == int64_t(-1),
94  "Arithmetic right shift assumed");
95 
100 static_assert((int64_t(-10) & 0xffff) == 0xfff6, "2-complement assumed");
101 
105 static_assert(std::numeric_limits<long long int>::max() ==
106  std::numeric_limits<int64_t>::max());
107 static_assert(std::numeric_limits<long long int>::min() ==
108  std::numeric_limits<int64_t>::min());
109 
110 #endif // BITCOIN_COMPAT_ASSUMPTIONS_H