From 4c5db96c36a3f1e245d6a42d09f612b9c016709c Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 12 Nov 2011 16:52:03 -0800 Subject: convert: simplify table conversion with templates --- host/lib/convert/convert_with_tables.cpp | 77 +++++++++++--------------------- 1 file changed, 25 insertions(+), 52 deletions(-) (limited to 'host/lib') diff --git a/host/lib/convert/convert_with_tables.cpp b/host/lib/convert/convert_with_tables.cpp index b3702e6ea..00bec95af 100644 --- a/host/lib/convert/convert_with_tables.cpp +++ b/host/lib/convert/convert_with_tables.cpp @@ -23,28 +23,19 @@ using namespace uhd::convert; static const size_t sc16_table_len = size_t(1 << 16); -#ifdef BOOST_BIG_ENDIAN -# define ITEM32_BE_TO_R16(x) boost::uint16_t(x >> 16) -# define ITEM32_BE_TO_I16(x) boost::uint16_t(x) -# define ITEM32_LE_TO_R16(x) boost::uint16_t(x) -# define ITEM32_LE_TO_I16(x) boost::uint16_t(x >> 16) -#else -# define ITEM32_BE_TO_R16(x) boost::uint16_t(x) -# define ITEM32_BE_TO_I16(x) boost::uint16_t(x >> 16) -# define ITEM32_LE_TO_R16(x) boost::uint16_t(x >> 16) -# define ITEM32_LE_TO_I16(x) boost::uint16_t(x) -#endif +typedef boost::uint16_t (*tohost16_type)(boost::uint16_t); /*********************************************************************** - * Implementation for sc16 be lookup table + * Implementation for sc16 lookup table **********************************************************************/ -template class convert_sc16_item32_be_1_to_fcxx_1 : public converter{ +template +class convert_sc16_item32_1_to_fcxx_1 : public converter{ public: - convert_sc16_item32_be_1_to_fcxx_1(void): _table(sc16_table_len){} + convert_sc16_item32_1_to_fcxx_1(void): _table(sc16_table_len){} void set_scalar(const double scalar){ for (size_t i = 0; i < sc16_table_len; i++){ - const boost::int16_t val = uhd::ntohx(boost::uint16_t(i & 0xffff)); + const boost::int16_t val = tohost(boost::uint16_t(i & 0xffff)); _table[i] = type(val*scalar); } } @@ -54,10 +45,10 @@ public: std::complex *output = reinterpret_cast *>(outputs[0]); for (size_t i = 0; i < nsamps; i++){ - const item32_t item_be = input[i]; + const item32_t item = input[i]; output[i] = std::complex( - _table[ITEM32_BE_TO_R16(item_be)], - _table[ITEM32_BE_TO_I16(item_be)] + _table[boost::uint16_t(item >> real_shift)], + _table[boost::uint16_t(item >> imag_shift)] ); } } @@ -67,53 +58,35 @@ private: }; /*********************************************************************** - * Implementation for sc16 le lookup table + * Factory functions and registration **********************************************************************/ -template class convert_sc16_item32_le_1_to_fcxx_1 : public converter{ -public: - convert_sc16_item32_le_1_to_fcxx_1(void): _table(sc16_table_len){} - - void set_scalar(const double scalar){ - for (size_t i = 0; i < sc16_table_len; i++){ - const boost::int16_t val = uhd::wtohx(boost::uint16_t(i & 0xffff)); - _table[i] = type(val*scalar); - } - } - - void operator()(const input_type &inputs, const output_type &outputs, const size_t nsamps){ - const item32_t *input = reinterpret_cast(inputs[0]); - std::complex *output = reinterpret_cast *>(outputs[0]); - for (size_t i = 0; i < nsamps; i++){ - const item32_t item_le = input[i]; - output[i] = std::complex( - _table[ITEM32_LE_TO_R16(item_le)], - _table[ITEM32_LE_TO_I16(item_le)] - ); - } - } - -private: - std::vector _table; -}; +#ifdef BOOST_BIG_ENDIAN +# define ITEM32_BE_TO_R16 16 +# define ITEM32_BE_TO_I16 0 +# define ITEM32_LE_TO_R16 0 +# define ITEM32_LE_TO_I16 16 +#else +# define ITEM32_BE_TO_R16 0 +# define ITEM32_BE_TO_I16 16 +# define ITEM32_LE_TO_R16 16 +# define ITEM32_LE_TO_I16 0 +#endif -/*********************************************************************** - * Factory functions and registration - **********************************************************************/ static converter::sptr make_convert_sc16_item32_be_1_to_fc32_1(void){ - return converter::sptr(new convert_sc16_item32_be_1_to_fcxx_1()); + return converter::sptr(new convert_sc16_item32_1_to_fcxx_1()); } static converter::sptr make_convert_sc16_item32_be_1_to_fc64_1(void){ - return converter::sptr(new convert_sc16_item32_be_1_to_fcxx_1()); + return converter::sptr(new convert_sc16_item32_1_to_fcxx_1()); } static converter::sptr make_convert_sc16_item32_le_1_to_fc32_1(void){ - return converter::sptr(new convert_sc16_item32_le_1_to_fcxx_1()); + return converter::sptr(new convert_sc16_item32_1_to_fcxx_1()); } static converter::sptr make_convert_sc16_item32_le_1_to_fc64_1(void){ - return converter::sptr(new convert_sc16_item32_le_1_to_fcxx_1()); + return converter::sptr(new convert_sc16_item32_1_to_fcxx_1()); } UHD_STATIC_BLOCK(register_convert_sc16_item32_1_to_fcxx_1){ -- cgit v1.2.3