caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Norman Hardy <norm@cap-lore.com>
To: Gabriel Kerneis <kerneis@pps.jussieu.fr>
Cc: Dario Teixeira <darioteixeira@yahoo.com>,
	caml-list@inria.fr, Sylvain Le Gall <sylvain@le-gall.net>
Subject: Re: [Caml-list] Re: Binary logarithm of a power of 2
Date: Sat, 28 May 2011 09:17:51 -0700	[thread overview]
Message-ID: <FF8C16D3-7D13-414D-9B2A-26E3F1FA6F89@cap-lore.com> (raw)
In-Reply-To: <20110528052959.GA5839@kerneis.info>


On 2011 May 27, at 22:29 , Gabriel Kerneis wrote:

> On Fri, May 27, 2011 at 10:21:09AM -0700, Dario Teixeira wrote:
>> 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
> 
> Some more of them:
> http://www.cl.cam.ac.uk/~am21/hakmemc.html
> 
> Those are the (famous) HAKMEM tricks, translated to C by Alan Mycroft.
> Probably of no practical use, but learning what the fixed point of the float
> function was on a PDP-10 is priceless...
> 
> Best,
> -- 
> Gabriel
> 
> -- 
> 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
> 

In the spirit of HAKMEM here is a routine, in C, that counts the low zero bits of an integer less than 2^31:
http://cap-lore.com/code/fb/
In machine language it might require just 7 instructions with no branches.
I think that it cannot be improved to handle input greater than 2^31.
A 63 bit version should be possible.



  reply	other threads:[~2011-05-28 16:18 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-27 17:21 Dario Teixeira
2011-05-28  5:29 ` Gabriel Kerneis
2011-05-28 16:17   ` Norman Hardy [this message]
  -- 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

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=FF8C16D3-7D13-414D-9B2A-26E3F1FA6F89@cap-lore.com \
    --to=norm@cap-lore.com \
    --cc=caml-list@inria.fr \
    --cc=darioteixeira@yahoo.com \
    --cc=kerneis@pps.jussieu.fr \
    --cc=sylvain@le-gall.net \
    /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).