From 1f596a23af0baafab89c7d101868380ccfe07dc2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 23 Jan 2017 15:48:14 -0500 Subject: [PATCH] BPMemory: Eliminate union type punning This is undefined behavior in C++. --- Source/Core/VideoCommon/BPMemory.cpp | 24 ++++++++++++++++++++++++ Source/Core/VideoCommon/BPMemory.h | 24 ++---------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Source/Core/VideoCommon/BPMemory.cpp b/Source/Core/VideoCommon/BPMemory.cpp index 1665976018..67eeb994b1 100644 --- a/Source/Core/VideoCommon/BPMemory.cpp +++ b/Source/Core/VideoCommon/BPMemory.cpp @@ -4,6 +4,30 @@ #include "VideoCommon/BPMemory.h" +#include + // BP state // STATE_TO_SAVE BPMemory bpmem; + +float FogParam0::GetA() const +{ + // scale mantissa from 11 to 23 bits + const u32 integral = (static_cast(sign) << 31) | (static_cast(exponent) << 23) | + (static_cast(mantissa) << 12); + + float real; + std::memcpy(&real, &integral, sizeof(u32)); + return real; +} + +float FogParam3::GetC() const +{ + // scale mantissa from 11 to 23 bits + const u32 integral = (static_cast(c_sign) << 31) | (static_cast(c_exp) << 23) | + (static_cast(c_mant) << 12); + + float real; + std::memcpy(&real, &integral, sizeof(u32)); + return real; +} diff --git a/Source/Core/VideoCommon/BPMemory.h b/Source/Core/VideoCommon/BPMemory.h index 96a657fced..17033bfbe9 100644 --- a/Source/Core/VideoCommon/BPMemory.h +++ b/Source/Core/VideoCommon/BPMemory.h @@ -674,17 +674,7 @@ union FogParam0 u32 sign : 1; }; - float GetA() - { - union - { - u32 i; - float f; - } dummy; - dummy.i = ((u32)sign << 31) | ((u32)exponent << 23) | - ((u32)mantissa << 12); // scale mantissa from 11 to 23 bits - return dummy.f; - } + float GetA() const; u32 hex; }; @@ -701,17 +691,7 @@ union FogParam3 }; // amount to subtract from eyespacez after range adjustment - float GetC() - { - union - { - u32 i; - float f; - } dummy; - dummy.i = ((u32)c_sign << 31) | ((u32)c_exp << 23) | - ((u32)c_mant << 12); // scale mantissa from 11 to 23 bits - return dummy.f; - } + float GetC() const; u32 hex; };