3 ms·
No, the compiler couldn't have kept resVal in a register because it doesn't know resVal doesn't alias with any of val1..val7. Therefore, it must store resVal ea
by brigade 14y ago
No, the compiler couldn't have kept resVal in a register because it doesn't know resVal doesn't alias with any of val1..val7. Therefore, it must store resVal each iteration, and it must load each of val1..val7 each iteration. Otherwise, it would violate the C++ standard.
Well, I'm assuming the programmer wrote it like that, because otherwise a smart compiler would have calculated val1+...+val7 before the loop and just added that precalculated value repeatedly.
Also it doesn't matter if it did keep resVal (and val1..val7) in a register, it would be every bit as fast as it is now.
Similarly, the compiler has no leeway about the order of floating point additions. If the C++ was written to have identical output as the asm, the compiler could not be faster than the asm without violating the C++ standard.
Again, OoOE plays absolutely positively no part whatsoever in making the asm slow. Neither does which values are in registers or memory.
It's slow because a chain of instructions that use the output of the previous as input cannot execute in fewer cycles than (latency)*(num instructions). Period.
It's the sort of thing that should have been written by the programmer as something like
res += (val1+val2)+(val3+val4);
res += (val5+val6)+(val7);
then we wouldn't be having this discussion...