I am a relatively new C programmer and I am writing a chess program that I want to make sure it runs as fast as possible. Here is a snippet of code from my program inside a loop in which I am encoding moves into a 16-bit variable. Each square takes up 6 bits.
Square targetSquare = GET_LS1B(leftPawnAttacks);
moveList[i] = (targetSquare << 4) | ((targetSquare - 7) << 10);
What I want to do, to improve readability, is define a new variable called originSquare like so
Square targetSquare = GET_LS1B(leftPawnAttacks);
Square originSquare = targetSquare - 7;
moveList[i] = (targetSquare << 4) | (originSquare << 10);
So the first 6 bits represent the origin square of the move and the next 6 represent the target square. In this particular loop, all the moves will be of the same type: the origin square will always be -7 from the target.
I don't want to waste instructions declaring Square originSquare and then looking up its value 1 line later, since this section of a loop is running thousands of times per second.
My question is: is there a way to tell the C compiler or preprocessor to just shove targetSquare - 7 where it sees originSquare within the scope of this loop, i.e. inline the variable, or will it do this already?
My best guess is that if I declare the variable const the compiler will likely do this on its own, but I'd like to be sure and learn more about C and compilation.