3 ms·
While I was in undergrad I toyed around with abusing the branch predictor on a few different machines, compiling something like the following with optimizations
by snerbles 4y ago
While I was in undergrad I toyed around with abusing the branch predictor on a few different machines, compiling something like the following with optimizations off - it performs an identical computation regardless of branch outcome:
void branchLoop(unsigned int condition, unsigned int &sum)
{
// put something suitably large here
unsigned int loopCount = 0x0fffffff;
unsigned int i;
// compile with -O0 or this gets optimized away
for (i = 0; i < loopCount; i++)
if ((i & condition) == 0)
sum++;
else
sum++;
}
The Core Duo on my Thinkpad T60 had some very distinct slowdowns on certain bit patterns, which were not repeatable on the handful of other CPUs I had access to at the time. I haven't tried this with more modern CPUs, however.
- gpderetta 4y agoPredictors are getting better and better at recognizing long patterns (sometime at the cost of not being optimal with short patterns).