So, I'm making a Hack CPU emulator, and I was wondering what the best way to calculate the output was. Would condensing the output calculations into one unreadable line be more efficient than calculating the result one step at a time? Does the compiler optimize it such that both options are fine? Basically, which of these is more efficient --
this:
word HackALU(word x, word y, bool zx, bool nx, bool zy, bool ny, bool f, bool no) { x = zx ? 0 : x; y = zy ? 0 : y; x = nx ? ~x : x; y = ny ? ~y : y; word result = f ? x + y : x & y; return no ? ~result : result; }
or this:
word HackALU(word x, word y, bool zx, bool nx, bool zy, bool ny, bool f, bool no) { return no ? ~(f ? ((nx ? ~(zx ? 0 : x) : (zx ? 0 : x)) + (ny ? ~(zy ? 0 : y) : (zy ? 0 : y))) : ((nx ? ~(zx ? 0 : x) : (zx ? 0 : x)) & (ny ? ~(zy ? 0 : y) : (zy ? 0 : y)))) : (f ? ((nx ? ~(zx ? 0 : x) : (zx ? 0 : x)) + (ny ? ~(zy ? 0 : y) : (zy ? 0 : y))) : ((nx ? ~(zx ? 0 : x) : (zx ? 0 : x)) & (ny ? ~(zy ? 0 : y) : (zy ? 0 : y)))); }
A good modern compiler will most likely generate identical code for both.