From 6c213ecd0927d10a98ba6ec64fbc9338516edee2 Mon Sep 17 00:00:00 2001 From: Steven Koo Date: Thu, 6 Aug 2020 21:18:17 -0500 Subject: rfnoc: Remove M_PI usage to fix Windows builds M_PI may not exist if _USE_MATH_DEFINES isn't defined before the first include of math.h or cmath on Windows. This changes avoids the issue all together by defining our own PI. --- host/include/uhd/rfnoc/siggen_block_control.hpp | 7 +++---- host/include/uhd/utils/math.hpp | 2 ++ host/lib/rfnoc/siggen_block_control.cpp | 7 +++---- host/tests/rfnoc_block_tests/siggen_block_test.cpp | 12 +++++------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/host/include/uhd/rfnoc/siggen_block_control.hpp b/host/include/uhd/rfnoc/siggen_block_control.hpp index 944ecc4e0..e599659b6 100644 --- a/host/include/uhd/rfnoc/siggen_block_control.hpp +++ b/host/include/uhd/rfnoc/siggen_block_control.hpp @@ -6,10 +6,9 @@ #pragma once -#define _USE_MATH_DEFINES #include #include -#include +#include #include namespace uhd { namespace rfnoc { @@ -195,8 +194,8 @@ public: if (sample_rate <= 0.0) { throw uhd::value_error("sample_rate must be > 0.0"); } - const double phase_inc = (frequency / sample_rate) * 2.0 * M_PI; - if (phase_inc < -M_PI || phase_inc > M_PI) { + const double phase_inc = (frequency / sample_rate) * 2.0 * uhd::math::PI; + if (phase_inc < -uhd::math::PI || phase_inc > uhd::math::PI) { throw uhd::value_error("frequency must be in [-samp_rate/2, samp_rate/2]"); } set_sine_phase_increment(phase_inc, port); diff --git a/host/include/uhd/utils/math.hpp b/host/include/uhd/utils/math.hpp index d442d6a94..becc256f1 100644 --- a/host/include/uhd/utils/math.hpp +++ b/host/include/uhd/utils/math.hpp @@ -30,6 +30,8 @@ namespace uhd { */ namespace math { +static const double PI = 3.14159265358979323846; + /*! * Define epsilon values for floating point comparisons. * diff --git a/host/lib/rfnoc/siggen_block_control.cpp b/host/lib/rfnoc/siggen_block_control.cpp index fe963a1c3..fa527f3f6 100644 --- a/host/lib/rfnoc/siggen_block_control.cpp +++ b/host/lib/rfnoc/siggen_block_control.cpp @@ -4,7 +4,6 @@ // SPDX-License-Identifier: GPL-3.0-or-later // -#define _USE_MATH_DEFINES #include #include #include @@ -12,8 +11,8 @@ #include #include #include +#include #include -#include #include #include @@ -182,12 +181,12 @@ private: }); register_property(&_prop_phase_inc.back(), [this, port]() { const double phase_inc = _prop_phase_inc.at(port).get(); - if (phase_inc < (-M_PI) || phase_inc > (M_PI)) { + if (phase_inc < (-uhd::math::PI) || phase_inc > (uhd::math::PI)) { throw uhd::value_error( "Phase increment value must be in [-pi, pi]"); } const int16_t phase_inc_scaled_rads_fp = - clamp((phase_inc / M_PI) * 8192.0); + clamp((phase_inc / uhd::math::PI) * 8192.0); _siggen_reg_iface.poke32( REG_PHASE_INC_OFFSET, phase_inc_scaled_rads_fp & 0xffff, port); }); diff --git a/host/tests/rfnoc_block_tests/siggen_block_test.cpp b/host/tests/rfnoc_block_tests/siggen_block_test.cpp index d495e7a19..6fdcb903b 100644 --- a/host/tests/rfnoc_block_tests/siggen_block_test.cpp +++ b/host/tests/rfnoc_block_tests/siggen_block_test.cpp @@ -4,8 +4,6 @@ // SPDX-License-Identifier: GPL-3.0-or-later // -#define _USE_MATH_DEFINES - #include "../rfnoc_graph_mock_nodes.hpp" #include #include @@ -13,11 +11,11 @@ #include #include #include +#include #include #include #include #include -#include #include using namespace uhd::rfnoc; @@ -125,7 +123,7 @@ public: static uint32_t phase_increment_to_register(double phase_inc) { const int16_t phase_inc_scaled_rads_fp = - clamp((phase_inc / M_PI) * 8192.0); + clamp((phase_inc / uhd::math::PI) * 8192.0); return static_cast(phase_inc_scaled_rads_fp) & 0xffff; } @@ -248,7 +246,7 @@ BOOST_FIXTURE_TEST_CASE(siggen_test_api, siggen_block_fixture) BOOST_CHECK_EQUAL(reg_iface->constants.at(port), siggen_mock_reg_iface_t::constant_to_register(constant)); BOOST_CHECK_EQUAL(test_siggen->get_constant(port), constant); - const double phase_inc = (port * M_PI / 16.0); + const double phase_inc = (port * uhd::math::PI / 16.0); test_siggen->set_sine_phase_increment(phase_inc, port); BOOST_CHECK_EQUAL(reg_iface->phase_increments.at(port), siggen_mock_reg_iface_t::phase_increment_to_register(phase_inc)); BOOST_CHECK_EQUAL(test_siggen->get_sine_phase_increment(port), phase_inc); @@ -256,7 +254,7 @@ BOOST_FIXTURE_TEST_CASE(siggen_test_api, siggen_block_fixture) const double freq = 1000 + (100 * port); const double samp_rate = 1e6; test_siggen->set_sine_frequency(freq, samp_rate, port); - const double calculated_phase_inc = freq / samp_rate * 2.0 * M_PI; + const double calculated_phase_inc = freq / samp_rate * 2.0 * uhd::math::PI; BOOST_CHECK_EQUAL(reg_iface->phase_increments.at(port), siggen_mock_reg_iface_t::phase_increment_to_register(calculated_phase_inc)); BOOST_CHECK_EQUAL( @@ -294,7 +292,7 @@ BOOST_FIXTURE_TEST_CASE(siggen_test_ranges, siggen_block_fixture) BOOST_CHECK_THROW(test_siggen->set_constant(bad_constant_q, port), uhd::value_error); BOOST_CHECK_THROW(test_siggen->set_constant(-bad_constant_q, port), uhd::value_error); - const double bad_phase_inc = 5 * M_PI; + const double bad_phase_inc = 5 * uhd::math::PI; BOOST_CHECK_THROW( test_siggen->set_sine_phase_increment(bad_phase_inc, port), uhd::value_error); BOOST_CHECK_THROW(test_siggen->set_sine_phase_increment(-bad_phase_inc, port), -- cgit v1.2.3