22 #ifndef DCCLConstants20091211H
23 #define DCCLConstants20091211H
29 #include <boost/type_traits/is_integral.hpp>
30 #include <boost/type_traits/is_floating_point.hpp>
31 #include <boost/utility/enable_if.hpp>
33 #include <google/protobuf/message.h>
34 #include <google/protobuf/descriptor.h>
36 #include "dccl/bitset.h"
41 inline unsigned floor_bits2bytes(
unsigned bits)
45 inline unsigned ceil_bits2bytes(
unsigned bits)
47 enum { BYTE_MASK = 7 };
48 return (bits& BYTE_MASK) ?
49 floor_bits2bytes(bits) + 1 :
50 floor_bits2bytes(bits);
55 typedef google::protobuf::uint32
uint32;
57 typedef google::protobuf::int32
int32;
59 typedef google::protobuf::uint64
uint64;
61 typedef google::protobuf::int64
int64;
63 const unsigned BITS_IN_BYTE = 8;
65 inline std::ostream& operator<<(std::ostream& out,
69 << msg.GetDescriptor()->name()
70 <<
"]] " << msg.DebugString());
73 template<
typename Float>
75 {
return std::floor(d + 0.5); }
81 template<
typename Float>
82 typename boost::enable_if<boost::is_floating_point<Float>, Float>::type round(Float value,
int precision)
84 Float scaling = std::pow(10.0, precision);
85 return round(value*scaling)/scaling;
92 template <
typename Float>
93 typename boost::enable_if<boost::is_floating_point<Float>, Float>::type quantize(Float value,
double interval)
96 return round(value / interval) * interval;
99 double interval_inv = 1.0 / interval;
100 return round(value * interval_inv) / interval_inv;
105 template<
typename Int>
106 Int abs(Int i) {
return (i < 0) ? -i : i; }
112 template<
typename Int>
113 typename boost::enable_if<boost::is_integral<Int>, Int>::type round(Int value,
int precision)
122 Int scaling = (Int)std::pow(10.0, -precision);
123 Int remainder = value % scaling;
126 if(remainder >= scaling/2)
137 template<
typename Int>
138 typename boost::enable_if<boost::is_integral<Int>, Int>::type quantize(Int value,
double interval)
140 if((interval-
static_cast<uint64_t
>(interval)) >= std::numeric_limits<double>::epsilon())
146 Int remainder = value %
static_cast<Int
>(interval);
148 if(remainder >= interval/2)