From 9162c86e21045f79f25b5e7f5b99fed722a208c0 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 20 Feb 2025 22:42:02 -0500 Subject: [PATCH] Test, improve BitStream. --- InstructionSets/CachingExecutor.hpp | 4 ++-- InstructionSets/Disassembler.hpp | 2 +- InstructionSets/M68k/Decoder.hpp | 4 ++-- Numeric/BitStream.hpp | 16 ++++++++----- Numeric/LFSR.hpp | 2 +- Numeric/Sizes.hpp | 24 ++++++++++++------- .../Clock Signal.xcodeproj/project.pbxproj | 4 ++++ 7 files changed, 35 insertions(+), 21 deletions(-) diff --git a/InstructionSets/CachingExecutor.hpp b/InstructionSets/CachingExecutor.hpp index 160b3ecfc..16b7753cf 100644 --- a/InstructionSets/CachingExecutor.hpp +++ b/InstructionSets/CachingExecutor.hpp @@ -46,8 +46,8 @@ template < > class CachingExecutor { public: using Performer = void (Executor::*)(); - using PerformerIndex = typename MinIntTypeValue::type; - using ProgramCounterType = typename MinIntTypeValue::type; + using PerformerIndex = min_int_for_value_t; + using ProgramCounterType = min_int_for_value_t; // MARK: - Parser call-ins. diff --git a/InstructionSets/Disassembler.hpp b/InstructionSets/Disassembler.hpp index 79d3bf603..acd295e15 100644 --- a/InstructionSets/Disassembler.hpp +++ b/InstructionSets/Disassembler.hpp @@ -29,7 +29,7 @@ template < typename AddressType > class Disassembler { public: - using ProgramCounterType = typename MinIntTypeValue::type; + using ProgramCounterType = min_int_for_value_t; /*! Adds the result of disassembling @c memory which is @c length @c MemoryWords long from @c start_address diff --git a/InstructionSets/M68k/Decoder.hpp b/InstructionSets/M68k/Decoder.hpp index 5ace4220b..ce2b0dc54 100644 --- a/InstructionSets/M68k/Decoder.hpp +++ b/InstructionSets/M68k/Decoder.hpp @@ -54,9 +54,9 @@ private: // integer size large enough to hold all actual operations plus the ephemeral // ones used here. Intention is to support table-based decoding, which will mean // making those integers less ephemeral, hence the desire to pick a minimum size. - using OpT = typename MinIntTypeValue< + using OpT = min_int_for_value_t< uint64_t(OperationMax::value) + 67 - >::type; + >; static constexpr auto OpMax = OpT(OperationMax::value); // Specific instruction decoders. diff --git a/Numeric/BitStream.hpp b/Numeric/BitStream.hpp index 78356e09f..98f879dee 100644 --- a/Numeric/BitStream.hpp +++ b/Numeric/BitStream.hpp @@ -10,6 +10,8 @@ #include "BitReverse.hpp" #include "Sizes.hpp" + +#include #include namespace Numeric { @@ -23,7 +25,7 @@ namespace Numeric { template class BitStream { public: - using IntT = min_int_size_t; + using IntT = min_int_for_bits_t; BitStream(std::function next_byte) : next_byte_(next_byte) {} @@ -33,6 +35,9 @@ public: /// `rbits` is ignored if `bits` is non-zero. template IntT next([[maybe_unused]] const size_t rbits = 0) { + static_assert(bits <= max_bits); + static constexpr size_t ShiftBitSize = sizeof(ShiftIntT) * 8; + const size_t required = bits ? bits : rbits; while(enqueued_ < required) { uint8_t next = next_byte_(); @@ -40,11 +45,11 @@ public: next = bit_reverse(next); } - input_ |= IntT(next) << (BitSize - 8 - enqueued_); + input_ |= ShiftIntT(next) << (ShiftBitSize - 8 - enqueued_); enqueued_ += 8; } - const auto result = IntT(input_ >> (BitSize - required)); + const auto result = IntT(input_ >> (ShiftBitSize - required)); input_ <<= required; enqueued_ -= required; return result; @@ -53,10 +58,9 @@ public: private: std::function next_byte_; + using ShiftIntT = min_int_for_bits_t; + ShiftIntT input_{}; size_t enqueued_{}; - - IntT input_{}; - static constexpr size_t BitSize = sizeof(IntT) * 8; }; } diff --git a/Numeric/LFSR.hpp b/Numeric/LFSR.hpp index 117ae7dad..f2cf4f7d8 100644 --- a/Numeric/LFSR.hpp +++ b/Numeric/LFSR.hpp @@ -77,6 +77,6 @@ private: IntType value_ = 0; }; -template class LFSRv: public LFSR::type, polynomial> {}; +template class LFSRv: public LFSR, polynomial> {}; } diff --git a/Numeric/Sizes.hpp b/Numeric/Sizes.hpp index 43b3940db..a6fbb8811 100644 --- a/Numeric/Sizes.hpp +++ b/Numeric/Sizes.hpp @@ -30,7 +30,7 @@ template using uint_t = typename uint_t_impl::type; * uint64_t. */ template -struct MinIntTypeValue { +struct MinIntForValue { using type = std::conditional_t< max_value <= std::numeric_limits::max(), uint8_t, @@ -43,7 +43,7 @@ struct MinIntTypeValue { > >; }; -template using min_int_value_t = typename MinIntTypeValue::type; +template using min_int_for_value_t = typename MinIntForValue::type; /*! Maps to the smallest integral type that can hold at least `max_bits` bits, from the following options: @@ -53,19 +53,25 @@ template using min_int_value_t = typename MinIntTypeValue -struct MinIntTypeSize { - static_assert(max_bits <= 64, "Only integers up to 64 bits are supported"); +template +struct MinIntForBits { + static_assert(min_bits <= 64, "Only integers up to 64 bits are supported"); using type = std::conditional_t< - max_bits <= 8, uint8_t, + min_bits <= 8, uint8_t, std::conditional_t< - max_bits <= 16, uint16_t, + min_bits <= 16, uint16_t, std::conditional_t< - max_bits <= 32, uint32_t, + min_bits <= 32, uint32_t, uint64_t > > >; }; -template using min_int_size_t = typename MinIntTypeValue::type; +template using min_int_for_bits_t = typename MinIntForBits::type; + +static_assert(std::is_same_v, uint8_t>); +static_assert(std::is_same_v, uint8_t>); +static_assert(std::is_same_v, uint16_t>); +static_assert(std::is_same_v, uint16_t>); +static_assert(std::is_same_v, uint32_t>); diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 9532d577f..349a4449f 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -235,6 +235,7 @@ 4B0ACC3123775819008902D0 /* TIASound.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B0ACC2123775819008902D0 /* TIASound.cpp */; }; 4B0ACC3223775819008902D0 /* Atari2600.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B0ACC2223775819008902D0 /* Atari2600.cpp */; }; 4B0ACC3323775819008902D0 /* Atari2600.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B0ACC2223775819008902D0 /* Atari2600.cpp */; }; + 4B0B23A12D6826DE00153879 /* NumericTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B0B23A02D6826DE00153879 /* NumericTests.mm */; }; 4B0CCC451C62D0B3001CAC5F /* CRT.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B0CCC421C62D0B3001CAC5F /* CRT.cpp */; }; 4B0DA67B282DCDF100C12F17 /* Instruction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B0DA67A282DCC4200C12F17 /* Instruction.cpp */; }; 4B0DA67C282DCDF300C12F17 /* Instruction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B0DA67A282DCC4200C12F17 /* Instruction.cpp */; }; @@ -1372,6 +1373,7 @@ 4B0ACC2423775819008902D0 /* PIA.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = PIA.hpp; sourceTree = ""; }; 4B0ACC2523775819008902D0 /* TIA.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = TIA.hpp; sourceTree = ""; }; 4B0B239F2D658C9400153879 /* BitStream.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = BitStream.hpp; sourceTree = ""; }; + 4B0B23A02D6826DE00153879 /* NumericTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NumericTests.mm; sourceTree = ""; }; 4B0CCC421C62D0B3001CAC5F /* CRT.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CRT.cpp; sourceTree = ""; }; 4B0CCC431C62D0B3001CAC5F /* CRT.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CRT.hpp; sourceTree = ""; }; 4B0DA67A282DCC4200C12F17 /* Instruction.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Instruction.cpp; sourceTree = ""; }; @@ -4702,6 +4704,7 @@ 4BA91E1C216D85BA00F79557 /* MasterSystemVDPTests.mm */, 4BC6237126F94BCB00F83DFE /* MintermTests.mm */, 4B98A0601FFADCDE00ADF63B /* MSXStaticAnalyserTests.mm */, + 4B0B23A02D6826DE00153879 /* NumericTests.mm */, 4BC0CB272446BC7B00A79DBB /* OPLTests.mm */, 4B121F9A1E06293F00BFDA12 /* PCMSegmentEventSourceTests.mm */, 4BD4A8CF1E077FD20020D856 /* PCMTrackTests.mm */, @@ -6631,6 +6634,7 @@ 4B06AB022C6460CB0034D014 /* Typer.cpp in Sources */, 4BA91E1D216D85BA00F79557 /* MasterSystemVDPTests.mm in Sources */, 4B98A0611FFADCDE00ADF63B /* MSXStaticAnalyserTests.mm in Sources */, + 4B0B23A12D6826DE00153879 /* NumericTests.mm in Sources */, 4BE34438238389E10058E78F /* AtariSTVideoTests.mm in Sources */, 4BEF6AAC1D35D1C400E73575 /* DPLLTests.swift in Sources */, 4BE76CF922641ED400ACD6FA /* QLTests.mm in Sources */,