Revision 79ea83d6
| b/host/lib/transport/convert_types_impl.hpp | ||
|---|---|---|
| 42 | 42 |
/*********************************************************************** |
| 43 | 43 |
* Convert complex short buffer to items32 |
| 44 | 44 |
**********************************************************************/ |
| 45 |
static UHD_INLINE item32_t sc16_to_item32(sc16_t num){
|
|
| 46 |
boost::uint16_t real = num.real(); |
|
| 47 |
boost::uint16_t imag = num.imag(); |
|
| 48 |
return (item32_t(real) << 16) | (item32_t(imag) << 0); |
|
| 49 |
} |
|
| 50 |
|
|
| 45 | 51 |
static UHD_INLINE void sc16_to_item32_nswap( |
| 46 | 52 |
const sc16_t *input, item32_t *output, size_t nsamps |
| 47 | 53 |
){
|
| 48 |
std::memcpy(output, input, nsamps*sizeof(item32_t)); |
|
| 54 |
for (size_t i = 0; i < nsamps; i++){
|
|
| 55 |
output[i] = sc16_to_item32(input[i]); |
|
| 56 |
} |
|
| 49 | 57 |
} |
| 50 | 58 |
|
| 51 | 59 |
static UHD_INLINE void sc16_to_item32_bswap( |
| 52 | 60 |
const sc16_t *input, item32_t *output, size_t nsamps |
| 53 | 61 |
){
|
| 54 |
const item32_t *item32_input = (const item32_t *)input; |
|
| 55 | 62 |
for (size_t i = 0; i < nsamps; i++){
|
| 56 |
output[i] = uhd::byteswap(item32_input[i]);
|
|
| 63 |
output[i] = uhd::byteswap(sc16_to_item32(input[i]));
|
|
| 57 | 64 |
} |
| 58 | 65 |
} |
| 59 | 66 |
|
| 60 | 67 |
/*********************************************************************** |
| 61 | 68 |
* Convert items32 buffer to complex short |
| 62 | 69 |
**********************************************************************/ |
| 70 |
static UHD_INLINE sc16_t item32_to_sc16(item32_t item){
|
|
| 71 |
return sc16_t( |
|
| 72 |
boost::int16_t(item >> 16), |
|
| 73 |
boost::int16_t(item >> 0) |
|
| 74 |
); |
|
| 75 |
} |
|
| 76 |
|
|
| 63 | 77 |
static UHD_INLINE void item32_to_sc16_nswap( |
| 64 | 78 |
const item32_t *input, sc16_t *output, size_t nsamps |
| 65 | 79 |
){
|
| 66 |
std::memcpy(output, input, nsamps*sizeof(item32_t)); |
|
| 80 |
for (size_t i = 0; i < nsamps; i++){
|
|
| 81 |
output[i] = item32_to_sc16(input[i]); |
|
| 82 |
} |
|
| 67 | 83 |
} |
| 68 | 84 |
|
| 69 | 85 |
static UHD_INLINE void item32_to_sc16_bswap( |
| 70 | 86 |
const item32_t *input, sc16_t *output, size_t nsamps |
| 71 | 87 |
){
|
| 72 |
item32_t *item32_output = (item32_t *)output; |
|
| 73 | 88 |
for (size_t i = 0; i < nsamps; i++){
|
| 74 |
item32_output[i] = uhd::byteswap(input[i]);
|
|
| 89 |
output[i] = item32_to_sc16(uhd::byteswap(input[i]));
|
|
| 75 | 90 |
} |
| 76 | 91 |
} |
| 77 | 92 |
|
| b/host/lib/transport/gen_convert_types.py | ||
|---|---|---|
| 95 | 95 |
size_t num_samps |
| 96 | 96 |
){
|
| 97 | 97 |
switch(get_pred(io_type, otw_type)){
|
| 98 |
#for $pred in range(4)
|
|
| 98 |
#for $pred in range(2**$ph.nbits)
|
|
| 99 | 99 |
case $pred: |
| 100 | 100 |
#set $out_type = $ph.get_host_type($pred) |
| 101 | 101 |
#set $in_type = $ph.get_dev_type($pred) |
| b/host/test/convert_types_test.cpp | ||
|---|---|---|
| 27 | 27 |
using namespace uhd; |
| 28 | 28 |
|
| 29 | 29 |
//typedefs for complex types |
| 30 |
typedef std::complex<boost::uint16_t> sc16_t;
|
|
| 30 |
typedef std::complex<boost::int16_t> sc16_t; |
|
| 31 | 31 |
typedef std::complex<float> fc32_t; |
| 32 | 32 |
|
| 33 | 33 |
//extract pointer to POD since using &vector.front() throws in MSVC |
| ... | ... | |
| 158 | 158 |
test_convert_types_fc32(nsamps, io_type, otw_type); |
| 159 | 159 |
} |
| 160 | 160 |
} |
| 161 |
|
|
| 162 |
/*********************************************************************** |
|
| 163 |
* Test float to short conversion loopback |
|
| 164 |
**********************************************************************/ |
|
| 165 |
BOOST_AUTO_TEST_CASE(test_convert_types_fc32_to_sc16){
|
|
| 166 |
io_type_t io_type_in(io_type_t::COMPLEX_FLOAT32); |
|
| 167 |
io_type_t io_type_out(io_type_t::COMPLEX_INT16); |
|
| 168 |
|
|
| 169 |
otw_type_t otw_type; |
|
| 170 |
otw_type.byteorder = otw_type_t::BO_NATIVE; |
|
| 171 |
otw_type.width = 16; |
|
| 172 |
|
|
| 173 |
const size_t nsamps = 13; |
|
| 174 |
std::vector<fc32_t> input(nsamps); |
|
| 175 |
BOOST_FOREACH(fc32_t &in, input) in = fc32_t( |
|
| 176 |
(std::rand()/float(RAND_MAX/2)) - 1, |
|
| 177 |
(std::rand()/float(RAND_MAX/2)) - 1 |
|
| 178 |
); |
|
| 179 |
|
|
| 180 |
//convert float to dev |
|
| 181 |
std::vector<boost::uint32_t> tmp(nsamps); |
|
| 182 |
transport::convert_io_type_to_otw_type( |
|
| 183 |
pod2ptr(input), io_type_in, |
|
| 184 |
pod2ptr(tmp), otw_type, |
|
| 185 |
nsamps |
|
| 186 |
); |
|
| 187 |
|
|
| 188 |
//convert dev to short |
|
| 189 |
std::vector<sc16_t> output(nsamps); |
|
| 190 |
transport::convert_otw_type_to_io_type( |
|
| 191 |
pod2ptr(tmp), otw_type, |
|
| 192 |
pod2ptr(output), io_type_out, |
|
| 193 |
nsamps |
|
| 194 |
); |
|
| 195 |
|
|
| 196 |
//test that the inputs and outputs match |
|
| 197 |
for (size_t i = 0; i < nsamps; i++){
|
|
| 198 |
BOOST_CHECK_CLOSE_FRACTION(input[i].real(), output[i].real()/float(32767), float(0.01)); |
|
| 199 |
BOOST_CHECK_CLOSE_FRACTION(input[i].imag(), output[i].imag()/float(32767), float(0.01)); |
|
| 200 |
} |
|
| 201 |
} |
|
| 202 |
|
|
| 203 |
/*********************************************************************** |
|
| 204 |
* Test short to float conversion loopback |
|
| 205 |
**********************************************************************/ |
|
| 206 |
BOOST_AUTO_TEST_CASE(test_convert_types_sc16_to_fc32){
|
|
| 207 |
io_type_t io_type_in(io_type_t::COMPLEX_INT16); |
|
| 208 |
io_type_t io_type_out(io_type_t::COMPLEX_FLOAT32); |
|
| 209 |
|
|
| 210 |
otw_type_t otw_type; |
|
| 211 |
otw_type.byteorder = otw_type_t::BO_NATIVE; |
|
| 212 |
otw_type.width = 16; |
|
| 213 |
|
|
| 214 |
const size_t nsamps = 13; |
|
| 215 |
std::vector<sc16_t> input(nsamps); |
|
| 216 |
BOOST_FOREACH(sc16_t &in, input) in = sc16_t( |
|
| 217 |
std::rand()-(RAND_MAX/2), |
|
| 218 |
std::rand()-(RAND_MAX/2) |
|
| 219 |
); |
|
| 220 |
|
|
| 221 |
//convert short to dev |
|
| 222 |
std::vector<boost::uint32_t> tmp(nsamps); |
|
| 223 |
transport::convert_io_type_to_otw_type( |
|
| 224 |
pod2ptr(input), io_type_in, |
|
| 225 |
pod2ptr(tmp), otw_type, |
|
| 226 |
nsamps |
|
| 227 |
); |
|
| 228 |
|
|
| 229 |
//convert dev to float |
|
| 230 |
std::vector<fc32_t> output(nsamps); |
|
| 231 |
transport::convert_otw_type_to_io_type( |
|
| 232 |
pod2ptr(tmp), otw_type, |
|
| 233 |
pod2ptr(output), io_type_out, |
|
| 234 |
nsamps |
|
| 235 |
); |
|
| 236 |
|
|
| 237 |
//test that the inputs and outputs match |
|
| 238 |
for (size_t i = 0; i < nsamps; i++){
|
|
| 239 |
BOOST_CHECK_CLOSE_FRACTION(input[i].real()/float(32767), output[i].real(), float(0.01)); |
|
| 240 |
BOOST_CHECK_CLOSE_FRACTION(input[i].imag()/float(32767), output[i].imag(), float(0.01)); |
|
| 241 |
} |
|
| 242 |
} |
|
Also available in: Unified diff