caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: optimize div to right shift (NOT!)
       [not found] <20101212172643.3EF80BC5D@yquem.inria.fr>
@ 2010-12-13 12:33 ` Jonathan Kimmitt
  2010-12-13 12:58   ` [Caml-list] " Török Edwin
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Kimmitt @ 2010-12-13 12:33 UTC (permalink / raw)
  To: caml-list


> A C compiler would optimize this to a right shift. Changing that to 'Int64.shift_right n 1' speeds
> up the code.

Sorry to be a pedant, but this is not correct. The optimisation is only possible when the arguments
are unsigned integers which I don't think is specifiable when working in OCAML

# Int64.shift_right (-2L) 1;;
- : int64 = -1L (So far, so good)
# Int64.div (-1L) 2L;;
- : int64 = 0L (Good)
# Int64.shift_right (-1L) 1;;
- : int64 = -1L (Duh)



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Re: optimize div to right shift (NOT!)
  2010-12-13 12:33 ` optimize div to right shift (NOT!) Jonathan Kimmitt
@ 2010-12-13 12:58   ` Török Edwin
  2010-12-13 13:03     ` Benedikt Meurer
  2010-12-13 20:13     ` Jon Harrop
  0 siblings, 2 replies; 4+ messages in thread
From: Török Edwin @ 2010-12-13 12:58 UTC (permalink / raw)
  To: Jonathan Kimmitt; +Cc: caml-list

On Mon, 13 Dec 2010 12:33:33 +0000
Jonathan Kimmitt <jonathan@kimmitt.co.uk> wrote:

> 
> > A C compiler would optimize this to a right shift. Changing that to
> > 'Int64.shift_right n 1' speeds up the code.
> 
> Sorry to be a pedant, but this is not correct. The optimisation is
> only possible when the arguments are unsigned integers 

That particular program never used negative integers.

> which I don't
> think is specifiable when working in OCAML

You are right, there is no way to tell ocaml that.

> 
> # Int64.shift_right (-2L) 1;;
> - : int64 = -1L (So far, so good)
> # Int64.div (-1L) 2L;;
> - : int64 = 0L (Good)
> # Int64.shift_right (-1L) 1;;
> - : int64 = -1L (Duh)

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

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):
http://www.hackersdelight.org/HDcode/magic.c.txt
http://www.hackersdelight.org/HDcode/magicu.c.txt

Best regards,
--Edwin


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Re: optimize div to right shift (NOT!)
  2010-12-13 12:58   ` [Caml-list] " Török Edwin
@ 2010-12-13 13:03     ` Benedikt Meurer
  2010-12-13 20:13     ` Jon Harrop
  1 sibling, 0 replies; 4+ messages in thread
From: Benedikt Meurer @ 2010-12-13 13:03 UTC (permalink / raw)
  To: caml-list


On Dec 13, 2010, at 13:58 , Török 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):
> http://www.hackersdelight.org/HDcode/magic.c.txt
> http://www.hackersdelight.org/HDcode/magicu.c.txt

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

> Best regards,
> --Edwin

greets,
Benedikt

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] Re: optimize div to right shift (NOT!)
  2010-12-13 12:58   ` [Caml-list] " Török Edwin
  2010-12-13 13:03     ` Benedikt Meurer
@ 2010-12-13 20:13     ` Jon Harrop
  1 sibling, 0 replies; 4+ messages in thread
From: Jon Harrop @ 2010-12-13 20:13 UTC (permalink / raw)
  To: 'Török Edwin'; +Cc: caml-list

Edwin wrote:
> http://www.hackersdelight.org/HDcode/magic.c.txt
> http://www.hackersdelight.org/HDcode/magicu.c.txt

Nice! :-)

Cheers,
Jon.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-12-13 20:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20101212172643.3EF80BC5D@yquem.inria.fr>
2010-12-13 12:33 ` optimize div to right shift (NOT!) Jonathan Kimmitt
2010-12-13 12:58   ` [Caml-list] " Török Edwin
2010-12-13 13:03     ` Benedikt Meurer
2010-12-13 20:13     ` Jon Harrop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).