Skip to content

Commit e40224d

Browse files
MarcoFalkesipa
authored andcommitted
Remove unused float serialization
1 parent b295395 commit e40224d

File tree

6 files changed

+15
-69
lines changed

6 files changed

+15
-69
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ BITCOIN_CORE_H = \
264264
util/tokenpipe.h \
265265
util/trace.h \
266266
util/translation.h \
267+
util/types.h \
267268
util/ui_change_type.h \
268269
util/url.h \
269270
util/vector.h \

src/serialize.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <prevector.h>
2525
#include <span.h>
26+
#include <util/types.h>
2627

2728
/**
2829
* The maximum size of a serialized object in bytes or number of elements
@@ -129,27 +130,13 @@ inline uint64_t ser_double_to_uint64(double x)
129130
static_assert(sizeof(tmp) == sizeof(x), "double and uint64_t assumed to have the same size");
130131
return tmp;
131132
}
132-
inline uint32_t ser_float_to_uint32(float x)
133-
{
134-
uint32_t tmp;
135-
std::memcpy(&tmp, &x, sizeof(x));
136-
static_assert(sizeof(tmp) == sizeof(x), "float and uint32_t assumed to have the same size");
137-
return tmp;
138-
}
139133
inline double ser_uint64_to_double(uint64_t y)
140134
{
141135
double tmp;
142136
std::memcpy(&tmp, &y, sizeof(y));
143137
static_assert(sizeof(tmp) == sizeof(y), "double and uint64_t assumed to have the same size");
144138
return tmp;
145139
}
146-
inline float ser_uint32_to_float(uint32_t y)
147-
{
148-
float tmp;
149-
std::memcpy(&tmp, &y, sizeof(y));
150-
static_assert(sizeof(tmp) == sizeof(y), "float and uint32_t assumed to have the same size");
151-
return tmp;
152-
}
153140

154141

155142
/////////////////////////////////////////////////////////////////
@@ -234,7 +221,7 @@ template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_wri
234221
template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
235222
template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
236223
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
237-
template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
224+
template<typename Stream> inline void Serialize(Stream& s, float a ) { static_assert(ALWAYS_FALSE<Stream>, "Not implemented"); }
238225
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
239226
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
240227
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
@@ -252,7 +239,7 @@ template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a =
252239
template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
253240
template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
254241
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
255-
template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
242+
template<typename Stream> inline void Unserialize(Stream& s, float& a ) { static_assert(ALWAYS_FALSE<Stream>, "Not implemented"); }
256243
template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
257244
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
258245
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }

src/test/fuzz/float.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,4 @@ FUZZ_TARGET(float)
2727
stream >> d_deserialized;
2828
assert(d == d_deserialized);
2929
}
30-
31-
{
32-
const float f = fuzzed_data_provider.ConsumeFloatingPoint<float>();
33-
(void)memusage::DynamicUsage(f);
34-
assert(ser_uint32_to_float(ser_float_to_uint32(f)) == f);
35-
36-
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
37-
stream << f;
38-
float f_deserialized;
39-
stream >> f_deserialized;
40-
assert(f == f_deserialized);
41-
}
4230
}

src/test/fuzz/util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noe
513513
WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
514514
WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
515515
WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
516-
WRITE_TO_STREAM_CASE(float, fuzzed_data_provider.ConsumeFloatingPoint<float>()),
517516
WRITE_TO_STREAM_CASE(double, fuzzed_data_provider.ConsumeFloatingPoint<double>()),
518517
WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
519518
WRITE_TO_STREAM_CASE(std::vector<char>, ConsumeRandomLengthIntegralVector<char>(fuzzed_data_provider)));
@@ -545,7 +544,6 @@ void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) no
545544
READ_FROM_STREAM_CASE(uint32_t),
546545
READ_FROM_STREAM_CASE(int64_t),
547546
READ_FROM_STREAM_CASE(uint64_t),
548-
READ_FROM_STREAM_CASE(float),
549547
READ_FROM_STREAM_CASE(double),
550548
READ_FROM_STREAM_CASE(std::string),
551549
READ_FROM_STREAM_CASE(std::vector<char>));

src/test/serialize_tests.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ BOOST_AUTO_TEST_CASE(sizes)
7070
BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
7171
BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
7272
BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
73-
BOOST_CHECK_EQUAL(sizeof(float), GetSerializeSize(float(0), 0));
7473
BOOST_CHECK_EQUAL(sizeof(double), GetSerializeSize(double(0), 0));
7574
// Bool is serialized as char
7675
BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0));
@@ -85,30 +84,10 @@ BOOST_AUTO_TEST_CASE(sizes)
8584
BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4U);
8685
BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
8786
BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U);
88-
BOOST_CHECK_EQUAL(GetSerializeSize(float(0), 0), 4U);
8987
BOOST_CHECK_EQUAL(GetSerializeSize(double(0), 0), 8U);
9088
BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
9189
}
9290

93-
BOOST_AUTO_TEST_CASE(floats_conversion)
94-
{
95-
// Choose values that map unambiguously to binary floating point to avoid
96-
// rounding issues at the compiler side.
97-
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x00000000), 0.0F);
98-
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f000000), 0.5F);
99-
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f800000), 1.0F);
100-
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40000000), 2.0F);
101-
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40800000), 4.0F);
102-
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x44444444), 785.066650390625F);
103-
104-
BOOST_CHECK_EQUAL(ser_float_to_uint32(0.0F), 0x00000000U);
105-
BOOST_CHECK_EQUAL(ser_float_to_uint32(0.5F), 0x3f000000U);
106-
BOOST_CHECK_EQUAL(ser_float_to_uint32(1.0F), 0x3f800000U);
107-
BOOST_CHECK_EQUAL(ser_float_to_uint32(2.0F), 0x40000000U);
108-
BOOST_CHECK_EQUAL(ser_float_to_uint32(4.0F), 0x40800000U);
109-
BOOST_CHECK_EQUAL(ser_float_to_uint32(785.066650390625F), 0x44444444U);
110-
}
111-
11291
BOOST_AUTO_TEST_CASE(doubles_conversion)
11392
{
11493
// Choose values that map unambiguously to binary floating point to avoid
@@ -135,26 +114,8 @@ Python code to generate the below hashes:
135114
def dsha256(x):
136115
return hashlib.sha256(hashlib.sha256(x).digest()).digest()
137116
138-
reversed_hex(dsha256(''.join(struct.pack('<f', x) for x in range(0,1000)))) == '8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c'
139117
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
140118
*/
141-
BOOST_AUTO_TEST_CASE(floats)
142-
{
143-
CDataStream ss(SER_DISK, 0);
144-
// encode
145-
for (int i = 0; i < 1000; i++) {
146-
ss << float(i);
147-
}
148-
BOOST_CHECK(Hash(ss) == uint256S("8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c"));
149-
150-
// decode
151-
for (int i = 0; i < 1000; i++) {
152-
float j;
153-
ss >> j;
154-
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
155-
}
156-
}
157-
158119
BOOST_AUTO_TEST_CASE(doubles)
159120
{
160121
CDataStream ss(SER_DISK, 0);

src/util/types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 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+
#ifndef BITCOIN_UTIL_TYPES_H
6+
#define BITCOIN_UTIL_TYPES_H
7+
8+
template <class>
9+
inline constexpr bool ALWAYS_FALSE{false};
10+
11+
#endif // BITCOIN_UTIL_TYPES_H

0 commit comments

Comments
 (0)