First post, by superfury
I just found out something strange. When I implement a simple if-else clause, the compiler seems to always compile it to a jump to the else clause, instead of the else clause being the one not jumped to.
So if I have the following code:
counter = (counter!=PWM->Amplitude); //Are we not to expire?
counter |= PWM->output; //Are we not to expire(already expired)?
if (counter) //To give a result?
{
result = PWM->result; //Get the result!
}
else //Are we to expire?
{
//Finished PWM sample!
PWM->output = AMPENV_RESULT_SILENCE; //We're finished! Return to 0V always for the rest of the period!
result = PWM->result = 0; //No output anymore!
}
return result; //Give the proper output as a 16-bit sample!
The compiler will generate assembly code that makes the if(counter) roughly equivalent to a "jz elseclauseaddress". This is kind of counter-intuitive, since (as far as I know) having an if condition being true is usually supposed to jump to the code within it, otherwise normally continuing executing the next instruction(which is supposed to be the else clause).
So you would expect a simple "if(x) whatifx else whatifnotx" to be compiled into a:
cmp x,0(might be omitted)
jnz whatifx
whatifnotx
jmp done
whatifx:
whatifx
done:
But instead, it reverses the whole thing, making it jump to the else section if x is zero and not jump if x isn't zero? Why would it do this(it destroys properly using the CPU caches)?
Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io