32 #include <netinet/in.h>
33 #include "globals.hpp"
43 #if __BYTE_ORDER == __BIG_ENDIAN
45 inline uint64_t hton64(uint64_t x) {
49 inline uint64_t ntoh64(uint64_t x) {
57 union { T t; uint32_t i; };
58 i = htobe32(
reinterpret_cast<uint32_t&
>(x));
62 union { T t; uint64_t i; };
63 i = htobe64(
reinterpret_cast<uint64_t&
>(x));
67 throw std::domain_error(
"can't handle hton for element of size: " + std::to_string(
sizeof(T)));
72 inline Global::dtime hton(Global::dtime x) {
73 auto i = htobe64(x.time_since_epoch().count());
74 auto d = Global::dtime::duration{i};
75 return Global::dtime(d);
84 #elif __BYTE_ORDER == __LITTLE_ENDIAN
86 inline uint64_t hton64(
size_t x) {
90 inline uint64_t ntoh64(
size_t x) {
105 #error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
109 std::string printBuf(
const char* buf,
unsigned len);
111 inline unsigned getAlignedLength(
size_t n,
unsigned nbyteAlignment) {
112 auto rem = n % nbyteAlignment;
113 return rem ? n + nbyteAlignment - rem : n;
116 bool operator<(
const sockaddr_in& sa1,
const sockaddr_in& sa2);
118 inline size_t ltostr_r(
long val,
char* buf,
size_t sz) {
124 size_t e = std::abs(
val) <= 1 ? 0 : log10(std::abs(
val));
132 throw std::range_error(
"ltostr_r: buffer too small");
135 buf[idx] = (
val % 10) +
'0';
143 bool readNumber(
const char*& s,
const char* e,
int& n,
bool dosign=
false);
151 template<
typename T,
typename U,
typename R>
153 inline R operator()(
const T& t,
const U& u)
const {
158 template<
typename T,
typename U,
typename R>
160 inline R operator()(
const T& t,
const U& u)
const {
165 template<
typename T,
typename U,
typename R>
167 inline R operator()(
const T& t,
const U& u)
const {
172 template<
typename T,
typename U,
typename R>
174 inline R operator()(
const T& t,
const U& u)
const {
179 template<
typename T,
typename U,
typename R>
181 inline R operator()(
const T& t,
const U& u)
const {
188 inline T operator()(
const T& t,
const T& u)
const {
189 if (std::isnan(t) || std::isnan(u)) {
192 return std::max(t, u);
198 inline T operator()(
const T& t,
const T& u)
const {
199 if (std::isnan(t) || std::isnan(u)) {
202 return std::min(t, u);
209 static inline std::function<T()> GenNbFun() {
211 return [=]()
mutable {
return ++count; };
214 inline std::function<std::string(std::string)> GensymFun() {
217 [=](std::string s)
mutable {
218 std::stringstream ss;
225 template <
typename Int>
226 constexpr Int next_power2(Int i) {
229 while (i > 0) { n <<= 1; i >>= 1; }