summaryrefslogtreecommitdiff
path: root/libraries/base/GHC/Int.hs
diff options
context:
space:
mode:
authorHerbert Valerio Riedel <hvr@gnu.org>2014-08-14 12:32:32 +0200
committerHerbert Valerio Riedel <hvr@gnu.org>2014-08-31 15:47:32 +0200
commita8a969ae7a05e408b29961d0a2ea621a16d73d3e (patch)
tree7e7e77b19c9d086347b91c763dc4f6ef063bde28 /libraries/base/GHC/Int.hs
parent393b820233caa00e428affc28e090b496d181664 (diff)
downloadhaskell-a8a969ae7a05e408b29961d0a2ea621a16d73d3e.tar.gz
Add `FiniteBits(count{Leading,Trailing}Zeros)`
This exposes the newly added CLZ/CTZ primops from e0c1767d0ea8d12e0a4badf43682a08784e379c6 (re #9340) via two new methods `countLeadingZeros` and `countTrailingZeros` in the `Data.Bits.FiniteBits` class. The original proposal can be found at http://www.haskell.org/pipermail/libraries/2014-August/023567.html Test Plan: successful validate Reviewers: ekmett, tibbe GHC Trac Issues: #9532 Differential Revision: https://phabricator.haskell.org/D158
Diffstat (limited to 'libraries/base/GHC/Int.hs')
-rw-r--r--libraries/base/GHC/Int.hs13
1 files changed, 13 insertions, 0 deletions
diff --git a/libraries/base/GHC/Int.hs b/libraries/base/GHC/Int.hs
index 467b3f4e30..a9743ce1a0 100644
--- a/libraries/base/GHC/Int.hs
+++ b/libraries/base/GHC/Int.hs
@@ -165,6 +165,8 @@ instance Bits Int8 where
instance FiniteBits Int8 where
finiteBitSize _ = 8
+ countLeadingZeros (I8# x#) = I# (word2Int# (clz8# (int2Word# x#)))
+ countTrailingZeros (I8# x#) = I# (word2Int# (ctz8# (int2Word# x#)))
{-# RULES
"fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
@@ -324,6 +326,8 @@ instance Bits Int16 where
instance FiniteBits Int16 where
finiteBitSize _ = 16
+ countLeadingZeros (I16# x#) = I# (word2Int# (clz16# (int2Word# x#)))
+ countTrailingZeros (I16# x#) = I# (word2Int# (ctz16# (int2Word# x#)))
{-# RULES
"fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
@@ -489,6 +493,8 @@ instance Bits Int32 where
instance FiniteBits Int32 where
finiteBitSize _ = 32
+ countLeadingZeros (I32# x#) = I# (word2Int# (clz32# (int2Word# x#)))
+ countTrailingZeros (I32# x#) = I# (word2Int# (ctz32# (int2Word# x#)))
{-# RULES
"fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
@@ -871,6 +877,13 @@ uncheckedIShiftRA64# = uncheckedIShiftRA#
instance FiniteBits Int64 where
finiteBitSize _ = 64
+#if WORD_SIZE_IN_BITS < 64
+ countLeadingZeros (I64# x#) = I# (word2Int# (clz64# (int64ToWord64# x#)))
+ countTrailingZeros (I64# x#) = I# (word2Int# (ctz64# (int64ToWord64# x#)))
+#else
+ countLeadingZeros (I64# x#) = I# (word2Int# (clz64# (int2Word# x#)))
+ countTrailingZeros (I64# x#) = I# (word2Int# (ctz64# (int2Word# x#)))
+#endif
instance Real Int64 where
toRational x = toInteger x % 1