{-# 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 GHC.Base
import GHC.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
    shiftR :: a -> Int -> a

instance UnsafeShift Word16 where
    {-# INLINE shiftL #-}
    shiftL :: Word16 -> Int -> Word16
shiftL (W16# Word#
x#) (I# Int#
i#) = Word# -> Word16
W16# (Word# -> Word#
narrow16Word# (Word#
x# Word# -> Int# -> Word#
`uncheckedShiftL#` Int#
i#))

    {-# INLINE shiftR #-}
    shiftR :: Word16 -> Int -> Word16
shiftR (W16# Word#
x#) (I# Int#
i#) = Word# -> Word16
W16# (Word#
x# Word# -> Int# -> Word#
`uncheckedShiftRL#` Int#
i#)

instance UnsafeShift Word32 where
    {-# INLINE shiftL #-}
    shiftL :: Word32 -> Int -> Word32
shiftL (W32# Word#
x#) (I# Int#
i#) = Word# -> Word32
W32# (Word# -> Word#
narrow32Word# (Word#
x# Word# -> Int# -> Word#
`uncheckedShiftL#` Int#
i#))

    {-# INLINE shiftR #-}
    shiftR :: Word32 -> Int -> Word32
shiftR (W32# Word#
x#) (I# Int#
i#) = Word# -> Word32
W32# (Word#
x# Word# -> Int# -> Word#
`uncheckedShiftRL#` Int#
i#)

instance UnsafeShift Word64 where
    {-# INLINE shiftL #-}
    shiftL :: Word64 -> Int -> Word64
shiftL (W64# Word#
x#) (I# Int#
i#) = Word# -> Word64
W64# (Word#
x# Word# -> Int# -> Word#
`uncheckedShiftL64#` Int#
i#)

    {-# INLINE shiftR #-}
    shiftR :: Word64 -> Int -> Word64
shiftR (W64# Word#
x#) (I# Int#
i#) = Word# -> Word64
W64# (Word#
x# Word# -> Int# -> Word#
`uncheckedShiftRL64#` Int#
i#)

instance UnsafeShift Int where
    {-# INLINE shiftL #-}
    shiftL :: Int -> Int -> Int
shiftL (I# Int#
x#) (I# Int#
i#) = Int# -> Int
I# (Int#
x# Int# -> Int# -> Int#
`iShiftL#` Int#
i#)

    {-# INLINE shiftR #-}
    shiftR :: Int -> Int -> Int
shiftR (I# Int#
x#) (I# Int#
i#) = Int# -> Int
I# (Int#
x# Int# -> Int# -> Int#
`iShiftRA#` Int#
i#)

{-
instance UnsafeShift Integer where
    {-# INLINE shiftL #-}
    shiftL = Bits.shiftL

    {-# INLINE shiftR #-}
    shiftR = Bits.shiftR
-}