From 396831b16f9d3fb5e8dfe58770e65b556b4c141c Mon Sep 17 00:00:00 2001 From: Morten Borup Petersen Date: Sat, 6 Apr 2019 09:53:10 +0100 Subject: [PATCH] Fix MULH[[S]U] instructions #34 Multiplications were executed in 32-bit types and as such the upper 32-bits which are required for the MULH instruction would be truncated. The result of which was that these instructions always returned 0. --- src/pipelineobjects.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/pipelineobjects.h b/src/pipelineobjects.h index 823eb5071..40639d468 100644 --- a/src/pipelineobjects.h +++ b/src/pipelineobjects.h @@ -5,12 +5,12 @@ #include "defines.h" #include "mainmemory.h" +#include #include #include #include #include #include -#include /* PIPELINE OBJECTS * all pipeline objects are built around the Signal class - a boolean vector of immutable size. @@ -71,17 +71,19 @@ class Signal : public SignalBase { // Casting operators explicit operator int() const { return signextend(m_value); } explicit operator uint32_t() const { return m_value; } + explicit operator uint64_t() const { return m_value; } + explicit operator int64_t() const { return signextend(static_cast(*this)); } explicit operator bool() const { return m_value & 0b1; } - Signal& operator = (const uint32_t& v){ + Signal& operator=(const uint32_t& v) { m_value = v; return *this; } - Signal& operator = (const int& v){ + Signal& operator=(const int& v) { *this = static_cast(v); return *this; } - Signal& operator = (const Signal& other){ + Signal& operator=(const Signal& other) { m_value = other.getValue(); return *this; } @@ -353,17 +355,17 @@ void ALU::update() { m_output = (int)*m_op1 * (int)*m_op2; break; case ALUOps::MULH: { - int64_t res = (int)*m_op1 * (int)*m_op2; + int64_t res = static_cast(*m_op1) * static_cast(*m_op2); m_output = static_cast(res >> 32); break; } case ALUOps::MULHU: { - int64_t res = (uint32_t)*m_op1 * (uint32_t)*m_op2; + int64_t res = static_cast(*m_op1) * static_cast(*m_op2); m_output = static_cast(res >> 32); break; } case ALUOps::MULHSU: { - int64_t res = (int)*m_op1 * (uint32_t)*m_op2; + int64_t res = static_cast(*m_op1) * static_cast(*m_op2); m_output = static_cast(res >> 32); break; }