summaryrefslogtreecommitdiff
path: root/libraries/base/GHC/Word.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/GHC/Word.hs
parent39b9f94a73895dd0f90dd4e12f78e3052b548de1 (diff)
downloadhaskell-925782ed21762ab62e7084c93eed11872410366a.tar.gz
Add bitSizeMaybe to Bits, and add FiniteBits class
Diffstat (limited to 'libraries/base/GHC/Word.hs')
-rw-r--r--libraries/base/GHC/Word.hs28
1 files changed, 23 insertions, 5 deletions
diff --git a/libraries/base/GHC/Word.hs b/libraries/base/GHC/Word.hs
index 781520fea0..c4324bf6cc 100644
--- a/libraries/base/GHC/Word.hs
+++ b/libraries/base/GHC/Word.hs
@@ -27,6 +27,7 @@ module GHC.Word (
) where
import Data.Bits
+import Data.Maybe
#if WORD_SIZE_IN_BITS < 64
import GHC.IntWord64
@@ -142,12 +143,16 @@ instance Bits Word8 where
(x# `uncheckedShiftRL#` (8# -# i'#))))
where
!i'# = word2Int# (int2Word# i# `and#` 7##)
- bitSize _ = 8
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
isSigned _ = False
popCount (W8# x#) = I# (word2Int# (popCnt8# x#))
bit = bitDefault
testBit = testBitDefault
+instance FiniteBits Word8 where
+ finiteBitSize _ = 8
+
{-# RULES
"fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8
"fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
@@ -285,12 +290,16 @@ instance Bits Word16 where
(x# `uncheckedShiftRL#` (16# -# i'#))))
where
!i'# = word2Int# (int2Word# i# `and#` 15##)
- bitSize _ = 16
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
isSigned _ = False
popCount (W16# x#) = I# (word2Int# (popCnt16# x#))
bit = bitDefault
testBit = testBitDefault
+instance FiniteBits Word16 where
+ finiteBitSize _ = 16
+
{-# RULES
"fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x#
"fromIntegral/Word16->Word16" fromIntegral = id :: Word16 -> Word16
@@ -469,12 +478,16 @@ instance Bits Word32 where
(x# `uncheckedShiftRL#` (32# -# i'#))))
where
!i'# = word2Int# (int2Word# i# `and#` 31##)
- bitSize _ = 32
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
isSigned _ = False
popCount (W32# x#) = I# (word2Int# (popCnt32# x#))
bit = bitDefault
testBit = testBitDefault
+instance FiniteBits Word32 where
+ finiteBitSize _ = 32
+
{-# RULES
"fromIntegral/Word8->Word32" fromIntegral = \(W8# x#) -> W32# x#
"fromIntegral/Word16->Word32" fromIntegral = \(W16# x#) -> W32# x#
@@ -602,7 +615,8 @@ instance Bits Word64 where
(x# `uncheckedShiftRL64#` (64# -# i'#)))
where
!i'# = word2Int# (int2Word# i# `and#` 63##)
- bitSize _ = 64
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
isSigned _ = False
popCount (W64# x#) = I# (word2Int# (popCnt64# x#))
bit = bitDefault
@@ -717,7 +731,8 @@ instance Bits Word64 where
(x# `uncheckedShiftRL#` (64# -# i'#)))
where
!i'# = word2Int# (int2Word# i# `and#` 63##)
- bitSize _ = 64
+ bitSizeMaybe i = Just (finiteBitSize i)
+ bitSize i = finiteBitSize i
isSigned _ = False
popCount (W64# x#) = I# (word2Int# (popCnt64# x#))
bit = bitDefault
@@ -736,6 +751,9 @@ uncheckedShiftRL64# = uncheckedShiftRL#
#endif
+instance FiniteBits Word64 where
+ finiteBitSize _ = 64
+
instance Show Word64 where
showsPrec p x = showsPrec p (toInteger x)