Skip to content

Commit 2f88390

Browse files
authored
Merge pull request #311 from obsidiansystems/less-unboxed-needed
Use base-provided unsafe shifts
2 parents 1b275ff + fe7ba1d commit 2f88390

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/Data/Text/Internal/Unsafe/Shift.hs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
{-# LANGUAGE CPP #-}
2+
#if MIN_VERSION_base(4,5,0)
3+
-- base-4.5.0 is 7.4, default sigs introduced in 7.2
4+
{-# LANGUAGE DefaultSignatures #-}
5+
#endif
16
{-# LANGUAGE MagicHash #-}
27

38
-- |
@@ -20,9 +25,13 @@ module Data.Text.Internal.Unsafe.Shift
2025
UnsafeShift(..)
2126
) where
2227

23-
-- import qualified Data.Bits as Bits
28+
#if MIN_VERSION_base(4,5,0)
29+
import qualified Data.Bits as Bits
30+
import Data.Word
31+
#else
2432
import GHC.Base
2533
import GHC.Word
34+
#endif
2635

2736
-- | This is a workaround for poor optimisation in GHC 6.8.2. It
2837
-- fails to notice constant-width shifts, and adds a test and branch
@@ -32,35 +41,54 @@ import GHC.Word
3241
-- greater than the size in bits of a machine Int#.
3342
class UnsafeShift a where
3443
shiftL :: a -> Int -> a
44+
#if MIN_VERSION_base(4,5,0)
45+
{-# INLINE shiftL #-}
46+
default shiftL :: Bits.Bits a => a -> Int -> a
47+
shiftL = Bits.unsafeShiftL
48+
#endif
49+
3550
shiftR :: a -> Int -> a
51+
#if MIN_VERSION_base(4,5,0)
52+
{-# INLINE shiftR #-}
53+
default shiftR :: Bits.Bits a => a -> Int -> a
54+
shiftR = Bits.unsafeShiftR
55+
#endif
3656

3757
instance UnsafeShift Word16 where
58+
#if !MIN_VERSION_base(4,5,0)
3859
{-# INLINE shiftL #-}
3960
shiftL (W16# x#) (I# i#) = W16# (narrow16Word# (x# `uncheckedShiftL#` i#))
4061

4162
{-# INLINE shiftR #-}
4263
shiftR (W16# x#) (I# i#) = W16# (x# `uncheckedShiftRL#` i#)
64+
#endif
4365

4466
instance UnsafeShift Word32 where
67+
#if !MIN_VERSION_base(4,5,0)
4568
{-# INLINE shiftL #-}
4669
shiftL (W32# x#) (I# i#) = W32# (narrow32Word# (x# `uncheckedShiftL#` i#))
4770

4871
{-# INLINE shiftR #-}
4972
shiftR (W32# x#) (I# i#) = W32# (x# `uncheckedShiftRL#` i#)
73+
#endif
5074

5175
instance UnsafeShift Word64 where
76+
#if !MIN_VERSION_base(4,5,0)
5277
{-# INLINE shiftL #-}
5378
shiftL (W64# x#) (I# i#) = W64# (x# `uncheckedShiftL64#` i#)
5479

5580
{-# INLINE shiftR #-}
5681
shiftR (W64# x#) (I# i#) = W64# (x# `uncheckedShiftRL64#` i#)
82+
#endif
5783

5884
instance UnsafeShift Int where
85+
#if !MIN_VERSION_base(4,5,0)
5986
{-# INLINE shiftL #-}
6087
shiftL (I# x#) (I# i#) = I# (x# `iShiftL#` i#)
6188

6289
{-# INLINE shiftR #-}
6390
shiftR (I# x#) (I# i#) = I# (x# `iShiftRA#` i#)
91+
#endif
6492

6593
{-
6694
instance UnsafeShift Integer where

0 commit comments

Comments
 (0)