on builder help sign in
text/Data/Text/UnsafeShift.hs
69 lines 1.9 kB history raw
1{-# LANGUAGE MagicHash #-}
3-- |
4-- Module : Data.Text.UnsafeShift
5-- Copyright : (c) Bryan O'Sullivan 2009
6--
7-- License : BSD-style
8-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
9-- duncan@haskell.org
10-- Stability : experimental
11-- Portability : GHC
12--
13-- Fast, unchecked bit shifting functions.
15module Data.Text.UnsafeShift
16 (
17 UnsafeShift(..)
18 ) where
20-- import qualified Data.Bits as Bits
21import GHC.Base
22import GHC.Word
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.
27--
28-- These functions are undefined when the amount being shifted by is
29-- greater than the size in bits of a machine Int#.
30class UnsafeShift a where
31 shiftL :: a -> Int -> a
32 shiftR :: a -> Int -> a
34instance UnsafeShift Word16 where
35 {-# INLINE shiftL #-}
36 shiftL (W16# x#) (I# i#) = W16# (narrow16Word# (x# `uncheckedShiftL#` i#))
38 {-# INLINE shiftR #-}
39 shiftR (W16# x#) (I# i#) = W16# (x# `uncheckedShiftRL#` i#)
41instance UnsafeShift Word32 where
42 {-# INLINE shiftL #-}
43 shiftL (W32# x#) (I# i#) = W32# (narrow32Word# (x# `uncheckedShiftL#` i#))
45 {-# INLINE shiftR #-}
46 shiftR (W32# x#) (I# i#) = W32# (x# `uncheckedShiftRL#` i#)
48instance 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#)
55instance 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{-
63instance UnsafeShift Integer where
64 {-# INLINE shiftL #-}
65 shiftL = Bits.shiftL
67 {-# INLINE shiftR #-}
68 shiftR = Bits.shiftR
69-}