Potential speed increase
Hello, guys.
I was thinking about the following:
stat += 100 * stat * modifier / 100
Now, I know why it's being used -- it's integer multiplication and division, which is way faster than floating point calculations.
Here's my idea:
stat += ((stat << 7) * modifier) >> 7
With this, you have only one integer multiplication instead two multiplications and one division.
I used to TA an assembly course at UIUC and we learned that integer shifts can be done in one clock cycle and integer multiplication can take quite a few clock cycles.
When I did verification simulations at AMD, I also verified that it takes multiple clock cycles on integer math (except for addition/subtraction) and AMD had the fastest ALU at the time.
However, shifts have always been able to be done in one clock cycle, guaranteed.
I know that 100 is a nice number to multiply by, but since its only use is to do a somewhat accurate integer percent calculation, I recommend doing a shift left (or right) by 7, which is multiplying by 128 and dividing my 128, only loads faster.
In the end, you're multiplying the whole equation by 1.
What is your opinion on this?
Thanks!
|