caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Re: Binary logarithm of a power of 2
@ 2011-05-27 17:21 Dario Teixeira
  2011-05-28  5:29 ` Gabriel Kerneis
  0 siblings, 1 reply; 13+ messages in thread
From: Dario Teixeira @ 2011-05-27 17:21 UTC (permalink / raw)
  To: caml-list, Sylvain Le Gall

Hi,

> The code is not bug proof, you should check it ;-)

Yeap, there's one bug which manifests itself on whole-byte boundaries
(ex: logbin_array 512l, logbin_array 65536l)...

> 
> You can except at least 18% increase in term of performance
> on amd64 and a 1% decrease on i386. 
 
In my synthetic benchmark it behaves as well as my initial C-based
but "alloc-happy" solution, so congratulations!  (Though it is of
course slower than the C-based "noalloc" version).

Incidentally, I found a web page with lots of cool bit-twiddling hacks,
which includes several solutions to the binary logarithm problem:
http://graphics.stanford.edu/~seander/bithacks.html

Cheers,
Dario



^ permalink raw reply	[flat|nested] 13+ messages in thread
* [Caml-list] Binary logarithm of a power of 2
@ 2011-05-26 15:51 Dario Teixeira
  2011-05-27 11:50 ` Dario Teixeira
  0 siblings, 1 reply; 13+ messages in thread
From: Dario Teixeira @ 2011-05-26 15:51 UTC (permalink / raw)
  To: caml-list

Hi,

In the critical path of an application I need to compute the binary
logarithm of an int32.  With one nuance: I know the input is always
a power of 2, which opens the door to bit-trickery (the log2 of a
power of 2 is simply the position of the sole bit set to '1').

GCC has a builtin function __builtin_ctz which I suppose will translate
into optimal assembly for all platforms, so my current solution uses a
small C stub that basically just invokes this builtin (see below).

Still, my question is whether this is indeed the optimal solution.
Is there some penalty associated with invoking C functions that may
negate the supposed advantage of __builtin_ctz?

Thanks in advance!
Best regards,
Dario Teixeira


external logbin: int32 -> int = "logbin_stub"

CAMLprim value logbin_stub (value v_num)
        {
        CAMLparam1 (v_num);
        CAMLlocal1 (ml_res);

        int32 num = Int32_val (v_num);
        int res = __builtin_ctz (num);

        ml_res = Val_int (res);
        CAMLreturn (ml_res);
        }



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

end of thread, other threads:[~2011-05-30 11:44 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-27 17:21 [Caml-list] Re: Binary logarithm of a power of 2 Dario Teixeira
2011-05-28  5:29 ` Gabriel Kerneis
2011-05-28 16:17   ` Norman Hardy
  -- strict thread matches above, loose matches on Subject: below --
2011-05-26 15:51 [Caml-list] " Dario Teixeira
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
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
2011-05-30 11:48         ` Jean-Christophe Filliâtre

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).