Skip to content

Commit

Permalink
styles: format
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 29, 2023
1 parent 9fc6c54 commit f0fa087
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 151 deletions.
166 changes: 95 additions & 71 deletions include/bitops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,111 @@

#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#include <bit>
namespace json::__bitops {
using std::countl_one;
using std::countr_one;
using std::countl_zero;
using std::countr_zero;
inline constexpr bool is_little_endian() {
return std::endian::native == std::endian::little;
}
namespace json::__bitops
{
using std::countl_one;
using std::countl_zero;
using std::countr_one;
using std::countr_zero;
inline constexpr bool is_little_endian()
{
return std::endian::native == std::endian::little;
}
}
#else
#include <cstdint>
namespace json::__bitops {
namespace json::__bitops
{
#if defined(__GNUC__) || defined(__clang__)
inline constexpr int countl_zero(uint32_t x) {
if constexpr (sizeof(uint32_t) == sizeof(unsigned int))
return x == 0 ? 32 : __builtin_clz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long))
return x == 0 ? 32 : __builtin_clzl(x);
return x == 0 ? 32 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint32_t x) {
if constexpr (sizeof(uint32_t) == sizeof(unsigned int))
return x == 0 ? 32 : __builtin_ctz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long))
return x == 0 ? 32 : __builtin_ctzl(x);
return x == 0 ? 32 : __builtin_ctzll(x);
}
inline constexpr int countl_zero(uint64_t x) {
return x == 0 ? 64 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint64_t x) {
return x == 0 ? 64 : __builtin_ctzll(x);
}
inline constexpr int countl_zero(uint32_t x)
{
if constexpr (sizeof(uint32_t) == sizeof(unsigned int)) return x == 0 ? 32 : __builtin_clz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long)) return x == 0 ? 32 : __builtin_clzl(x);
return x == 0 ? 32 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint32_t x)
{
if constexpr (sizeof(uint32_t) == sizeof(unsigned int)) return x == 0 ? 32 : __builtin_ctz(x);
if constexpr (sizeof(uint32_t) == sizeof(unsigned long)) return x == 0 ? 32 : __builtin_ctzl(x);
return x == 0 ? 32 : __builtin_ctzll(x);
}
inline constexpr int countl_zero(uint64_t x)
{
return x == 0 ? 64 : __builtin_clzll(x);
}
inline constexpr int countr_zero(uint64_t x)
{
return x == 0 ? 64 : __builtin_ctzll(x);
}
#elif defined(_MSC_VER)
#ifdef __AVX2__
// lzcnt intrinsics is not constexpr
inline int countl_zero(uint32_t x) {
return __lzcnt(x);
}
inline int countr_zero(uint32_t x) {
return _tzcnt_u32(x);
}
inline int countl_zero(uint64_t x) {
return (int)__lzcnt64(x);
}
inline int countr_zero(uint64_t x) {
return (int)_tzcnt_u64(x);
}
// lzcnt intrinsics is not constexpr
inline int countl_zero(uint32_t x)
{
return __lzcnt(x);
}
inline int countr_zero(uint32_t x)
{
return _tzcnt_u32(x);
}
inline int countl_zero(uint64_t x)
{
return (int)__lzcnt64(x);
}
inline int countr_zero(uint64_t x)
{
return (int)_tzcnt_u64(x);
}
#else
inline constexpr int countl_zero(uint32_t x) {
unsigned long index = 0;
return _BitScanReverse(&index, x) ? 31 - index : 32;
}
inline constexpr int countr_zero(uint32_t x) {
unsigned long index = 0;
return _BitScanForward(&index, x) ? index : 32;
}
inline constexpr int countl_zero(uint64_t x) {
unsigned long index = 0;
return _BitScanReverse64(&index, x) ? 63 - index : 64;
}
inline constexpr int countr_zero(uint64_t x) {
unsigned long index = 0;
return _BitScanForward64(&index, x) ? index : 64;
}
inline constexpr int countl_zero(uint32_t x)
{
unsigned long index = 0;
return _BitScanReverse(&index, x) ? 31 - index : 32;
}
inline constexpr int countr_zero(uint32_t x)
{
unsigned long index = 0;
return _BitScanForward(&index, x) ? index : 32;
}
inline constexpr int countl_zero(uint64_t x)
{
unsigned long index = 0;
return _BitScanReverse64(&index, x) ? 63 - index : 64;
}
inline constexpr int countr_zero(uint64_t x)
{
unsigned long index = 0;
return _BitScanForward64(&index, x) ? index : 64;
}
#endif // __AVX2__
#else // compiler
#else // compiler
#error "bring your own bit counting implementation"
#endif
inline int countl_one(uint32_t x) { return countl_zero(~x); }
inline int countr_one(uint32_t x) { return countr_zero(~x); }
inline int countl_one(uint64_t x) { return countl_zero(~x); }
inline int countr_one(uint64_t x) { return countr_zero(~x); }
inline int countl_one(uint32_t x)
{
return countl_zero(~x);
}
inline int countr_one(uint32_t x)
{
return countr_zero(~x);
}
inline int countl_one(uint64_t x)
{
return countl_zero(~x);
}
inline int countr_one(uint64_t x)
{
return countr_zero(~x);
}

// no constexpr endian awareness before C++20
inline bool is_little_endian() {
union {
uint32_t u32;
uint8_t u8;
} u = { 0x01020304 };
return u.u8 == 4;
}
// no constexpr endian awareness before C++20
inline bool is_little_endian()
{
union {
uint32_t u32;
uint8_t u8;
} u = { 0x01020304 };
return u.u8 == 4;
}
} // namespace json::__bitops
#endif // C++20
87 changes: 48 additions & 39 deletions include/packed_bytes.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#pragma once

#include <cstdint>
#include <cstring>
#include <type_traits>

#include "bitops.hpp"

#if defined(__GNUC__) || defined(__clang__)
#define __packed_bytes_strong_inline __attribute__((always_inline))
#define __packed_bytes_strong_inline __attribute__((always_inline))
#elif defined(_MSC_VER)
#define __packed_bytes_strong_inline __forceinline
#else
#define __packed_bytes_strong_inline inline
#endif

struct packed_bytes_trait_none {
struct packed_bytes_trait_none
{
static constexpr bool available = false;
};

template <size_t N>
struct packed_bytes {
struct packed_bytes
{
using traits = packed_bytes_trait_none;
};

Expand All @@ -27,100 +31,105 @@ struct packed_bytes {
#include "packed_bytes_arm.hpp"
#endif

struct packed_bytes_trait_uint64 {
struct packed_bytes_trait_uint64
{
static constexpr bool available = sizeof(void*) >= 8;
static constexpr auto step = 8;
using value_type = std::enable_if_t<sizeof(void*) >= 8, uint64_t>;

__packed_bytes_strong_inline static value_type load_unaligned(const void *ptr) {
__packed_bytes_strong_inline static value_type load_unaligned(const void* ptr)
{
value_type result;
memcpy((void*)&result, ptr, 8);
return result;
}

__packed_bytes_strong_inline static value_type less(value_type x, uint8_t n) {
return (((x) - UINT64_C(0x0101010101010101) * (n)) & ~(x) & UINT64_C(0x8080808080808080));
}

__packed_bytes_strong_inline static value_type is_zero_memberwise(value_type v) {
return (((v) - UINT64_C(0x0101010101010101)) & ~(v) & UINT64_C(0x8080808080808080));
__packed_bytes_strong_inline static value_type less(value_type x, uint8_t n)
{
return (((x)-UINT64_C(0x0101010101010101) * (n)) & ~(x)&UINT64_C(0x8080808080808080));
}

__packed_bytes_strong_inline static bool is_all_zero(value_type v)
__packed_bytes_strong_inline static value_type is_zero_memberwise(value_type v)
{
return v == UINT64_C(0);
return (((v)-UINT64_C(0x0101010101010101)) & ~(v)&UINT64_C(0x8080808080808080));
}

__packed_bytes_strong_inline static value_type equal(value_type x, uint8_t n) {
__packed_bytes_strong_inline static bool is_all_zero(value_type v) { return v == UINT64_C(0); }

__packed_bytes_strong_inline static value_type equal(value_type x, uint8_t n)
{
return is_zero_memberwise((x) ^ (UINT64_C(0x0101010101010101) * (n)));
}

__packed_bytes_strong_inline static value_type bitwise_or(value_type a, value_type b) {
return a | b;
}
__packed_bytes_strong_inline static value_type bitwise_or(value_type a, value_type b) { return a | b; }

__packed_bytes_strong_inline static size_t first_nonzero_byte(value_type x) {
__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;
else
return json::__bitops::countl_zero(x) / 8;
}
};

struct packed_bytes_trait_uint32 {
struct packed_bytes_trait_uint32
{
static constexpr bool available = true;
static constexpr auto step = 4;
using value_type = uint32_t;

__packed_bytes_strong_inline static value_type load_unaligned(const void *ptr) {
__packed_bytes_strong_inline static value_type load_unaligned(const void* ptr)
{
value_type result;
memcpy((void*)&result, ptr, 4);
return result;
}

__packed_bytes_strong_inline static value_type less(value_type x, uint8_t n) {
__packed_bytes_strong_inline static value_type less(value_type x, uint8_t n)
{
return (((x) - ~UINT32_C(0) / 255 * (n)) & ~(x) & ~UINT32_C(0) / 255 * 128);
}

__packed_bytes_strong_inline static value_type is_zero_memberwise(value_type v) {
return (((v) - UINT32_C(0x01010101)) & ~(v) & UINT32_C(0x80808080));;
__packed_bytes_strong_inline static value_type is_zero_memberwise(value_type v)
{
return (((v)-UINT32_C(0x01010101)) & ~(v)&UINT32_C(0x80808080));
;
}

__packed_bytes_strong_inline static bool is_all_zero(value_type v) {
return v == UINT32_C(0);
}
__packed_bytes_strong_inline static bool is_all_zero(value_type v) { return v == UINT32_C(0); }

__packed_bytes_strong_inline static value_type equal(value_type x, uint8_t n) {
__packed_bytes_strong_inline static value_type equal(value_type x, uint8_t n)
{
return is_zero_memberwise((x) ^ (~UINT32_C(0) / 255 * (n)));
}

__packed_bytes_strong_inline static value_type bitwise_or(value_type a, value_type b) {
return a | b;
}
__packed_bytes_strong_inline static value_type bitwise_or(value_type a, value_type b) { return a | b; }

__packed_bytes_strong_inline static size_t first_nonzero_byte(value_type x) {
__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;
else
return json::__bitops::countl_zero(x) / 8;
}
};
template <>
struct packed_bytes<8> {
struct packed_bytes<8>
{
using traits = std::enable_if_t<packed_bytes_trait_uint64::available, packed_bytes_trait_uint64>;
};

template <>
struct packed_bytes<4> {
struct packed_bytes<4>
{
using traits = packed_bytes_trait_uint32;
};

template <size_t N>
using packed_bytes_trait = typename packed_bytes<N>::traits;

using packed_bytes_trait_max = std::conditional_t<packed_bytes_trait<32>::available, packed_bytes_trait<32>,
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>
>>>;

using packed_bytes_trait_max =
std::conditional_t<packed_bytes_trait<32>::available, packed_bytes_trait<32>,
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>>>>;
Loading

0 comments on commit f0fa087

Please sign in to comment.