Commit a1f974cd authored by Bodigrim's avatar Bodigrim Committed by Xia Li-yao
Browse files

Decomission Data.Text.Internal.Unsafe.Shift

parent 5513b0c8
......@@ -207,6 +207,7 @@ import Control.DeepSeq (NFData(rnf))
import Control.Exception (assert)
import GHC.Stack (HasCallStack)
#endif
import Data.Bits (shiftL)
import Data.Char (isSpace)
import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
......@@ -232,7 +233,6 @@ import Data.Text.Internal.Unsafe.Char (unsafeChr)
import qualified Data.Text.Internal.Functions as F
import qualified Data.Text.Internal.Encoding.Utf16 as U16
import Data.Text.Internal.Search (indices)
import Data.Text.Internal.Unsafe.Shift (UnsafeShift(..))
#if defined(__HADDOCK__)
import Data.ByteString (ByteString)
import qualified Data.Text.Lazy as L
......
......@@ -46,9 +46,8 @@ import GHC.Base (sizeofByteArray#, sizeofMutableByteArray#)
import GHC.Stack (HasCallStack)
#endif
import Control.Monad.ST.Unsafe (unsafeIOToST)
import Data.Bits ((.&.), xor)
import Data.Bits ((.&.), xor, shiftL, shiftR)
import Data.Text.Internal.Unsafe (inlinePerformIO)
import Data.Text.Internal.Unsafe.Shift (shiftL, shiftR)
import Foreign.C.Types (CInt(CInt), CSize(CSize))
import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
indexWord16Array#, newByteArray#,
......
......@@ -62,7 +62,7 @@ import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO)
import Control.Exception (evaluate, try, throwIO, ErrorCall(ErrorCall))
import Control.Monad.ST (runST)
import Data.Bits ((.&.))
import Data.Bits ((.&.), shiftR)
import Data.ByteString as B
import qualified Data.ByteString.Internal as B
import Data.Foldable (traverse_)
......@@ -71,7 +71,6 @@ import Data.Text.Internal (Text(..), safe, text)
import Data.Text.Internal.Functions
import Data.Text.Internal.Private (runText)
import Data.Text.Internal.Unsafe.Char (ord, unsafeWrite)
import Data.Text.Internal.Unsafe.Shift (shiftR)
import Data.Text.Show ()
import Data.Text.Unsafe (unsafeDupablePerformIO)
import Data.Word (Word8, Word16, Word32)
......
......@@ -37,13 +37,13 @@ module Data.Text.Internal.Encoding.Fusion
#if defined(ASSERTS)
import Control.Exception (assert)
#endif
import Data.Bits (shiftL, shiftR)
import Data.ByteString.Internal (ByteString(..), mallocByteString, memcpy)
import Data.Text.Internal.Fusion (Step(..), Stream(..))
import Data.Text.Internal.Fusion.Size
import Data.Text.Encoding.Error
import Data.Text.Internal.Encoding.Fusion.Common
import Data.Text.Internal.Unsafe.Char (unsafeChr, unsafeChr8, unsafeChr32)
import Data.Text.Internal.Unsafe.Shift (shiftL, shiftR)
import Data.Text.Internal.Functions (unsafeWithForeignPtr)
import Data.Word (Word8, Word16, Word32)
import Foreign.ForeignPtr (ForeignPtr)
......
......@@ -29,11 +29,10 @@ module Data.Text.Internal.Encoding.Fusion.Common
, restreamUtf32BE
) where
import Data.Bits ((.&.))
import Data.Bits ((.&.), shiftR)
import Data.Text.Internal.Fusion (Step(..), Stream(..))
import Data.Text.Internal.Fusion.Types (RS(..))
import Data.Text.Internal.Unsafe.Char (ord)
import Data.Text.Internal.Unsafe.Shift (shiftR)
import Data.Word (Word8)
restreamUtf16BE :: Stream Char -> Stream Word8
......
......@@ -33,9 +33,8 @@ module Data.Text.Internal.Encoding.Utf8
, validate4
) where
import Data.Bits ((.&.))
import Data.Bits ((.&.), shiftR)
import Data.Text.Internal.Unsafe.Char (ord)
import Data.Text.Internal.Unsafe.Shift (shiftR)
import GHC.Exts
import GHC.Word (Word8(..))
......
......@@ -52,11 +52,10 @@ module Data.Text.Internal.Fusion
import Prelude (Bool(..), Char, Maybe(..), Monad(..), Int,
Num(..), Ord(..), ($), (&&),
fromIntegral, otherwise)
import Data.Bits ((.&.))
import Data.Bits ((.&.), shiftL, shiftR)
import Data.Text.Internal (Text(..))
import Data.Text.Internal.Private (runText)
import Data.Text.Internal.Unsafe.Char (ord, unsafeChr, unsafeWrite)
import Data.Text.Internal.Unsafe.Shift (shiftL, shiftR)
import qualified Data.Text.Array as A
import qualified Data.Text.Internal.Fusion.Common as S
import Data.Text.Internal.Fusion.Types
......
......@@ -38,8 +38,8 @@ module Data.Text.Internal.Lazy
, chunkOverhead
) where
import Data.Bits (shiftL)
import Data.Text ()
import Data.Text.Internal.Unsafe.Shift (shiftL)
import Data.Typeable (Typeable)
import Foreign.Storable (sizeOf)
import qualified Data.Text.Internal as T
......
......@@ -32,6 +32,7 @@ module Data.Text.Internal.Lazy.Encoding.Fusion
, module Data.Text.Internal.Encoding.Fusion.Common
) where
import Data.Bits (shiftL)
import Data.ByteString.Lazy.Internal (ByteString(..), defaultChunkSize)
import qualified Data.ByteString as B
import qualified Data.ByteString.Unsafe as B
......@@ -41,7 +42,6 @@ import Data.Text.Encoding.Error
import Data.Text.Internal.Fusion (Step(..), Stream(..))
import Data.Text.Internal.Fusion.Size
import Data.Text.Internal.Unsafe.Char (unsafeChr, unsafeChr8, unsafeChr32)
import Data.Text.Internal.Unsafe.Shift (shiftL)
import Data.Text.Internal.Functions (unsafeWithForeignPtr)
import Data.Word (Word8, Word16, Word32)
import qualified Data.Text.Internal.Encoding.Utf8 as U8
......
......@@ -26,6 +26,7 @@ module Data.Text.Internal.Lazy.Fusion
) where
import Prelude hiding (length)
import Data.Bits (shiftL)
import qualified Data.Text.Internal.Fusion.Common as S
import Control.Monad.ST (runST)
import Data.Text.Internal.Fusion.Types
......@@ -34,7 +35,6 @@ import Data.Text.Internal.Lazy
import qualified Data.Text.Internal as I
import qualified Data.Text.Array as A
import Data.Text.Internal.Unsafe.Char (unsafeWrite)
import Data.Text.Internal.Unsafe.Shift (shiftL)
import Data.Text.Unsafe (Iter(..), iter)
import Data.Int (Int64)
#if defined(ASSERTS)
......
......@@ -22,6 +22,7 @@ module Data.Text.Internal.Lazy.Search
indices
) where
import Data.Bits (unsafeShiftL)
import qualified Data.Text.Array as A
import Data.Int (Int64)
import Data.Word (Word16, Word64)
......@@ -29,7 +30,6 @@ import qualified Data.Text.Internal as T
import Data.Text.Internal.Fusion.Types (PairS(..))
import Data.Text.Internal.Lazy (Text(..), foldlChunks)
import Data.Bits ((.|.), (.&.))
import Data.Text.Internal.Unsafe.Shift (shiftL)
-- | /O(n+m)/ Find the offsets of all non-overlapping indices of
-- @needle@ within @haystack@.
......@@ -76,7 +76,7 @@ indices needle@(Chunk n ns) _haystack@(Chunk k ks)
(mask :: Word64) :*: skip = buildTable n ns 0 0 0 (nlen-2)
swizzle :: Word16 -> Word64
swizzle w = 1 `shiftL` (word16ToInt w .&. 0x3f)
swizzle w = 1 `unsafeShiftL` (word16ToInt w .&. 0x3f)
buildTable (T.Text xarr xoff xlen) xs = go
where
......
......@@ -34,8 +34,7 @@ module Data.Text.Internal.Search
import qualified Data.Text.Array as A
import Data.Word (Word64, Word16)
import Data.Text.Internal (Text(..))
import Data.Bits ((.|.), (.&.))
import Data.Text.Internal.Unsafe.Shift (shiftL)
import Data.Bits ((.|.), (.&.), unsafeShiftL)
data T = {-# UNPACK #-} !Word64 :* {-# UNPACK #-} !Int
......@@ -69,7 +68,7 @@ indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
| otherwise = skp
swizzle :: Word16 -> Word64
swizzle k = 1 `shiftL` (word16ToInt k .&. 0x3f)
swizzle k = 1 `unsafeShiftL` (word16ToInt k .&. 0x3f)
scan !i
| i > ldiff = []
......
......@@ -26,8 +26,7 @@ module Data.Text.Internal.Unsafe.Char
) where
import Control.Monad.ST (ST)
import Data.Bits ((.&.))
import Data.Text.Internal.Unsafe.Shift (shiftR)
import Data.Bits ((.&.), shiftR)
import GHC.Exts (Char(..), Int(..), chr#, ord#, word2Int#)
import GHC.Word (Word8(..), Word16(..), Word32(..))
import qualified Data.Text.Array as A
......
{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE MagicHash #-}
-- |
-- Module : Data.Text.Internal.Unsafe.Shift
-- Copyright : (c) Bryan O'Sullivan 2009
--
-- License : BSD-style
-- Maintainer : bos@serpentine.com
-- Stability : experimental
-- Portability : GHC
--
-- /Warning/: this is an internal module, and does not have a stable
-- API or name. Functions in this module may not check or enforce
-- preconditions expected by public modules. Use at your own risk!
--
-- Fast, unchecked bit shifting functions.
module Data.Text.Internal.Unsafe.Shift
(
UnsafeShift(..)
) where
import qualified Data.Bits as Bits
import Data.Word
-- | This is a workaround for poor optimisation in GHC 6.8.2. It
-- fails to notice constant-width shifts, and adds a test and branch
-- to every shift. This imposes about a 10% performance hit.
--
-- These functions are undefined when the amount being shifted by is
-- greater than the size in bits of a machine Int#.
class UnsafeShift a where
shiftL :: a -> Int -> a
{-# INLINE shiftL #-}
default shiftL :: Bits.Bits a => a -> Int -> a
shiftL = Bits.unsafeShiftL
shiftR :: a -> Int -> a
{-# INLINE shiftR #-}
default shiftR :: Bits.Bits a => a -> Int -> a
shiftR = Bits.unsafeShiftR
instance UnsafeShift Word16 where
instance UnsafeShift Word32 where
instance UnsafeShift Word64 where
instance UnsafeShift Int where
......@@ -18,9 +18,7 @@ import Test.Tasty.QuickCheck (testProperty)
import Test.QuickCheck hiding ((.&.))
import Tests.QuickCheckUtils
import Tests.Utils
import qualified Data.Bits as Bits (shiftL, shiftR)
import qualified Data.Text as T
import qualified Data.Text.Internal.Unsafe.Shift as U
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
......@@ -47,21 +45,6 @@ t_mul64 a b = mulRef a b === eval mul64 a b
t_mul :: Int -> Int -> Property
t_mul a b = mulRef a b === eval mul a b
-- Bit shifts.
shiftL w = forAll (choose (0,width-1)) $ \k -> Bits.shiftL w k == U.shiftL w k
where width = round (log (fromIntegral m) / log 2 :: Double)
(m,_) = (maxBound, m == w)
shiftR w = forAll (choose (0,width-1)) $ \k -> Bits.shiftR w k == U.shiftR w k
where width = round (log (fromIntegral m) / log 2 :: Double)
(m,_) = (maxBound, m == w)
shiftL_Int = shiftL :: Int -> Property
shiftL_Word16 = shiftL :: Word16 -> Property
shiftL_Word32 = shiftL :: Word32 -> Property
shiftR_Int = shiftR :: Int -> Property
shiftR_Word16 = shiftR :: Word16 -> Property
shiftR_Word32 = shiftR :: Word32 -> Property
-- Misc.
t_dropWord16 m t = dropWord16 m t `T.isSuffixOf` t
......@@ -98,15 +81,6 @@ testLowLevel =
testProperty "t_mul64" t_mul64
],
testGroup "shifts" [
testProperty "shiftL_Int" shiftL_Int,
testProperty "shiftL_Word16" shiftL_Word16,
testProperty "shiftL_Word32" shiftL_Word32,
testProperty "shiftR_Int" shiftR_Int,
testProperty "shiftR_Word16" shiftR_Word16,
testProperty "shiftR_Word32" shiftR_Word32
],
testGroup "misc" [
testProperty "t_dropWord16" t_dropWord16,
testProperty "t_takeWord16" t_takeWord16,
......
......@@ -114,7 +114,6 @@ library
Data.Text.Internal.Search
Data.Text.Internal.Unsafe
Data.Text.Internal.Unsafe.Char
Data.Text.Internal.Unsafe.Shift
Data.Text.Lazy
Data.Text.Lazy.Builder
Data.Text.Lazy.Builder.Int
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment