caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Török Edwin" <edwintorok@gmail.com>
To: Jonathan Kimmitt <jonathan@kimmitt.co.uk>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Re: optimize div to right shift (NOT!)
Date: Mon, 13 Dec 2010 14:58:06 +0200	[thread overview]
Message-ID: <20101213145806.7a6f9ac0@deb0> (raw)
In-Reply-To: <BB6FC70B-2E26-4BD2-8335-FB31E90FA377@kimmitt.co.uk>

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


  reply	other threads:[~2010-12-13 12:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20101212172643.3EF80BC5D@yquem.inria.fr>
2010-12-13 12:33 ` Jonathan Kimmitt
2010-12-13 12:58   ` Török Edwin [this message]
2010-12-13 13:03     ` [Caml-list] " Benedikt Meurer
2010-12-13 20:13     ` Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101213145806.7a6f9ac0@deb0 \
    --to=edwintorok@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=jonathan@kimmitt.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).