13 #include <type_traits> 16 #ifdef __SIZEOF_INT128__ 17 #define BITCPY_INT128_CONDITIONAL_DEFINE(...) __VA_ARGS__ 19 #define BITCPY_INT128_CONDITIONAL_DEFINE_C(...) __VA_ARGS__ static_assert(true, "") 22 #define BITCPY_INT128_CONDITIONAL_DEFINE(...) 24 #define BITCPY_INT128_CONDITIONAL_DEFINE_C(...) static_assert(true, "") 31 namespace bitprint_detail
36 using type =
typename std::make_signed<T>::type;
38 BITCPY_INT128_CONDITIONAL_DEFINE_C(
40 struct make_signed<__uint128_t> {
41 using type = __int128_t;
46 using type =
typename std::make_unsigned<T>::type;
48 BITCPY_INT128_CONDITIONAL_DEFINE_C(
50 struct make_unsigned<__uint128_t> {
51 using type = __uint128_t;
54 struct make_unsigned<__int128_t> {
55 using type = __uint128_t;
58 struct make_unsigned<float>
60 using type = uint32_t;
63 struct make_unsigned<double>
65 using type = uint64_t;
67 template <
bool use_brackets>
68 void print_bracket_newline(
const bool add_newline)
71 printf((add_newline ?
"}\n" :
"}"));
80 inline void printhex(
const uint8_t data,
const bool add_newline =
true)
83 printf(
"0x%02" PRIX8
"\n", data);
85 printf(
"0x%02" PRIX8, data);
91 inline void printhex(
const uint16_t data,
const bool add_newline =
true)
94 printf(
"0x%04" PRIX16
"\n", data);
96 printf(
"0x%04" PRIX16, data);
102 inline void printhex(
const uint32_t data,
const bool add_newline =
true)
105 printf(
"0x%08" PRIX32
"\n", data);
107 printf(
"0x%08" PRIX32, data);
113 inline void printhex(
const uint64_t data,
const bool add_newline =
true)
116 printf(
"0x%08" PRIX32
"%08" PRIX32
"\n",
117 static_cast<uint32_t>(data >> 32),
118 static_cast<uint32_t>(data));
120 printf(
"0x%08" PRIX32
"%08" PRIX32,
121 static_cast<uint32_t>(data >> 32),
122 static_cast<uint32_t>(data));
124 BITCPY_INT128_CONDITIONAL_DEFINE_C(
127 inline void printhex(
const __uint128_t data,
const bool add_newline =
true)
130 printf(
"0x%08" PRIX32
"%08" PRIX32
"%08" PRIX32
"%08" PRIX32
"\n",
131 static_cast<uint32_t>(data >> 32 * 3),
132 static_cast<uint32_t>(data >> 32 * 2),
133 static_cast<uint32_t>(data >> 32 * 1),
134 static_cast<uint32_t>(data));
136 printf(
"0x%08" PRIX32
"%08" PRIX32
"%08" PRIX32
"%08" PRIX32,
137 static_cast<uint32_t>(data >> 32 * 3),
138 static_cast<uint32_t>(data >> 32 * 2),
139 static_cast<uint32_t>(data >> 32 * 1),
140 static_cast<uint32_t>(data));
146 inline void printhex(
const bool data,
const bool add_newline =
true)
148 printhex(uint8_t(data), add_newline);
154 template <
typename T = void,
155 typename std::enable_if<
156 std::is_signed<T>::value
157 BITCPY_INT128_CONDITIONAL_DEFINE(|| std::is_same<T, __int128_t>::value),
158 T>::type * =
nullptr>
159 void printhex(
const T data,
const bool add_newline =
true)
161 typedef typename bitprint_detail::make_unsigned<T>::type data_as_unsigned_type;
165 data_as_unsigned_type unsigned_value;
167 punned_data.original_value = data;
168 printhex(punned_data.unsigned_value, add_newline);
175 template <
bool use_brackets = true,
typename T =
void,
size_t N = 0u>
176 void printhex(
const T (&data)[N],
const bool add_newline =
true)
180 for (
size_t i = 0; i < N; i++)
183 printf(
"%s", (i + 1u < N ?
", " :
""));
185 bitprint_detail::print_bracket_newline<use_brackets>(add_newline);
192 template <
bool use_brackets = true,
typename T =
void,
size_t N = 0u>
193 void printbin(
const T (&data)[N],
const bool add_newline =
true)
197 for (
size_t i = 0; i < N; i++)
199 for (
size_t ii = 0; ii <
sizeof(T); ii++)
201 const uint8_t data_element =
static_cast<uint8_t
>(
202 (data[i] >> (
sizeof(T) * 8 - (ii + 1) * 8)) & 0xFFu);
203 printf(
"%c%c%c%c%c%c%c%c",
204 ((data_element & 0x80u) ?
'1' :
'0'),
205 ((data_element & 0x40u) ?
'1' :
'0'),
206 ((data_element & 0x20u) ?
'1' :
'0'),
207 ((data_element & 0x10u) ?
'1' :
'0'),
208 ((data_element & 0x08u) ?
'1' :
'0'),
209 ((data_element & 0x04u) ?
'1' :
'0'),
210 ((data_element & 0x02u) ?
'1' :
'0'),
211 ((data_element & 0x01u) ?
'1' :
'0'));
213 printf(
"%s", (i + 1u < N ?
", " :
""));
215 bitprint_detail::print_bracket_newline<use_brackets>(add_newline);
221 template <typename T, typename std::enable_if<!std::is_array<T>::value && !std::is_same<T, bool>::value, T>::type * =
nullptr>
222 void printbin(
const T data,
const bool add_newline =
true)
224 typedef const T data_as_array_type[1];
225 printbin<false>(*
reinterpret_cast<data_as_array_type *
>(&data), add_newline);
231 inline void printbin(
const bool data,
const bool add_newline =
true)
233 printbin(uint8_t(data), add_newline);
237 #endif // _BITPRINT_H_ void printbin(const T(&data)[N], const bool add_newline=true)
prints an array in binary format
Definition: bitprint.h:193
void printhex(const uint8_t data, const bool add_newline=true)
prints uint8_t value in hex format
Definition: bitprint.h:80
CppSerdes library namespace.
Definition: bitcpy_common.h:69