26 #ifndef DCCLConstants20091211H
27 #define DCCLConstants20091211H
32 #include <type_traits>
34 #include <google/protobuf/descriptor.h>
35 #include <google/protobuf/message.h>
41 inline unsigned floor_bits2bytes(
unsigned bits) {
return bits >> 3; }
44 inline unsigned ceil_bits2bytes(
unsigned bits)
50 return (bits & BYTE_MASK) ? floor_bits2bytes(bits) + 1 : floor_bits2bytes(bits);
55 typedef google::protobuf::uint32
uint32;
57 using int32 = google::protobuf::int32;
59 using uint64 = google::protobuf::uint64;
61 using int64 = google::protobuf::int64;
63 const unsigned BITS_IN_BYTE = 8;
67 return (out <<
"[[" << msg.GetDescriptor()->name() <<
"]] " << msg.DebugString());
70 template <
typename Float> Float round(Float d) {
return std::floor(d + 0.5); }
76 template <
typename Float>
77 typename std::enable_if<std::is_floating_point<Float>::value, Float>::type round(Float value,
80 Float scaling = std::pow(10.0, precision);
81 return round(value * scaling) / scaling;
88 template <
typename Float>
89 typename std::enable_if<std::is_floating_point<Float>::value, Float>::type quantize(Float value,
93 return round(value / interval) * interval;
96 double interval_inv = 1.0 / interval;
97 return round(value * interval_inv) / interval_inv;
102 template <
typename Int> Int abs(Int i) {
return (i < 0) ? -i : i; }
108 template <
typename Int>
109 typename std::enable_if<std::is_integral<Int>::value, Int>::type round(Int value,
int precision)
118 Int scaling = (Int)std::pow(10.0, -precision);
119 Int remainder = value % scaling;
122 if (remainder >= scaling / 2)
133 template <
typename Int>
134 typename std::enable_if<std::is_integral<Int>::value, Int>::type quantize(Int value,
137 if ((interval -
static_cast<uint64_t
>(interval)) >= std::numeric_limits<double>::epsilon())
143 Int remainder = value %
static_cast<Int
>(interval);
145 if (remainder >= interval / 2)