From 9e47ad607b597954e786db6614f5b42123184ccd Mon Sep 17 00:00:00 2001
From: Ben Hilburn <ben.hilburn@ettus.com>
Date: Wed, 27 Nov 2013 12:11:23 -0800
Subject: Squashed merge of Coverity fixes.

---
 host/lib/convert/convert_fc32_item32.cpp | 17 +++++++-------
 host/lib/convert/convert_pack_sc12.cpp   |  2 +-
 host/lib/convert/convert_unpack_sc12.cpp |  2 +-
 host/lib/convert/sse2_fc32_to_sc16.cpp   | 38 +++++++++++++++++++++++---------
 host/lib/convert/sse2_sc16_to_fc32.cpp   | 38 +++++++++++++++++++++++---------
 5 files changed, 67 insertions(+), 30 deletions(-)

(limited to 'host/lib/convert')

diff --git a/host/lib/convert/convert_fc32_item32.cpp b/host/lib/convert/convert_fc32_item32.cpp
index 29bfefd46..641fc2608 100644
--- a/host/lib/convert/convert_fc32_item32.cpp
+++ b/host/lib/convert/convert_fc32_item32.cpp
@@ -28,7 +28,7 @@ typedef boost::uint32_t (*to32_type)(boost::uint32_t);
 template <typename type, to32_type tohost>
 struct convert_fc32_item32_1_to_star_1 : public converter
 {
-    convert_fc32_item32_1_to_star_1(void)
+    convert_fc32_item32_1_to_star_1(void):_scalar(0.0)
     {
         //NOP
     }
@@ -48,9 +48,9 @@ struct convert_fc32_item32_1_to_star_1 : public converter
         {
             const item32_t i32 = tohost(input[i++]);
             const item32_t q32 = tohost(input[i++]);
-            const float i_f32 = reinterpret_cast<const float &>(i32);
-            const float q_f32 = reinterpret_cast<const float &>(q32);
-            output[o] = std::complex<type>(type(i_f32*_scalar), type(q_f32*_scalar));
+            const float *i_f32p = reinterpret_cast<const float *>(&i32);
+            const float *q_f32p = reinterpret_cast<const float *>(&q32);
+            output[o] = std::complex<type>(type((*i_f32p)*_scalar), type((*q_f32p)*_scalar));
         }
     }
 
@@ -60,7 +60,7 @@ struct convert_fc32_item32_1_to_star_1 : public converter
 template <typename type, to32_type towire>
 struct convert_star_1_to_fc32_item32_1 : public converter
 {
-    convert_star_1_to_fc32_item32_1(void)
+    convert_star_1_to_fc32_item32_1(void):_scalar(0.0)
     {
         //NOP
     }
@@ -80,9 +80,10 @@ struct convert_star_1_to_fc32_item32_1 : public converter
         {
             const float i_f32 = type(input[i].real()*_scalar);
             const float q_f32 = type(input[i].imag()*_scalar);
-            const item32_t i32 = towire(reinterpret_cast<const item32_t &>(i_f32));
-            const item32_t q32 = towire(reinterpret_cast<const item32_t &>(q_f32));
-            output[o++] = i32; output[o++] = q32;
+            const item32_t *i32p = reinterpret_cast<const item32_t *>(&i_f32);
+            const item32_t *q32p = reinterpret_cast<const item32_t *>(&q_f32);
+            output[o++] = towire(*i32p);
+            output[o++] = towire(*q32p);
         }
     }
 
diff --git a/host/lib/convert/convert_pack_sc12.cpp b/host/lib/convert/convert_pack_sc12.cpp
index 680814994..92cd5d152 100644
--- a/host/lib/convert/convert_pack_sc12.cpp
+++ b/host/lib/convert/convert_pack_sc12.cpp
@@ -67,7 +67,7 @@ void convert_star_4_to_sc12_item32_3
 template <typename type, towire32_type towire>
 struct convert_star_1_to_sc12_item32_1 : public converter
 {
-    convert_star_1_to_sc12_item32_1(void)
+    convert_star_1_to_sc12_item32_1(void):_scalar(0.0)
     {
         //NOP
     }
diff --git a/host/lib/convert/convert_unpack_sc12.cpp b/host/lib/convert/convert_unpack_sc12.cpp
index e98ab73f1..a2aec2ae5 100644
--- a/host/lib/convert/convert_unpack_sc12.cpp
+++ b/host/lib/convert/convert_unpack_sc12.cpp
@@ -84,7 +84,7 @@ void convert_sc12_item32_3_to_star_4
 template <typename type, tohost32_type tohost>
 struct convert_sc12_item32_1_to_star_1 : public converter
 {
-    convert_sc12_item32_1_to_star_1(void)
+    convert_sc12_item32_1_to_star_1(void):_scalar(0.0)
     {
         //NOP
     }
diff --git a/host/lib/convert/sse2_fc32_to_sc16.cpp b/host/lib/convert/sse2_fc32_to_sc16.cpp
index 90bf0ed04..a83e9b46c 100644
--- a/host/lib/convert/sse2_fc32_to_sc16.cpp
+++ b/host/lib/convert/sse2_fc32_to_sc16.cpp
@@ -27,6 +27,7 @@ DECLARE_CONVERTER(fc32, 1, sc16_item32_le, 1, PRIORITY_SIMD){
 
     const __m128 scalar = _mm_set_ps1(float(scale_factor));
 
+    // this macro converts values faster by using SSE intrinsics to convert 4 values at a time
     #define convert_fc32_1_to_item32_1_nswap_guts(_al_)                 \
     for (; i+3 < nsamps; i+=4){                                         \
         /* load from input */                                           \
@@ -48,17 +49,25 @@ DECLARE_CONVERTER(fc32, 1, sc16_item32_le, 1, PRIORITY_SIMD){
 
     size_t i = 0;
 
-    //dispatch according to alignment
+    // need to dispatch according to alignment for fastest conversion
     switch (size_t(input) & 0xf){
-    case 0x8:
-        xx_to_item32_sc16<uhd::htowx>(input, output, 1, scale_factor); i++;
     case 0x0:
+        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
+        convert_fc32_1_to_item32_1_nswap_guts(_)
+        break;
+    case 0x8:
+        // the first sample is 8-byte aligned - process it to align the remainder of the samples to 16-bytes
+        xx_to_item32_sc16<uhd::htowx>(input, output, 1, scale_factor);
+        i++;
+        // do faster processing of the bulk of the samples now that we are 16-byte aligned
         convert_fc32_1_to_item32_1_nswap_guts(_)
         break;
-    default: convert_fc32_1_to_item32_1_nswap_guts(u_)
+    default:
+        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load
+        convert_fc32_1_to_item32_1_nswap_guts(u_)
     }
 
-    //convert remainder
+    // convert any remaining samples
     xx_to_item32_sc16<uhd::htowx>(input+i, output+i, nsamps-i, scale_factor);
 }
 
@@ -68,6 +77,7 @@ DECLARE_CONVERTER(fc32, 1, sc16_item32_be, 1, PRIORITY_SIMD){
 
     const __m128 scalar = _mm_set_ps1(float(scale_factor));
 
+    // this macro converts values faster by using SSE intrinsics to convert 4 values at a time
     #define convert_fc32_1_to_item32_1_bswap_guts(_al_)                 \
     for (; i+3 < nsamps; i+=4){                                         \
         /* load from input */                                           \
@@ -88,16 +98,24 @@ DECLARE_CONVERTER(fc32, 1, sc16_item32_be, 1, PRIORITY_SIMD){
 
     size_t i = 0;
 
-    //dispatch according to alignment
+    // need to dispatch according to alignment for fastest conversion
     switch (size_t(input) & 0xf){
-    case 0x8:
-        xx_to_item32_sc16<uhd::htonx>(input, output, 1, scale_factor); i++;
     case 0x0:
+        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
+        convert_fc32_1_to_item32_1_bswap_guts(_)
+    case 0x8:
+        // the first value is 8-byte aligned - process it and prepare the bulk of the data for fast conversion
+        xx_to_item32_sc16<uhd::htonx>(input, output, 1, scale_factor);
+        i++;
+        // do faster processing of the remaining samples now that we are 16-byte aligned
         convert_fc32_1_to_item32_1_bswap_guts(_)
         break;
-    default: convert_fc32_1_to_item32_1_bswap_guts(u_)
+        break;
+    default:
+        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load
+        convert_fc32_1_to_item32_1_bswap_guts(u_)
     }
 
-    //convert remainder
+    // convert any remaining samples
     xx_to_item32_sc16<uhd::htonx>(input+i, output+i, nsamps-i, scale_factor);
 }
diff --git a/host/lib/convert/sse2_sc16_to_fc32.cpp b/host/lib/convert/sse2_sc16_to_fc32.cpp
index c03e41585..0ac7f1798 100644
--- a/host/lib/convert/sse2_sc16_to_fc32.cpp
+++ b/host/lib/convert/sse2_sc16_to_fc32.cpp
@@ -28,6 +28,7 @@ DECLARE_CONVERTER(sc16_item32_le, 1, fc32, 1, PRIORITY_SIMD){
     const __m128 scalar = _mm_set_ps1(float(scale_factor)/(1 << 16));
     const __m128i zeroi = _mm_setzero_si128();
 
+    // this macro converts values faster by using SSE intrinsics to convert 4 values at a time
     #define convert_item32_1_to_fc32_1_nswap_guts(_al_)                 \
     for (; i+3 < nsamps; i+=4){                                         \
         /* load from input */                                           \
@@ -50,17 +51,25 @@ DECLARE_CONVERTER(sc16_item32_le, 1, fc32, 1, PRIORITY_SIMD){
 
     size_t i = 0;
 
-    //dispatch according to alignment
+    // need to dispatch according to alignment for fastest conversion
     switch (size_t(output) & 0xf){
-    case 0x8:
-        item32_sc16_to_xx<uhd::htowx>(input, output, 1, scale_factor); i++;
     case 0x0:
+        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
+        convert_item32_1_to_fc32_1_nswap_guts(_)
+        break;
+    case 0x8:
+        // the first sample is 8-byte aligned - process it to align the remainder of the samples to 16-bytes
+        item32_sc16_to_xx<uhd::htowx>(input, output, 1, scale_factor);
+        i++;
+        // do faster processing of the bulk of the samples now that we are 16-byte aligned
         convert_item32_1_to_fc32_1_nswap_guts(_)
         break;
-    default: convert_item32_1_to_fc32_1_nswap_guts(u_)
+    default:
+        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load and store
+        convert_item32_1_to_fc32_1_nswap_guts(u_)
     }
 
-    //convert remainder
+    // convert any remaining samples
     item32_sc16_to_xx<uhd::htowx>(input+i, output+i, nsamps-i, scale_factor);
 }
 
@@ -71,6 +80,7 @@ DECLARE_CONVERTER(sc16_item32_be, 1, fc32, 1, PRIORITY_SIMD){
     const __m128 scalar = _mm_set_ps1(float(scale_factor)/(1 << 16));
     const __m128i zeroi = _mm_setzero_si128();
 
+    // this macro converts values faster by using SSE intrinsics to convert 4 values at a time
     #define convert_item32_1_to_fc32_1_bswap_guts(_al_)                 \
     for (; i+3 < nsamps; i+=4){                                         \
         /* load from input */                                           \
@@ -92,16 +102,24 @@ DECLARE_CONVERTER(sc16_item32_be, 1, fc32, 1, PRIORITY_SIMD){
 
     size_t i = 0;
 
-    //dispatch according to alignment
+    // need to dispatch according to alignment for fastest conversion
     switch (size_t(output) & 0xf){
-    case 0x8:
-        item32_sc16_to_xx<uhd::htonx>(input, output, 1, scale_factor); i++;
     case 0x0:
+        // the data is 16-byte aligned, so do the fast processing of the bulk of the samples
+        convert_item32_1_to_fc32_1_bswap_guts(_)
+        break;
+    case 0x8:
+        // the first sample is 8-byte aligned - process it to align the remainder of the samples to 16-bytes
+        item32_sc16_to_xx<uhd::htonx>(input, output, 1, scale_factor);
+        i++;
+        // do faster processing of the bulk of the samples now that we are 16-byte aligned
         convert_item32_1_to_fc32_1_bswap_guts(_)
         break;
-    default: convert_item32_1_to_fc32_1_bswap_guts(u_)
+    default:
+        // we are not 8 or 16-byte aligned, so do fast processing with the unaligned load and store
+        convert_item32_1_to_fc32_1_bswap_guts(u_)
     }
 
-    //convert remainder
+    // convert any remaining samples
     item32_sc16_to_xx<uhd::htonx>(input+i, output+i, nsamps-i, scale_factor);
 }
-- 
cgit v1.2.3