Bitcoin Core  26.1.0
P2P Digital Currency
serfloat_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <hash.h>
6 #include <test/util/random.h>
8 #include <util/serfloat.h>
9 #include <serialize.h>
10 #include <streams.h>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 #include <cmath>
15 #include <limits>
16 
17 BOOST_FIXTURE_TEST_SUITE(serfloat_tests, BasicTestingSetup)
18 
19 namespace {
20 
21 uint64_t TestDouble(double f) {
22  uint64_t i = EncodeDouble(f);
23  double f2 = DecodeDouble(i);
24  if (std::isnan(f)) {
25  // NaN is not guaranteed to round-trip exactly.
26  BOOST_CHECK(std::isnan(f2));
27  } else {
28  // Everything else is.
29  BOOST_CHECK(!std::isnan(f2));
30  uint64_t i2 = EncodeDouble(f2);
31  BOOST_CHECK_EQUAL(f, f2);
32  BOOST_CHECK_EQUAL(i, i2);
33  }
34  return i;
35 }
36 
37 } // namespace
38 
39 BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
40  BOOST_CHECK_EQUAL(TestDouble(0.0), 0U);
41  BOOST_CHECK_EQUAL(TestDouble(-0.0), 0x8000000000000000);
42  BOOST_CHECK_EQUAL(TestDouble(std::numeric_limits<double>::infinity()), 0x7ff0000000000000U);
43  BOOST_CHECK_EQUAL(TestDouble(-std::numeric_limits<double>::infinity()), 0xfff0000000000000);
44  BOOST_CHECK_EQUAL(TestDouble(0.5), 0x3fe0000000000000ULL);
45  BOOST_CHECK_EQUAL(TestDouble(1.0), 0x3ff0000000000000ULL);
46  BOOST_CHECK_EQUAL(TestDouble(2.0), 0x4000000000000000ULL);
47  BOOST_CHECK_EQUAL(TestDouble(4.0), 0x4010000000000000ULL);
48  BOOST_CHECK_EQUAL(TestDouble(785.066650390625), 0x4088888880000000ULL);
49 
50  // Roundtrip test on IEC559-compatible systems
51  if (std::numeric_limits<double>::is_iec559) {
52  BOOST_CHECK_EQUAL(sizeof(double), 8U);
53  BOOST_CHECK_EQUAL(sizeof(uint64_t), 8U);
54  // Test extreme values
55  TestDouble(std::numeric_limits<double>::min());
56  TestDouble(-std::numeric_limits<double>::min());
57  TestDouble(std::numeric_limits<double>::max());
58  TestDouble(-std::numeric_limits<double>::max());
59  TestDouble(std::numeric_limits<double>::lowest());
60  TestDouble(-std::numeric_limits<double>::lowest());
61  TestDouble(std::numeric_limits<double>::quiet_NaN());
62  TestDouble(-std::numeric_limits<double>::quiet_NaN());
63  TestDouble(std::numeric_limits<double>::signaling_NaN());
64  TestDouble(-std::numeric_limits<double>::signaling_NaN());
65  TestDouble(std::numeric_limits<double>::denorm_min());
66  TestDouble(-std::numeric_limits<double>::denorm_min());
67  // Test exact encoding: on currently supported platforms, EncodeDouble
68  // should produce exactly the same as the in-memory representation for non-NaN.
69  for (int j = 0; j < 1000; ++j) {
70  // Iterate over 9 specific bits exhaustively; the others are chosen randomly.
71  // These specific bits are the sign bit, and the 2 top and bottom bits of
72  // exponent and mantissa in the IEEE754 binary64 format.
73  for (int x = 0; x < 512; ++x) {
74  uint64_t v = InsecureRandBits(64);
75  v &= ~(uint64_t{1} << 0);
76  if (x & 1) v |= (uint64_t{1} << 0);
77  v &= ~(uint64_t{1} << 1);
78  if (x & 2) v |= (uint64_t{1} << 1);
79  v &= ~(uint64_t{1} << 50);
80  if (x & 4) v |= (uint64_t{1} << 50);
81  v &= ~(uint64_t{1} << 51);
82  if (x & 8) v |= (uint64_t{1} << 51);
83  v &= ~(uint64_t{1} << 52);
84  if (x & 16) v |= (uint64_t{1} << 52);
85  v &= ~(uint64_t{1} << 53);
86  if (x & 32) v |= (uint64_t{1} << 53);
87  v &= ~(uint64_t{1} << 61);
88  if (x & 64) v |= (uint64_t{1} << 61);
89  v &= ~(uint64_t{1} << 62);
90  if (x & 128) v |= (uint64_t{1} << 62);
91  v &= ~(uint64_t{1} << 63);
92  if (x & 256) v |= (uint64_t{1} << 63);
93  double f;
94  memcpy(&f, &v, 8);
95  uint64_t v2 = TestDouble(f);
96  if (!std::isnan(f)) BOOST_CHECK_EQUAL(v, v2);
97  }
98  }
99  }
100 }
101 
102 /*
103 Python code to generate the below hashes:
104 
105  def reversed_hex(x):
106  return bytes(reversed(x)).hex()
107 
108  def dsha256(x):
109  return hashlib.sha256(hashlib.sha256(x).digest()).digest()
110 
111  reversed_hex(dsha256(b''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
112 */
114 {
115  DataStream ss{};
116  // encode
117  for (int i = 0; i < 1000; i++) {
118  ss << EncodeDouble(i);
119  }
120  BOOST_CHECK(Hash(ss) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
121 
122  // decode
123  for (int i = 0; i < 1000; i++) {
124  uint64_t val;
125  ss >> val;
126  double j = DecodeDouble(val);
127  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
128  }
129 }
130 
uint64_t EncodeDouble(double f) noexcept
Definition: serfloat.cpp:37
Basic testing setup.
Definition: setup_common.h:49
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:192
BOOST_AUTO_TEST_SUITE_END()
uint256 uint256S(const char *str)
Definition: uint256.h:119
double DecodeDouble(uint64_t v) noexcept
Definition: serfloat.cpp:10
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
static uint64_t InsecureRandBits(int bits)
Definition: random.h:55
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:76
BOOST_AUTO_TEST_CASE(double_serfloat_tests)
#define BOOST_CHECK(expr)
Definition: object.cpp:17