diff options
author | Josh Blum <josh@joshknows.com> | 2012-05-08 14:14:21 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2012-05-08 14:14:21 -0700 |
commit | 8a98d68933265f043bf95f9f273f9829619114c8 (patch) | |
tree | 7432f0182b9d04db2a56634a95904efe8c74b27c /host/lib/convert/convert_common.hpp | |
parent | c42c02f03774db7df34a09c021e907ca7a048d46 (diff) | |
download | uhd-8a98d68933265f043bf95f9f273f9829619114c8.tar.gz uhd-8a98d68933265f043bf95f9f273f9829619114c8.tar.bz2 uhd-8a98d68933265f043bf95f9f273f9829619114c8.zip |
convert: fixed sc8 convert issue w/ undefined behaviour
Its important to use a signed cast when converting float to int.
Then assign that signed int to an unsigned type of the same width.
Its undefined behaviour when converting a negative float to an unsigned int.
Diffstat (limited to 'host/lib/convert/convert_common.hpp')
-rw-r--r-- | host/lib/convert/convert_common.hpp | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/host/lib/convert/convert_common.hpp b/host/lib/convert/convert_common.hpp index 29ea94870..7626e4d87 100644 --- a/host/lib/convert/convert_common.hpp +++ b/host/lib/convert/convert_common.hpp @@ -194,11 +194,13 @@ static UHD_INLINE void item32_sc8_to_fc64(item32_t item, fc64_t &out0, fc64_t &o * Convert complex char to items32 sc8 buffer **********************************************************************/ static UHD_INLINE item32_t sc8_to_item32_sc8(sc8_t in0, sc8_t in1, double){ + boost::uint8_t real0 = boost::int8_t(in0.real()); + boost::uint8_t imag0 = boost::int8_t(in0.imag()); + boost::uint8_t real1 = boost::int8_t(in1.real()); + boost::uint8_t imag1 = boost::int8_t(in1.imag()); return - (item32_t(boost::uint8_t(in0.real())) << 8) | - (item32_t(boost::uint8_t(in0.imag())) << 0) | - (item32_t(boost::uint8_t(in1.real())) << 24) | - (item32_t(boost::uint8_t(in1.imag())) << 16) + (item32_t(real0) << 8) | (item32_t(imag0) << 0) | + (item32_t(real1) << 24) | (item32_t(imag1) << 16) ; } @@ -206,11 +208,13 @@ static UHD_INLINE item32_t sc8_to_item32_sc8(sc8_t in0, sc8_t in1, double){ * Convert complex short to items32 sc8 buffer **********************************************************************/ static UHD_INLINE item32_t sc16_to_item32_sc8(sc16_t in0, sc16_t in1, double){ + boost::uint8_t real0 = boost::int8_t(in0.real()); + boost::uint8_t imag0 = boost::int8_t(in0.imag()); + boost::uint8_t real1 = boost::int8_t(in1.real()); + boost::uint8_t imag1 = boost::int8_t(in1.imag()); return - (item32_t(boost::uint8_t(in0.real())) << 8) | - (item32_t(boost::uint8_t(in0.imag())) << 0) | - (item32_t(boost::uint8_t(in1.real())) << 24) | - (item32_t(boost::uint8_t(in1.imag())) << 16) + (item32_t(real0) << 8) | (item32_t(imag0) << 0) | + (item32_t(real1) << 24) | (item32_t(imag1) << 16) ; } @@ -218,11 +222,13 @@ static UHD_INLINE item32_t sc16_to_item32_sc8(sc16_t in0, sc16_t in1, double){ * Convert complex float to items32 sc8 buffer **********************************************************************/ static UHD_INLINE item32_t fc32_to_item32_sc8(fc32_t in0, fc32_t in1, double scale_factor){ + boost::uint8_t real0 = boost::int8_t(in0.real()*float(scale_factor)); + boost::uint8_t imag0 = boost::int8_t(in0.imag()*float(scale_factor)); + boost::uint8_t real1 = boost::int8_t(in1.real()*float(scale_factor)); + boost::uint8_t imag1 = boost::int8_t(in1.imag()*float(scale_factor)); return - (item32_t(boost::uint8_t(in0.real()*float(scale_factor))) << 8) | - (item32_t(boost::uint8_t(in0.imag()*float(scale_factor))) << 0) | - (item32_t(boost::uint8_t(in1.real()*float(scale_factor))) << 24) | - (item32_t(boost::uint8_t(in1.imag()*float(scale_factor))) << 16) + (item32_t(real0) << 8) | (item32_t(imag0) << 0) | + (item32_t(real1) << 24) | (item32_t(imag1) << 16) ; } @@ -230,11 +236,13 @@ static UHD_INLINE item32_t fc32_to_item32_sc8(fc32_t in0, fc32_t in1, double sca * Convert complex double to items32 sc8 buffer **********************************************************************/ static UHD_INLINE item32_t fc64_to_item32_sc8(fc64_t in0, fc64_t in1, double scale_factor){ + boost::uint8_t real0 = boost::int8_t(in0.real()*(scale_factor)); + boost::uint8_t imag0 = boost::int8_t(in0.imag()*(scale_factor)); + boost::uint8_t real1 = boost::int8_t(in1.real()*(scale_factor)); + boost::uint8_t imag1 = boost::int8_t(in1.imag()*(scale_factor)); return - (item32_t(boost::uint8_t(in0.real()*(scale_factor))) << 8) | - (item32_t(boost::uint8_t(in0.imag()*(scale_factor))) << 0) | - (item32_t(boost::uint8_t(in1.real()*(scale_factor))) << 24) | - (item32_t(boost::uint8_t(in1.imag()*(scale_factor))) << 16) + (item32_t(real0) << 8) | (item32_t(imag0) << 0) | + (item32_t(real1) << 24) | (item32_t(imag1) << 16) ; } |