From 836796fff42f9ed15b619daabb54191527bf436e Mon Sep 17 00:00:00 2001 From: John Greb Date: Sat, 22 Nov 2014 21:55:35 +0000 Subject: [PATCH] Strict aliasing. --- CMakeLists.txt | 11 ++-- plugins/samplesource/CMakeLists.txt | 2 +- sdrbase/mainwindow.cpp | 10 ++-- sdrbase/mainwindow.ui | 28 ++++------ sdrbase/util/simpleserializer.cpp | 82 ++++++++++++++++++----------- 5 files changed, 75 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index be45d9c10..29e8ed02b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,6 @@ project(sdrangelove) set(CMAKE_BUILD_TYPE "Release") #set(CMAKE_BUILD_TYPE "ReleaseWithDebugInfo") #set(CMAKE_BUILD_TYPE "Debug") -add_definitions(-Wall -fno-strict-aliasing) - set(QT_USE_QTOPENGL TRUE) set(CMAKE_AUTOMOC ON) @@ -99,7 +97,7 @@ set(sdrbase_SOURCES sdrbase/util/message.cpp sdrbase/util/messagequeue.cpp - sdrbase/util/miniz.cpp + #sdrbase/util/miniz.cpp sdrbase/util/simpleserializer.cpp sdrbase/util/spinlock.cpp ) @@ -170,7 +168,7 @@ set(sdrbase_HEADERS include/util/export.h include/util/message.h include/util/messagequeue.h - include/util/miniz.h + #include/util/miniz.h include/util/simpleserializer.h include/util/spinlock.h ) @@ -265,8 +263,9 @@ if(MSVC) set( CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG" ) add_definitions (/D "_CRT_SECURE_NO_WARNINGS") else() -set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -msse2" ) -set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -msse2" ) + #set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -msse2" ) + #set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -msse2" ) + add_definitions(-msse2 -Wall) endif() ############################################################################## diff --git a/plugins/samplesource/CMakeLists.txt b/plugins/samplesource/CMakeLists.txt index 2885bec52..a27242b3a 100644 --- a/plugins/samplesource/CMakeLists.txt +++ b/plugins/samplesource/CMakeLists.txt @@ -1,7 +1,7 @@ project(samplesource) find_package(LibUSB) -find_package(LibOsmoSDR) +#find_package(LibOsmoSDR) add_subdirectory(gnuradio) diff --git a/sdrbase/mainwindow.cpp b/sdrbase/mainwindow.cpp index 9cbfb0fb1..c6a457163 100644 --- a/sdrbase/mainwindow.cpp +++ b/sdrbase/mainwindow.cpp @@ -339,8 +339,8 @@ void MainWindow::updateStatus() m_engineRunning->setColor(Qt::gray); m_engineError->setColor(Qt::gray); statusBar()->clearMessage(); - if(m_startOsmoSDRUpdateAfterStop) - on_actionOsmoSDR_Firmware_Upgrade_triggered(); + //if(m_startOsmoSDRUpdateAfterStop) + // on_actionOsmoSDR_Firmware_Upgrade_triggered(); break; case DSPEngine::StRunning: @@ -355,8 +355,8 @@ void MainWindow::updateStatus() m_engineRunning->setColor(Qt::gray); m_engineError->setColor(Qt::red); statusBar()->showMessage(tr("Error: %1").arg(m_dspEngine->errorMessage())); - if(m_startOsmoSDRUpdateAfterStop) - on_actionOsmoSDR_Firmware_Upgrade_triggered(); + //if(m_startOsmoSDRUpdateAfterStop) + // on_actionOsmoSDR_Firmware_Upgrade_triggered(); break; } m_lastEngineState = state; @@ -401,6 +401,7 @@ void MainWindow::on_action_View_Fullscreen_toggled(bool checked) void MainWindow::on_actionOsmoSDR_Firmware_Upgrade_triggered() { +#if 0 DSPEngine::State engineState = m_dspEngine->state(); if((engineState != DSPEngine::StIdle) && (engineState != DSPEngine::StError)) { m_startOsmoSDRUpdateAfterStop = true; @@ -412,6 +413,7 @@ void MainWindow::on_actionOsmoSDR_Firmware_Upgrade_triggered() OsmoSDRUpgrade osmoSDRUpgrade; osmoSDRUpgrade.exec(); */ +#endif } void MainWindow::on_presetSave_clicked() diff --git a/sdrbase/mainwindow.ui b/sdrbase/mainwindow.ui index b6ff71072..94f5ec8e9 100644 --- a/sdrbase/mainwindow.ui +++ b/sdrbase/mainwindow.ui @@ -50,15 +50,9 @@ &File - - - - - &Options - - - + + @@ -79,9 +73,9 @@ &Help - + @@ -100,7 +94,7 @@ - + @@ -371,11 +365,11 @@ F11 - + true @@ -407,16 +401,16 @@ &Preferences... - + Loaded &Plugins... diff --git a/sdrbase/util/simpleserializer.cpp b/sdrbase/util/simpleserializer.cpp index 206c9b468..76961ac25 100644 --- a/sdrbase/util/simpleserializer.cpp +++ b/sdrbase/util/simpleserializer.cpp @@ -165,8 +165,14 @@ void SimpleSerializer::writeU64(quint32 id, quint64 value) m_data.push_back((char)((value >> (i * 8)) & 0xff)); } +union floatasint { + quint32 u; + float f; +}; + void SimpleSerializer::writeFloat(quint32 id, float value) { + union floatasint tmp; if(id == 0) { qCritical("SimpleSerializer: ID 0 is not allowed"); return; @@ -175,15 +181,21 @@ void SimpleSerializer::writeFloat(quint32 id, float value) if(!writeTag(TFloat, id, 4)) return; - quint32 tmp = *((quint32*)&value); - m_data.push_back((char)((tmp >> 24) & 0xff)); - m_data.push_back((char)((tmp >> 16) & 0xff)); - m_data.push_back((char)((tmp >> 8) & 0xff)); - m_data.push_back((char)(tmp & 0xff)); + tmp.f = value; + m_data.push_back((char)((tmp.u >> 24) & 0xff)); + m_data.push_back((char)((tmp.u >> 16) & 0xff)); + m_data.push_back((char)((tmp.u >> 8) & 0xff)); + m_data.push_back((char)(tmp.u & 0xff)); } +union doubleasint { + quint64 u; + double d; +}; + void SimpleSerializer::writeDouble(quint32 id, double value) { + union doubleasint tmp; if(id == 0) { qCritical("SimpleSerializer: ID 0 is not allowed"); return; @@ -192,15 +204,15 @@ void SimpleSerializer::writeDouble(quint32 id, double value) if(!writeTag(TDouble, id, 8)) return; - quint64 tmp = *((quint64*)&value); - m_data.push_back((char)((tmp >> 56) & 0xff)); - m_data.push_back((char)((tmp >> 48) & 0xff)); - m_data.push_back((char)((tmp >> 40) & 0xff)); - m_data.push_back((char)((tmp >> 32) & 0xff)); - m_data.push_back((char)((tmp >> 24) & 0xff)); - m_data.push_back((char)((tmp >> 16) & 0xff)); - m_data.push_back((char)((tmp >> 8) & 0xff)); - m_data.push_back((char)(tmp & 0xff)); + tmp.d = value; + m_data.push_back((char)((tmp.u >> 56) & 0xff)); + m_data.push_back((char)((tmp.u >> 48) & 0xff)); + m_data.push_back((char)((tmp.u >> 40) & 0xff)); + m_data.push_back((char)((tmp.u >> 32) & 0xff)); + m_data.push_back((char)((tmp.u >> 24) & 0xff)); + m_data.push_back((char)((tmp.u >> 16) & 0xff)); + m_data.push_back((char)((tmp.u >> 8) & 0xff)); + m_data.push_back((char)(tmp.u & 0xff)); } void SimpleSerializer::writeBool(quint32 id, bool value) @@ -414,7 +426,7 @@ returnDefault: bool SimpleDeserializer::readFloat(quint32 id, float* result, float def) const { uint readOfs; - quint32 tmp; + union floatasint tmp; Elements::const_iterator it = m_elements.constFind(id); if(it == m_elements.constEnd()) goto returnDefault; @@ -424,10 +436,10 @@ bool SimpleDeserializer::readFloat(quint32 id, float* result, float def) const goto returnDefault; readOfs = it->ofs; - tmp = 0; + tmp.u = 0; for(int i = 0; i < 4; i++) - tmp = (tmp << 8) | readByte(&readOfs); - *result = *((float*)&tmp); + tmp.u = (tmp.u << 8) | readByte(&readOfs); + *result = tmp.f; return true; returnDefault: @@ -438,7 +450,7 @@ returnDefault: bool SimpleDeserializer::readDouble(quint32 id, double* result, double def) const { uint readOfs; - quint64 tmp; + union doubleasint tmp; Elements::const_iterator it = m_elements.constFind(id); if(it == m_elements.constEnd()) goto returnDefault; @@ -448,10 +460,10 @@ bool SimpleDeserializer::readDouble(quint32 id, double* result, double def) cons goto returnDefault; readOfs = it->ofs; - tmp = 0; + tmp.u = 0; for(int i = 0; i < 8; i++) - tmp = (tmp << 8) | readByte(&readOfs); - *result = *((double*)&tmp); + tmp.u = (tmp.u << 8) | readByte(&readOfs); + *result = tmp.d; return true; returnDefault: @@ -459,11 +471,21 @@ returnDefault: return false; } +union real4asint { + quint32 u; + Real r; +}; + +union real8asint { + quint64 u; + Real r; +}; + bool SimpleDeserializer::readReal(quint32 id, Real* result, Real def) const { if(sizeof(Real) == 4) { uint readOfs; - quint32 tmp; + union real4asint tmp; Elements::const_iterator it = m_elements.constFind(id); if(it == m_elements.constEnd()) goto returnDefault32; @@ -473,10 +495,10 @@ bool SimpleDeserializer::readReal(quint32 id, Real* result, Real def) const goto returnDefault32; readOfs = it->ofs; - tmp = 0; + tmp.u = 0; for(int i = 0; i < 4; i++) - tmp = (tmp << 8) | readByte(&readOfs); - *result = *((Real*)&tmp); + tmp.u = (tmp.u << 8) | readByte(&readOfs); + *result = tmp.r; return true; returnDefault32: @@ -484,7 +506,7 @@ bool SimpleDeserializer::readReal(quint32 id, Real* result, Real def) const return false; } else { uint readOfs; - quint64 tmp; + union real8asint tmp; Elements::const_iterator it = m_elements.constFind(id); if(it == m_elements.constEnd()) goto returnDefault64; @@ -494,10 +516,10 @@ bool SimpleDeserializer::readReal(quint32 id, Real* result, Real def) const goto returnDefault64; readOfs = it->ofs; - tmp = 0; + tmp.u = 0; for(int i = 0; i < 8; i++) - tmp = (tmp << 8) | readByte(&readOfs); - *result = *((Real*)&tmp); + tmp.u = (tmp.u << 8) | readByte(&readOfs); + *result = tmp.r; return true; returnDefault64: