Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Jul 12, 2023
1 parent 0a7860f commit f8f0834
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 92 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function(boost_json_setup_properties target)
Boost::container
Boost::core
Boost::describe
Boost::endian
Boost::mp11
Boost::system
Boost::throw_exception
Expand Down
7 changes: 4 additions & 3 deletions include/boost/json/basic_parser_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,8 @@ parse_escaped(
{
// KRYSTIAN TODO: this could be done
// with fewer instructions
digit = detail::load_little_endian<4>(
cs.begin() + 1);
digit = endian::endian_load<int, 4, endian::order::little>(
reinterpret_cast<unsigned char const*>(cs.begin() + 1) );
int d4 = detail::hex_digit(static_cast<
unsigned char>(digit >> 24));
int d3 = detail::hex_digit(static_cast<
Expand Down Expand Up @@ -1366,7 +1366,8 @@ parse_escaped(
return fail(cs.begin(), error::syntax, &loc);
}
++cs;
digit = detail::load_little_endian<4>(cs.begin());
digit = endian::endian_load<int, 4, endian::order::little>(
reinterpret_cast<unsigned char const*>(cs.begin()) );
d4 = detail::hex_digit(static_cast<
unsigned char>(digit >> 24));
d3 = detail::hex_digit(static_cast<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ uint64_t read_u64(const char *chars) {
}
return val;
}
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
uint64_t val = endian::load_little_u64(
reinterpret_cast<unsigned char const*>(chars) );
return val;
}

Expand All @@ -63,11 +59,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
::memcpy(chars, &val, sizeof(uint64_t));
endian::store_little_u64( reinterpret_cast<unsigned char*>(chars), val );
}

// credit @aqrit
Expand Down
19 changes: 1 addition & 18 deletions include/boost/json/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <boost/config.hpp>
#include <boost/assert.hpp>
#include <boost/endian/conversion.hpp>
#include <boost/throw_exception.hpp>
#include <cstdint>
#include <type_traits>
Expand Down Expand Up @@ -244,24 +245,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
9 changes: 2 additions & 7 deletions include/boost/json/detail/sse2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,16 @@ inline uint64_t parse_unsigned( uint64_t r, char const * p, std::size_t n ) noex
r = r * 10 + p[2] - '0';
r = r * 10 + p[3] - '0';
#else
uint32_t v;
std::memcpy( &v, p, 4 );

uint32_t v = endian::load_little_u32(
reinterpret_cast<unsigned char const*>(p) );
v -= 0x30303030;

unsigned w0 = v & 0xFF;
unsigned w1 = (v >> 8) & 0xFF;
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
31 changes: 9 additions & 22 deletions include/boost/json/detail/utf8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ namespace boost {
namespace json {
namespace detail {

template<int N>
std::uint32_t
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
return v;
}

inline
uint16_t
classify_utf8(char c)
Expand Down Expand Up @@ -74,6 +59,8 @@ inline
bool
is_valid_utf8(const char* p, uint16_t first)
{
auto const up = reinterpret_cast<unsigned char const*>(p);

uint32_t v;
switch(first >> 8)
{
Expand All @@ -82,37 +69,37 @@ is_valid_utf8(const char* p, uint16_t first)

// 2 bytes, second byte [80, BF]
case 1:
v = load_little_endian<2>(p);
v = endian::endian_load<uint32_t, 2, endian::order::little>(up);
return (v & 0xC000) == 0x8000;

// 3 bytes, second byte [A0, BF]
case 2:
v = load_little_endian<3>(p);
v = endian::endian_load<uint32_t, 3, endian::order::little>(up);
return (v & 0xC0E000) == 0x80A000;

// 3 bytes, second byte [80, BF]
case 3:
v = load_little_endian<3>(p);
v = endian::endian_load<uint32_t, 3, endian::order::little>(up);
return (v & 0xC0C000) == 0x808000;

// 3 bytes, second byte [80, 9F]
case 4:
v = load_little_endian<3>(p);
v = endian::endian_load<uint32_t, 3, endian::order::little>(up);
return (v & 0xC0E000) == 0x808000;

// 4 bytes, second byte [90, BF]
case 5:
v = load_little_endian<4>(p);
v = endian::endian_load<uint32_t, 4, endian::order::little>(up);
return (v & 0xC0C0FF00) + 0x7F7F7000 <= 0x2F00;

// 4 bytes, second byte [80, BF]
case 6:
v = load_little_endian<4>(p);
v = endian::endian_load<uint32_t, 4, endian::order::little>(up);
return (v & 0xC0C0C000) == 0x80808000;

// 4 bytes, second byte [80, 8F]
case 7:
v = load_little_endian<4>(p);
v = endian::endian_load<uint32_t, 4, endian::order::little>(up);
return (v & 0xC0C0F000) == 0x80808000;
}
}
Expand Down
31 changes: 0 additions & 31 deletions test/utf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,6 @@ namespace json {
class utf8_test
{
public:
void
testLoadLittleEndian()
{
BOOST_TEST(
detail::load_little_endian<4>("\x01\x02\x03\x04\xFF")
== 0x04030201);

BOOST_TEST(
detail::load_little_endian<4>("\x12\x34\x56\x78\xFF")
== 0x78563412);

BOOST_TEST(
detail::load_little_endian<4>("\xFE\xDC\xBA\x98\xFF")
== 0x98BADCFE);

BOOST_TEST(
detail::load_little_endian<3>("\x12\x45\xFE\xFF")
== 0x00FE4512);

BOOST_TEST(
detail::load_little_endian<3>("\xE0\xA0\x80\xFF")
== 0x0080A0E0);

BOOST_TEST(
detail::load_little_endian<2>("\x37\xFC\xFF")
== 0x0000FC37);

BOOST_TEST(detail::load_little_endian<1>("\xF1\xFF") == 0x000000F1);
}

void
testClassifyUtf8()
{
Expand Down Expand Up @@ -166,7 +136,6 @@ class utf8_test
void
run()
{
testLoadLittleEndian();
testClassifyUtf8();
testIsValidUtf8();
testUtf8Sequence();
Expand Down

0 comments on commit f8f0834

Please sign in to comment.