It seems that the condition used in the ternary operation can affect the generated code.
It also appears that the triple option may generate less efficient code than a simple if / else.
So, changing the loop code in the second snippest to:
if (numerator >= 0) check += (int) ((numerator * qdi.Multiplier) >> qdi.Shift); else check += (int) -((-numerator * qdi.Multiplier) >> qdi.Shift);
or
if (numerator < 0) check += (int) -((-numerator * qdi.Multiplier) >> qdi.Shift); else check += (int) ((numerator * qdi.Multiplier) >> qdi.Shift);
or:
check += numerator < 0 ? (int) -((-numerator * qdi.Multiplier) >> qdi.Shift) : (int) ((numerator * qdi.Multiplier) >> qdi.Shift);
will create faster working code.
Actually, I find it a little worrying that three of the four combinations produce fast code, and the other can produce slow code ... sometimes.
source share