aboutsummaryrefslogtreecommitdiffstats
path: root/host/include
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2022-01-12 14:47:54 +0100
committerAaron Rossetto <aaron.rossetto@ni.com>2022-02-04 13:16:00 -0600
commit42ff4fb7f5a3e37714f025473f95089bf1ff8c98 (patch)
tree6d70a54cd704c95149f4b2c03bf32505fe26014a /host/include
parent3e496cbda1d809d2ca15f69cfa231424bf47179f (diff)
downloaduhd-42ff4fb7f5a3e37714f025473f95089bf1ff8c98.tar.gz
uhd-42ff4fb7f5a3e37714f025473f95089bf1ff8c98.tar.bz2
uhd-42ff4fb7f5a3e37714f025473f95089bf1ff8c98.zip
uhd: Harmonize fuzzy frequency comparisons
Throughout UHD, we often do floating-point comparisons for frequency ranges that require resilience to floating point rounding errors. Most of the time the checks look like this: ```cpp if (fp_compare_epsilon<double>(freq) > boundary) { // ... } ``` The exception is the N320 daughterboard control, which uses a custom epsilon: ```cpp if (fp_compare_epsilon<double>(freq, RHODIUM_FREQ_COMPARE_EPSILON) > boundary) { // ... } ``` This was, for the most part, not by design, but because authors simply didn't think about which epsilon value was appropriate for the frequency comparison. This was complicated by the fact that fp_compare_epsilon previously had some issues. This patch introduces FREQ_COMPARE_EPSILON, which is a sensible default value for fp_compare_epsilon when doing frequency comparisons (note that fp_compare_delta already had such a value). Also, it introduces freq_compare_epsilon(x), which is a shorthand for fp_compare_epsilon<double>(x, FREQ_COMPARE_EPSILON). We then replace all occurrences of fp_compare_epsilon<double> which are specific to frequency checks with freq_compare_epsilon.
Diffstat (limited to 'host/include')
-rw-r--r--host/include/uhd/utils/math.hpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/host/include/uhd/utils/math.hpp b/host/include/uhd/utils/math.hpp
index 6ee46e98a..c44aa4db9 100644
--- a/host/include/uhd/utils/math.hpp
+++ b/host/include/uhd/utils/math.hpp
@@ -51,6 +51,16 @@ static const double PI = 3.14159265358979323846;
static const float SINGLE_PRECISION_EPSILON = 1.19e-7f;
static const double DOUBLE_PRECISION_EPSILON = 2.22e-16;
+//! Epsilon value for when using fp_compare_epsilon to compare frequencies
+//
+// Note that this is a UHD/USRP-specific constant. In UHD, frequencies are on
+// the order of 1e9 (GHz) or below. We will declare frequencies as equal if they
+// are within a millihertz. For the purpose of
+// comparing frequencies, we "lose" 9 decimal places for the integer
+// component of the frequency, so we choose this epsilon value for floating
+// point comparison of frequencies.
+static constexpr double FREQ_COMPARE_EPSILON = 1e-12;
+
namespace fp_compare {
/*!
@@ -142,6 +152,22 @@ UHD_INLINE bool operator>(double lhs, fp_compare_epsilon<float_t> rhs);
template <typename float_t>
UHD_INLINE bool operator>=(double lhs, fp_compare_epsilon<float_t> rhs);
+//! An alias for fp_compare_epsilon, but with defaults for frequencies
+class freq_compare_epsilon : public fp_compare_epsilon<double>
+{
+public:
+ UHD_INLINE freq_compare_epsilon(double value)
+ : fp_compare_epsilon<double>(value, FREQ_COMPARE_EPSILON)
+ {
+ }
+
+ UHD_INLINE freq_compare_epsilon(const freq_compare_epsilon& copy)
+ : fp_compare_epsilon<double>(copy)
+ {
+ }
+};
+
+
} // namespace fp_compare