My teacher claims that the processor can sometimes do FPU operations in parallel. Like this:
float a = 3.14;
float b = 5.12;
float c;
float d = 3.02;
float e = 2.52;
float f;
c = a + b;
f = e + d;
So, as I've heard, the 2 add operations above would be executed quicker than:
float a = 3.14;
float b = 5.12;
float c;
float d = 3.02;
float e = 2.52;
float f;
c = a + b;
f = c + d;
because the processor has to wait until c gets computed.
I wanted to verify this, so I wrote a function that does the second thing, and it measures the time by checking the Time Stamp Counter:
flds h # st(7)
flds g # st(6)
flds f # st(5)
flds e # st(4)
flds d # st(3)
flds c # st(2)
flds b # st(1)
flds a # st(0)
fadd %st, %st(1) # i = a + b
fmul %st, %st(2) # j = i * c
fadd %st, %st(3) # k = j + d
fmul %st, %st(4) # l = k + e
fadd %st, %st(5) # m = l + f
fmul %st, %st(6) # n = m * g
fadd %st, %st(7) # o = n + h
Those are not independent. Now, I am trying to write independent ones. But the problem is, no matter what I actually do, the value is always saved to ST(0) (no matter which instruction I use), optionally it can then be popped, but that still means we have to wait until computation.
I looked at the code generated by a compiler (gcc -S). It simply doesn't operate like this on st registers. For every number, it does:
flds number
fstps -some_value(%ebp)
And then (for example, for a and b, where -4(%ebp) is a, -8(%ebp) is b):
flds -4(%ebp)
fadds -8(%ebp) # i = a + b
fstps -32(%ebp)
So it firstly loads to FPU, and pops back to the normal stack. Then, it pops one value (to st(0)), adds to that value, and the result is popped back. So it's still not independent, because we have to wait until st(0) gets freed.
Did my teacher say something wrong, or is there a way to make them independent that would give a noticeably different execution time when I measure it?