Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Sep 15, 2022

No need to have a (naive) copy of the ReadLE64 logic inside uint256::GetUint64, when we have an optimized function for exactly that.

@davidgumberg
Copy link
Contributor

davidgumberg commented Sep 16, 2022

ACK 04fee75

The existing code assumes host endianness.

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 04fee75 review, this use of ReadLE64() is similar to the existing invocation by Num3072::Num3072(), sanity checked that before and after this change GetUint64() returns the same result (debug build, clang 13)

--- a/src/uint256.h
+++ b/src/uint256.h
@@ -7,6 +7,7 @@
 #include <crypto/common.h>
+#include <logging.h>
 #include <span.h>
 
@@ -85,7 +86,20 @@ public:
 
     uint64_t GetUint64(int pos) const
     {
-        return ReadLE64(m_data + pos * 8);
+        const uint8_t* ptr = m_data + pos * 8;
+        uint64_t x = ((uint64_t)ptr[0]) | \
+                     ((uint64_t)ptr[1]) << 8 | \
+                     ((uint64_t)ptr[2]) << 16 | \
+                     ((uint64_t)ptr[3]) << 24 | \
+                     ((uint64_t)ptr[4]) << 32 | \
+                     ((uint64_t)ptr[5]) << 40 | \
+                     ((uint64_t)ptr[6]) << 48 | \
+                     ((uint64_t)ptr[7]) << 56;
+        LogPrintf("GetUint64 old: %s\n", x);
+
+        const auto y = ReadLE64(m_data + pos * 8);
+        LogPrintf("GetUint64 new: %s\n", y);
+
+        assert(x == y);
+        return y;
     }
2022-09-16T09:13:53Z GetUint64 old: 2830827502690553650
2022-09-16T09:13:53Z GetUint64 new: 2830827502690553650
2022-09-16T09:13:53Z GetUint64 old: 13427380872610120290
2022-09-16T09:13:53Z GetUint64 new: 13427380872610120290
2022-09-16T09:13:53Z GetUint64 old: 12183277635375193944
2022-09-16T09:13:53Z GetUint64 new: 12183277635375193944
2022-09-16T09:13:53Z GetUint64 old: 16580367070613284548
2022-09-16T09:13:53Z GetUint64 new: 16580367070613284548
2022-09-16T09:13:53Z GetUint64 old: 16949092074016075712
2022-09-16T09:13:53Z GetUint64 new: 16949092074016075712
2022-09-16T09:13:53Z GetUint64 old: 14891467430351852074
2022-09-16T09:13:53Z GetUint64 new: 14891467430351852074
2022-09-16T09:13:53Z GetUint64 old: 2319828850847527094
2022-09-16T09:13:53Z GetUint64 new: 2319828850847527094
2022-09-16T09:13:53Z GetUint64 old: 921024485749193602
2022-09-16T09:13:53Z GetUint64 new: 921024485749193602

@fanquake fanquake merged commit 2530a24 into bitcoin:master Sep 16, 2022
@sipa
Copy link
Member Author

sipa commented Sep 16, 2022

The existing code assumes host endianness.

No, it does not. Both the old and new code interpret the bytes as little-endian.

@davidgumberg
Copy link
Contributor

The existing code assumes host endianness.

No, it does not. Both the old and new code interpret the bytes as little-endian.

Right, I meant that the old code interprets the bytes as little endian and stores them as little endian, even if the host machine is big-endian. ReadLE64 invokes le64toh which does not make an assumption about "host" endianness.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Sep 20, 2022
…plicating logic

04fee75 Use ReadLE64 in uint256::GetUint64() instead of duplicating logic (Pieter Wuille)

Pull request description:

  No need to have a (naive) copy of the `ReadLE64` logic inside `uint256::GetUint64`, when we have an optimized function for exactly that.

ACKs for top commit:
  davidgumberg:
    ACK 04fee75
  jonatack:
    ACK 04fee75 review, this use of ReadLE64() is similar to the existing invocation by Num3072::Num3072(), sanity checked that before and after this change GetUint64() returns the same result (debug build, clang 13)

Tree-SHA512: 0fc2681536a18d82408411bcc6d5c6445fb96793fa43ff4021cd2933d46514c725318da35884f428d1799023921f33f8af091ef428ceb96a50866ac53a345356
@bitcoin bitcoin locked and limited conversation to collaborators Sep 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants