From 74f45ae2a723107adce1038a708d2958aca97487 Mon Sep 17 00:00:00 2001
From: Martin Braun <martin.braun@ettus.com>
Date: Mon, 22 Jan 2018 09:54:46 -0800
Subject: rfnoc: Factor out ceil_log2() into central location

- New file: uhdlib/utils/math.hpp
---
 host/lib/include/uhdlib/utils/math.hpp | 25 +++++++++++++++++++++++++
 host/lib/rfnoc/ddc_block_ctrl_impl.cpp |  9 +++------
 host/lib/rfnoc/duc_block_ctrl_impl.cpp | 11 ++++-------
 3 files changed, 32 insertions(+), 13 deletions(-)
 create mode 100644 host/lib/include/uhdlib/utils/math.hpp

diff --git a/host/lib/include/uhdlib/utils/math.hpp b/host/lib/include/uhdlib/utils/math.hpp
new file mode 100644
index 000000000..eeb420727
--- /dev/null
+++ b/host/lib/include/uhdlib/utils/math.hpp
@@ -0,0 +1,25 @@
+//
+// Copyright 2018 Ettus Research, a National Instruments Company
+//
+// SPDX-License-Identifier: GPL-3.0
+//
+
+// More math, but not meant for public API
+
+#ifndef INCLUDED_UHDLIB_UTILS_MATH_HPP
+#define INCLUDED_UHDLIB_UTILS_MATH_HPP
+
+#include <cmath>
+
+namespace uhd { namespace math {
+
+/*! log2(num), rounded up to the nearest integer.
+ */
+template <class T>
+T ceil_log2(T num){
+    return std::ceil(std::log(num)/std::log(T(2)));
+}
+
+}} /* namespace uhd::math */
+
+#endif /* INCLUDED_UHDLIB_UTILS_MATH_HPP */
diff --git a/host/lib/rfnoc/ddc_block_ctrl_impl.cpp b/host/lib/rfnoc/ddc_block_ctrl_impl.cpp
index 9c7764045..8697c84cf 100644
--- a/host/lib/rfnoc/ddc_block_ctrl_impl.cpp
+++ b/host/lib/rfnoc/ddc_block_ctrl_impl.cpp
@@ -9,6 +9,7 @@
 #include <uhd/utils/log.hpp>
 #include <uhd/convert.hpp>
 #include <uhd/types/ranges.hpp>
+#include <uhdlib/utils/math.hpp>
 #include <uhdlib/utils/narrow.hpp>
 #include <uhdlib/utils/compat_check.hpp>
 #include <boost/math/special_functions/round.hpp>
@@ -16,11 +17,6 @@
 
 using namespace uhd::rfnoc;
 
-// TODO move this to a central location
-template <class T> T ceil_log2(T num){
-    return std::ceil(std::log(num)/std::log(T(2)));
-}
-
 class ddc_block_ctrl_impl : public ddc_block_ctrl
 {
 public:
@@ -301,7 +297,8 @@ private:
         // cost us small signal performance, thus we do no provide compensation gain for a saturated front end and allow
         // the signal to clip in the H/W as needed. If we wished to avoid the signal clipping in these circumstances then adjust code to read:
         // _scaling_adjustment = std::pow(2, ceil_log2(rate_pow))/(CORDIC_GAIN*rate_pow*1.415);
-        const double scaling_adjustment = std::pow(2, ceil_log2(rate_pow))/(CORDIC_GAIN*rate_pow);
+        const double scaling_adjustment =
+            std::pow(2, uhd::math::ceil_log2(rate_pow))/(CORDIC_GAIN*rate_pow);
         update_scalar(scaling_adjustment, chan);
         return input_rate/decim_rate;
     }
diff --git a/host/lib/rfnoc/duc_block_ctrl_impl.cpp b/host/lib/rfnoc/duc_block_ctrl_impl.cpp
index 0e1521d9f..c8989d65c 100644
--- a/host/lib/rfnoc/duc_block_ctrl_impl.cpp
+++ b/host/lib/rfnoc/duc_block_ctrl_impl.cpp
@@ -9,18 +9,14 @@
 #include <uhd/utils/log.hpp>
 #include <uhd/convert.hpp>
 #include <uhd/types/ranges.hpp>
-#include <uhdlib/utils/narrow.hpp>
 #include <uhdlib/utils/compat_check.hpp>
+#include <uhdlib/utils/math.hpp>
+#include <uhdlib/utils/narrow.hpp>
 #include <boost/math/special_functions/round.hpp>
 #include <cmath>
 
 using namespace uhd::rfnoc;
 
-// TODO move this to a central location
-template <class T> T ceil_log2(T num){
-    return std::ceil(std::log(num)/std::log(T(2)));
-}
-
 // TODO remove this once we have actual lambdas
 static double lambda_forward_prop(uhd::property_tree::sptr tree, uhd::fs_path prop, double value)
 {
@@ -250,7 +246,8 @@ private:
         // This must also encompass the CORDIC gain
         static const double CONSTANT_GAIN = 1.1644;
 
-        const double scaling_adjustment = std::pow(2, ceil_log2(rate_pow))/(CONSTANT_GAIN*rate_pow);
+        const double scaling_adjustment =
+            std::pow(2, uhd::math::ceil_log2(rate_pow))/(CONSTANT_GAIN*rate_pow);
         update_scalar(scaling_adjustment, chan);
         return output_rate/interp_rate;
     }
-- 
cgit v1.2.3