summaryrefslogtreecommitdiff
path: root/libraries/base/GHC/Word.hs
diff options
context:
space:
mode:
authorJohan Tibell <johan.tibell@gmail.com>2011-08-23 14:58:13 +0200
committerJohan Tibell <johan.tibell@gmail.com>2011-08-25 21:19:30 +0200
commit4dfdbd1cd6c44b4d3d56ab303f4bc8de60ae55e4 (patch)
treee2ebc3507cd245c0d6a54f6bb5e03031e120d1fd /libraries/base/GHC/Word.hs
parent65ba73bc3a4622bb05324a933b003923d4b4f5ab (diff)
downloadhaskell-4dfdbd1cd6c44b4d3d56ab303f4bc8de60ae55e4.tar.gz
Add Data.Bits.popCount
Diffstat (limited to 'libraries/base/GHC/Word.hs')
-rw-r--r--libraries/base/GHC/Word.hs6
1 files changed, 6 insertions, 0 deletions
diff --git a/libraries/base/GHC/Word.hs b/libraries/base/GHC/Word.hs
index 99ac8a7544..2714898978 100644
--- a/libraries/base/GHC/Word.hs
+++ b/libraries/base/GHC/Word.hs
@@ -180,6 +180,7 @@ instance Bits Word where
!wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
bitSize _ = WORD_SIZE_IN_BITS
isSigned _ = False
+ popCount (W# x#) = I# (word2Int# (popCnt# x#))
{-# RULES
"fromIntegral/Int->Word" fromIntegral = \(I# x#) -> W# (int2Word# x#)
@@ -286,6 +287,7 @@ instance Bits Word8 where
!i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
bitSize _ = 8
isSigned _ = False
+ popCount (W8# x#) = I# (word2Int# (popCnt8# x#))
{-# RULES
"fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8
@@ -419,6 +421,7 @@ instance Bits Word16 where
!i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
bitSize _ = 16
isSigned _ = False
+ popCount (W16# x#) = I# (word2Int# (popCnt16# x#))
{-# RULES
"fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x#
@@ -593,6 +596,7 @@ instance Bits Word32 where
!i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
bitSize _ = 32
isSigned _ = False
+ popCount (W32# x#) = I# (word2Int# (popCnt32# x#))
{-# RULES
"fromIntegral/Word8->Word32" fromIntegral = \(W8# x#) -> W32# x#
@@ -719,6 +723,7 @@ instance Bits Word64 where
!i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
bitSize _ = 64
isSigned _ = False
+ popCount (W64# x#) = I# (word2Int# (popCnt64# x#))
-- give the 64-bit shift operations the same treatment as the 32-bit
-- ones (see GHC.Base), namely we wrap them in tests to catch the
@@ -825,6 +830,7 @@ instance Bits Word64 where
!i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
bitSize _ = 64
isSigned _ = False
+ popCount (W64# x#) = I# (word2Int# (popCnt64# x#))
{-# RULES
"fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#