Skip to content

Commit cf96de4

Browse files
committed
Optimize be::unsafe::store(uint256)
1 parent cd92f5c commit cf96de4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

include/intx/intx.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,25 @@ inline void store(uint8_t* dst, const T& x) noexcept
20742074
const auto d = to_big_endian(x);
20752075
std::memcpy(dst, &d, sizeof(d));
20762076
}
2077+
2078+
/// Specialization for uint256.
2079+
inline void store(uint8_t* dst, const uint256& x) noexcept
2080+
{
2081+
// Store byte-swapped words in primitive temporaries. This helps with memory aliasing
2082+
// and GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107837
2083+
// TODO: Use std::byte instead of uint8_t.
2084+
const auto v0 = to_big_endian(x[0]);
2085+
const auto v1 = to_big_endian(x[1]);
2086+
const auto v2 = to_big_endian(x[2]);
2087+
const auto v3 = to_big_endian(x[3]);
2088+
2089+
// Store words in reverse (big-endian) order, write addresses are ascending.
2090+
std::memcpy(dst, &v3, sizeof(v3));
2091+
std::memcpy(dst + 8, &v2, sizeof(v2));
2092+
std::memcpy(dst + 16, &v1, sizeof(v1));
2093+
std::memcpy(dst + 24, &v0, sizeof(v0));
2094+
}
2095+
20772096
} // namespace unsafe
20782097

20792098
} // namespace be

0 commit comments

Comments
 (0)