<div dir="ltr">+1</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Aug 15, 2014 at 10:22 AM, Herbert Valerio Riedel <span dir="ltr"><<a href="mailto:hvr@gnu.org" target="_blank">hvr@gnu.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello *,<br>
<br>
As GHC 7.10.1 will have support for new assembler-optimized CLZ & CTZ[1]<br>
primops[2], it'd be useful to provide also a convenient high-level<br>
interface to avoid having to work with -XMagicHash and unboxed values.<br>
<br>
To this end, I hereby propose to add two new methods to the 'FiniteBits'<br>
class, specifically<br>
<br>
<br>
  class Bits b => FiniteBits b where<br>
    {- ... -}<br>
<br>
    countLeadingZeros :: b -> Int<br>
    countLeadingZeros x = (w-1) - go (w-1)<br>
      where<br>
        go i | i < 0       = i -- no bit set<br>
             | testBit x i = i<br>
             | otherwise   = go (i-1)<br>
<br>
        w = finiteBitSize x<br>
<br>
    countTrailingZeros :: b -> Int<br>
    countTrailingZeros x = go 0<br>
      where<br>
        go i | i >= w      = i<br>
             | testBit x i = i<br>
             | otherwise   = go (i+1)<br>
<br>
        w = finiteBitSize x<br>
<br>
<br>
The full patch (including Haddock doc-strings) is available for code<br>
review at<br>
<br>
  <a href="https://phabricator.haskell.org/D158" target="_blank">https://phabricator.haskell.org/D158</a><br>
<br>
I suggest to try to keep the discussion/voting/bikeshedding about the<br>
proposal proper here (including any bike-shedding about naming). At same<br>
time, I'd like to invite you to try out the Phab code-revision tool[3]<br>
for pointing-out/discussing technical issues with the proposed patch.<br>
<br>
<br>
Cheers,<br>
  hvr<br>
<br>
 [1]: <a href="http://en.wikipedia.org/wiki/Find_first_set" target="_blank">http://en.wikipedia.org/wiki/Find_first_set</a> provides a good<br>
      overview why CLZ/CTZ are desirable primitive operations.<br>
<br>
 [2]: <a href="http://git.haskell.org/ghc.git/commit/e0c1767d0ea8d12e0a4badf43682a08784e379c6" target="_blank">http://git.haskell.org/ghc.git/commit/e0c1767d0ea8d12e0a4badf43682a08784e379c6</a><br>
<br>
 [3]: Phab code-revisions allow you to directly annotate code-fragments.<br>
      However, after having written inline annotations, you have to actually<br>
      submit those by submitting a non-inline (possibly empty) comment<br>
      (see bottom of that page) to make them visible for everyone.<br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
</blockquote></div><br></div>