diff options
author | Martin Braun <martin.braun@ettus.com> | 2018-07-10 22:24:49 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2018-07-11 10:11:28 -0700 |
commit | b185c9447a4a33bff14fd697dc294b9ee5d298b3 (patch) | |
tree | bda446edfd03f0b32d12fbbc121597bfdf7487c6 /host | |
parent | 826279754529f76a0eb617cf02ade75a4145904f (diff) | |
download | uhd-b185c9447a4a33bff14fd697dc294b9ee5d298b3.tar.gz uhd-b185c9447a4a33bff14fd697dc294b9ee5d298b3.tar.bz2 uhd-b185c9447a4a33bff14fd697dc294b9ee5d298b3.zip |
soft_register: Remove compiler warning, add unit test
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/utils/soft_register.hpp | 10 | ||||
-rw-r--r-- | host/tests/CMakeLists.txt | 3 | ||||
-rw-r--r-- | host/tests/soft_reg_test.cpp | 33 |
3 files changed, 41 insertions, 5 deletions
diff --git a/host/include/uhd/utils/soft_register.hpp b/host/include/uhd/utils/soft_register.hpp index ac6ad4b35..fea4c0791 100644 --- a/host/include/uhd/utils/soft_register.hpp +++ b/host/include/uhd/utils/soft_register.hpp @@ -85,14 +85,16 @@ namespace soft_reg_field { template<typename data_t> UHD_INLINE data_t mask(const soft_reg_field_t field) { - static const data_t ONE = static_cast<data_t>(1); + constexpr data_t ONE = static_cast<data_t>(1); + constexpr data_t ALL_ONES = ~static_cast<data_t>(0); //Behavior for the left shift operation is undefined in C++ //if the shift amount is >= bitwidth of the datatype //So we treat that as a special case with a branch predicition hint - if (likely((sizeof(data_t)*8) != width(field))) + if (likely((sizeof(data_t)*8) != width(field))) { return ((ONE<<width(field))-ONE)<<shift(field); - else - return (0-ONE)<<shift(field); + } else { + return ALL_ONES<<shift(field); + } } } diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 7d53b50c4..ba0484e86 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -1,4 +1,4 @@ -# + # Copyright 2010-2015 Ettus Research LLC # Copyright 2018 Ettus Research, a National Instruments Company # @@ -34,6 +34,7 @@ SET(test_sources ranges_test.cpp sid_t_test.cpp sensors_test.cpp + soft_reg_test.cpp sph_recv_test.cpp sph_send_test.cpp subdev_spec_test.cpp diff --git a/host/tests/soft_reg_test.cpp b/host/tests/soft_reg_test.cpp new file mode 100644 index 000000000..8e53c61ca --- /dev/null +++ b/host/tests/soft_reg_test.cpp @@ -0,0 +1,33 @@ +// +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhd/utils/soft_register.hpp> +#include <boost/test/unit_test.hpp> + +using namespace uhd; + +BOOST_AUTO_TEST_CASE(test_soft_reg_field) { + UHD_DEFINE_SOFT_REG_FIELD(test_reg1, /* width */ 1, /* shift */ 0); + BOOST_CHECK_EQUAL(soft_reg_field::width(test_reg1), 1); + BOOST_CHECK_EQUAL(soft_reg_field::shift(test_reg1), 0); + BOOST_CHECK_EQUAL(soft_reg_field::mask<uint32_t>(test_reg1), 1<<0); + + UHD_DEFINE_SOFT_REG_FIELD(test_reg2, /* width */ 5, /* shift */ 4); + BOOST_CHECK_EQUAL(soft_reg_field::width(test_reg2), 5); + BOOST_CHECK_EQUAL(soft_reg_field::shift(test_reg2), 4); + BOOST_CHECK_EQUAL(soft_reg_field::mask<uint32_t>(test_reg2), 0x1F<<4); + + UHD_DEFINE_SOFT_REG_FIELD(test_reg3, /* width */ 9, /* shift */ 0); + BOOST_CHECK_EQUAL(soft_reg_field::width(test_reg3), 9); + BOOST_CHECK_EQUAL(soft_reg_field::shift(test_reg3), 0); + BOOST_CHECK_EQUAL(soft_reg_field::mask<uint8_t>(test_reg3), 0xFF); + + // This one is platform dependent: + UHD_DEFINE_SOFT_REG_FIELD(test_reg4, /* width */ 33, /* shift */ 0); + BOOST_CHECK_EQUAL(soft_reg_field::width(test_reg4), 33); + BOOST_CHECK_EQUAL(soft_reg_field::shift(test_reg4), 0); + BOOST_CHECK_EQUAL(soft_reg_field::mask<size_t>(test_reg4), ~size_t(0) & 0x1FFFFFFFF); +} |