@@ -4,6 +4,7 @@
44-- |
55-- Module : Data.Binary.Bits.Get
66-- Copyright : (c) Lennart Kolmodin 2010-2011
7+-- (c) Sylvain Henry 2015
78-- License : BSD3-style (see LICENSE)
89--
910-- Maintainer : kolmodin@gmail.com
@@ -55,13 +56,15 @@ module Data.Binary.Bits.Get
5556 -- $bitget
5657
5758 BitGet
58− , BitOrder(..)
5959 , runBitGet
60− , selectBitOrder
6160
6261 -- ** Get bytes
6362 , getBool
63+ , getBool#
6464 , getWord8
65+ , getWord16
66+ , getWord32
67+ , getWord64
6568 , getWord16be
6669 , getWord32be
6770 , getWord64be
@@ -79,6 +82,9 @@ module Data.Binary.Bits.Get
7982 -- ** Read in Blocks
8083 , bool
8184 , word8
85+ , word16
86+ , word32
87+ , word64
8288 , word16be
8389 , word32be
8490 , word64be
@@ -91,6 +97,9 @@ module Data.Binary.Bits.Get
9197
9298import Data.Binary.Get as B ( Get, getLazyByteString, isEmpty )
9399import 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
94103
95104import Data.ByteString as B
96105import qualified Data.ByteString.Lazy as L
@@ -106,12 +115,8 @@ import Control.Applicative
106115import Control.Monad (when,foldM_)
107116
108117import Prelude as P
109−
110−#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
111−import GHC.Base
112118import GHC.Word
113−#endif
114−
119+import GHC.Base (uncheckedShiftLWord8#, wordToWord8#, Int (..), andWord8#, Word8#, (-#), Int#, xorI#, eqWord8#, TYPE)
115120
116121-- $bitget
117122-- Parse bits using a monad.
@@ -164,16 +169,6 @@ data S = S {-# UNPACK #-} !ByteString -- Input
164169 !BitOrder -- Bit order
165170 deriving (Show)
166171
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−
177172-- | Increment the current bit offset
178173incS :: Int -> S -> S
179174incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
@@ -182,83 +177,70 @@ incS o (S bs n bo) = S (unsafeDrop d bs) n' bo
182177 !d = byte_offset o'
183178 !n' = bit_offset o'
184179
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−
214180-- | Read a single bit
215181readBool :: S -> Bool
216182readBool (S bs o bo) = case bo of
217183 BB -> testBit (unsafeHead bs) (7-o)
184+ BL -> testBit (unsafeHead bs) (7-o)
185+ LL -> testBit (unsafeHead bs) o
218186 LB -> testBit (unsafeHead bs) o
219187
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+
220200-- | Extract a range of bits from (ws :: ByteString)
221201--
222202-- Constraint: 8 * (length ws -1 ) < o+n <= 8 * length ws
223−extract :: (Num a, Bits a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
203+extract :: (Num a, FastBits a) => BitOrder -> ByteString -> Int -> Int -> a
224204extract bo bs o n
225205 | n == 0 = 0
226206 | B.length bs == 0 = error "Empty ByteString"
227− | otherwise = mask n (foldlWithIndex' f 0 bs)
207+ | otherwise = rev . mask n . foldlWithIndex' f 0 $ bs
228208 where
229209 -- B.foldl' with index
230210 foldlWithIndex' op b = fst . B.foldl' g (b,0)
231211 where g (b',i) w = (op b' w i, (i+1))
232212
233213 -- 'or' correctly shifted words
234− f b w i = b .|. (fromIntegral w `fastShift` off i)
214+ f b w i = b .|. (fromIntegral w `fastShift` off i)
235215
236216 -- co-offset
237− r = B.length bs * 8 - (o + n)
217+ r = B.length bs * 8 - (o + n)
238218
239219 -- shift offset depending on the byte position (0..B.length-1)
240− off i = case bo of
220+ off i = case bo of
221+ LL -> 8*i - o
241222 LB -> 8*i - o
242223 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
243232
244233
245234-- | Generic readWord
246−readWord :: (Num a, Bits a, FastBits a) => Int -> S -> a
235+readWord :: (Num a, FastBits a) => Int -> S -> a
247236readWord n (S bs o bo)
248237 | n == 0 = 0
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
238+ | otherwise = extract bo (unsafeTake nbytes bs) o n
239+ where nbytes = byte_offset (o+n+7)
258240
259241-- | Check that the number of bits to read is not greater than the first parameter
260242{-# INLINE readWordChecked #-}
261−readWordChecked :: (Num a, Bits a, FastBits a) => Int -> Int -> S -> a
243+readWordChecked :: (Num a, FastBits a) => Int -> Int -> S -> a
262244readWordChecked m n s
263245 | n > m = error $ "Tried to read more than " ++ show m ++ " bits (" ++ show n ++")"
264246 | otherwise = readWord n s
@@ -266,36 +248,37 @@ readWordChecked m n s
266248-- | Read the given number of bytes and return them in Big-Endian order
267249--
268250-- Examples:
269−-- BB: xxxABCDE FGHIJKLM NOPQRxxx -> ABCDEFGH IJKLMNOP QRxxxxxx
270−-- LB: NOPQRxxx FGHIJKLM xxxABCDE -> ABCDEFGH IJKLMNOP QRxxxxxx
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
271255readByteString :: Int -> S -> ByteString
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)
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+
299282
300283------------------------------------------------------------------------
301284-- | 'BitGet' is a monad, applicative and a functor. See 'runBitGet'
@@ -304,7 +287,6 @@ newtype BitGet a = B { runState :: S -> Get (S,a) }
304287
305288instance Monad BitGet where
306289 return x = B $ \s -> return (s,x)
307− fail str = B $ \s -> putBackState s >> fail str
308290 (B f) >>= g = B $ \s -> do (s',a) <- f s
309291 runState (g a) s'
310292
@@ -315,6 +297,29 @@ instance Applicative BitGet where
315297 pure x = return x
316298 fm <*> m = fm >>= \f -> m >>= \v -> return (f v)
317299
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+
318323-- | Run a 'BitGet' within the Binary packages 'Get' monad. If a byte has
319324-- been partially consumed it will be discarded once 'runBitGet' is finished.
320325runBitGet :: BitGet a -> Get a
@@ -362,23 +367,6 @@ ensureBits n = do
362367 put B.empty
363368 return (S (bs`append`bs') o bo, ())
364369
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−
382370-- | Test whether all input has been consumed, i.e. there are no remaining
383371-- undecoded bytes.
384372isEmpty :: BitGet Bool
@@ -390,21 +378,37 @@ isEmpty = B $ \ (S bs o bo) -> if B.null bs
390378getBool :: BitGet Bool
391379getBool = block bool
392380
381+-- | Get 1 bit as a 'Bool'.
382+getBool# :: BitGet Int
383+getBool# = block bool#
384+
393385-- | Get @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
394386getWord8 :: Int -> BitGet Word8
395387getWord8 n = block (word8 n)
396388
397389-- | Get @n@ bits as a 'Word16'. @n@ must be within @[0..16]@.
390+getWord16 :: Int -> BitGet Word16
391+getWord16 n = block (word16 n)
392+
398393getWord16be :: Int -> BitGet Word16
399−getWord16be n = block (word16be n)
394+getWord16be = getWord16
395+{-# DEPRECATED getWord16be "Use 'getWord16' instead" #-}
400396
401397-- | Get @n@ bits as a 'Word32'. @n@ must be within @[0..32]@.
398+getWord32 :: Int -> BitGet Word32
399+getWord32 n = block (word32 n)
400+
402401getWord32be :: Int -> BitGet Word32
403−getWord32be n = block (word32be n)
402+getWord32be = getWord32
403+{-# DEPRECATED getWord32be "Use 'getWord32' instead" #-}
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+
406409getWord64be :: Int -> BitGet Word64
407−getWord64be n = block (word64be n)
410+getWord64be = getWord64
411+{-# DEPRECATED getWord64be "Use 'getWord64' instead" #-}
408412
409413-- | Get @n@ bytes as a 'ByteString'.
410414getByteString :: Int -> BitGet ByteString
@@ -453,105 +457,40 @@ block (Block i p) = do
453457bool :: Block Bool
454458bool = Block 1 readBool
455459
460+-- | Read a 1 bit 'Bool'.
461+bool# :: Block Int
462+bool# = Block 1 readBool#
463+
456464-- | Read @n@ bits as a 'Word8'. @n@ must be within @[0..8]@.
457465word8 :: Int -> Block Word8
458466word8 n = Block n (readWordChecked 8 n)
459467
460468-- | 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+
461472word16be :: Int -> Block Word16
462−word16be n = Block n (readWordChecked 16 n)
473+word16be = word16
474+{-# DEPRECATED word16be "Use 'word16' instead" #-}
463475
464476-- | 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+
465480word32be :: Int -> Block Word32
466−word32be n = Block n (readWordChecked 32 n)
481+word32be = word32
482+{-# DEPRECATED word32be "Use 'word32' instead" #-}
467483
468484-- | 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+
469488word64be :: Int -> Block Word64
470−word64be n = Block n (readWordChecked 64 n)
489+word64be = word64
490+{-# DEPRECATED word64be "Use 'word64' instead" #-}
471491
472492-- | Read @n@ bytes as a 'ByteString'.
473493byteString :: Int -> Block ByteString
474494byteString n | n > 0 = Block (n*8) (readByteString n)
475495 | otherwise = Block 0 (\_ -> B.empty)
476496
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