@@ -0,0 +1,67 @@
1+{-# LANGUAGE MagicHash #-}
4+-- Module : Data.Text.UnsafeShift
5+-- Copyright : (c) Bryan O'Sullivan 2009
8+-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
10+-- Stability : experimental
13+-- Fast, unchecked bit shifting functions.
15+module Data.Text.UnsafeShift
20+import qualified Data.Bits as Bits
24+-- | This is a workaround for poor optimisation in GHC 6.8.2. It
25+-- fails to notice constant-width shifts, and adds a test and branch
26+-- to every shift. This imposes about a 10% performance hit.
28+-- These functions are undefined when the amount being shifted by is
29+-- greater than the size in bits of a machine Int#.
30+class UnsafeShift a where
31+ shiftL :: a -> Int -> a
32+ shiftR :: a -> Int -> a
34+instance UnsafeShift Word16 where
35+ {-# INLINE shiftL #-}
36+ shiftL (W16# x#) (I# i#) = W16# (x# `uncheckedShiftL#` i#)
38+ {-# INLINE shiftR #-}
39+ shiftR (W16# x#) (I# i#) = W16# (x# `uncheckedShiftRL#` i#)
41+instance UnsafeShift Word32 where
42+ {-# INLINE shiftL #-}
43+ shiftL (W32# x#) (I# i#) = W32# (x# `uncheckedShiftL#` i#)
45+ {-# INLINE shiftR #-}
46+ shiftR (W32# x#) (I# i#) = W32# (x# `uncheckedShiftRL#` i#)
48+instance UnsafeShift Word64 where
49+ {-# INLINE shiftL #-}
50+ shiftL (W64# x#) (I# i#) = W64# (x# `uncheckedShiftL64#` i#)
52+ {-# INLINE shiftR #-}
53+ shiftR (W64# x#) (I# i#) = W64# (x# `uncheckedShiftRL64#` i#)
55+instance UnsafeShift Int where
56+ {-# INLINE shiftL #-}
57+ shiftL (I# x#) (I# i#) = I# (x# `iShiftL#` i#)
59+ {-# INLINE shiftR #-}
60+ shiftR (I# x#) (I# i#) = I# (x# `iShiftRA#` i#)
62+instance UnsafeShift Integer where
63+ {-# INLINE shiftL #-}
66+ {-# INLINE shiftR #-}