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