1#define KBLIB_DEF_MACROS 1
12static_assert(CHAR_BIT == 8,
"8-bit bytes");
13static_assert(
sizeof(short) == 2,
"16-bit shorts");
14static_assert(
sizeof(int) == 4,
"32-bit ints");
15static_assert(
sizeof(long) == 8,
"64-bit longs");
17static_assert(std::is_same<kblib::uint_smallest_t<0>,
unsigned char>::value,
19static_assert(std::is_same<kblib::uint_smallest_t<1>,
unsigned char>::value,
21static_assert(std::is_same<kblib::uint_smallest_t<255>,
unsigned char>::value,
23static_assert(std::is_same<kblib::uint_smallest_t<256>,
unsigned short>::value,
26 std::is_same<kblib::uint_smallest_t<32768>,
unsigned short>::value,
29 std::is_same<kblib::uint_smallest_t<65535>,
unsigned short>::value,
31static_assert(std::is_same<kblib::uint_smallest_t<65536>,
unsigned int>::value,
34 std::is_same<kblib::uint_smallest_t<1u << 31u>,
unsigned int>::value,
37 std::is_same<kblib::uint_smallest_t<UINT_MAX>,
unsigned int>::value,
40 std::is_same<kblib::uint_smallest_t<1ul << 32u>,
unsigned long>::value,
43 std::is_same<kblib::uint_smallest_t<1ul << 63u>,
unsigned long>::value,
47static_assert(std::is_same<kblib::int_smallest_t<1>,
signed char>::value,
49static_assert(std::is_same<kblib::int_smallest_t<127>,
signed char>::value,
51static_assert(std::is_same<kblib::int_smallest_t<256>,
signed short>::value,
53static_assert(std::is_same<kblib::int_smallest_t<32767>,
signed short>::value,
55static_assert(std::is_same<kblib::int_smallest_t<32768>,
signed int>::value,
57static_assert(std::is_same<kblib::int_smallest_t<65536>,
signed int>::value,
59static_assert(std::is_same<kblib::int_smallest_t<INT_MAX>,
signed int>::value,
68 REQUIRE(
test.insert({0b1000100010000000, 10}, 1));
71 REQUIRE(not +
"test.at({0b1000100010000000, 10}) has UB");
78 template <
typename T,
typename std::enable_if<
79 std::is_trivially_copyable<T>::value,
int>::type
83 std::memcpy(&t,
buf,
sizeof(T));
87 std::vector<char>
buf;
108#if __cpp_lib_is_pointer_interconvertible
109static_assert(std::is_pointer_interconvertible_with_class(&
Addr1::raw));
111 std::is_pointer_interconvertible_with_class(&
decltype(
Addr1::raw)::raw_));
115 static_assert(
sizeof(
Addr1) == 2,
"");
119 REQUIRE(
a.raw == 0b0001'1001'1100'1100);
120 REQUIRE(
a.cX() == 0b0'1100);
121 REQUIRE(
a.cY == 0b0'1110);
122 REQUIRE(
a.nt == 0b10);
123 REQUIRE(
a.fY == 0b001);
149 static_assert(
sizeof(
Addr) == 2,
"");
151 static_assert(
a.raw == 0b0001'1001'1100'1100,
"");
152 static_assert(
a.cX() == 0b0'1100,
"");
153 static_assert(
a.cY() == 0b0'1110,
"");
154 static_assert(
a.nt() == 0b10,
"");
155 static_assert(
a.fY() == 0b001,
"");
157 static_assert(Addr::get_cX_v<
a.raw> == 0b0'1100,
"");
158 static_assert(Addr::get_cY_v<
a.raw> == 0b0'1110,
"");
159 static_assert(Addr::get_nt_v<
a.raw> == 0b10,
"");
160 static_assert(Addr::get_fY_v<
a.raw> == 0b001,
"");
162 constexpr Addr b = {Addr::set_h_v<0b001'1001, Addr::set_l_v<0b1100'1100>>};
164 static_assert(Addr::get_cX_v<b.
raw> == 0b0'1100,
"");
165 static_assert(Addr::get_cY_v<b.
raw> == 0b0'1110,
"");
166 static_assert(Addr::get_nt_v<b.
raw> == 0b10,
"");
167 static_assert(Addr::get_fY_v<b.
raw> == 0b001,
"");
180 std::uintptr_t ival = 0xABCD;
184 REQUIRE(pun.
val == ival);
185 std::array<char16_t,
sizeof(
char*) / 2> ustr{u
'\xABCD', u
'\0'};
186 decltype(ustr) t = pun.
s;
188 REQUIRE(
decltype(ustr)(pun.
s) == ustr);
193 REQUIRE(pun.
val == kblib::byte_cast<std::uintptr_t>(c));
198 char16_t[
sizeof(
char*) / 2]>
200 void(kblib::get<0>(pun));
203 std::uintptr_t ival = 0xABCD;
205 REQUIRE(
std::equal(std::begin(pun.get<0>()), std::begin(pun.get<0>()) + 4,
207 REQUIRE(pun.get<2>() == ival);
208 std::array<char16_t,
sizeof(
char*) / 2> ustr{u
'\xABCD', u
'\0'};
209 decltype(ustr) t = pun.get<3>();
211 REQUIRE(
decltype(ustr)(pun.get<3>()) == ustr);
216 REQUIRE(pun.get<2>() == kblib::byte_cast<std::uintptr_t>(c));
KBLIB_NODISCARD constexpr auto test_bitfield() -> Addr
Provides bit-manipulation functions and classes.
#define BITFIELD(offset, size, name, raw)
Provides generic facilities for hashing data, and aliases for standard unordered containers using the...
constexpr auto test() noexcept -> bool
constexpr auto a(const std::initializer_list< T > &a) -> auto
Index an array literal without naming its type.
typename int_smallest< I >::type int_smallest_t
constexpr auto to_bytes_le(Integral ival, CharT(&dest)[sizeof(Integral)]) noexcept -> void
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
KBLIB_NODISCARD auto operator[](std::size_t p) -> ret_proxy
Implements a bitfield abstraction. May be used in a union with other bitfields.
#define KBLIB_NODISCARD
This internal macro is used to provide a fallback for [[nodiscard]] in C++14.
bitfield< 12, 3, uint16_t > fY
bitfield< 0, 16, uint16_t > raw
bitfield< 0, 5, uint16_t > cX
bitfield< 5, 5, uint16_t > cY
bitfield< 0, 14, uint16_t > addr
bitfield< 10, 2, uint16_t > nt
bitfield< 8, 7, uint16_t > h
bitfield< 0, 8, uint16_t > l
kblib::union_pun< std::uintptr_t, &punner::storage > val
kblib::union_pun< char16_t[sizeof(char *)/2], &punner::storage > s
char storage[sizeof(char *)]
kblib::union_pun< const char *, &punner::storage > ptr