memcmp.hpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef BOOST_HASH2_DETAIL_MEMCMP_HPP_INCLUDED
  2. #define BOOST_HASH2_DETAIL_MEMCMP_HPP_INCLUDED
  3. // Copyright 2024 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/hash2/detail/is_constant_evaluated.hpp>
  7. #include <boost/config.hpp>
  8. #include <cstring>
  9. namespace boost
  10. {
  11. namespace hash2
  12. {
  13. namespace detail
  14. {
  15. #if defined(BOOST_NO_CXX14_CONSTEXPR)
  16. BOOST_FORCEINLINE int memcmp( unsigned char const* p, unsigned char const* q, std::size_t n ) noexcept
  17. {
  18. return std::memcmp( p, q, n );
  19. }
  20. #else
  21. constexpr int memcmp( unsigned char const* p, unsigned char const* q, std::size_t n ) noexcept
  22. {
  23. if( !detail::is_constant_evaluated() )
  24. {
  25. return std::memcmp( p, q, n );
  26. }
  27. else
  28. {
  29. for( std::size_t i = 0; i < n; ++i )
  30. {
  31. if( p[ i ] != q[ i ] ) return p[ i ] - q[ i ];
  32. }
  33. return 0;
  34. }
  35. }
  36. #endif
  37. } // namespace detail
  38. } // namespace hash2
  39. } // namespace boost
  40. #endif // #ifndef BOOST_HASH2_DETAIL_MEMCMP_HPP_INCLUDED