Skip to content

Commit

Permalink
Add detail/reverse.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Oct 24, 2024
1 parent 798fd1a commit f4db0bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions include/boost/hash2/detail/reverse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef BOOST_HASH2_DETAIL_REVERSE_HPP_INCLUDED
#define BOOST_HASH2_DETAIL_REVERSE_HPP_INCLUDED

// Copyright 2024 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <cstdint>
#include <cstring>

namespace boost
{
namespace hash2
{
namespace detail
{

template<std::size_t N> void reverse( unsigned char (&d)[ N ], void const* s )
{
unsigned char const* s2 = static_cast<unsigned char const*>( s );

for( std::size_t i = 0; i < N; ++i )
{
d[ i ] = s2[ N-1-i ];
}
}

} // namespace detail
} // namespace hash2
} // namespace boost

#endif // #ifndef BOOST_HASH2_DETAIL_REVERSE_HPP_INCLUDED
4 changes: 2 additions & 2 deletions include/boost/hash2/hash_append.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/hash2/is_contiguously_hashable.hpp>
#include <boost/hash2/get_integral_result.hpp>
#include <boost/hash2/endian.hpp>
#include <boost/hash2/detail/reverse.hpp>
#include <boost/container_hash/is_range.hpp>
#include <boost/container_hash/is_contiguous_range.hpp>
#include <boost/container_hash/is_unordered_range.hpp>
Expand Down Expand Up @@ -153,9 +154,8 @@ template<class Hash, class Flavor, class T>
constexpr auto N = sizeof(T);

unsigned char tmp[ N ];
std::memcpy( tmp, &v, N );

std::reverse( tmp, tmp + N );
detail::reverse( tmp, &v );

hash2::hash_append_range( h, f, tmp, tmp + N );
}
Expand Down

0 comments on commit f4db0bf

Please sign in to comment.