Skip to content

Commit

Permalink
Rewrite float Cop1::convert(uint32_t) so that it no longer breaks str…
Browse files Browse the repository at this point in the history
…ict-aliasing rules
  • Loading branch information
Souzooka committed May 3, 2019
1 parent 17e46b2 commit cb956c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/core/ee/cop1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cmath>
#include <cstdio>
#include <cstring>
#include "cop1.hpp"

#define printf(fmt, ...)(0)
Expand All @@ -10,19 +11,22 @@ Cop1::Cop1()
}

//Returns a floating point value that may be changed to accomodate the FPU's non-compliance to the IEEE 754 standard.
float Cop1::convert(uint32_t value)
float Cop1::convert(uint32_t value) const noexcept
{
float result;
switch(value & 0x7F800000)
{
case 0x0:
value &= 0x80000000;
return *(float*)&value;
break;
case 0x7F800000:
value = (value & 0x80000000)|0x7F7FFFFF;
return *(float*)&value;
break;
default:
return *(float*)&value;
break;
}
memcpy(&result, &value, sizeof(float));
return result;
}

void Cop1::check_overflow(uint32_t& dest, bool set_flags)
Expand Down
2 changes: 1 addition & 1 deletion src/core/ee/cop1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Cop1
COP1_REG gpr[32];
COP1_REG accumulator;

float convert(uint32_t value);
float convert(uint32_t value) const noexcept;
void check_overflow(uint32_t& dest, bool set_flags);
void check_underflow(uint32_t& dest, bool set_flags);
public:
Expand Down

0 comments on commit cb956c6

Please sign in to comment.