Version franaise
Home About Download Resources Contact us

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: optimize div to right shift (NOT!)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-12-13 (13:03)
From: Benedikt Meurer <benedikt.meurer@g...>
Subject: Re: [Caml-list] Re: optimize div to right shift (NOT!)

On Dec 13, 2010, at 13:58 , Trk Edwin wrote:

> It is still possible to avoid the division, gcc generates this:
> 	movq	%rdi, %rax
> 	shrq	$63, %rax
> 	addq	%rdi, %rax
> 	sarq	%rax
> Or a better example with division by 8:
> 	leaq	7(%rdi), %rax
> 	testq	%rdi, %rdi
> 	cmovns	%rdi, %rax
> 	sarq	$3, %rax

ocamlopt does exactly this, atleast for x86-64.

> And division by non-power of two integers can optimized by replacing it
> with multiplication with its inverse (which gcc and llvm don't do for
> signed divisions, only for unsigned ones):

The AMD64 optimization guide gives additional pointers here, i.e. it also lists replacement sequences for multiplication by constant.

> Best regards,
> --Edwin