hw-bits 0.0.0.12 → 0.1.0.0
raw patch · 5 files changed
+106/−5 lines, 5 filesdep ~basedep ~hw-prim
Dependency ranges changed: base, hw-prim
Files
- README.md +1/−1
- hw-bits.cabal +5/−4
- src/HaskellWorks/Data/Bits/BitLength.hs +17/−0
- src/HaskellWorks/Data/Bits/BitWise.hs +17/−0
- src/HaskellWorks/Data/Bits/Broadword.hs +66/−0
README.md view
@@ -1,4 +1,4 @@ # hw-bits-[](https://circleci.com/gh/haskell-works/hw-bits/tree/0.0-branch)+[](https://circleci.com/gh/haskell-works/hw-bits/tree/0-branch) Facilities for manipulating bits.
hw-bits.cabal view
@@ -1,5 +1,5 @@ name: hw-bits-version: 0.0.0.12+version: 0.1.0.0 synopsis: Conduits for tokenizing streams. description: Please see README.md homepage: http://github.com/haskell-works/hw-bits#readme@@ -38,6 +38,7 @@ , HaskellWorks.Data.Bits.BitShow , HaskellWorks.Data.Bits.BitShown , HaskellWorks.Data.Bits.BitWise+ , HaskellWorks.Data.Bits.Broadword , HaskellWorks.Data.Bits.ElemFixedBitSize , HaskellWorks.Data.Bits.FixedBitSize , HaskellWorks.Data.Bits.FromBitTextByteString@@ -50,7 +51,7 @@ , HaskellWorks.Data.Bits.Word build-depends: base >= 4 && < 5 , bytestring- , hw-prim >= 0.0.3+ , hw-prim >= 0.1.0.0 , parsec , vector @@ -69,7 +70,7 @@ , bytestring , hspec , hw-bits- , hw-prim+ , hw-prim >= 0.1.0.0 , QuickCheck , vector ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall@@ -88,5 +89,5 @@ Build-Depends: base >= 4 && < 5 , criterion , hw-bits- , hw-prim >= 0.0.3+ , hw-prim >= 0.1.0.0 , vector
src/HaskellWorks/Data/Bits/BitLength.hs view
@@ -17,6 +17,7 @@ import qualified Data.Vector as DV import qualified Data.Vector.Storable as DVS import Data.Word+import HaskellWorks.Data.Naive import HaskellWorks.Data.Positioning import HaskellWorks.Data.Vector.VectorLike import Prelude as P@@ -64,6 +65,22 @@ {-# INLINE bitLength #-} instance BitLength Word64 where+ bitLength _ = 64+ {-# INLINE bitLength #-}++instance BitLength (Naive Word8) where+ bitLength _ = 8+ {-# INLINE bitLength #-}++instance BitLength (Naive Word16) where+ bitLength _ = 16+ {-# INLINE bitLength #-}++instance BitLength (Naive Word32) where+ bitLength _ = 32+ {-# INLINE bitLength #-}++instance BitLength (Naive Word64) where bitLength _ = 64 {-# INLINE bitLength #-}
src/HaskellWorks/Data/Bits/BitWise.hs view
@@ -19,6 +19,7 @@ import qualified Data.Vector.Storable as DVS import Data.Word import HaskellWorks.Data.Bits.BitLength+import HaskellWorks.Data.Naive import HaskellWorks.Data.Positioning import HaskellWorks.Data.Vector.VectorLike as VL import Prelude as P@@ -90,6 +91,22 @@ instance TestBit Word64 where (.?.) w n = B.testBit w (fromIntegral (getPosition n))+ {-# INLINE (.?.) #-}++instance TestBit (Naive Word8) where+ (.?.) w n = B.testBit (naive w) (fromIntegral (getPosition n))+ {-# INLINE (.?.) #-}++instance TestBit (Naive Word16) where+ (.?.) w n = B.testBit (naive w) (fromIntegral (getPosition n))+ {-# INLINE (.?.) #-}++instance TestBit (Naive Word32) where+ (.?.) w n = B.testBit (naive w) (fromIntegral (getPosition n))+ {-# INLINE (.?.) #-}++instance TestBit (Naive Word64) where+ (.?.) w n = B.testBit (naive w) (fromIntegral (getPosition n)) {-# INLINE (.?.) #-} instance TestBit (DV.Vector Word8) where
+ src/HaskellWorks/Data/Bits/Broadword.hs view
@@ -0,0 +1,66 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeFamilies #-}++-- |+-- Copyright: 2016 John Ky+-- License: MIT+--+-- Succinct operations.+module HaskellWorks.Data.Bits.Broadword+ ( Broadword(..)+ , broadword+ , h+ , l+ , lsb+ , kBitDiff+ , kBitDiffPos+ , kBitDiffUnsafe+ ) where++import qualified Data.Bits as DB+import Data.Word+import HaskellWorks.Data.Bits.BitWise++newtype Broadword a = Broadword a deriving (Eq, Show)++broadword :: Broadword Word64 -> Word64+broadword (Broadword a) = a+{-# INLINE broadword #-}++l :: Int -> Word64+l 2 = 0x5555555555555555+l 4 = 0x1111111111111111+l 8 = 0x0101010101010101+l 16 = 0x0001000100010001+l 32 = 0x0000000100000001+l 64 = 0x0000000000000001+l k = error ("Invalid h k where k = " ++ show k)+{-# INLINE l #-}++h :: Int -> Word64+h 2 = 0xaaaaaaaaaaaaaaaa+h 4 = 0x8888888888888888+h 8 = 0x8080808080808080+h 16 = 0x8000800080008000+h 32 = 0x8000000080000000+h 64 = 0x8000000000000000+h k = error ("Invalid h k where k = " ++ show k)+{-# INLINE h #-}++kBitDiff :: Int -> Word64 -> Word64 -> Word64+kBitDiff k x y = ((x .|. h k) - (y .&. comp (h k))) .^. ((x .^. comp y) .&. h k)+{-# INLINE kBitDiff #-}++kBitDiffPos :: Int -> Word64 -> Word64 -> Word64+kBitDiffPos k x y = let d = kBitDiff k x y in d .&. ((d .>. fromIntegral (k - 1)) - 1)+{-# INLINE kBitDiffPos #-}++kBitDiffUnsafe :: Int -> Word64 -> Word64 -> Word64+kBitDiffUnsafe k x y = ((x .|. h k) - y) .^. h k+{-# INLINE kBitDiffUnsafe #-}++lsb :: Word64 -> Word64+lsb w = let r = fromIntegral (DB.countTrailingZeros w) in r .|. negate ((r .>. 6) .&. 0x1)+{-# INLINE lsb #-}