summaryrefslogtreecommitdiff
path: root/num_util.hpp
diff options
context:
space:
mode:
authorJan Huwald <jh@sotun.de>2016-01-11 11:52:36 (GMT)
committerJan Huwald <jh@sotun.de>2016-01-11 12:19:06 (GMT)
commit2281eb34dca09f067ea77b1906cfac15d7b8955c (patch)
treea6a62aa8a22c642bb16e1a1aee21f7926ccdf311 /num_util.hpp
Initial release of the HRTC libraryHEADmaster
Diffstat (limited to 'num_util.hpp')
-rw-r--r--num_util.hpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/num_util.hpp b/num_util.hpp
new file mode 100644
index 0000000..ffa808d
--- /dev/null
+++ b/num_util.hpp
@@ -0,0 +1,53 @@
+/* Copyright 2014-2016 Jan Huwald, Stephan Richter
+
+ This file is part of HRTC.
+
+ HRTC is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ HRTC is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+ License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program (see file LICENSE). If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#pragma once
+
+#include <assert.h>
+#include <boost/integer.hpp>
+using boost::int_t;
+using boost::uint_t;
+
+#include "common.hpp"
+
+/// quantization
+
+template<typename Real>
+typename int_t<bitsizeof<Real>()>::least
+quantize(Real v, Real quantum) {
+ return round(v / quantum);
+}
+
+template<typename Real>
+Real quant2real(typename int_t<bitsizeof<Real>()>::least i, Real quantum) {
+ return i * quantum;
+}
+
+// Convert a signed integer into an unsigned by storing the sign in
+// the least significant bit. This allows to efficiently store small
+// negative quantities using the integer_encoding_library (which only
+// deals well with small unsigned values).
+uint32_t signed2unsigned(int32_t v) {
+ return (v >= 0) ? ((uint32_t( v ) << 1) | 0)
+ : ((uint32_t(abs(v)) << 1) | 1);
+}
+
+int32_t unsigned2signed(uint32_t v) {
+ return (v & 1) ? -(v >> 1)
+ : (v >> 1);
+}
contact: Jan Huwald // Impressum