Kevin Vance - Optimizing compilers are sick

Entries | Archive | Friends | Friends' Friends | User Info

01:47 am

Optimizing compilers are sick

Sunday, June 24th, 2007
Previous Entry Share Next Entry
Evidently,

    f(x) = x / 60

is better stated as

    f(x) = (0x88888889 * x) >> 37

*closes disassembler*

...

*hides in a corner*
Link )Reply )

Comments
[User Picture]From: det3
2007-06-24 06:43 pm (UTC)
Ah... I love the smell of burned opcodes in the morning.

But, that's the way the ALU would do it too... multiply then shift. I would assume that it's quicker to multiply by a constant and shift rather than have to fetch two memory locations and perform the division operation. Is this on X86?
(Reply) (Thread)
[User Picture]From: kvance
2007-06-24 07:13 pm (UTC)
Yes, x86. GCC even does it at -O0.

I've never heard of this before, but it makes sense.
(Reply) (Parent) (Thread)
[User Picture]From: det3
2007-06-24 07:28 pm (UTC)
Ja. A lot can be gleaned from the IA-32 Intel Architecture Software Developer's Manual. It's a 4-book three volume set that goes into every nook and cranny of the Intel implementation of X86. You can download the PDFs from that URL, or you can order a printed copy for free if you prefer treeware.
(Reply) (Parent) (Thread)
[User Picture]From: kvance
2007-06-24 07:43 pm (UTC)
Ah, some light reading :)

Thanks for the link. I'm guessing there's a lot of stuff in there that I really *should* know by now.
(Reply) (Parent) (Thread)
[User Picture]From: det3
2007-06-24 07:46 pm (UTC)
And many things you don't ever EVER want to know. It's like the difference between being a boyfriend and a OB-GYN.

My brain got sufficiently twisted when I looked through the way the CPU does memory management. I think I irrevocably damaged some gray matter in that incident.

But seriously, if you ever want to hand-optimize code, the 'blue crate' as I call it is invaluable. I use it for optimizing DSP algorithms through the SIMD pipelines, among other things.
(Reply) (Parent) (Thread)