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,
72 REQUIRE(not +
"test.at({0b1000100010000000, 10}) has UB");
79 template <
typename T,
typename std::enable_if<
84 std::memcpy(&t,
buf,
sizeof(T));
88 std::vector<char>
buf;
109#if __cpp_lib_is_pointer_interconvertible
110static_assert(std::is_pointer_interconvertible_with_class(&
Addr1::raw));
112 std::is_pointer_interconvertible_with_class(&
decltype(
Addr1::raw)::raw_));
116 static_assert(
sizeof(
Addr1) == 2,
"");
120 REQUIRE(
a.raw == 0b0001'1001'1100'1100);
150 static_assert(
sizeof(
Addr) == 2,
"");
152 static_assert(
a.raw == 0b0001'1001'1100'1100,
"");
153 static_assert(
a.cX() == 0b0'1100,
"");
154 static_assert(
a.cY() == 0b0'1110,
"");
155 static_assert(
a.nt() == 0b10,
"");
156 static_assert(
a.fY() == 0b001,
"");
158 static_assert(Addr::get_cX_v<
a.raw> == 0b0'1100,
"");
159 static_assert(Addr::get_cY_v<
a.raw> == 0b0'1110,
"");
160 static_assert(Addr::get_nt_v<
a.raw> == 0b10,
"");
161 static_assert(Addr::get_fY_v<
a.raw> == 0b001,
"");
163 constexpr Addr b = {Addr::set_h_v<0b001'1001, Addr::set_l_v<0b1100'1100>>};
165 static_assert(Addr::get_cX_v<b.
raw> == 0b0'1100,
"");
166 static_assert(Addr::get_cY_v<b.
raw> == 0b0'1110,
"");
167 static_assert(Addr::get_nt_v<b.
raw> == 0b10,
"");
168 static_assert(Addr::get_fY_v<b.
raw> == 0b001,
"");
181 std::uintptr_t ival = 0xABCD;
186 std::array<char16_t,
sizeof(
char*) / 2> ustr{u
'\xABCD', u
'\0'};
187 decltype(ustr) t = pun.
s;
189 REQUIRE(
decltype(ustr)(pun.
s) == ustr);
194 REQUIRE(pun.
val == kblib::byte_cast<std::uintptr_t>(c));
199 char16_t[
sizeof(
char*) / 2]>
201 void(kblib::get<0>(pun));
204 std::uintptr_t ival = 0xABCD;
209 std::array<char16_t,
sizeof(
char*) / 2> ustr{u
'\xABCD', u
'\0'};
210 decltype(ustr) t = pun.get<3>();
212 REQUIRE(
decltype(ustr)(pun.get<3>()) == ustr);
217 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
GeneratorWrapper< T > value(T &&value)
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