From d199adb027f91fbbcf1644540c9b5426e448f961 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sat, 29 May 2021 10:31:32 +0100 Subject: [PATCH] Tidy EightBit library header usage (avoids compilation error with latest VS2019, "Memory.h") Signed-off-by: Adrian Conlon --- inc/EightBitCompilerDefinitions.h | 45 ++++++++++++++----------------- inc/Processor.h | 8 +++++- src/BigEndianProcessor.cpp | 2 +- src/Bus.cpp | 8 +++--- src/ClockedChip.cpp | 2 +- src/Device.cpp | 2 +- src/EightBit.vcxproj | 4 --- src/EventArgs.cpp | 2 +- src/InputOutput.cpp | 4 +-- src/IntelHexFile.cpp | 2 +- src/IntelProcessor.cpp | 2 +- src/LittleEndianProcessor.cpp | 2 +- src/Makefile | 2 -- src/Memory.cpp | 2 +- src/Processor.cpp | 10 +------ src/Ram.cpp | 2 +- src/Rom.cpp | 2 +- src/UnusedMemory.cpp | 2 +- 18 files changed, 45 insertions(+), 58 deletions(-) diff --git a/inc/EightBitCompilerDefinitions.h b/inc/EightBitCompilerDefinitions.h index 6fec689..0481030 100644 --- a/inc/EightBitCompilerDefinitions.h +++ b/inc/EightBitCompilerDefinitions.h @@ -1,14 +1,15 @@ #pragma once -#include - #ifdef _MSC_VER # include #endif #ifdef __GNUG__ # include -#else +#endif + +#if !(defined(_MSC_VER) || defined(__GNUG__)) +# include # include #endif @@ -16,12 +17,13 @@ namespace EightBit { [[nodiscard]] int countBits(uint8_t value) noexcept; [[nodiscard]] bool oddParity(uint8_t value) noexcept; [[nodiscard]] int findFirstSet(unsigned long value) noexcept; - constexpr void assume(int expression); } inline int EightBit::countBits(uint8_t value) noexcept { -#ifdef _MSC_VER +#if defined(_MSC_VER) return __popcnt(value); +#elif defined(__GNUG__) + return __builtin_popcount(value); #else /* Published in 1988, the C Programming Language 2nd Ed. @@ -40,12 +42,16 @@ inline int EightBit::countBits(uint8_t value) noexcept { } inline bool EightBit::oddParity(const uint8_t value) noexcept { +#ifdef __GNUG__ + return __builtin_parity(value) +#else return countBits(value) % 2; +#endif } inline int EightBit::findFirstSet(const unsigned long value) noexcept { -#ifdef _MSC_VER - unsigned long index; +#if defined(_MSC_VER) + unsigned long index = 0; if (_BitScanForward(&index, value)) return index + 1; return 0; @@ -60,44 +66,33 @@ inline int EightBit::findFirstSet(const unsigned long value) noexcept { #endif } -inline constexpr void EightBit::assume(const int expression) { -#ifdef _MSC_VER - __assume(expression); -#elif defined(__GNUG__) - if (!expression) - __builtin_unreachable(); -#else - assert(expression); -#endif -} - -#define ASSUME(x) EightBit::assume(x) +#define PARITY(x) EightBit::oddParity(x) #ifdef _MSC_VER +# define ASSUME(x) __assume(x); + # define LIKELY(x) (x) # define UNLIKELY(x) (x) -# define PARITY(x) EightBit::oddParity(x) - # define UNREACHABLE { ASSUME(0); throw std::exception("unreachable"); } #elif defined(__GNUG__) +# define ASSUME(x) { if (!x) __builtin_unreachable(); } + # define LIKELY(x) __builtin_expect(!!(x), 1) # define UNLIKELY(x) __builtin_expect(!!(x), 0) -# define PARITY(x) __builtin_parity(x) - # define UNREACHABLE __builtin_unreachable(); #else +# define ASSUME(x) assert(x); + # define LIKELY(x) (x) # define UNLIKELY(x) (x) -# define PARITY(x) EightBit::oddParity(x) - # define UNREACHABLE ASSUME(0) #endif diff --git a/inc/Processor.h b/inc/Processor.h index 80c5047..c79308e 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -11,9 +11,15 @@ namespace EightBit { class Processor : public ClockedChip { public: + // http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend // b: number of bits representing the number in x // x: sign extend this b-bit number to r - [[nodiscard]] static int8_t signExtend(int b, uint8_t x) noexcept; + [[nodiscard]] static constexpr int8_t signExtend(int b, uint8_t x) noexcept { + const uint8_t m = bit(b - 1); // mask can be pre-computed if b is fixed + x = x & (bit(b) - 1); // (Skip this if bits in x above position b are already zero.) + const auto result = (x ^ m) - m; + return result; + } ~Processor() = default; diff --git a/src/BigEndianProcessor.cpp b/src/BigEndianProcessor.cpp index 527bdb9..3d6334b 100644 --- a/src/BigEndianProcessor.cpp +++ b/src/BigEndianProcessor.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "BigEndianProcessor.h" +#include "../inc/BigEndianProcessor.h" EightBit::BigEndianProcessor::BigEndianProcessor(Bus& memory) : Processor(memory) {} diff --git a/src/Bus.cpp b/src/Bus.cpp index b4f8b14..cdcc1b1 100644 --- a/src/Bus.cpp +++ b/src/Bus.cpp @@ -1,8 +1,8 @@ #include "stdafx.h" -#include "Bus.h" -#include "Ram.h" -#include "IntelHexFile.h" -#include "EightBitCompilerDefinitions.h" +#include "../inc/Bus.h" +#include "../inc/Ram.h" +#include "../inc/IntelHexFile.h" +#include "../inc/EightBitCompilerDefinitions.h" void EightBit::Bus::raisePOWER() {} diff --git a/src/ClockedChip.cpp b/src/ClockedChip.cpp index 86f5bef..74697c5 100644 --- a/src/ClockedChip.cpp +++ b/src/ClockedChip.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "ClockedChip.h" +#include "../inc/ClockedChip.h" void EightBit::ClockedChip::tick(const int extra) { for (int i = 0; i < extra; ++i) diff --git a/src/Device.cpp b/src/Device.cpp index 3b96d04..26792d3 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -1,4 +1,4 @@ #include "stdafx.h" -#include "Device.h" +#include "../inc/Device.h" DEFINE_PIN_LEVEL_CHANGERS(POWER, Device); diff --git a/src/EightBit.vcxproj b/src/EightBit.vcxproj index 45ab58e..07106e7 100644 --- a/src/EightBit.vcxproj +++ b/src/EightBit.vcxproj @@ -70,19 +70,15 @@ - ..\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath) AllRules.ruleset - ..\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath) AllRules.ruleset - ..\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath) AllRules.ruleset - ..\inc;$(VC_IncludePath);$(WindowsSDK_IncludePath) AllRules.ruleset diff --git a/src/EventArgs.cpp b/src/EventArgs.cpp index 0a11942..19d7781 100644 --- a/src/EventArgs.cpp +++ b/src/EventArgs.cpp @@ -1,4 +1,4 @@ #include "stdafx.h" -#include "EventArgs.h" +#include "../inc/EventArgs.h" EightBit::EventArgs EightBit::EventArgs::m_empty; diff --git a/src/InputOutput.cpp b/src/InputOutput.cpp index 8d60061..9e911a3 100644 --- a/src/InputOutput.cpp +++ b/src/InputOutput.cpp @@ -1,9 +1,9 @@ #include "stdafx.h" -#include "InputOutput.h" +#include "../inc/InputOutput.h" #include -#include "Register.h" +#include "../inc/Register.h" size_t EightBit::InputOutput::size() const noexcept { return 0x100; diff --git a/src/IntelHexFile.cpp b/src/IntelHexFile.cpp index 0bd364d..7aae45d 100644 --- a/src/IntelHexFile.cpp +++ b/src/IntelHexFile.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "IntelHexFile.h" +#include "../inc/IntelHexFile.h" #include diff --git a/src/IntelProcessor.cpp b/src/IntelProcessor.cpp index b060891..9081982 100644 --- a/src/IntelProcessor.cpp +++ b/src/IntelProcessor.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "IntelProcessor.h" +#include "../inc/IntelProcessor.h" EightBit::IntelProcessor::IntelProcessor(Bus& bus) : LittleEndianProcessor(bus) { diff --git a/src/LittleEndianProcessor.cpp b/src/LittleEndianProcessor.cpp index d0e356f..c03e589 100644 --- a/src/LittleEndianProcessor.cpp +++ b/src/LittleEndianProcessor.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "LittleEndianProcessor.h" +#include "../inc/LittleEndianProcessor.h" EightBit::LittleEndianProcessor::LittleEndianProcessor(Bus& memory) : Processor(memory) {} diff --git a/src/Makefile b/src/Makefile index f8f28ab..9a55500 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,5 @@ LIB = libeightbit.a -CXXFLAGS = -I ../inc - CXXFILES = BigEndianProcessor.cpp Bus.cpp ClockedChip.cpp Device.cpp EventArgs.cpp InputOutput.cpp IntelHexFile.cpp IntelProcessor.cpp LittleEndianProcessor.cpp Memory.cpp Processor.cpp Ram.cpp Rom.cpp UnusedMemory.cpp include ../compile.mk diff --git a/src/Memory.cpp b/src/Memory.cpp index e87441a..6e022e9 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "Memory.h" +#include "../inc/Memory.h" #include diff --git a/src/Processor.cpp b/src/Processor.cpp index e43ba5b..1043614 100644 --- a/src/Processor.cpp +++ b/src/Processor.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "Processor.h" +#include "../inc/Processor.h" EightBit::Processor::Processor(Bus& bus) : m_bus(bus) { @@ -86,14 +86,6 @@ int EightBit::Processor::execute(const uint8_t value) { return execute(); } -// http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend -int8_t EightBit::Processor::signExtend(const int b, uint8_t x) noexcept { - const uint8_t m = bit(b - 1); // mask can be pre-computed if b is fixed - x = x & (bit(b) - 1); // (Skip this if bits in x above position b are already zero.) - const auto result = (x ^ m) - m; - return result; -} - void EightBit::Processor::jump(const register16_t destination) noexcept { PC() = destination; } diff --git a/src/Ram.cpp b/src/Ram.cpp index 7c6a78e..bed23f3 100644 --- a/src/Ram.cpp +++ b/src/Ram.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "Ram.h" +#include "../inc/Ram.h" EightBit::Ram::Ram(const size_t size) noexcept : Rom(size) {} diff --git a/src/Rom.cpp b/src/Rom.cpp index 3a73389..6dfc257 100644 --- a/src/Rom.cpp +++ b/src/Rom.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "Rom.h" +#include "../inc/Rom.h" #include diff --git a/src/UnusedMemory.cpp b/src/UnusedMemory.cpp index 6ce4dc7..0d9ada3 100644 --- a/src/UnusedMemory.cpp +++ b/src/UnusedMemory.cpp @@ -1,5 +1,5 @@ #include "stdafx.h" -#include "UnusedMemory.h" +#include "../inc/UnusedMemory.h" EightBit::UnusedMemory::UnusedMemory(const size_t size, const uint8_t value) : m_size(size), m_value(value) {}