From b2c8c3ff818e05e4d10413a5720222c8f635aa1c Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Tue, 16 May 2017 01:24:03 +0200 Subject: [PATCH] DelayQueue tuning: * Remove stringstream from DelayQueueMember::name * Use a bit mask instead of modulo in DelayQueue --- src/common/smartmod.hxx | 86 ++++++++++++++++++++++ src/emucore/tia/DelayQueue.hxx | 30 +++++--- src/emucore/tia/DelayQueueIteratorImpl.hxx | 18 ++--- src/emucore/tia/DelayQueueMember.hxx | 24 +++--- src/emucore/tia/TIA.hxx | 4 +- 5 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 src/common/smartmod.hxx diff --git a/src/common/smartmod.hxx b/src/common/smartmod.hxx new file mode 100644 index 000000000..bd1b293f1 --- /dev/null +++ b/src/common/smartmod.hxx @@ -0,0 +1,86 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-2017 by Bradford W. Mott, Stephen Anthony +// and the Stella Team +// +// See the file "License.txt" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +//============================================================================ + +#ifndef SMARTMOD_HXX +#define SMARTMOD_HXX + +#include "bspf.hxx" + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template +uInt8 smartmod(uInt8 x) +{ + return x % base; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<2>(uInt8 x) +{ + return x & 0x01; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<4>(uInt8 x) +{ + return x & 0x03; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<8>(uInt8 x) +{ + return x & 0x07; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<16>(uInt8 x) +{ + return x & 0x0F; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<32>(uInt8 x) +{ + return x & 0x1F; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<64>(uInt8 x) +{ + return x & 0x3F; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<128>(uInt8 x) +{ + return x & 0x7F; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template<> +inline uInt8 smartmod<256>(uInt8 x) +{ + return x & 0xFF; +} + +#endif // SMARTMOD_HXX \ No newline at end of file diff --git a/src/emucore/tia/DelayQueue.hxx b/src/emucore/tia/DelayQueue.hxx index dc81a9e5b..2a42812b6 100644 --- a/src/emucore/tia/DelayQueue.hxx +++ b/src/emucore/tia/DelayQueue.hxx @@ -20,12 +20,13 @@ #include "Serializable.hxx" #include "bspf.hxx" +#include "smartmod.hxx" #include "DelayQueueMember.hxx" -template +template class DelayQueueIteratorImpl; -template +template class DelayQueue : public Serializable { public: @@ -47,7 +48,7 @@ class DelayQueue : public Serializable */ bool save(Serializer& out) const override; bool load(Serializer& in) override; - string name() const override { return "TIA_DelayQueue"; } + string name() const override; private: DelayQueueMember myMembers[length]; @@ -66,7 +67,7 @@ class DelayQueue : public Serializable // ############################################################################ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template DelayQueue::DelayQueue() : myIndex(0) { @@ -74,7 +75,7 @@ DelayQueue::DelayQueue() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template void DelayQueue::push(uInt8 address, uInt8 value, uInt8 delay) { if (delay >= length) @@ -85,14 +86,14 @@ void DelayQueue::push(uInt8 address, uInt8 value, uInt8 delay) if (currentIndex < 0xFF) myMembers[currentIndex].remove(address); - uInt8 index = (myIndex + delay) % length; + uInt8 index = smartmod(myIndex + delay); myMembers[index].push(address, value); myIndices[address] = index; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template void DelayQueue::reset() { for (uInt8 i = 0; i < length; i++) @@ -103,7 +104,7 @@ void DelayQueue::reset() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template template void DelayQueue::execute(T executor) { @@ -116,11 +117,11 @@ void DelayQueue::execute(T executor) currentMember.clear(); - myIndex = (myIndex + 1) % length; + myIndex = smartmod(myIndex + 1); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template bool DelayQueue::save(Serializer& out) const { try @@ -143,7 +144,7 @@ bool DelayQueue::save(Serializer& out) const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template bool DelayQueue::load(Serializer& in) { try @@ -165,4 +166,11 @@ bool DelayQueue::load(Serializer& in) return true; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +template +string DelayQueue::name() const +{ + return "TIA_DelayQueue"; +} + #endif // TIA_DELAY_QUEUE diff --git a/src/emucore/tia/DelayQueueIteratorImpl.hxx b/src/emucore/tia/DelayQueueIteratorImpl.hxx index c84a36c33..819ffe132 100644 --- a/src/emucore/tia/DelayQueueIteratorImpl.hxx +++ b/src/emucore/tia/DelayQueueIteratorImpl.hxx @@ -23,7 +23,7 @@ #include "DelayQueueMember.hxx" #include "DelayQueueIterator.hxx" -template +template class DelayQueueIteratorImpl : public DelayQueueIterator { public: @@ -55,8 +55,8 @@ class DelayQueueIteratorImpl : public DelayQueueIterator // ############################################################################ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template -DelayQueueIteratorImpl::DelayQueueIteratorImpl( +template +DelayQueueIteratorImpl::DelayQueueIteratorImpl( const DelayQueue& delayQueue ) : myDelayQueue(delayQueue), @@ -68,14 +68,14 @@ DelayQueueIteratorImpl::DelayQueueIteratorImpl( } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template bool DelayQueueIteratorImpl::isValid() const { return myDelayCycle < length; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template uInt8 DelayQueueIteratorImpl::delay() const { if (!isValid()) { @@ -86,7 +86,7 @@ uInt8 DelayQueueIteratorImpl::delay() const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template uInt8 DelayQueueIteratorImpl::address() const { if (!isValid()) { @@ -97,7 +97,7 @@ uInt8 DelayQueueIteratorImpl::address() const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template uInt8 DelayQueueIteratorImpl::value() const { if (!isValid()) { @@ -108,7 +108,7 @@ uInt8 DelayQueueIteratorImpl::value() const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template bool DelayQueueIteratorImpl::next() { if (!isValid()) return false; @@ -126,7 +126,7 @@ bool DelayQueueIteratorImpl::next() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template uInt8 DelayQueueIteratorImpl::currentIndex() const { return (myDelayQueue.myIndex + myDelayCycle) % length; diff --git a/src/emucore/tia/DelayQueueMember.hxx b/src/emucore/tia/DelayQueueMember.hxx index fcd5564c2..754971197 100644 --- a/src/emucore/tia/DelayQueueMember.hxx +++ b/src/emucore/tia/DelayQueueMember.hxx @@ -21,7 +21,7 @@ #include "Serializable.hxx" #include "bspf.hxx" -template +template class DelayQueueMember : public Serializable { public: @@ -65,13 +65,13 @@ class DelayQueueMember : public Serializable { // ############################################################################ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template DelayQueueMember::DelayQueueMember() : mySize(0) {} // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template void DelayQueueMember::push(uInt8 address, uInt8 value) { if (mySize == capacity) throw runtime_error("delay queue overflow"); @@ -81,7 +81,7 @@ void DelayQueueMember::push(uInt8 address, uInt8 value) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template void DelayQueueMember::remove(uInt8 address) { uInt8 index; @@ -100,14 +100,14 @@ void DelayQueueMember::remove(uInt8 address) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template void DelayQueueMember::clear() { mySize = 0; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template bool DelayQueueMember::save(Serializer& out) const { try @@ -130,7 +130,7 @@ bool DelayQueueMember::save(Serializer& out) const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template bool DelayQueueMember::load(Serializer& in) { try @@ -154,14 +154,10 @@ bool DelayQueueMember::load(Serializer& in) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -template +template string DelayQueueMember::name() const { - stringstream ss; - - ss << "TIA_DelayQueueMember<" << capacity << ">"; - - return ss.str(); + return "TIA_DelayQueueMember"; } -#endif // TIA_DELAY_QUEUE_MEMBER +#endif // TIA_DELAY_QUEUE_MEMBER \ No newline at end of file diff --git a/src/emucore/tia/TIA.hxx b/src/emucore/tia/TIA.hxx index 9d773d73e..ad14fc9d9 100644 --- a/src/emucore/tia/TIA.hxx +++ b/src/emucore/tia/TIA.hxx @@ -449,8 +449,8 @@ class TIA : public Device Sound& mySound; Settings& mySettings; - static constexpr int delayQueueLength = 10; - static constexpr int delayQueueSize = 20; + static constexpr unsigned delayQueueLength = 16; + static constexpr unsigned delayQueueSize = 16; DelayQueue myDelayQueue; FrameManager myFrameManager;