caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Till Varoquaux <till@pps.jussieu.fr>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: Binary logarithm of a power of 2
Date: Fri, 27 May 2011 14:46:07 -0400	[thread overview]
Message-ID: <BANLkTim049Ufr2H9E=AKmRmaiXPmZLS_kQ@mail.gmail.com> (raw)
In-Reply-To: <BANLkTi=UPCy+ZErybor6aDUxvk1EkgsfpA@mail.gmail.com>

Nevermind. I am actually running slower, bad benchmark.

Shameful,
Till

On Fri, May 27, 2011 at 2:37 PM, Till Varoquaux <till@pps.jussieu.fr> wrote:
> And you can probably gain on XL by dropping down to ints and not using refs:
>
> let lb5 n i = if n >= 0x2 then i+1 else i
>
> let lb4 n i =
>  if n >= 0x4 then lb5 (n asr 2) (i+2)
>  else lb5 n i
>
> let lb3 n i =
>  if n >= 0x10 then lb4 (n asr 4) (i+4)
>  else lb4 n i
>
> let lb2 n i =
>  if n >= 0x100 then lb3 (n asr 8) (i+8)
>  else lb3 n i
>
> let logbin n =
>  if n >= 0x10000 then lb2 (n asr 16) 16
>  else lb2 n 0
>
> (note that you can drop to ints right in logbin itself if you really
> need your input to be int32s and you aren't on a 32 bit machine).
>
> This is thoroughly untested.
>
> Till
>
> On Fri, May 27, 2011 at 2:04 PM, Dario Teixeira <darioteixeira@yahoo.com> wrote:
>> Hi,
>>
>>> Simpler and maybe faster:
>>>
>>> let logbin (n: int32) =
>>>   let n = ref (Int32.add n 0l) and i = ref 0 in
>>>   if !n >= 0x10000l then (n := Int32.shift_right !n 16; i := 16);
>>>   if !n >= 0x100l then (n := Int32.shift_right !n 8; i := !i + 8);
>>>   if !n >= 0x10l then (n := Int32.shift_right !n 4; i := !i + 4);
>>>   if !n >= 0x4l then (n := Int32.shift_right !n 2; i := !i + 2);
>>>   if !n >= 0x2l then !i + 1 else !i
>>
>> Thanks!  In my synthetic benchmark, this pure Ocaml solution comes very
>> close to the C-based "noalloc" one (the record holder so far), at least
>> on an x86_64.
>>
>> Note that __builtin_ctz actually translates into a single opcode where
>> available (BSFL in x86_64), and I expect that a modern CPU will do a
>> decent job with it.  Therefore, despite that a C-based solution will
>> most likely prevent inlining (right?), it may be hard to beat...
>>
>> Cheers,
>> Dario Teixeira
>>
>>
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa-roc.inria.fr/wws/info/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>>
>


  reply	other threads:[~2011-05-27 18:46 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-26 15:51 [Caml-list] " Dario Teixeira
2011-05-26 19:28 ` Goswin von Brederlow
2011-05-27 11:50 ` Dario Teixeira
2011-05-27 13:26   ` [Caml-list] " Sylvain Le Gall
2011-05-27 17:15     ` Xavier Leroy
2011-05-27 18:04       ` Dario Teixeira
2011-05-27 18:37         ` Till Varoquaux
2011-05-27 18:46           ` Till Varoquaux [this message]
2011-05-28  0:28             ` Harrison, John R
2011-05-28 15:58         ` Richard W.M. Jones
2011-05-28 16:04           ` Edgar Friendly
2011-05-28 17:50       ` Matteo Frigo
     [not found]         ` <BANLkTimtMZvoBKHznBYH91czci=YdiRcog@mail.gmail.com>
2011-05-28 21:13           ` Fwd: " Johannes
2011-05-30 11:48         ` Jean-Christophe Filliâtre
2011-05-27 17:21 Dario Teixeira
2011-05-28  5:29 ` Gabriel Kerneis
2011-05-28 16:17   ` Norman Hardy

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='BANLkTim049Ufr2H9E=AKmRmaiXPmZLS_kQ@mail.gmail.com' \
    --to=till@pps.jussieu.fr \
    --cc=caml-list@inria.fr \
    --cc=darioteixeira@yahoo.com \
    /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).