Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Binary logarithm of a power of 2
Date: Thu, 26 May 2011 21:28:26 +0200	[thread overview]
Message-ID: <87wrhdtg6d.fsf@frosties.localnet> (raw)
In-Reply-To: <707567.2845.qm@web111505.mail.gq1.yahoo.com> (Dario Teixeira's message of "Thu, 26 May 2011 08:51:04 -0700 (PDT)")

Dario Teixeira <darioteixeira@yahoo.com> writes:

> 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?

I doubt you can make an ocaml function that does this faster than
invoking a C function. Esspecially with noalloc that gives you verry
little leeway over the optimized __builtin_ctz. I doubt you can
translate the asm tricks gcc does to ocaml without loosing performance.

> 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);
>         }

There are 2 improvements you could make:

1) flag the stub as noalloc and drop the CAML*.
2) add a compiler builtin to the ocaml compiler.

Although the 2nd is probably going to far.

MfG
        Goswin

  reply	other threads:[~2011-05-26 19:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-26 15:51 Dario Teixeira
2011-05-26 19:28 ` Goswin von Brederlow [this message]
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
     [not found]         ` <BANLkTimtMZvoBKHznBYH91czci=YdiRcog@mail.gmail.com>
2011-05-28 21:13           ` Fwd: " Johannes
2011-05-30 11:48         ` Jean-Christophe Filliâtre

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=87wrhdtg6d.fsf@frosties.localnet \
    --to=goswin-v-b@web.de \
    --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