From adf506a41ecd9450ededbf5cb9c53b96722ecfa1 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sun, 25 Feb 2018 19:48:01 +0000 Subject: [PATCH] Optimisation: Prefer return by value to return by reference. ~10% speed-up! Just watch a video by Chandler Carruth from 2015, where he talked about C++ optimisers... Signed-off-by: Adrian Conlon --- Intel8080/src/Intel8080.cpp | 18 +++++++++--------- LR35902/src/LR35902.cpp | 14 +++++++------- M6502/inc/mos6502.h | 2 +- Z80/src/Z80.cpp | 24 ++++++++++++------------ inc/IntelProcessor.h | 6 +++--- inc/Processor.h | 22 +++++++++++----------- src/IntelProcessor.cpp | 8 +++++--- 7 files changed, 48 insertions(+), 46 deletions(-) diff --git a/Intel8080/src/Intel8080.cpp b/Intel8080/src/Intel8080.cpp index 44ba0d9..b9e3030 100644 --- a/Intel8080/src/Intel8080.cpp +++ b/Intel8080/src/Intel8080.cpp @@ -322,7 +322,7 @@ void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, i case 1: // 16-bit load immediate/add switch (q) { case 0: // LD rp,nn - fetchWord(RP(p)); + RP(p) = fetchWord(); addCycles(10); break; case 1: // ADD HL,rp @@ -346,12 +346,12 @@ void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, i addCycles(7); break; case 2: // LD (nn),HL - fetchWord(); + MEMPTR() = fetchWord(); setWord(HL()); addCycles(16); break; case 3: // LD (nn),A - fetchWord(); + MEMPTR() = fetchWord(); setByte(MEMPTR(), a); addCycles(13); break; @@ -370,12 +370,12 @@ void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, i addCycles(7); break; case 2: // LD HL,(nn) - fetchWord(); - getWord(HL()); + MEMPTR() = fetchWord(); + HL() = getWord(); addCycles(16); break; case 3: // LD A,(nn) - fetchWord(); + MEMPTR() = fetchWord(); a = getByte(MEMPTR()); addCycles(13); break; @@ -508,7 +508,7 @@ void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, i case 1: // POP & various ops switch (q) { case 0: // POP rp2[p] - popWord(RP2(p)); + RP2(p) = popWord(); addCycles(10); break; case 1: @@ -538,7 +538,7 @@ void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, i case 3: // Assorted operations switch (y) { case 0: // JP nn - fetchWord(); + MEMPTR() = fetchWord(); jump(); addCycles(10); break; @@ -582,7 +582,7 @@ void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, i case 1: switch (p) { case 0: // CALL nn - fetchWord(); + MEMPTR() = fetchWord(); call(); addCycles(17); break; diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 27bb3e9..cb2404d 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -424,7 +424,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int addCycle(); break; case 1: // GB: LD (nn),SP - fetchWord(); + MEMPTR() = fetchWord(); setWord(SP()); addCycles(5); break; @@ -451,7 +451,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int case 1: // 16-bit load immediate/add switch (q) { case 0: // LD rp,nn - fetchWord(RP(p)); + RP(p) = fetchWord(); addCycles(3); break; case 1: // ADD HL,rp @@ -674,7 +674,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int case 1: // POP & various ops switch (q) { case 0: // POP rp2[p] - popWord(RP2(p)); + RP2(p) = popWord(); addCycles(3); break; case 1: @@ -717,7 +717,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int addCycles(2); break; case 5: // GB: LD (nn),A - fetchWord(); + MEMPTR() = fetchWord(); setByte(MEMPTR(), a); addCycles(4); break; @@ -726,7 +726,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int addCycles(2); break; case 7: // GB: LD A,(nn) - fetchWord(); + MEMPTR() = fetchWord(); a = getByte(MEMPTR()); addCycles(4); break; @@ -737,7 +737,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int case 3: // Assorted operations switch (y) { case 0: // JP nn - fetchWord(); + MEMPTR() = fetchWord(); jump(); addCycles(4); break; @@ -769,7 +769,7 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int case 1: switch (p) { case 0: // CALL nn - fetchWord(); + MEMPTR() = fetchWord(); call(); addCycles(6); break; diff --git a/M6502/inc/mos6502.h b/M6502/inc/mos6502.h index 161ab55..4249f23 100644 --- a/M6502/inc/mos6502.h +++ b/M6502/inc/mos6502.h @@ -75,7 +75,7 @@ namespace EightBit { // Address resolution void Address_Absolute() { - fetchWord(); + MEMPTR() = fetchWord(); } void Address_ZeroPage() { diff --git a/Z80/src/Z80.cpp b/Z80/src/Z80.cpp index 0d270bb..ad6c8c9 100644 --- a/Z80/src/Z80.cpp +++ b/Z80/src/Z80.cpp @@ -870,12 +870,12 @@ void EightBit::Z80::executeED(uint8_t& a, uint8_t& f, const int x, const int y, case 3: // Retrieve/store register pair from/to immediate address switch (q) { case 0: // LD (nn), rp[p] - fetchWord(); + MEMPTR() = fetchWord(); setWord(RP(p)); break; case 1: // LD rp[p], (nn) - fetchWord(); - getWord(RP(p)); + MEMPTR() = fetchWord(); + RP(p) = getWord(); break; default: UNREACHABLE; @@ -1097,7 +1097,7 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int case 1: // 16-bit load immediate/add switch (q) { case 0: // LD rp,nn - fetchWord(RP(p)); + RP(p) = fetchWord(); addCycles(10); break; case 1: // ADD HL,rp @@ -1125,12 +1125,12 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int addCycles(7); break; case 2: // LD (nn),HL - fetchWord(); + MEMPTR() = fetchWord(); setWord(HL2()); addCycles(16); break; case 3: // LD (nn),A - fetchWord(); + MEMPTR() = fetchWord(); setByte(MEMPTR().word++, a); MEMPTR().high = a; addCycles(13); @@ -1152,12 +1152,12 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int addCycles(7); break; case 2: // LD HL,(nn) - fetchWord(); - getWord(HL2()); + MEMPTR() = fetchWord(); + HL2() = getWord(); addCycles(16); break; case 3: // LD A,(nn) - fetchWord(); + MEMPTR() = fetchWord(); a = getByte(MEMPTR().word++); addCycles(13); break; @@ -1328,7 +1328,7 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int case 1: // POP & various ops switch (q) { case 0: // POP rp2[p] - popWord(RP2(p)); + RP2(p) = popWord(); addCycles(10); break; case 1: @@ -1364,7 +1364,7 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int case 3: // Assorted operations switch (y) { case 0: // JP nn - fetchWord(); + MEMPTR() = fetchWord(); jump(); addCycles(10); break; @@ -1417,7 +1417,7 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int case 1: switch (p) { case 0: // CALL nn - fetchWord(); + MEMPTR() = fetchWord(); call(); addCycles(17); break; diff --git a/inc/IntelProcessor.h b/inc/IntelProcessor.h index aca5ed7..86b02ed 100644 --- a/inc/IntelProcessor.h +++ b/inc/IntelProcessor.h @@ -120,7 +120,7 @@ namespace EightBit { // - void getWord(register16_t& value); + register16_t getWord(); void setWord(register16_t value); // @@ -132,14 +132,14 @@ namespace EightBit { } bool callConditional(int condition) { - fetchWord(); + MEMPTR() = fetchWord(); if (condition) call(); return condition != 0; } bool jumpConditional(int conditional) { - fetchWord(); + MEMPTR() = fetchWord(); if (conditional) jump(); return conditional != 0; diff --git a/inc/Processor.h b/inc/Processor.h index 9449140..9437409 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -110,13 +110,11 @@ namespace EightBit { return getByte(PC().word++); } - void fetchWord(register16_t& output) { - output.low = fetchByte(); - output.high = fetchByte(); - } - - void fetchWord() { - fetchWord(MEMPTR()); + register16_t fetchWord() { + register16_t returned; + returned.low = fetchByte(); + returned.high = fetchByte(); + return returned; } uint8_t getByte() { return BUS().read(); } @@ -133,9 +131,11 @@ namespace EightBit { push(value.low); } - void popWord(register16_t& output) { - output.low = pop(); - output.high = pop(); + register16_t popWord() { + register16_t returned; + returned.low = pop(); + returned.high = pop(); + return returned; } void jump() { @@ -148,7 +148,7 @@ namespace EightBit { } void ret() { - popWord(MEMPTR()); + MEMPTR() = popWord(); jump(); } diff --git a/src/IntelProcessor.cpp b/src/IntelProcessor.cpp index 5dd2b96..f15ad6a 100644 --- a/src/IntelProcessor.cpp +++ b/src/IntelProcessor.cpp @@ -20,10 +20,12 @@ uint8_t EightBit::IntelProcessor::pop() { return getByte(SP().word++); } -void EightBit::IntelProcessor::getWord(register16_t& value) { - value.low = getByte(MEMPTR().word++); +EightBit::register16_t EightBit::IntelProcessor::getWord() { + register16_t returned; + returned.low = getByte(MEMPTR().word++); BUS().ADDRESS().word++; - value.high = getByte(); + returned.high = getByte(); + return returned; } void EightBit::IntelProcessor::setWord(register16_t value) {