Skip to content

Commit

Permalink
use Boost.Endian for endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Feb 12, 2024
1 parent bacc644 commit 5663210
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 36 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function(boost_json_setup_properties target)
Boost::container_hash
Boost::core
Boost::describe
Boost::endian
Boost::mp11
Boost::system
Boost::throw_exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef BOOST_JSON_DETAIL_CHARCONV_DETAIL_FASTFLOAT_ASCII_NUMBER_HPP
#define BOOST_JSON_DETAIL_CHARCONV_DETAIL_FASTFLOAT_ASCII_NUMBER_HPP

#include <boost/endian/conversion.hpp>
#include <boost/json/detail/charconv/detail/fast_float/float_common.hpp>
#include <cctype>
#include <cstdint>
Expand Down Expand Up @@ -46,10 +47,7 @@ uint64_t read_u64(const char *chars) {
}
uint64_t val;
::memcpy(&val, chars, sizeof(uint64_t));
#ifdef BOOST_JSON_BIG_ENDIAN
// Need to read as-if the number was in little-endian order.
val = byteswap(val);
#endif
endian::little_to_native_inplace(val);
return val;
}

Expand All @@ -63,10 +61,7 @@ void write_u64(uint8_t *chars, uint64_t val) {
}
return;
}
#ifdef BOOST_JSON_BIG_ENDIAN
// Need to read as-if the number was in little-endian order.
val = byteswap(val);
#endif
endian::native_to_little_inplace(val);
::memcpy(chars, &val, sizeof(uint64_t));
}

Expand Down
18 changes: 0 additions & 18 deletions include/boost/json/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,6 @@
# endif
#endif


#if ! defined(BOOST_JSON_BIG_ENDIAN) && ! defined(BOOST_JSON_LITTLE_ENDIAN)
// Copied from Boost.Endian
# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define BOOST_JSON_LITTLE_ENDIAN
# elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define BOOST_JSON_BIG_ENDIAN
# elif defined(__LITTLE_ENDIAN__)
# define BOOST_JSON_LITTLE_ENDIAN
# elif defined(__BIG_ENDIAN__)
# define BOOST_JSON_BIG_ENDIAN
# elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
# define BOOST_JSON_LITTLE_ENDIAN
# else
# error The Boost.JSON library could not determine the endianness of this platform. Define either BOOST_JSON_BIG_ENDIAN or BOOST_JSON_LITTLE_ENDIAN.
# endif
#endif

#if defined(__cpp_constinit) && __cpp_constinit >= 201907L
# define BOOST_JSON_CONSTINIT constinit
#elif defined(__has_cpp_attribute) && defined(__clang__)
Expand Down
5 changes: 1 addition & 4 deletions include/boost/json/detail/sse2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ inline uint64_t parse_unsigned( uint64_t r, char const * p, std::size_t n ) noex
#else
uint32_t v;
std::memcpy( &v, p, 4 );
endian::native_to_little_inplace(v);

v -= 0x30303030;

Expand All @@ -335,11 +336,7 @@ inline uint64_t parse_unsigned( uint64_t r, char const * p, std::size_t n ) noex
unsigned w2 = (v >> 16) & 0xFF;
unsigned w3 = (v >> 24);

#ifdef BOOST_JSON_BIG_ENDIAN
r = (((r * 10 + w3) * 10 + w2) * 10 + w1) * 10 + w0;
#else
r = (((r * 10 + w0) * 10 + w1) * 10 + w2) * 10 + w3;
#endif
#endif
p += 4;
n -= 4;
Expand Down
8 changes: 2 additions & 6 deletions include/boost/json/detail/utf8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef BOOST_JSON_DETAIL_UTF8_HPP
#define BOOST_JSON_DETAIL_UTF8_HPP

#include <boost/endian/conversion.hpp>
#include <boost/json/detail/config.hpp>

#include <cstddef>
Expand All @@ -26,12 +27,7 @@ load_little_endian(void const* p)
{
std::uint32_t v = 0;
std::memcpy(&v, p, N);
#ifdef BOOST_JSON_BIG_ENDIAN
v = ((v & 0xFF000000) >> 24) |
((v & 0x00FF0000) >> 8) |
((v & 0x0000FF00) << 8) |
((v & 0x000000FF) << 24);
#endif
endian::little_to_native_inplace(v);
return v;
}

Expand Down
1 change: 1 addition & 0 deletions test/cmake-subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_subdirectory(../../../container boostorg/container)
add_subdirectory(../../../container_hash boostorg/container_hash)
add_subdirectory(../../../core boostorg/core)
add_subdirectory(../../../describe boostorg/describe)
add_subdirectory(../../../endian boostorg/endian)
add_subdirectory(../../../intrusive boostorg/intrusive)
add_subdirectory(../../../json boostorg/json)
add_subdirectory(../../../move boostorg/move)
Expand Down

0 comments on commit 5663210

Please sign in to comment.