Seems much too slow for what it is. (using GCC 3.2 -O3)Code:`int dx, dy;`

...

return ((dx << 4) + (dx << 2) + (dy << 4) - (dy << 2)) >> 4;

Printable View

- 07-17-2005joedslow functionCode:
`int dx, dy;`

...

return ((dx << 4) + (dx << 2) + (dy << 4) - (dy << 2)) >> 4;

- 07-17-2005Salem
How did you measure how long it takes?

It generates all of 11 instructions here, difficult to see how it could be any less since you start out with 8 operators in your expression. - 07-17-2005laserlight
Could simplify to:

(5 * dx + 3 * dy) / 4

though since you like using the shifts:

(dx + (dx << 2) + dy + (dy << 1)) >> 2 - 07-17-2005Salem
Ah, it's not premature optimisation at work is it?

- 07-17-2005laserlightQuote:

Ah, it's not premature optimisation at work is it?

- 07-17-2005joed
It's part of a slowish rendering thing, and runs this function hundreds of times per pixel. There are a couple of oddities in the generated code, like jumping to a subroutine just to negate ecx, then jumping back. I'll try rewriting it in asm, thanks for the help.

- 07-17-2005quzah
Get rid of your function. Make it a macro.

Quzah. - 07-17-2005BrianQuote:

Originally Posted by**quzah**

Exactly, it's probably the overhead of calling the function more than anything.

Alternately you could put "inline" before the function definition but making it a macro would be better practice.