Skip to content

Commit

Permalink
chore: move packed_bytes to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 29, 2023
1 parent f0fa087 commit 9ebf292
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions include/bitops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#include <bit>
namespace json::__bitops
namespace json::_bitops
{
using std::countl_one;
using std::countl_zero;
Expand All @@ -15,7 +15,7 @@ inline constexpr bool is_little_endian()
}
#else
#include <cstdint>
namespace json::__bitops
namespace json::_bitops
{
#if defined(__GNUC__) || defined(__clang__)
inline constexpr int countl_zero(uint32_t x)
Expand Down Expand Up @@ -108,5 +108,5 @@ inline bool is_little_endian()
} u = { 0x01020304 };
return u.u8 == 4;
}
} // namespace json::__bitops
} // namespace json::_bitops
#endif // C++20
2 changes: 1 addition & 1 deletion include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class basic_object
// ****************************

template <typename string_t = default_string_t, typename parsing_t = void,
typename accel_traits = packed_bytes_trait_max>
typename accel_traits = _packed_bytes::packed_bytes_trait_max>
class parser
{
public:
Expand Down
19 changes: 13 additions & 6 deletions include/packed_bytes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define __packed_bytes_strong_inline inline
#endif

namespace json::_packed_bytes
{
struct packed_bytes_trait_none
{
static constexpr bool available = false;
Expand All @@ -24,13 +26,16 @@ struct packed_bytes
{
using traits = packed_bytes_trait_none;
};
}

#if defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP)
#include "packed_bytes_x86.hpp"
#elif defined(__ARM_NEON) || defined(_M_ARM) || defined(_M_ARM64)
#include "packed_bytes_arm.hpp"
#endif

namespace json::_packed_bytes
{
struct packed_bytes_trait_uint64
{
static constexpr bool available = sizeof(void*) >= 8;
Expand Down Expand Up @@ -65,10 +70,10 @@ struct packed_bytes_trait_uint64

__packed_bytes_strong_inline static size_t first_nonzero_byte(value_type x)
{
if (json::__bitops::is_little_endian())
return json::__bitops::countr_zero(x) / 8;
if (_bitops::is_little_endian())
return _bitops::countr_zero(x) / 8;
else
return json::__bitops::countl_zero(x) / 8;
return _bitops::countl_zero(x) / 8;
}
};

Expand Down Expand Up @@ -107,10 +112,10 @@ struct packed_bytes_trait_uint32

__packed_bytes_strong_inline static size_t first_nonzero_byte(value_type x)
{
if (json::__bitops::is_little_endian())
return json::__bitops::countr_zero(x) / 8;
if (_bitops::is_little_endian())
return _bitops::countr_zero(x) / 8;
else
return json::__bitops::countl_zero(x) / 8;
return _bitops::countl_zero(x) / 8;
}
};
template <>
Expand All @@ -133,3 +138,5 @@ using packed_bytes_trait_max =
std::conditional_t<packed_bytes_trait<16>::available, packed_bytes_trait<16>,
std::conditional_t<packed_bytes_trait<8>::available, packed_bytes_trait<8>,
packed_bytes_trait<4>>>>;

} // namespace json::_packed_bytes
6 changes: 4 additions & 2 deletions include/packed_bytes_arm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define __packed_bytes_trait_arm64
#endif

namespace json::_packed_bytes
{
struct packed_bytes_trait_neon
{
static constexpr bool available = true;
Expand Down Expand Up @@ -47,7 +49,7 @@ struct packed_bytes_trait_neon
auto cmp = equal(x, 0);
auto res = vshrn_n_u16(cmp, 4);
auto mask64 = vget_lane_u64(vreinterpret_u64_u8(res), 0);
return json::__bitops::countr_one(mask64) >> 2;
return _bitops::countr_one(mask64) >> 2;
}
};

Expand All @@ -56,5 +58,5 @@ struct packed_bytes<16>
{
using traits = packed_bytes_trait_neon;
};

}
#endif
11 changes: 8 additions & 3 deletions include/packed_bytes_x86.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <smmintrin.h>
#endif

namespace json::_packed_bytes
{
struct packed_bytes_trait_sse
{
static constexpr bool available = true;
Expand Down Expand Up @@ -54,7 +56,7 @@ struct packed_bytes_trait_sse
{
auto cmp = _mm_cmpeq_epi8(x, _mm_set1_epi8(0));
auto mask = (uint16_t)_mm_movemask_epi8(cmp);
return json::__bitops::countr_one((uint32_t)mask);
return _bitops::countr_one((uint32_t)mask);
}
};

Expand All @@ -63,10 +65,12 @@ struct packed_bytes<16>
{
using traits = packed_bytes_trait_sse;
};

}
#ifdef __AVX2__
#include <immintrin.h>

namespace json::_packed_bytes
{
struct packed_bytes_trait_avx2
{
static constexpr bool available = true;
Expand Down Expand Up @@ -107,7 +111,7 @@ struct packed_bytes_trait_avx2
auto cmp = _mm256_cmpeq_epi8(x, _mm256_set1_epi8(0));
auto mask = (uint32_t)_mm256_movemask_epi8(cmp);
// AVX512 alternative: _mm_cmpeq_epi8_mask
return json::__bitops::countr_one(mask);
return _bitops::countr_one(mask);
}
};

Expand All @@ -116,5 +120,6 @@ struct packed_bytes<32>
{
using traits = packed_bytes_trait_avx2;
};
}

#endif

0 comments on commit 9ebf292

Please sign in to comment.