DCCL v4
common.h
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef DCCLConstants20091211H
23 #define DCCLConstants20091211H
24 
25 #include <iostream>
26 #include <cmath>
27 #include <limits>
28 
29 #include <boost/type_traits/is_integral.hpp>
30 #include <boost/type_traits/is_floating_point.hpp>
31 #include <boost/utility/enable_if.hpp>
32 
33 #include <google/protobuf/message.h>
34 #include <google/protobuf/descriptor.h>
35 
36 #include "dccl/bitset.h"
37 
38 
39 namespace dccl
40 {
41  inline unsigned floor_bits2bytes(unsigned bits)
42  { return bits >> 3; }
43  // more efficient way to do ceil(total_bits / 8)
44  // to get the number of bytes rounded up.
45  inline unsigned ceil_bits2bytes(unsigned bits)
46  {
47  enum { BYTE_MASK = 7 }; // 00000111
48  return (bits& BYTE_MASK) ?
49  floor_bits2bytes(bits) + 1 :
50  floor_bits2bytes(bits);
51  }
52 
53  // use the Google Protobuf types as they handle system quirks already
55  typedef google::protobuf::uint32 uint32;
57  typedef google::protobuf::int32 int32;
59  typedef google::protobuf::uint64 uint64;
61  typedef google::protobuf::int64 int64;
62 
63  const unsigned BITS_IN_BYTE = 8;
64 
65  inline std::ostream& operator<<(std::ostream& out,
66  const google::protobuf::Message& msg)
67  {
68  return (out << "[["
69  << msg.GetDescriptor()->name()
70  << "]] " << msg.DebugString());
71  }
72 
73  template<typename Float>
74  Float round(Float d)
75  { return std::floor(d + 0.5); }
76 
81  template<typename Float>
82  typename boost::enable_if<boost::is_floating_point<Float>, Float>::type round(Float value, int precision)
83  {
84  Float scaling = std::pow(10.0, precision);
85  return round(value*scaling)/scaling;
86  }
87 
92  template <typename Float>
93  typename boost::enable_if<boost::is_floating_point<Float>, Float>::type quantize(Float value, double interval)
94  {
95  if(interval >= 1)
96  return round(value / interval) * interval;
97  else
98  {
99  double interval_inv = 1.0 / interval;
100  return round(value * interval_inv) / interval_inv;
101  }
102  }
103 
104  // C++98 has no long long overload for abs
105  template<typename Int>
106  Int abs(Int i) { return (i < 0) ? -i : i; }
107 
112  template<typename Int>
113  typename boost::enable_if<boost::is_integral<Int>, Int>::type round(Int value, int precision)
114  {
115  if(precision >= 0)
116  {
117  // doesn't mean anything to round an integer to positive precision
118  return value;
119  }
120  else
121  {
122  Int scaling = (Int)std::pow(10.0, -precision);
123  Int remainder = value % scaling;
124 
125  value -= remainder;
126  if(remainder >= scaling/2)
127  value += scaling;
128 
129  return value;
130  }
131  }
132 
137  template<typename Int>
138  typename boost::enable_if<boost::is_integral<Int>, Int>::type quantize(Int value, double interval)
139  {
140  if((interval-static_cast<uint64_t>(interval)) >= std::numeric_limits<double>::epsilon())
141  {
142  // doesn't mean anything to quantize an integer with a fractional interval
143  return value;
144  }
145 
146  Int remainder = value % static_cast<Int>(interval);
147  value -= remainder;
148  if(remainder >= interval/2)
149  value += interval;
150  return value;
151  }
152 }
153 #endif
dccl::int32
google::protobuf::int32 int32
a signed 32 bit integer
Definition: common.h:57
dccl
Dynamic Compact Control Language namespace.
Definition: gen_units_class_plugin.h:49
dccl::uint64
google::protobuf::uint64 uint64
an unsigned 64 bit integer
Definition: common.h:59
dccl::uint32
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition: common.h:55
dccl::int64
google::protobuf::int64 int64
a signed 64 bit integer
Definition: common.h:61
Message