@@ -4,7 +4,6 @@
44-- |
55-- Module : Data.Binary.Bits.Get
66-- Copyright : (c) Lennart Kolmodin 2010-2011
7−-- (c) Sylvain Henry 2015
87-- License : BSD3-style (see LICENSE)
98--
109-- Maintainer : kolmodin@gmail.com
@@ -56,15 +55,13 @@ module Data.Binary.Bits.Get
5655 -- $bitget
5756
5857 BitGet
58+ , BitOrder(..)
5959 , runBitGet
60+ , selectBitOrder
6061
6162 -- ** Get bytes
6263 , getBool
63− , getBool#
6464 , getWord8
65− , getWord16
66− , getWord32
67− , getWord64
6865 , getWord16be
6966 , getWord32be
7067 , getWord64be
@@ -82,9 +79,6 @@ module Data.Binary.Bits.Get
8279 -- ** Read in Blocks
8380 , bool
8481 , word8
85− , word16
86− , word32
87− , word64
8882 , word16be
8983 , word32be
9084 , word64be
@@ -97,9 +91,6 @@ module Data.Binary.Bits.Get
9791
9892import Data.Binary.Get as B ( Get, getLazyByteString, isEmpty )
9993import Data.Binary.Get.Internal as B ( get, put, ensureN )
100−import Data.Binary.Bits.BitOrder
101−import Data.Binary.Bits.Internal
102−import Data.Binary.Bits.Alignment
10394
10495import Data.ByteString as B
10596import qualified Data.ByteString.Lazy as L
@@ -115,8 +106,12 @@ import Control.Applicative
115106import Control.Monad (when,foldM_)
116107
117108import Prelude as P
109+
110+#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
111+import GHC.Base
118112import GHC.Word
119−import GHC.Base (uncheckedShiftLWord8#, wordToWord8#, Int (..), andWord8#, Word8#, (-#), Int#, xorI#, eqWord8#, TYPE)
113+#endif
114+
120115
121116-- $bitget
122117-- Parse bits using a monad.
@@ -169,6 +164,16 @@ data S = S {-# UNPACK #-} !ByteString -- Input
169164 !BitOrder -- Bit order
170165 deriving (Show)
171166
167+-- | Compute bit offset (equivalent to x `mod` 8 but faster)
168+bit_offset :: Int -> Int
169+bit_offset n = make_mask 3 .&. n
170+{-# INLINE bit_offset #-}
171+
172+-- | Compute byte offset (equivalent to x `div` 8 but faster)
173+byte_offset :: Int -> Int
174+byte_offset n = n `shiftR` 3
175+{-# INLINE byte_offset #-}
176+
172177-- | Increment the current bit offset
173178incS :: Int -> S -> S
174179incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
@@ -177,70 +182,83 @@ incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
177182 !d = byte_offset o'
178183 !n' = bit_offset o'
179184
185+-- | Select the bit ordering
186+selectBitOrderS :: BitOrder -> S -> S
187+selectBitOrderS bo (S bs o _) = S bs o bo
188+
189+-- | make_mask 3 = 00000111
190+make_mask :: (Bits a, Num a) => Int -> a
191+make_mask n = (1 `shiftL` fromIntegral n) - 1
192+{-# SPECIALIZE make_mask :: Int -> Int #-}
193+{-# SPECIALIZE make_mask :: Int -> Word #-}
194+{-# SPECIALIZE make_mask :: Int -> Word8 #-}
195+{-# SPECIALIZE make_mask :: Int -> Word16 #-}
196+{-# SPECIALIZE make_mask :: Int -> Word32 #-}
197+{-# SPECIALIZE make_mask :: Int -> Word64 #-}
198+
199+-- | Keep only the n least-significant bits of the given value
200+mask :: (Bits a, Num a) => Int -> a -> a
201+mask n v = v .&. make_mask n
202+{-# INLINE mask #-}
203+
204+-- | Bit order
205+--
206+-- E.g. two words of 5 bits: ABCDE, VWXYZ
207+-- - BB: ABCDEVWX YZxxxxxx
208+-- - LB: XYZABCDE xxxxxxVW
209+-- - BL: EDCBAZYX WVxxxxxx -- not implemented
210+-- - LL: XWVEDCBA xxxxxxZY -- not implemented
211+data BitOrder = BB | LB deriving (Show)
212+
213+
180214-- | Read a single bit
181215readBool :: S -> Bool
182216readBool (S bs o bo) = case bo of
183217 BB -> testBit (unsafeHead bs) (7-o)
184− BL -> testBit (unsafeHead bs) (7-o)
185− LL -> testBit (unsafeHead bs) o
186218 LB -> testBit (unsafeHead bs) o
187219
188−-- | Read a single bit
189−readBool# :: S -> Int
190−readBool# (S bs (I# o) bo) = case bo of
191− BB -> testBit# (7# -# o)
192− BL -> testBit# (7# -# o)
193− LL -> testBit# o
194− LB -> testBit# o
195− where
196− !(W8# w) = unsafeHead bs
197− testBit# n = I# (((w `andWord8#` (wordToWord8# 1## `uncheckedShiftLWord8#` n)) `eqWord8#` wordToWord8# 0##) `xorI#` 1#)
198−
199−
200220-- | Extract a range of bits from (ws :: ByteString)
201221--
202222-- Constraint: 8 * (length ws -1 ) < o+n <= 8 * length ws
203−extract :: (Num a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
223+extract :: (Num a, Bits a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
204224extract bo bs o n
205225 | n == 0 = 0
206226 | B.length bs == 0 = error "Empty ByteString"
207− | otherwise = rev . mask n . foldlWithIndex' f 0 $ bs
227+ | otherwise = mask n (foldlWithIndex' f 0 bs)
208228 where
209229 -- B.foldl' with index
210230 foldlWithIndex' op b = fst . B.foldl' g (b,0)
211231 where g (b',i) w = (op b' w i, (i+1))
212232
213233 -- 'or' correctly shifted words
214− f b w i = b .|. (fromIntegral w `fastShift` off i)
234+ f b w i = b .|. (fromIntegral w `fastShift` off i)
215235
216236 -- co-offset
217− r = B.length bs * 8 - (o + n)
237+ r = B.length bs * 8 - (o + n)
218238
219239 -- shift offset depending on the byte position (0..B.length-1)
220− off i = case bo of
221− LL -> 8*i - o
240+ off i = case bo of
222241 LB -> 8*i - o
223242 BB -> (B.length bs -1 - i) * 8 - r
224− BL -> (B.length bs -1 - i) * 8 - r
225−
226− -- reverse bits if necessary
227− rev = case bo of
228− LB -> reverseBits n
229− BL -> reverseBits n
230− BB -> id
231− LL -> id
232243
233244
234245-- | Generic readWord
235−readWord :: (Num a, FastBits a) => Int -> S -> a
246+readWord :: (Num a, Bits a, FastBits a) => Int -> S -> a
236247readWord n (S bs o bo)
237248 | n == 0 = 0
238− | otherwise = extract bo (unsafeTake nbytes bs) o n
239− where nbytes = byte_offset (o+n+7)
249+ | o+n <= 8 = extract bo (unsafeTake 1 bs) o n
250+ | o+n <= 16 = extract bo (unsafeTake 2 bs) o n
251+ | o+n <= 24 = extract bo (unsafeTake 3 bs) o n
252+ | o+n <= 32 = extract bo (unsafeTake 4 bs) o n
253+ | o+n <= 40 = extract bo (unsafeTake 5 bs) o n
254+ | o+n <= 48 = extract bo (unsafeTake 6 bs) o n
255+ | o+n <= 56 = extract bo (unsafeTake 7 bs) o n
256+ | o+n <= 64 = extract bo (unsafeTake 8 bs) o n
257+ | otherwise = extract bo (unsafeTake ((o+n+7) `div` 8) bs) o n
240258
241259-- | Check that the number of bits to read is not greater than the first parameter
242260{-# INLINE readWordChecked #-}
243−readWordChecked :: (Num a, FastBits a) => Int -> Int -> S -> a
261+readWordChecked :: (Num a, Bits a, FastBits a) => Int -> Int -> S -> a
244262readWordChecked m n s
245263 | n > m = error $ "Tried to read more than " ++ show m ++ " bits (" ++ show n ++")"
246264 | otherwise = readWord n s
@@ -248,37 +266,36 @@ readWordChecked m n s
248266-- | Read the given number of bytes and return them in Big-Endian order
249267--
250268-- Examples:
251−-- BB: xxxABCDE FGHIJKLM NOPxxxxx -> ABCDEFGH IJKLMNOP
252−-- LL: LMNOPxxx DEFGHIJK xxxxxABC -> ABCDEFGH IJKLMNOP
253−-- BL: xxxPONML KJIHGFED CBAxxxxx -> ABCDEFGH IJKLMNOP
254−-- LB: EDCBAxxx MLKJIHGF xxxxxPON -> ABCDEFGH IJKLMNOP
269+-- BB: xxxABCDE FGHIJKLM NOPQRxxx -> ABCDEFGH IJKLMNOP QRxxxxxx
270+-- LB: NOPQRxxx FGHIJKLM xxxABCDE -> ABCDEFGH IJKLMNOP QRxxxxxx
255271readByteString :: Int -> S -> ByteString
256−readByteString n (S bs o bo) =
257− let
258− bs' = unsafeTake (n+1) bs
259− bs'' = unsafeTake n bs
260− rev = B.map (reverseBits 8)
261− in case (o,bo) of
262− (0,BB) -> bs''
263− (0,LL) -> B.reverse bs''
264− (0,LB) -> rev bs''
265− (0,BL) -> rev . B.reverse $ bs''
266− (_,LL) -> readByteString n (S (B.reverse bs') (8-o) BB)
267− (_,BL) -> rev . B.reverse $ readByteString n (S bs' o BB)
268− (_,LB) -> rev . B.reverse $ readByteString n (S bs' o LL)
269− (_,BB) -> unsafePerformIO $ do
270− let len = n+1
271− ptr <- mallocBytes len
272− let f r i = do
273− let
274− w = unsafeIndex bs (len-i)
275− w' = (w `fastShiftL` o) .|. r
276− r' = w `fastShiftR` (8-o)
277− poke (ptr `plusPtr` (len-i)) w'
278− return r'
279− foldM_ f 0 [1..len]
280− unsafeInit <$> unsafePackMallocCStringLen (ptr,len)
281−
272+readByteString n (S bs o bo)
273+ | o == 0 = unsafeTake n bs
274+ | otherwise = unsafePerformIO $ do
275+ let len = n+1
276+ ptr <- mallocBytes len
277+ case bo of
278+ BB -> do
279+ let f r i = do
280+ let
281+ w = unsafeIndex bs (len-i)
282+ w' = (w `fastShiftL` o) .|. r
283+ r' = w `fastShiftR` (8-o)
284+ poke (ptr `plusPtr` (len-i)) w'
285+ return r'
286+ foldM_ f 0 [1..len]
287+ unsafeInit <$> unsafePackMallocCStringLen (ptr,len)
288+
289+ LB -> do
290+ let f r i = do
291+ let
292+ w = unsafeIndex bs (i-1)
293+ w' = (w `fastShiftL` (8-o)) .|. r
294+ r' = w `fastShiftR` o
295+ poke (ptr `plusPtr` (len-i)) w'
296+ return r'
297+ foldM_ f 0 [1..len]
298+ unsafeTail <$> unsafePackMallocCStringLen (ptr,len)
282299
283300------------------------------------------------------------------------
284301-- | 'BitGet' is a monad, applicative and a functor. See 'runBitGet'
@@ -287,6 +304,7 @@ newtype BitGet a = B { runState :: S -> Get (S,a) }
287304
288305instance Monad BitGet where
289306 return x = B $ \s -> return (s,x)
307+ fail str = B $ \s -> putBackState s >> fail str
290308 (B f) >>= g = B $ \s -> do (s',a) <- f s
291309 runState (g a) s'
292310
@@ -297,29 +315,6 @@ instance Applicative BitGet where
297315 pure x = return x
298316 fm <*> m = fm >>= \f -> m >>= \v -> return (f v)
299317
300−instance BitOrderable BitGet where
301− setBitOrder bo = do
302− (S bs o _) <- getState
303− putState (S bs o bo)
304−
305− getBitOrder = do
306− (S _ _ bo) <- getState
307− return bo
308−
309−instance Alignable BitGet where
310− -- | Skip the given number of bits
311− skipBits n = do
312− ensureBits n
313− withState (incS n)
314−
315− -- | Skip bits if necessary to align to the next byte
316− alignByte = do
317− (S _ o _) <- getState
318− when (o /= 0) $
319− skipBits (8-o)
320−
321−
322−
323318-- | Run a 'BitGet' within the Binary packages 'Get' monad. If a byte has
324319-- been partially consumed it will be discarded once 'runBitGet' is finished.
325320runBitGet :: BitGet a -> Get a
@@ -367,6 +362,23 @@ ensureBits n = do
367362 put B.empty
368363 return (S (bs`append`bs') o bo, ())
369364
365+-- | Skip the given number of bits
366+skipBits :: Int -> BitGet ()
367+skipBits n = do
368+ ensureBits n
369+ withState (incS n)
370+
371+-- | Skip bits if necessary to align to the next byte
372+alignByte :: BitGet ()
373+alignByte = do
374+ (S _ o _) <- getState
375+ when (o /= 0) $
376+ skipBits (8-o)
377+
378+-- | Select the bit ordering
379+selectBitOrder :: BitOrder -> BitGet ()
380+selectBitOrder = withState . selectBitOrderS
381+
370382-- | Test whether all input has been consumed, i.e. there are no remaining
371383-- undecoded bytes.
372384isEmpty :: BitGet Bool
@@ -378,37 +390,21 @@ isEmpty = B $ \ (S bs o bo) -> if B.null bs
378390getBool :: BitGet Bool
379391getBool = block bool
380392
381−-- | Get 1 bit as a 'Bool'.
382−getBool# :: BitGet Int
383−getBool# = block bool#
384−
385393-- | Get @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
386394getWord8 :: Int -> BitGet Word8
387395getWord8 n = block (word8 n)
388396
389397-- | Get @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
390−getWord16 :: Int -> BitGet Word16
391−getWord16 n = block (word16 n)
392−
393398getWord16be :: Int -> BitGet Word16
394−getWord16be = getWord16
395−{-# DEPRECATED getWord16be "Use 'getWord16' instead" #-}
399+getWord16be n = block (word16be n)
396400
397401-- | Get @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
398−getWord32 :: Int -> BitGet Word32
399−getWord32 n = block (word32 n)
400−
401402getWord32be :: Int -> BitGet Word32
402−getWord32be = getWord32
403−{-# DEPRECATED getWord32be "Use 'getWord32' instead" #-}
403+getWord32be n = block (word32be n)
404404
405405-- | Get @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
406−getWord64 :: Int -> BitGet Word64
407−getWord64 n = block (word64 n)
408−
409406getWord64be :: Int -> BitGet Word64
410−getWord64be = getWord64
411−{-# DEPRECATED getWord64be "Use 'getWord64' instead" #-}
407+getWord64be n = block (word64be n)
412408
413409-- | Get @n@ bytes as a 'ByteString'.
414410getByteString :: Int -> BitGet ByteString
@@ -457,40 +453,105 @@ block (Block i p) = do
457453bool :: Block Bool
458454bool = Block 1 readBool
459455
460−-- | Read a 1 bit 'Bool'.
461−bool# :: Block Int
462−bool# = Block 1 readBool#
463−
464456-- | Read @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
465457word8 :: Int -> Block Word8
466458word8 n = Block n (readWordChecked 8 n)
467459
468460-- | Read @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
469−word16 :: Int -> Block Word16
470−word16 n = Block n (readWordChecked 16 n)
471−
472461word16be :: Int -> Block Word16
473−word16be = word16
474−{-# DEPRECATED word16be "Use 'word16' instead" #-}
462+word16be n = Block n (readWordChecked 16 n)
475463
476464-- | Read @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
477−word32 :: Int -> Block Word32
478−word32 n = Block n (readWordChecked 32 n)
479−
480465word32be :: Int -> Block Word32
481−word32be = word32
482−{-# DEPRECATED word32be "Use 'word32' instead" #-}
466+word32be n = Block n (readWordChecked 32 n)
483467
484468-- | Read @n@ bits as a 'Word64'. @n@ must be within @[0..64]@.
485−word64 :: Int -> Block Word64
486−word64 n = Block n (readWordChecked 64 n)
487−
488469word64be :: Int -> Block Word64
489−word64be = word64
490−{-# DEPRECATED word64be "Use 'word64' instead" #-}
470+word64be n = Block n (readWordChecked 64 n)
491471
492472-- | Read @n@ bytes as a 'ByteString'.
493473byteString :: Int -> Block ByteString
494474byteString n | n > 0 = Block (n*8) (readByteString n)
495475 | otherwise = Block 0 (\_ -> B.empty)
496476
477+
478+---------------------------------------------------------------------
479+-- Unchecked shifts, from the "binary" package
480+
481+-- | Class for types supporting fast bit shifting
482+class FastBits a where
483+ fastShiftR :: a -> Int -> a
484+ fastShiftL :: a -> Int -> a
485+
486+ {-# INLINE fastShift #-}
487+ fastShift :: a -> Int -> a
488+ fastShift x n
489+ | n > 0 = fastShiftL x n
490+ | n < 0 = fastShiftR x (negate n)
491+ | otherwise = x
492+
493+instance FastBits Word8 where
494+ fastShiftR = shiftr_w8
495+ fastShiftL = shiftl_w8
496+
497+instance FastBits Word16 where
498+ fastShiftR = shiftr_w16
499+ fastShiftL = shiftl_w16
500+
501+instance FastBits Word32 where
502+ fastShiftR = shiftr_w32
503+ fastShiftL = shiftl_w32
504+
505+instance FastBits Word64 where
506+ fastShiftR = shiftr_w64
507+ fastShiftL = shiftl_w64
508+
509+
510+shiftl_w8 :: Word8 -> Int -> Word8
511+shiftl_w16 :: Word16 -> Int -> Word16
512+shiftl_w32 :: Word32 -> Int -> Word32
513+shiftl_w64 :: Word64 -> Int -> Word64
514+
515+shiftr_w8 :: Word8 -> Int -> Word8
516+shiftr_w16 :: Word16 -> Int -> Word16
517+shiftr_w32 :: Word32 -> Int -> Word32
518+shiftr_w64 :: Word64 -> Int -> Word64
519+
520+#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
521+shiftl_w8 (W8# w) (I# i) = W8# (w `uncheckedShiftL#` i)
522+shiftl_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftL#` i)
523+shiftl_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftL#` i)
524+
525+shiftr_w8 (W8# w) (I# i) = W8# (w `uncheckedShiftRL#` i)
526+shiftr_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftRL#` i)
527+shiftr_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftRL#` i)
528+
529+
530+#if WORD_SIZE_IN_BITS < 64
531+shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)
532+shiftr_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftRL64#` i)
533+
534+#if __GLASGOW_HASKELL__ <= 606
535+-- Exported by GHC.Word in GHC 6.8 and higher
536+foreign import ccall unsafe "stg_uncheckedShiftL64"
537+ uncheckedShiftL64# :: Word64# -> Int# -> Word64#
538+foreign import ccall unsafe "stg_uncheckedShiftRL64"
539+ uncheckedShiftRL64# :: Word64# -> Int# -> Word64#
540+#endif
541+
542+#else
543+shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL#` i)
544+shiftr_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftRL#` i)
545+#endif
546+
547+#else
548+shiftl_w8 = shiftL
549+shiftl_w16 = shiftL
550+shiftl_w32 = shiftL
551+shiftl_w64 = shiftL
552+
553+shiftr_w8 = shiftR
554+shiftr_w16 = shiftR
555+shiftr_w32 = shiftR
556+shiftr_w64 = shiftR
557+#endif