summaryrefslogtreecommitdiff
path: root/libraries/base/Data/Bits.hs
diff options
context:
space:
mode:
authorIan Lynagh <ian@well-typed.com>2012-09-23 13:28:44 +0100
committerIan Lynagh <ian@well-typed.com>2012-09-23 13:28:44 +0100
commit925782ed21762ab62e7084c93eed11872410366a (patch)
tree7af08e4dfcfd66ea93aec31d362a3ff67c0fe16d /libraries/base/Data/Bits.hs
parent39b9f94a73895dd0f90dd4e12f78e3052b548de1 (diff)
downloadhaskell-925782ed21762ab62e7084c93eed11872410366a.tar.gz
Add bitSizeMaybe to Bits, and add FiniteBits class
Diffstat (limited to 'libraries/base/Data/Bits.hs')
-rw-r--r--libraries/base/Data/Bits.hs25
1 files changed, 23 insertions, 2 deletions
diff --git a/libraries/base/Data/Bits.hs b/libraries/base/Data/Bits.hs
index 6d97352382..97edb3485a 100644
--- a/libraries/base/Data/Bits.hs
+++ b/libraries/base/Data/Bits.hs
@@ -30,6 +30,7 @@ module Data.Bits (
clearBit, -- :: a -> Int -> a
complementBit, -- :: a -> Int -> a
testBit, -- :: a -> Int -> Bool
+ bitSizeMaybe,
bitSize, -- :: a -> Int
isSigned, -- :: a -> Bool
shiftL, shiftR, -- :: a -> Int -> a
@@ -37,6 +38,7 @@ module Data.Bits (
rotateL, rotateR, -- :: a -> Int -> a
popCount -- :: a -> Int
),
+ FiniteBits(finiteBitSize),
bitDefault,
testBitDefault,
@@ -54,6 +56,7 @@ module Data.Bits (
#endif
#ifdef __GLASGOW_HASKELL__
+import Data.Maybe
import GHC.Enum
import GHC.Num
import GHC.Base
@@ -152,6 +155,12 @@ class Eq a => Bits a where
testBit :: a -> Int -> Bool
{-| Return the number of bits in the type of the argument. The actual
+ value of the argument is ignored. Returns Nothing
+ for types that do not have a fixed bitsize, like 'Integer'.
+ -}
+ bitSizeMaybe :: a -> Maybe Int
+
+ {-| Return the number of bits in the type of the argument. The actual
value of the argument is ignored. The function 'bitSize' is
undefined for types that do not have a fixed bitsize, like 'Integer'.
-}
@@ -238,6 +247,9 @@ class Eq a => Bits a where
known as the population count or the Hamming weight. -}
popCount :: a -> Int
+class Bits b => FiniteBits b where
+ finiteBitSize :: b -> Int
+
-- | Default implementation for 'bit'.
--
-- Note that: @bitDefault i = 1 `shiftL` i@
@@ -297,7 +309,8 @@ instance Bits Int where
!x'# = int2Word# x#
!i'# = word2Int# (int2Word# i# `and#` int2Word# (wsib -# 1#))
!wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
- bitSize _ = WORD_SIZE_IN_BITS
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
popCount (I# x#) = I# (word2Int# (popCnt# (int2Word# x#)))
@@ -338,6 +351,9 @@ instance Bits Int where
isSigned _ = True
+instance FiniteBits Int where
+ finiteBitSize _ = WORD_SIZE_IN_BITS
+
#ifdef __NHC__
foreign import ccall nhc_primIntAnd :: Int -> Int -> Int
foreign import ccall nhc_primIntOr :: Int -> Int -> Int
@@ -371,11 +387,15 @@ instance Bits Word where
where
!i'# = word2Int# (int2Word# i# `and#` int2Word# (wsib -# 1#))
!wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
- bitSize _ = WORD_SIZE_IN_BITS
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
isSigned _ = False
popCount (W# x#) = I# (word2Int# (popCnt# x#))
bit = bitDefault
testBit = testBitDefault
+
+instance FiniteBits Word where
+ finiteBitSize _ = WORD_SIZE_IN_BITS
#endif
instance Bits Integer where
@@ -413,6 +433,7 @@ instance Bits Integer where
rotate x i = shift x i -- since an Integer never wraps around
+ bitSizeMaybe _ = Nothing
bitSize _ = error "Data.Bits.bitSize(Integer)"
isSigned _ = True