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 <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2018-02-25 19:48:01 +00:00
parent d34b161255
commit adf506a41e
7 changed files with 48 additions and 46 deletions

View File

@ -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;

View File

@ -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;

View File

@ -75,7 +75,7 @@ namespace EightBit {
// Address resolution
void Address_Absolute() {
fetchWord();
MEMPTR() = fetchWord();
}
void Address_ZeroPage() {

View File

@ -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;

View File

@ -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;

View File

@ -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();
}

View File

@ -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) {