diff --git a/Basement/Alg/UTF8.hs b/Basement/Alg/UTF8.hs index 0a90a8c..d0436f7 100644 --- a/Basement/Alg/UTF8.hs +++ b/Basement/Alg/UTF8.hs @@ -39,6 +39,7 @@ import Basement.PrimType import Basement.UTF8.Helper import Basement.UTF8.Table import Basement.UTF8.Types +import Basement.HeadHackageUtils nextAscii :: Indexable container Word8 => container -> Offset Word8 -> StepASCII nextAscii ba n = StepASCII w @@ -95,8 +96,8 @@ nextWith h ba n = prev :: Indexable container Word8 => container -> Offset Word8 -> StepBack prev ba offset = case index ba prevOfs1 of - (W8# v1) | isContinuation# v1 -> atLeast2 (maskContinuation# v1) - | otherwise -> StepBack (toChar# v1) prevOfs1 + (W8# v1) | isContinuation# (word8ToWordCompat# v1) -> atLeast2 (maskContinuation# (word8ToWordCompat# v1)) + | otherwise -> StepBack (toChar# (word8ToWordCompat# v1)) prevOfs1 where sz1 = CountOf 1 !prevOfs1 = offset `offsetMinusE` sz1 @@ -105,15 +106,15 @@ prev ba offset = prevOfs4 = prevOfs3 `offsetMinusE` sz1 atLeast2 !v = case index ba prevOfs2 of - (W8# v2) | isContinuation# v2 -> atLeast3 (or# (uncheckedShiftL# (maskContinuation# v2) 6#) v) - | otherwise -> StepBack (toChar# (or# (uncheckedShiftL# (maskHeader2# v2) 6#) v)) prevOfs2 + (W8# v2) | isContinuation# (word8ToWordCompat# v2) -> atLeast3 (or# (uncheckedShiftL# (maskContinuation# (word8ToWordCompat# v2)) 6#) v) + | otherwise -> StepBack (toChar# (or# (uncheckedShiftL# (maskHeader2# (word8ToWordCompat# v2)) 6#) v)) prevOfs2 atLeast3 !v = case index ba prevOfs3 of - (W8# v3) | isContinuation# v3 -> atLeast4 (or# (uncheckedShiftL# (maskContinuation# v3) 12#) v) - | otherwise -> StepBack (toChar# (or# (uncheckedShiftL# (maskHeader3# v3) 12#) v)) prevOfs3 + (W8# v3) | isContinuation# (word8ToWordCompat# v3) -> atLeast4 (or# (uncheckedShiftL# (maskContinuation# (word8ToWordCompat# v3)) 12#) v) + | otherwise -> StepBack (toChar# (or# (uncheckedShiftL# (maskHeader3# (word8ToWordCompat# v3)) 12#) v)) prevOfs3 atLeast4 !v = case index ba prevOfs4 of - (W8# v4) -> StepBack (toChar# (or# (uncheckedShiftL# (maskHeader4# v4) 18#) v)) prevOfs4 + (W8# v4) -> StepBack (toChar# (or# (uncheckedShiftL# (maskHeader4# (word8ToWordCompat# v4)) 18#) v)) prevOfs4 prevSkip :: Indexable container Word8 => container -> Offset Word8 -> Offset Word8 prevSkip ba offset = loop (offset `offsetMinusE` sz1) @@ -139,21 +140,21 @@ writeUTF8 mba !i !c !(I# xi) = fromEnum c !x = int2Word# xi - encode1 = write mba i (W8# x) >> pure (i + Offset 1) + encode1 = write mba i (W8# (wordToWord8Compat# x)) >> pure (i + Offset 1) encode2 = do let x1 = or# (uncheckedShiftRL# x 6#) 0xc0## x2 = toContinuation x - write mba i (W8# x1) - write mba (i+1) (W8# x2) + write mba i (W8# (wordToWord8Compat# x1)) + write mba (i+1) (W8# (wordToWord8Compat# x2)) pure (i + Offset 2) encode3 = do let x1 = or# (uncheckedShiftRL# x 12#) 0xe0## x2 = toContinuation (uncheckedShiftRL# x 6#) x3 = toContinuation x - write mba i (W8# x1) - write mba (i+Offset 1) (W8# x2) - write mba (i+Offset 2) (W8# x3) + write mba i (W8# (wordToWord8Compat# x1)) + write mba (i+Offset 1) (W8# (wordToWord8Compat# x2)) + write mba (i+Offset 2) (W8# (wordToWord8Compat# x3)) pure (i + Offset 3) encode4 = do @@ -161,10 +162,10 @@ writeUTF8 mba !i !c x2 = toContinuation (uncheckedShiftRL# x 12#) x3 = toContinuation (uncheckedShiftRL# x 6#) x4 = toContinuation x - write mba i (W8# x1) - write mba (i+Offset 1) (W8# x2) - write mba (i+Offset 2) (W8# x3) - write mba (i+Offset 3) (W8# x4) + write mba i (W8# (wordToWord8Compat# x1)) + write mba (i+Offset 1) (W8# (wordToWord8Compat# x2)) + write mba (i+Offset 2) (W8# (wordToWord8Compat# x3)) + write mba (i+Offset 3) (W8# (wordToWord8Compat# x4)) pure (i + Offset 4) toContinuation :: Word# -> Word# diff --git a/Basement/Base16.hs b/Basement/Base16.hs index 3ed321d..ec7da5a 100644 --- a/Basement/Base16.hs +++ b/Basement/Base16.hs @@ -12,6 +12,7 @@ module Basement.Base16 import GHC.Prim import GHC.Types import GHC.Word +import Basement.HeadHackageUtils import Basement.Types.Char7 data Base16Escape = Base16Escape {-# UNPACK #-} !Char7 {-# UNPACK #-} !Char7 @@ -27,11 +28,11 @@ unsafeConvertByte :: Word# -> (# Word#, Word# #) unsafeConvertByte b = (# r tableHi b, r tableLo b #) where r :: Table -> Word# -> Word# - r (Table !table) index = indexWord8OffAddr# table (word2Int# index) + r (Table !table) index = word8ToWordCompat# (indexWord8OffAddr# table (word2Int# index)) {-# INLINE unsafeConvertByte #-} escapeByte :: Word8 -> Base16Escape -escapeByte !(W8# b) = Base16Escape (r tableHi b) (r tableLo b) +escapeByte !(W8# b) = Base16Escape (r tableHi (word8ToWordCompat# b)) (r tableLo (word8ToWordCompat# b)) where r :: Table -> Word# -> Char7 r (Table !table) index = Char7 (W8# (indexWord8OffAddr# table (word2Int# index))) @@ -43,8 +44,8 @@ hexWord16 (W16# w) = (toChar w1,toChar w2,toChar w3,toChar w4) where toChar :: Word# -> Char toChar c = C# (chr# (word2Int# c)) - !(# w1, w2 #) = unsafeConvertByte (uncheckedShiftRL# w 8#) - !(# w3, w4 #) = unsafeConvertByte (and# w 0xff##) + !(# w1, w2 #) = unsafeConvertByte (uncheckedShiftRL# (word16ToWordCompat# w) 8#) + !(# w3, w4 #) = unsafeConvertByte (and# (word16ToWordCompat# w) 0xff##) -- | hex word32 hexWord32 :: Word32 -> (Char, Char, Char, Char, Char, Char, Char, Char) @@ -53,10 +54,10 @@ hexWord32 (W32# w) = (toChar w1,toChar w2,toChar w3,toChar w4 where toChar :: Word# -> Char toChar c = C# (chr# (word2Int# c)) - !(# w1, w2 #) = unsafeConvertByte (uncheckedShiftRL# w 24#) - !(# w3, w4 #) = unsafeConvertByte (and# (uncheckedShiftRL# w 16#) 0xff##) - !(# w5, w6 #) = unsafeConvertByte (and# (uncheckedShiftRL# w 8#) 0xff##) - !(# w7, w8 #) = unsafeConvertByte (and# w 0xff##) + !(# w1, w2 #) = unsafeConvertByte (uncheckedShiftRL# (word32ToWordCompat# w) 24#) + !(# w3, w4 #) = unsafeConvertByte (and# (uncheckedShiftRL# (word32ToWordCompat# w) 16#) 0xff##) + !(# w5, w6 #) = unsafeConvertByte (and# (uncheckedShiftRL# (word32ToWordCompat# w) 8#) 0xff##) + !(# w7, w8 #) = unsafeConvertByte (and# (word32ToWordCompat# w) 0xff##) data Table = Table Addr# diff --git a/Basement/Bits.hs b/Basement/Bits.hs index f304a86..9a48c3c 100644 --- a/Basement/Bits.hs +++ b/Basement/Bits.hs @@ -41,6 +41,7 @@ import Basement.Types.Word256 (Word256) import qualified Basement.Types.Word256 as Word256 import Basement.IntegralConv (wordToInt) import Basement.Nat +import Basement.HeadHackageUtils import qualified Prelude import qualified Data.Bits as OldBits @@ -237,27 +238,27 @@ instance FiniteBitsOps Word8 where numberOfBits _ = 8 rotateL (W8# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = W8# x# - | otherwise = W8# (narrow8Word# ((x# `uncheckedShiftL#` i'#) `or#` - (x# `uncheckedShiftRL#` (8# -# i'#)))) + | otherwise = W8# (narrow8WordCompat# ((word8ToWordCompat# x# `uncheckedShiftL#` i'#) `or#` + (word8ToWordCompat# x# `uncheckedShiftRL#` (8# -# i'#)))) where !i'# = word2Int# (int2Word# i# `and#` 7##) rotateR (W8# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = W8# x# - | otherwise = W8# (narrow8Word# ((x# `uncheckedShiftRL#` i'#) `or#` - (x# `uncheckedShiftL#` (8# -# i'#)))) + | otherwise = W8# (narrow8WordCompat# ((word8ToWordCompat# x# `uncheckedShiftRL#` i'#) `or#` + (word8ToWordCompat# x# `uncheckedShiftL#` (8# -# i'#)))) where !i'# = word2Int# (int2Word# i# `and#` 7##) - bitFlip (W8# x#) = W8# (x# `xor#` mb#) + bitFlip (W8# x#) = W8# (wordToWord8Compat# (word8ToWordCompat# x# `xor#` word8ToWordCompat# mb#)) where !(W8# mb#) = maxBound - popCount (W8# x#) = CountOf $ wordToInt (W# (popCnt8# x#)) - countLeadingZeros (W8# w#) = CountOf $ wordToInt (W# (clz8# w#)) - countTrailingZeros (W8# w#) = CountOf $ wordToInt (W# (ctz8# w#)) + popCount (W8# x#) = CountOf $ wordToInt (W# (popCnt8# (word8ToWordCompat# x#))) + countLeadingZeros (W8# w#) = CountOf $ wordToInt (W# (clz8# (word8ToWordCompat# w#))) + countTrailingZeros (W8# w#) = CountOf $ wordToInt (W# (ctz8# (word8ToWordCompat# w#))) instance BitOps Word8 where - (W8# x#) .&. (W8# y#) = W8# (x# `and#` y#) - (W8# x#) .|. (W8# y#) = W8# (x# `or#` y#) - (W8# x#) .^. (W8# y#) = W8# (x# `xor#` y#) - (W8# x#) .<<. (CountOf (I# i#)) = W8# (narrow8Word# (x# `shiftL#` i#)) - (W8# x#) .>>. (CountOf (I# i#)) = W8# (narrow8Word# (x# `shiftRL#` i#)) + (W8# x#) .&. (W8# y#) = W8# (wordToWord8Compat# (word8ToWordCompat# x# `and#` word8ToWordCompat# y#)) + (W8# x#) .|. (W8# y#) = W8# (wordToWord8Compat# (word8ToWordCompat# x# `or#` word8ToWordCompat# y#)) + (W8# x#) .^. (W8# y#) = W8# (wordToWord8Compat# (word8ToWordCompat# x# `xor#` word8ToWordCompat# y#)) + (W8# x#) .<<. (CountOf (I# i#)) = W8# (narrow8WordCompat# (word8ToWordCompat# x# `shiftL#` i#)) + (W8# x#) .>>. (CountOf (I# i#)) = W8# (narrow8WordCompat# (word8ToWordCompat# x# `shiftRL#` i#)) -- Word16 --------------------------------------------------------------------- @@ -265,27 +266,27 @@ instance FiniteBitsOps Word16 where numberOfBits _ = 16 rotateL (W16# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = W16# x# - | otherwise = W16# (narrow16Word# ((x# `uncheckedShiftL#` i'#) `or#` - (x# `uncheckedShiftRL#` (16# -# i'#)))) + | otherwise = W16# (narrow16WordCompat# ((word16ToWordCompat# x# `uncheckedShiftL#` i'#) `or#` + (word16ToWordCompat# x# `uncheckedShiftRL#` (16# -# i'#)))) where !i'# = word2Int# (int2Word# i# `and#` 15##) rotateR (W16# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = W16# x# - | otherwise = W16# (narrow16Word# ((x# `uncheckedShiftRL#` i'#) `or#` - (x# `uncheckedShiftL#` (16# -# i'#)))) + | otherwise = W16# (narrow16WordCompat# ((word16ToWordCompat# x# `uncheckedShiftRL#` i'#) `or#` + (word16ToWordCompat# x# `uncheckedShiftL#` (16# -# i'#)))) where !i'# = word2Int# (int2Word# i# `and#` 15##) - bitFlip (W16# x#) = W16# (x# `xor#` mb#) + bitFlip (W16# x#) = W16# (wordToWord16Compat# (word16ToWordCompat# x# `xor#` word16ToWordCompat# mb#)) where !(W16# mb#) = maxBound - popCount (W16# x#) = CountOf $ wordToInt (W# (popCnt16# x#)) - countLeadingZeros (W16# w#) = CountOf $ wordToInt (W# (clz16# w#)) - countTrailingZeros (W16# w#) = CountOf $ wordToInt (W# (ctz16# w#)) + popCount (W16# x#) = CountOf $ wordToInt (W# (popCnt16# (word16ToWordCompat# x#))) + countLeadingZeros (W16# w#) = CountOf $ wordToInt (W# (clz16# (word16ToWordCompat# w#))) + countTrailingZeros (W16# w#) = CountOf $ wordToInt (W# (ctz16# (word16ToWordCompat# w#))) instance BitOps Word16 where - (W16# x#) .&. (W16# y#) = W16# (x# `and#` y#) - (W16# x#) .|. (W16# y#) = W16# (x# `or#` y#) - (W16# x#) .^. (W16# y#) = W16# (x# `xor#` y#) - (W16# x#) .<<. (CountOf (I# i#)) = W16# (narrow16Word# (x# `shiftL#` i#)) - (W16# x#) .>>. (CountOf (I# i#)) = W16# (narrow16Word# (x# `shiftRL#` i#)) + (W16# x#) .&. (W16# y#) = W16# (wordToWord16Compat# (word16ToWordCompat# x# `and#` word16ToWordCompat# y#)) + (W16# x#) .|. (W16# y#) = W16# (wordToWord16Compat# (word16ToWordCompat# x# `or#` word16ToWordCompat# y#)) + (W16# x#) .^. (W16# y#) = W16# (wordToWord16Compat# (word16ToWordCompat# x# `xor#` word16ToWordCompat# y#)) + (W16# x#) .<<. (CountOf (I# i#)) = W16# (narrow16WordCompat# (word16ToWordCompat# x# `shiftL#` i#)) + (W16# x#) .>>. (CountOf (I# i#)) = W16# (narrow16WordCompat# (word16ToWordCompat# x# `shiftRL#` i#)) -- Word32 --------------------------------------------------------------------- @@ -293,27 +294,27 @@ instance FiniteBitsOps Word32 where numberOfBits _ = 32 rotateL (W32# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = W32# x# - | otherwise = W32# (narrow32Word# ((x# `uncheckedShiftL#` i'#) `or#` - (x# `uncheckedShiftRL#` (32# -# i'#)))) + | otherwise = W32# (narrow32WordCompat# ((word32ToWordCompat# x# `uncheckedShiftL#` i'#) `or#` + (word32ToWordCompat# x# `uncheckedShiftRL#` (32# -# i'#)))) where !i'# = word2Int# (int2Word# i# `and#` 31##) rotateR (W32# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = W32# x# - | otherwise = W32# (narrow32Word# ((x# `uncheckedShiftRL#` i'#) `or#` - (x# `uncheckedShiftL#` (32# -# i'#)))) + | otherwise = W32# (narrow32WordCompat# ((word32ToWordCompat# x# `uncheckedShiftRL#` i'#) `or#` + (word32ToWordCompat# x# `uncheckedShiftL#` (32# -# i'#)))) where !i'# = word2Int# (int2Word# i# `and#` 31##) - bitFlip (W32# x#) = W32# (x# `xor#` mb#) + bitFlip (W32# x#) = W32# (wordToWord32Compat# (word32ToWordCompat# x# `xor#` word32ToWordCompat# mb#)) where !(W32# mb#) = maxBound - popCount (W32# x#) = CountOf $ wordToInt (W# (popCnt32# x#)) - countLeadingZeros (W32# w#) = CountOf $ wordToInt (W# (clz32# w#)) - countTrailingZeros (W32# w#) = CountOf $ wordToInt (W# (ctz32# w#)) + popCount (W32# x#) = CountOf $ wordToInt (W# (popCnt32# (word32ToWordCompat# x#))) + countLeadingZeros (W32# w#) = CountOf $ wordToInt (W# (clz32# (word32ToWordCompat# w#))) + countTrailingZeros (W32# w#) = CountOf $ wordToInt (W# (ctz32# (word32ToWordCompat# w#))) instance BitOps Word32 where - (W32# x#) .&. (W32# y#) = W32# (x# `and#` y#) - (W32# x#) .|. (W32# y#) = W32# (x# `or#` y#) - (W32# x#) .^. (W32# y#) = W32# (x# `xor#` y#) - (W32# x#) .<<. (CountOf (I# i#)) = W32# (narrow32Word# (x# `shiftL#` i#)) - (W32# x#) .>>. (CountOf (I# i#)) = W32# (narrow32Word# (x# `shiftRL#` i#)) + (W32# x#) .&. (W32# y#) = W32# (wordToWord32Compat# (word32ToWordCompat# x# `and#` word32ToWordCompat# y#)) + (W32# x#) .|. (W32# y#) = W32# (wordToWord32Compat# (word32ToWordCompat# x# `or#` word32ToWordCompat# y#)) + (W32# x#) .^. (W32# y#) = W32# (wordToWord32Compat# (word32ToWordCompat# x# `xor#` word32ToWordCompat# y#)) + (W32# x#) .<<. (CountOf (I# i#)) = W32# (narrow32WordCompat# (word32ToWordCompat# x# `shiftL#` i#)) + (W32# x#) .>>. (CountOf (I# i#)) = W32# (narrow32WordCompat# (word32ToWordCompat# x# `shiftRL#` i#)) -- Word --------------------------------------------------------------------- @@ -463,28 +464,28 @@ instance FiniteBitsOps Int8 where numberOfBits _ = 8 rotateL (I8# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = I8# x# - | otherwise = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + | otherwise = I8# (narrow8IntCompat# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (8# -# i'#))))) where - !x'# = narrow8Word# (int2Word# x#) + !x'# = narrow8Word# (int2Word# (int8ToIntCompat# x#)) !i'# = word2Int# (int2Word# i# `and#` 7##) rotateR (I8# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = I8# x# - | otherwise = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftRL#` i'#) `or#` + | otherwise = I8# (narrow8IntCompat# (word2Int# ((x'# `uncheckedShiftRL#` i'#) `or#` (x'# `uncheckedShiftL#` (8# -# i'#))))) where - !x'# = narrow8Word# (int2Word# x#) + !x'# = narrow8Word# (int2Word# (int8ToIntCompat# x#)) !i'# = word2Int# (int2Word# i# `and#` 7##) - bitFlip (I8# x#) = I8# (word2Int# (not# (int2Word# x#))) - popCount (I8# x#) = CountOf $ wordToInt (W# (popCnt8# (int2Word# x#))) - countLeadingZeros (I8# w#) = CountOf $ wordToInt (W# (clz8# (int2Word# w#))) - countTrailingZeros (I8# w#) = CountOf $ wordToInt (W# (ctz8# (int2Word# w#))) + bitFlip (I8# x#) = I8# (intToInt8Compat# (word2Int# (not# (int2Word# (int8ToIntCompat# x#))))) + popCount (I8# x#) = CountOf $ wordToInt (W# (popCnt8# (int2Word# (int8ToIntCompat# x#)))) + countLeadingZeros (I8# w#) = CountOf $ wordToInt (W# (clz8# (int2Word# (int8ToIntCompat# w#)))) + countTrailingZeros (I8# w#) = CountOf $ wordToInt (W# (ctz8# (int2Word# (int8ToIntCompat# w#)))) instance BitOps Int8 where - (I8# x#) .&. (I8# y#) = I8# (x# `andI#` y#) - (I8# x#) .|. (I8# y#) = I8# (x# `orI#` y#) - (I8# x#) .^. (I8# y#) = I8# (x# `xorI#` y#) - (I8# x#) .<<. (CountOf (I# i#)) = I8# (narrow8Int# (x# `iShiftL#` i#)) - (I8# x#) .>>. (CountOf (I# i#)) = I8# (narrow8Int# (x# `iShiftRL#` i#)) + (I8# x#) .&. (I8# y#) = I8# (intToInt8Compat# (int8ToIntCompat# x# `andI#` int8ToIntCompat# y#)) + (I8# x#) .|. (I8# y#) = I8# (intToInt8Compat# (int8ToIntCompat# x# `orI#` int8ToIntCompat# y#)) + (I8# x#) .^. (I8# y#) = I8# (intToInt8Compat# (int8ToIntCompat# x# `xorI#` int8ToIntCompat# y#)) + (I8# x#) .<<. (CountOf (I# i#)) = I8# (narrow8IntCompat# (int8ToIntCompat# x# `iShiftL#` i#)) + (I8# x#) .>>. (CountOf (I# i#)) = I8# (narrow8IntCompat# (int8ToIntCompat# x# `iShiftRL#` i#)) -- Int16 ---------------------------------------------------------------------- @@ -492,28 +493,28 @@ instance FiniteBitsOps Int16 where numberOfBits _ = 16 rotateL (I16# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = I16# x# - | otherwise = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + | otherwise = I16# (narrow16IntCompat# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (16# -# i'#))))) where - !x'# = narrow16Word# (int2Word# x#) + !x'# = narrow16Word# (int2Word# (int16ToIntCompat# x#)) !i'# = word2Int# (int2Word# i# `and#` 15##) rotateR (I16# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = I16# x# - | otherwise = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftRL#` i'#) `or#` + | otherwise = I16# (narrow16IntCompat# (word2Int# ((x'# `uncheckedShiftRL#` i'#) `or#` (x'# `uncheckedShiftL#` (16# -# i'#))))) where - !x'# = narrow16Word# (int2Word# x#) + !x'# = narrow16Word# (int2Word# (int16ToIntCompat# x#)) !i'# = word2Int# (int2Word# i# `and#` 15##) - bitFlip (I16# x#) = I16# (word2Int# (not# (int2Word# x#))) - popCount (I16# x#) = CountOf $ wordToInt (W# (popCnt16# (int2Word# x#))) - countLeadingZeros (I16# w#) = CountOf $ wordToInt (W# (clz16# (int2Word# w#))) - countTrailingZeros (I16# w#) = CountOf $ wordToInt (W# (ctz16# (int2Word# w#))) + bitFlip (I16# x#) = I16# (intToInt16Compat# (word2Int# (not# (int2Word# (int16ToIntCompat# x#))))) + popCount (I16# x#) = CountOf $ wordToInt (W# (popCnt16# (int2Word# (int16ToIntCompat# x#)))) + countLeadingZeros (I16# w#) = CountOf $ wordToInt (W# (clz16# (int2Word# (int16ToIntCompat# w#)))) + countTrailingZeros (I16# w#) = CountOf $ wordToInt (W# (ctz16# (int2Word# (int16ToIntCompat# w#)))) instance BitOps Int16 where - (I16# x#) .&. (I16# y#) = I16# (x# `andI#` y#) - (I16# x#) .|. (I16# y#) = I16# (x# `orI#` y#) - (I16# x#) .^. (I16# y#) = I16# (x# `xorI#` y#) - (I16# x#) .<<. (CountOf (I# i#)) = I16# (narrow16Int# (x# `iShiftL#` i#)) - (I16# x#) .>>. (CountOf (I# i#)) = I16# (narrow16Int# (x# `iShiftRL#` i#)) + (I16# x#) .&. (I16# y#) = I16# (intToInt16Compat# (int16ToIntCompat# x# `andI#` int16ToIntCompat# y#)) + (I16# x#) .|. (I16# y#) = I16# (intToInt16Compat# (int16ToIntCompat# x# `orI#` int16ToIntCompat# y#)) + (I16# x#) .^. (I16# y#) = I16# (intToInt16Compat# (int16ToIntCompat# x# `xorI#` int16ToIntCompat# y#)) + (I16# x#) .<<. (CountOf (I# i#)) = I16# (narrow16IntCompat# (int16ToIntCompat# x# `iShiftL#` i#)) + (I16# x#) .>>. (CountOf (I# i#)) = I16# (narrow16IntCompat# (int16ToIntCompat# x# `iShiftRL#` i#)) -- Int32 ---------------------------------------------------------------------- @@ -521,28 +522,28 @@ instance FiniteBitsOps Int32 where numberOfBits _ = 32 rotateL (I32# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = I32# x# - | otherwise = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` + | otherwise = I32# (narrow32IntCompat# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (32# -# i'#))))) where - !x'# = narrow32Word# (int2Word# x#) + !x'# = narrow32Word# (int2Word# (int32ToIntCompat# x#)) !i'# = word2Int# (int2Word# i# `and#` 31##) rotateR (I32# x#) (CountOf (I# i#)) | isTrue# (i'# ==# 0#) = I32# x# - | otherwise = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftRL#` i'#) `or#` + | otherwise = I32# (narrow32IntCompat# (word2Int# ((x'# `uncheckedShiftRL#` i'#) `or#` (x'# `uncheckedShiftL#` (32# -# i'#))))) where - !x'# = narrow32Word# (int2Word# x#) + !x'# = narrow32Word# (int2Word# (int32ToIntCompat# x#)) !i'# = word2Int# (int2Word# i# `and#` 31##) - bitFlip (I32# x#) = I32# (word2Int# (not# (int2Word# x#))) - popCount (I32# x#) = CountOf $ wordToInt (W# (popCnt32# (int2Word# x#))) - countLeadingZeros (I32# w#) = CountOf $ wordToInt (W# (clz32# (int2Word# w#))) - countTrailingZeros (I32# w#) = CountOf $ wordToInt (W# (ctz32# (int2Word# w#))) + bitFlip (I32# x#) = I32# (intToInt32Compat# (word2Int# (not# (int2Word# (int32ToIntCompat# x#))))) + popCount (I32# x#) = CountOf $ wordToInt (W# (popCnt32# (int2Word# (int32ToIntCompat# x#)))) + countLeadingZeros (I32# w#) = CountOf $ wordToInt (W# (clz32# (int2Word# (int32ToIntCompat# w#)))) + countTrailingZeros (I32# w#) = CountOf $ wordToInt (W# (ctz32# (int2Word# (int32ToIntCompat# w#)))) instance BitOps Int32 where - (I32# x#) .&. (I32# y#) = I32# (x# `andI#` y#) - (I32# x#) .|. (I32# y#) = I32# (x# `orI#` y#) - (I32# x#) .^. (I32# y#) = I32# (x# `xorI#` y#) - (I32# x#) .<<. (CountOf (I# i#)) = I32# (narrow32Int# (x# `iShiftL#` i#)) - (I32# x#) .>>. (CountOf (I# i#)) = I32# (narrow32Int# (x# `iShiftRL#` i#)) + (I32# x#) .&. (I32# y#) = I32# (intToInt32Compat# (int32ToIntCompat# x# `andI#` int32ToIntCompat# y#)) + (I32# x#) .|. (I32# y#) = I32# (intToInt32Compat# (int32ToIntCompat# x# `orI#` int32ToIntCompat# y#)) + (I32# x#) .^. (I32# y#) = I32# (intToInt32Compat# (int32ToIntCompat# x# `xorI#` int32ToIntCompat# y#)) + (I32# x#) .<<. (CountOf (I# i#)) = I32# (narrow32IntCompat# (int32ToIntCompat# x# `iShiftL#` i#)) + (I32# x#) .>>. (CountOf (I# i#)) = I32# (narrow32IntCompat# (int32ToIntCompat# x# `iShiftRL#` i#)) -- Int64 ---------------------------------------------------------------------- diff --git a/Basement/Cast.hs b/Basement/Cast.hs index ecccba1..e8e9de2 100644 --- a/Basement/Cast.hs +++ b/Basement/Cast.hs @@ -18,6 +18,7 @@ module Basement.Cast import qualified Basement.Block.Base as Block import Basement.Compat.Base import Basement.Compat.Natural +import Basement.HeadHackageUtils import Basement.Numerical.Number import Basement.Numerical.Conversion import Basement.PrimType @@ -58,22 +59,22 @@ class Cast source destination where Block.unsafeRead (Block.unsafeRecast mba) 0 instance Cast Int8 Word8 where - cast (I8# i) = W8# (narrow8Word# (int2Word# i)) + cast (I8# i) = W8# (narrow8WordCompat# (int2Word# (int8ToIntCompat# i))) instance Cast Int16 Word16 where - cast (I16# i) = W16# (narrow16Word# (int2Word# i)) + cast (I16# i) = W16# (narrow16WordCompat# (int2Word# (int16ToIntCompat# i))) instance Cast Int32 Word32 where - cast (I32# i) = W32# (narrow32Word# (int2Word# i)) + cast (I32# i) = W32# (narrow32WordCompat# (int2Word# (int32ToIntCompat# i))) instance Cast Int64 Word64 where cast = int64ToWord64 instance Cast Int Word where cast (I# i) = W# (int2Word# i) instance Cast Word8 Int8 where - cast (W8# i) = I8# (narrow8Int# (word2Int# i)) + cast (W8# i) = I8# (narrow8IntCompat# (word2Int# (word8ToWordCompat# i))) instance Cast Word16 Int16 where - cast (W16# i) = I16# (narrow16Int# (word2Int# i)) + cast (W16# i) = I16# (narrow16IntCompat# (word2Int# (word16ToWordCompat# i))) instance Cast Word32 Int32 where - cast (W32# i) = I32# (narrow32Int# (word2Int# i)) + cast (W32# i) = I32# (narrow32IntCompat# (word2Int# (word32ToWordCompat# i))) instance Cast Word64 Int64 where cast = word64ToInt64 instance Cast Word Int where diff --git a/Basement/From.hs b/Basement/From.hs index 4f51154..be645d9 100644 --- a/Basement/From.hs +++ b/Basement/From.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -36,6 +37,9 @@ import Basement.Compat.Base -- basic instances import GHC.Types import GHC.Prim +#if __GLASGOW_HASKELL__ >= 903 + hiding (word64ToWord#) +#endif import GHC.Int import GHC.Word import Basement.Numerical.Number @@ -54,6 +58,7 @@ import Basement.These import Basement.PrimType (PrimType, PrimSize) import Basement.Types.OffsetSize import Basement.Compat.Natural +import Basement.HeadHackageUtils import qualified Prelude (fromIntegral) -- nat instances @@ -102,79 +107,79 @@ instance IsIntegral n => From n Integer where from = toInteger instance From Int8 Int16 where - from (I8# i) = I16# i + from (I8# i) = I16# (intToInt16Compat# (int8ToIntCompat# i)) instance From Int8 Int32 where - from (I8# i) = I32# i + from (I8# i) = I32# (intToInt32Compat# (int8ToIntCompat# i)) instance From Int8 Int64 where - from (I8# i) = intToInt64 (I# i) + from (I8# i) = intToInt64 (I# (int8ToIntCompat# i)) instance From Int8 Int where - from (I8# i) = I# i + from (I8# i) = I# (int8ToIntCompat# i) instance From Int16 Int32 where - from (I16# i) = I32# i + from (I16# i) = I32# (intToInt32Compat# (int16ToIntCompat# i)) instance From Int16 Int64 where - from (I16# i) = intToInt64 (I# i) + from (I16# i) = intToInt64 (I# (int16ToIntCompat# i)) instance From Int16 Int where - from (I16# i) = I# i + from (I16# i) = I# (int16ToIntCompat# i) instance From Int32 Int64 where - from (I32# i) = intToInt64 (I# i) + from (I32# i) = intToInt64 (I# (int32ToIntCompat# i)) instance From Int32 Int where - from (I32# i) = I# i + from (I32# i) = I# (int32ToIntCompat# i) instance From Int Int64 where from = intToInt64 instance From Word8 Word16 where - from (W8# i) = W16# i + from (W8# i) = W16# (wordToWord16Compat# (word8ToWordCompat# i)) instance From Word8 Word32 where - from (W8# i) = W32# i + from (W8# i) = W32# (wordToWord32Compat# (word8ToWordCompat# i)) instance From Word8 Word64 where - from (W8# i) = wordToWord64 (W# i) + from (W8# i) = wordToWord64 (W# (word8ToWordCompat# i)) instance From Word8 Word128 where - from (W8# i) = Word128 0 (wordToWord64 $ W# i) + from (W8# i) = Word128 0 (wordToWord64 $ W# (word8ToWordCompat# i)) instance From Word8 Word256 where - from (W8# i) = Word256 0 0 0 (wordToWord64 $ W# i) + from (W8# i) = Word256 0 0 0 (wordToWord64 $ W# (word8ToWordCompat# i)) instance From Word8 Word where - from (W8# i) = W# i + from (W8# i) = W# (word8ToWordCompat# i) instance From Word8 Int16 where - from (W8# w) = I16# (word2Int# w) + from (W8# w) = I16# (intToInt16Compat# (word2Int# (word8ToWordCompat# w))) instance From Word8 Int32 where - from (W8# w) = I32# (word2Int# w) + from (W8# w) = I32# (intToInt32Compat# (word2Int# (word8ToWordCompat# w))) instance From Word8 Int64 where - from (W8# w) = intToInt64 (I# (word2Int# w)) + from (W8# w) = intToInt64 (I# (word2Int# (word8ToWordCompat# w))) instance From Word8 Int where - from (W8# w) = I# (word2Int# w) + from (W8# w) = I# (word2Int# (word8ToWordCompat# w)) instance From Word16 Word32 where - from (W16# i) = W32# i + from (W16# i) = W32# (wordToWord32Compat# (word16ToWordCompat# i)) instance From Word16 Word64 where - from (W16# i) = wordToWord64 (W# i) + from (W16# i) = wordToWord64 (W# (word16ToWordCompat# i)) instance From Word16 Word128 where - from (W16# i) = Word128 0 (wordToWord64 $ W# i) + from (W16# i) = Word128 0 (wordToWord64 $ W# (word16ToWordCompat# i)) instance From Word16 Word256 where - from (W16# i) = Word256 0 0 0 (wordToWord64 $ W# i) + from (W16# i) = Word256 0 0 0 (wordToWord64 $ W# (word16ToWordCompat# i)) instance From Word16 Word where - from (W16# i) = W# i + from (W16# i) = W# (word16ToWordCompat# i) instance From Word16 Int32 where - from (W16# w) = I32# (word2Int# w) + from (W16# w) = I32# (intToInt32Compat# (word2Int# (word16ToWordCompat# w))) instance From Word16 Int64 where - from (W16# w) = intToInt64 (I# (word2Int# w)) + from (W16# w) = intToInt64 (I# (word2Int# (word16ToWordCompat# w))) instance From Word16 Int where - from (W16# w) = I# (word2Int# w) + from (W16# w) = I# (word2Int# (word16ToWordCompat# w)) instance From Word32 Word64 where - from (W32# i) = wordToWord64 (W# i) + from (W32# i) = wordToWord64 (W# (word32ToWordCompat# i)) instance From Word32 Word128 where - from (W32# i) = Word128 0 (wordToWord64 $ W# i) + from (W32# i) = Word128 0 (wordToWord64 $ W# (word32ToWordCompat# i)) instance From Word32 Word256 where - from (W32# i) = Word256 0 0 0 (wordToWord64 $ W# i) + from (W32# i) = Word256 0 0 0 (wordToWord64 $ W# (word32ToWordCompat# i)) instance From Word32 Word where - from (W32# i) = W# i + from (W32# i) = W# (word32ToWordCompat# i) instance From Word32 Int64 where - from (W32# w) = intToInt64 (I# (word2Int# w)) + from (W32# w) = intToInt64 (I# (word2Int# (word32ToWordCompat# w))) instance From Word32 Int where - from (W32# w) = I# (word2Int# w) + from (W32# w) = I# (word2Int# (word32ToWordCompat# w)) instance From Word64 Word128 where from w = Word128 0 w @@ -270,11 +275,11 @@ instance (NatWithinBound (CountOf ty) n, KnownNat n, PrimType ty) tryFrom = BlockN.toBlockN . UArray.toBlock . BoxArray.mapToUnboxed id instance (KnownNat n, NatWithinBound Word8 n) => From (Zn64 n) Word8 where - from = narrow . unZn64 where narrow (W64# w) = W8# (narrow8Word# (word64ToWord# w)) + from = narrow . unZn64 where narrow (W64# w) = W8# (narrow8WordCompat# (word64ToWord# w)) instance (KnownNat n, NatWithinBound Word16 n) => From (Zn64 n) Word16 where - from = narrow . unZn64 where narrow (W64# w) = W16# (narrow16Word# (word64ToWord# w)) + from = narrow . unZn64 where narrow (W64# w) = W16# (narrow16WordCompat# (word64ToWord# w)) instance (KnownNat n, NatWithinBound Word32 n) => From (Zn64 n) Word32 where - from = narrow . unZn64 where narrow (W64# w) = W32# (narrow32Word# (word64ToWord# w)) + from = narrow . unZn64 where narrow (W64# w) = W32# (narrow32WordCompat# (word64ToWord# w)) instance From (Zn64 n) Word64 where from = unZn64 instance From (Zn64 n) Word128 where @@ -283,11 +288,11 @@ instance From (Zn64 n) Word256 where from = from . unZn64 instance (KnownNat n, NatWithinBound Word8 n) => From (Zn n) Word8 where - from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W8# (narrow8Word# (word64ToWord# w)) + from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W8# (narrow8WordCompat# (word64ToWord# w)) instance (KnownNat n, NatWithinBound Word16 n) => From (Zn n) Word16 where - from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W16# (narrow16Word# (word64ToWord# w)) + from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W16# (narrow16WordCompat# (word64ToWord# w)) instance (KnownNat n, NatWithinBound Word32 n) => From (Zn n) Word32 where - from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W32# (narrow32Word# (word64ToWord# w)) + from = narrow . naturalToWord64 . unZn where narrow (W64# w) = W32# (narrow32WordCompat# (word64ToWord# w)) instance (KnownNat n, NatWithinBound Word64 n) => From (Zn n) Word64 where from = naturalToWord64 . unZn instance (KnownNat n, NatWithinBound Word128 n) => From (Zn n) Word128 where diff --git a/Basement/HeadHackageUtils.hs b/Basement/HeadHackageUtils.hs new file mode 100644 index 0000000..62fecde --- /dev/null +++ b/Basement/HeadHackageUtils.hs @@ -0,0 +1,119 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE MagicHash #-} +module Basement.HeadHackageUtils where + +import GHC.Exts + +#if MIN_VERSION_base(4,16,0) +int8ToIntCompat# :: Int8# -> Int# +int8ToIntCompat# = int8ToInt# + +int16ToIntCompat# :: Int16# -> Int# +int16ToIntCompat# = int16ToInt# + +int32ToIntCompat# :: Int32# -> Int# +int32ToIntCompat# = int32ToInt# + +word8ToWordCompat# :: Word8# -> Word# +word8ToWordCompat# = word8ToWord# + +word16ToWordCompat# :: Word16# -> Word# +word16ToWordCompat# = word16ToWord# + +word32ToWordCompat# :: Word32# -> Word# +word32ToWordCompat# = word32ToWord# + +intToInt8Compat# :: Int# -> Int8# +intToInt8Compat# = intToInt8# + +intToInt16Compat# :: Int# -> Int16# +intToInt16Compat# = intToInt16# + +intToInt32Compat# :: Int# -> Int32# +intToInt32Compat# = intToInt32# + +wordToWord8Compat# :: Word# -> Word8# +wordToWord8Compat# = wordToWord8# + +wordToWord16Compat# :: Word# -> Word16# +wordToWord16Compat# = wordToWord16# + +wordToWord32Compat# :: Word# -> Word32# +wordToWord32Compat# = wordToWord32# + +-- + +narrow8IntCompat# :: Int# -> Int8# +narrow8IntCompat# = intToInt8# + +narrow16IntCompat# :: Int# -> Int16# +narrow16IntCompat# = intToInt16# + +narrow32IntCompat# :: Int# -> Int32# +narrow32IntCompat# = intToInt32# + +narrow8WordCompat# :: Word# -> Word8# +narrow8WordCompat# = wordToWord8# + +narrow16WordCompat# :: Word# -> Word16# +narrow16WordCompat# = wordToWord16# + +narrow32WordCompat# :: Word# -> Word32# +narrow32WordCompat# = wordToWord32# +#else +-- No-ops +int8ToIntCompat# :: Int# -> Int# +int8ToIntCompat# x = x + +int16ToIntCompat# :: Int# -> Int# +int16ToIntCompat# x = x + +int32ToIntCompat# :: Int# -> Int# +int32ToIntCompat# x = x + +word8ToWordCompat# :: Word# -> Word# +word8ToWordCompat# x = x + +word16ToWordCompat# :: Word# -> Word# +word16ToWordCompat# x = x + +word32ToWordCompat# :: Word# -> Word# +word32ToWordCompat# x = x + +intToInt8Compat# :: Int# -> Int# +intToInt8Compat# x = x + +intToInt16Compat# :: Int# -> Int# +intToInt16Compat# x = x + +intToInt32Compat# :: Int# -> Int# +intToInt32Compat# x = x + +wordToWord8Compat# :: Word# -> Word# +wordToWord8Compat# x = x + +wordToWord16Compat# :: Word# -> Word# +wordToWord16Compat# x = x + +wordToWord32Compat# :: Word# -> Word# +wordToWord32Compat# x = x + +-- Actual narrowing +narrow8IntCompat# :: Int# -> Int# +narrow8IntCompat# = narrow8Int# + +narrow16IntCompat# :: Int# -> Int# +narrow16IntCompat# = narrow16Int# + +narrow32IntCompat# :: Int# -> Int# +narrow32IntCompat# = narrow32Int# + +narrow8WordCompat# :: Word# -> Word# +narrow8WordCompat# = narrow8Word# + +narrow16WordCompat# :: Word# -> Word# +narrow16WordCompat# = narrow16Word# + +narrow32WordCompat# :: Word# -> Word# +narrow32WordCompat# = narrow32Word# +#endif diff --git a/Basement/IntegralConv.hs b/Basement/IntegralConv.hs index aff92b1..357bcdf 100644 --- a/Basement/IntegralConv.hs +++ b/Basement/IntegralConv.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-} @@ -21,11 +22,15 @@ module Basement.IntegralConv import GHC.Types import GHC.Prim +#if __GLASGOW_HASKELL__ >= 903 + hiding (word64ToWord#) +#endif import GHC.Int import GHC.Word import Prelude (Integer, fromIntegral) import Basement.Compat.Base import Basement.Compat.Natural +import Basement.HeadHackageUtils import Basement.Numerical.Number import Basement.Numerical.Conversion @@ -58,69 +63,69 @@ instance IsNatural a => IntegralUpsize a Natural where integralUpsize = toNatural instance IntegralUpsize Int8 Int16 where - integralUpsize (I8# i) = I16# i + integralUpsize (I8# i) = I16# (intToInt16Compat# (int8ToIntCompat# i)) instance IntegralUpsize Int8 Int32 where - integralUpsize (I8# i) = I32# i + integralUpsize (I8# i) = I32# (intToInt32Compat# (int8ToIntCompat# i)) instance IntegralUpsize Int8 Int64 where - integralUpsize (I8# i) = intToInt64 (I# i) + integralUpsize (I8# i) = intToInt64 (I# (int8ToIntCompat# i)) instance IntegralUpsize Int8 Int where - integralUpsize (I8# i) = I# i + integralUpsize (I8# i) = I# (int8ToIntCompat# i) instance IntegralUpsize Int16 Int32 where - integralUpsize (I16# i) = I32# i + integralUpsize (I16# i) = I32# (intToInt32Compat# (int16ToIntCompat# i)) instance IntegralUpsize Int16 Int64 where - integralUpsize (I16# i) = intToInt64 (I# i) + integralUpsize (I16# i) = intToInt64 (I# (int16ToIntCompat# i)) instance IntegralUpsize Int16 Int where - integralUpsize (I16# i) = I# i + integralUpsize (I16# i) = I# (int16ToIntCompat# i) instance IntegralUpsize Int32 Int64 where - integralUpsize (I32# i) = intToInt64 (I# i) + integralUpsize (I32# i) = intToInt64 (I# (int32ToIntCompat# i)) instance IntegralUpsize Int32 Int where - integralUpsize (I32# i) = I# i + integralUpsize (I32# i) = I# (int32ToIntCompat# i) instance IntegralUpsize Int Int64 where integralUpsize = intToInt64 instance IntegralUpsize Word8 Word16 where - integralUpsize (W8# i) = W16# i + integralUpsize (W8# i) = W16# (wordToWord16Compat# (word8ToWordCompat# i)) instance IntegralUpsize Word8 Word32 where - integralUpsize (W8# i) = W32# i + integralUpsize (W8# i) = W32# (wordToWord32Compat# (word8ToWordCompat# i)) instance IntegralUpsize Word8 Word64 where - integralUpsize (W8# i) = wordToWord64 (W# i) + integralUpsize (W8# i) = wordToWord64 (W# (word8ToWordCompat# i)) instance IntegralUpsize Word8 Word where - integralUpsize (W8# i) = W# i + integralUpsize (W8# i) = W# (word8ToWordCompat# i) instance IntegralUpsize Word8 Int16 where - integralUpsize (W8# w) = I16# (word2Int# w) + integralUpsize (W8# w) = I16# (intToInt16Compat# (word2Int# (word8ToWordCompat# w))) instance IntegralUpsize Word8 Int32 where - integralUpsize (W8# w) = I32# (word2Int# w) + integralUpsize (W8# w) = I32# (intToInt32Compat# (word2Int# (word8ToWordCompat# w))) instance IntegralUpsize Word8 Int64 where - integralUpsize (W8# w) = intToInt64 (I# (word2Int# w)) + integralUpsize (W8# w) = intToInt64 (I# (word2Int# (word8ToWordCompat# w))) instance IntegralUpsize Word8 Int where - integralUpsize (W8# w) = I# (word2Int# w) + integralUpsize (W8# w) = I# (word2Int# (word8ToWordCompat# w)) instance IntegralUpsize Word16 Word32 where - integralUpsize (W16# i) = W32# i + integralUpsize (W16# i) = W32# (wordToWord32Compat# (word16ToWordCompat# i)) instance IntegralUpsize Word16 Word64 where - integralUpsize (W16# i) = wordToWord64 (W# i) + integralUpsize (W16# i) = wordToWord64 (W# (word16ToWordCompat# i)) instance IntegralUpsize Word16 Word where - integralUpsize (W16# i) = W# i + integralUpsize (W16# i) = W# (word16ToWordCompat# i) instance IntegralUpsize Word32 Word64 where - integralUpsize (W32# i) = wordToWord64 (W# i) + integralUpsize (W32# i) = wordToWord64 (W# (word32ToWordCompat# i)) instance IntegralUpsize Word32 Word where - integralUpsize (W32# i) = W# i + integralUpsize (W32# i) = W# (word32ToWordCompat# i) instance IntegralUpsize Word Word64 where integralUpsize = wordToWord64 instance IntegralDownsize Int Int8 where - integralDownsize (I# i) = I8# (narrow8Int# i) + integralDownsize (I# i) = I8# (narrow8IntCompat# i) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Int Int16 where - integralDownsize (I# i) = I16# (narrow16Int# i) + integralDownsize (I# i) = I16# (narrow16IntCompat# i) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Int Int32 where - integralDownsize (I# i) = I32# (narrow32Int# i) + integralDownsize (I# i) = I32# (narrow32IntCompat# i) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Int64 Int8 where @@ -137,34 +142,34 @@ instance IntegralDownsize Int64 Int where integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word64 Word8 where - integralDownsize (W64# i) = W8# (narrow8Word# (word64ToWord# i)) + integralDownsize (W64# i) = W8# (narrow8WordCompat# (word64ToWord# i)) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word64 Word16 where - integralDownsize (W64# i) = W16# (narrow16Word# (word64ToWord# i)) + integralDownsize (W64# i) = W16# (narrow16WordCompat# (word64ToWord# i)) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word64 Word32 where - integralDownsize (W64# i) = W32# (narrow32Word# (word64ToWord# i)) + integralDownsize (W64# i) = W32# (narrow32WordCompat# (word64ToWord# i)) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word Word8 where - integralDownsize (W# w) = W8# (narrow8Word# w) + integralDownsize (W# w) = W8# (narrow8WordCompat# w) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word Word16 where - integralDownsize (W# w) = W16# (narrow16Word# w) + integralDownsize (W# w) = W16# (narrow16WordCompat# w) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word Word32 where - integralDownsize (W# w) = W32# (narrow32Word# w) + integralDownsize (W# w) = W32# (narrow32WordCompat# w) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word32 Word8 where - integralDownsize (W32# i) = W8# (narrow8Word# i) + integralDownsize (W32# i) = W8# (narrow8WordCompat# (word32ToWordCompat# i)) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word32 Word16 where - integralDownsize (W32# i) = W16# (narrow16Word# i) + integralDownsize (W32# i) = W16# (narrow16WordCompat# (word32ToWordCompat# i)) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Word16 Word8 where - integralDownsize (W16# i) = W8# (narrow8Word# i) + integralDownsize (W16# i) = W8# (narrow8WordCompat# (word16ToWordCompat# i)) integralDownsizeCheck = integralDownsizeBounded integralDownsize instance IntegralDownsize Integer Int8 where diff --git a/Basement/Numerical/Additive.hs b/Basement/Numerical/Additive.hs index 7973887..1fd2091 100644 --- a/Basement/Numerical/Additive.hs +++ b/Basement/Numerical/Additive.hs @@ -21,6 +21,7 @@ import GHC.Prim import GHC.Int import GHC.Word import Basement.Bounded +import Basement.HeadHackageUtils import Basement.Nat import Basement.Types.Word128 (Word128) import Basement.Types.Word256 (Word256) @@ -65,15 +66,15 @@ instance Additive Int where scale = scaleNum instance Additive Int8 where azero = 0 - (I8# a) + (I8# b) = I8# (narrow8Int# (a +# b)) + (I8# a) + (I8# b) = I8# (narrow8IntCompat# (int8ToIntCompat# a +# int8ToIntCompat# b)) scale = scaleNum instance Additive Int16 where azero = 0 - (I16# a) + (I16# b) = I16# (narrow16Int# (a +# b)) + (I16# a) + (I16# b) = I16# (narrow16IntCompat# (int16ToIntCompat# a +# int16ToIntCompat# b)) scale = scaleNum instance Additive Int32 where azero = 0 - (I32# a) + (I32# b) = I32# (narrow32Int# (a +# b)) + (I32# a) + (I32# b) = I32# (narrow32IntCompat# (int32ToIntCompat# a +# int32ToIntCompat# b)) scale = scaleNum instance Additive Int64 where azero = 0 @@ -93,15 +94,15 @@ instance Additive Natural where scale = scaleNum instance Additive Word8 where azero = 0 - (W8# a) + (W8# b) = W8# (narrow8Word# (a `plusWord#` b)) + (W8# a) + (W8# b) = W8# (narrow8WordCompat# (word8ToWordCompat# a `plusWord#` word8ToWordCompat# b)) scale = scaleNum instance Additive Word16 where azero = 0 - (W16# a) + (W16# b) = W16# (narrow16Word# (a `plusWord#` b)) + (W16# a) + (W16# b) = W16# (narrow16WordCompat# (word16ToWordCompat# a `plusWord#` word16ToWordCompat# b)) scale = scaleNum instance Additive Word32 where azero = 0 - (W32# a) + (W32# b) = W32# (narrow32Word# (a `plusWord#` b)) + (W32# a) + (W32# b) = W32# (narrow32WordCompat# (word32ToWordCompat# a `plusWord#` word32ToWordCompat# b)) scale = scaleNum instance Additive Word64 where azero = 0 diff --git a/Basement/Numerical/Conversion.hs b/Basement/Numerical/Conversion.hs index a86d195..f967c34 100644 --- a/Basement/Numerical/Conversion.hs +++ b/Basement/Numerical/Conversion.hs @@ -18,8 +18,12 @@ module Basement.Numerical.Conversion #include "MachDeps.h" +import Basement.HeadHackageUtils import GHC.Types import GHC.Prim +#if __GLASGOW_HASKELL__ >= 903 + hiding (word64ToWord#) +#endif import GHC.Int import GHC.Word @@ -81,7 +85,7 @@ data Word32x2 = Word32x2 {-# UNPACK #-} !Word32 #if WORD_SIZE_IN_BITS == 64 word64ToWord32s :: Word64 -> Word32x2 -word64ToWord32s (W64# w64) = Word32x2 (W32# (uncheckedShiftRL# w64 32#)) (W32# (narrow32Word# w64)) +word64ToWord32s (W64# w64) = Word32x2 (W32# (wordToWord32Compat# (uncheckedShiftRL# w64 32#))) (W32# (narrow32WordCompat# w64)) #else word64ToWord32s :: Word64 -> Word32x2 word64ToWord32s (W64# w64) = Word32x2 (W32# (word64ToWord# (uncheckedShiftRL64# w64 32#))) (W32# (word64ToWord# w64)) diff --git a/Basement/String.hs b/Basement/String.hs index 980434f..4d2edce 100644 --- a/Basement/String.hs +++ b/Basement/String.hs @@ -129,6 +129,7 @@ import qualified Basement.Alg.UTF8 as UTF8 import qualified Basement.Alg.String as Alg import Basement.Types.Char7 (Char7(..), c7Upper, c7Lower) import qualified Basement.Types.Char7 as Char7 +import Basement.HeadHackageUtils import GHC.Prim import GHC.ST import GHC.Types @@ -229,13 +230,13 @@ nextWithIndexer :: (Offset Word8 -> Word8) -> Offset Word8 -> (Char, Offset Word8) nextWithIndexer getter off = - case getNbBytes# h of - 0# -> (toChar h, off + 1) + case getNbBytes# (word8ToWordCompat# h) of + 0# -> (toChar (word8ToWordCompat# h), off + 1) 1# -> (toChar (decode2 (getter $ off + 1)), off + 2) 2# -> (toChar (decode3 (getter $ off + 1) (getter $ off + 2)), off + 3) 3# -> (toChar (decode4 (getter $ off + 1) (getter $ off + 2) (getter $ off + 3)) , off + 4) - r -> error ("next: internal error: invalid input: " <> show (I# r) <> " " <> show (W# h)) + r -> error ("next: internal error: invalid input: " <> show (I# r) <> " " <> show (W# (word8ToWordCompat# h))) where !(W8# h) = getter off @@ -244,21 +245,21 @@ nextWithIndexer getter off = decode2 :: Word8 -> Word# decode2 (W8# c1) = - or# (uncheckedShiftL# (and# h 0x1f##) 6#) - (and# c1 0x3f##) + or# (uncheckedShiftL# (and# (word8ToWordCompat# h) 0x1f##) 6#) + (and# (word8ToWordCompat# c1) 0x3f##) decode3 :: Word8 -> Word8 -> Word# decode3 (W8# c1) (W8# c2) = - or# (uncheckedShiftL# (and# h 0xf##) 12#) - (or# (uncheckedShiftL# (and# c1 0x3f##) 6#) - (and# c2 0x3f##)) + or# (uncheckedShiftL# (and# (word8ToWordCompat# h) 0xf##) 12#) + (or# (uncheckedShiftL# (and# (word8ToWordCompat# c1) 0x3f##) 6#) + (and# (word8ToWordCompat# c2) 0x3f##)) decode4 :: Word8 -> Word8 -> Word8 -> Word# decode4 (W8# c1) (W8# c2) (W8# c3) = - or# (uncheckedShiftL# (and# h 0x7##) 18#) - (or# (uncheckedShiftL# (and# c1 0x3f##) 12#) - (or# (uncheckedShiftL# (and# c2 0x3f##) 6#) - (and# c3 0x3f##)) + or# (uncheckedShiftL# (and# (word8ToWordCompat# h) 0x7##) 18#) + (or# (uncheckedShiftL# (and# (word8ToWordCompat# c1) 0x3f##) 12#) + (or# (uncheckedShiftL# (and# (word8ToWordCompat# c2) 0x3f##) 6#) + (and# (word8ToWordCompat# c3) 0x3f##)) ) writeWithBuilder :: (PrimMonad st, Monad st) @@ -273,25 +274,25 @@ writeWithBuilder c !(I# xi) = fromEnum c !x = int2Word# xi - encode1 = Vec.builderAppend (W8# x) + encode1 = Vec.builderAppend (W8# (wordToWord8Compat# x)) encode2 = do let x1 = or# (uncheckedShiftRL# x 6#) 0xc0## x2 = toContinuation x - Vec.builderAppend (W8# x1) >> Vec.builderAppend (W8# x2) + Vec.builderAppend (W8# (wordToWord8Compat# x1)) >> Vec.builderAppend (W8# (wordToWord8Compat# x2)) encode3 = do let x1 = or# (uncheckedShiftRL# x 12#) 0xe0## x2 = toContinuation (uncheckedShiftRL# x 6#) x3 = toContinuation x - Vec.builderAppend (W8# x1) >> Vec.builderAppend (W8# x2) >> Vec.builderAppend (W8# x3) + Vec.builderAppend (W8# (wordToWord8Compat# x1)) >> Vec.builderAppend (W8# (wordToWord8Compat# x2)) >> Vec.builderAppend (W8# (wordToWord8Compat# x3)) encode4 = do let x1 = or# (uncheckedShiftRL# x 18#) 0xf0## x2 = toContinuation (uncheckedShiftRL# x 12#) x3 = toContinuation (uncheckedShiftRL# x 6#) x4 = toContinuation x - Vec.builderAppend (W8# x1) >> Vec.builderAppend (W8# x2) >> Vec.builderAppend (W8# x3) >> Vec.builderAppend (W8# x4) + Vec.builderAppend (W8# (wordToWord8Compat# x1)) >> Vec.builderAppend (W8# (wordToWord8Compat# x2)) >> Vec.builderAppend (W8# (wordToWord8Compat# x3)) >> Vec.builderAppend (W8# (wordToWord8Compat# x4)) toContinuation :: Word# -> Word# toContinuation w = or# (and# w 0x3f##) 0x80## diff --git a/Basement/String/Encoding/ASCII7.hs b/Basement/String/Encoding/ASCII7.hs index 23b0b06..6bc01dd 100644 --- a/Basement/String/Encoding/ASCII7.hs +++ b/Basement/String/Encoding/ASCII7.hs @@ -26,13 +26,14 @@ import Basement.UArray.Mutable (MUArray) import Basement.MutableBuilder import Basement.String.Encoding.Encoding +import Basement.HeadHackageUtils -- | validate a given byte is within ASCII characters encoring size -- -- This function check the 8th bit is set to 0 -- isAscii :: Word8 -> Bool -isAscii (W8# w) = W8# (and# w 0x80## ) == 0 +isAscii (W8# w) = W8# (wordToWord8Compat# (and# (word8ToWordCompat# w) 0x80## )) == 0 {-# INLINE isAscii #-} data ASCII7_Invalid @@ -60,7 +61,7 @@ next :: (Offset Word8 -> Word8) -- ^ either successfully validated the ASCII char and returned the -- next index or fail with an error next getter off - | isAscii w8 = Right (toChar w, off + 1) + | isAscii w8 = Right (toChar (word8ToWordCompat# w), off + 1) | otherwise = Left $ ByteOutOfBound w8 where !w8@(W8# w) = getter off @@ -81,4 +82,4 @@ write c | otherwise = throw $ CharNotAscii c where w8 :: Char -> Word8 - w8 (C# ch) = W8# (int2Word# (ord# ch)) + w8 (C# ch) = W8# (wordToWord8Compat# (int2Word# (ord# ch))) diff --git a/Basement/String/Encoding/ISO_8859_1.hs b/Basement/String/Encoding/ISO_8859_1.hs index 9f25822..e8d5aca 100644 --- a/Basement/String/Encoding/ISO_8859_1.hs +++ b/Basement/String/Encoding/ISO_8859_1.hs @@ -17,6 +17,7 @@ import Basement.Compat.Base import Basement.Types.OffsetSize import Basement.Numerical.Additive import Basement.Monad +import Basement.HeadHackageUtils import GHC.Prim import GHC.Word @@ -47,7 +48,7 @@ instance Encoding ISO_8859_1 where next :: (Offset Word8 -> Word8) -> Offset Word8 -> Either ISO_8859_1_Invalid (Char, Offset Word8) -next getter off = Right (toChar w, off + aone) +next getter off = Right (toChar (word8ToWordCompat# w), off + aone) where !(W8# w) = getter off toChar :: Word# -> Char @@ -57,7 +58,7 @@ write :: (PrimMonad st, Monad st) => Char -> Builder (UArray Word8) (MUArray Word8) Word8 st err () write c@(C# ch) - | c <= toEnum 0xFF = builderAppend (W8# x) + | c <= toEnum 0xFF = builderAppend (W8# (wordToWord8Compat# x)) | otherwise = throw $ NotISO_8859_1 c where x :: Word# diff --git a/Basement/String/Encoding/UTF16.hs b/Basement/String/Encoding/UTF16.hs index 70da506..d6fb9ae 100644 --- a/Basement/String/Encoding/UTF16.hs +++ b/Basement/String/Encoding/UTF16.hs @@ -23,6 +23,7 @@ import Basement.Numerical.Additive import Basement.UArray import Basement.UArray.Mutable (MUArray) import Basement.MutableBuilder +import Basement.HeadHackageUtils import Basement.String.Encoding.Encoding @@ -54,8 +55,8 @@ next :: (Offset Word16 -> Word16) -> Offset Word16 -> Either UTF16_Invalid (Char, Offset Word16) next getter off - | h < 0xd800 = Right (toChar hh, off + Offset 1) - | h >= 0xe000 = Right (toChar hh, off + Offset 1) + | h < 0xd800 = Right (toChar (word16ToWordCompat# hh), off + Offset 1) + | h >= 0xe000 = Right (toChar (word16ToWordCompat# hh), off + Offset 1) | otherwise = nextContinuation where h :: Word16 @@ -63,13 +64,13 @@ next getter off toChar :: Word# -> Char toChar w = C# (chr# (word2Int# w)) to32 :: Word16 -> Word32 - to32 (W16# w) = W32# w + to32 (W16# w) = W32# (wordToWord32Compat# (word16ToWordCompat# w)) nextContinuation | cont >= 0xdc00 && cont < 0xe00 = let !(W32# w) = ((to32 h .&. 0x3ff) `shiftL` 10) .|. (to32 cont .&. 0x3ff) - in Right (toChar w, off + Offset 2) + in Right (toChar (word32ToWordCompat# w), off + Offset 2) | otherwise = Left InvalidContinuation where cont :: Word16 @@ -86,12 +87,12 @@ write c | otherwise = throw $ InvalidUnicode c where w16 :: Char -> Word16 - w16 (C# ch) = W16# (int2Word# (ord# ch)) + w16 (C# ch) = W16# (wordToWord16Compat# (int2Word# (ord# ch))) to16 :: Word32 -> Word16 to16 = Prelude.fromIntegral wHigh :: Char -> (Word16, Word16) wHigh (C# ch) = - let v = W32# (minusWord# (int2Word# (ord# ch)) 0x10000##) + let v = W32# (wordToWord32Compat# (minusWord# (int2Word# (ord# ch)) 0x10000##)) in (0xdc00 .|. to16 (v `shiftR` 10), 0xd800 .|. to16 (v .&. 0x3ff)) diff --git a/Basement/String/Encoding/UTF32.hs b/Basement/String/Encoding/UTF32.hs index cf81150..2981a4a 100644 --- a/Basement/String/Encoding/UTF32.hs +++ b/Basement/String/Encoding/UTF32.hs @@ -21,6 +21,7 @@ import Basement.Numerical.Additive import Basement.UArray import Basement.UArray.Mutable (MUArray) import Basement.MutableBuilder +import Basement.HeadHackageUtils import Basement.String.Encoding.Encoding @@ -43,7 +44,7 @@ next getter off = Right (char, off + Offset 1) where !(W32# hh) = getter off char :: Char - char = C# (chr# (word2Int# hh)) + char = C# (chr# (word2Int# (word32ToWordCompat# hh))) write :: (PrimMonad st, Monad st) => Char @@ -52,4 +53,4 @@ write c = builderAppend w32 where !(C# ch) = c w32 :: Word32 - w32 = W32# (int2Word# (ord# ch)) + w32 = W32# (wordToWord32Compat# (int2Word# (ord# ch))) diff --git a/Basement/Types/Char7.hs b/Basement/Types/Char7.hs index 756f255..3cdf130 100644 --- a/Basement/Types/Char7.hs +++ b/Basement/Types/Char7.hs @@ -37,6 +37,7 @@ import Data.Bits import Data.Maybe import Basement.Compat.Base import Basement.Compat.Primitive (bool#) +import Basement.HeadHackageUtils -- | ASCII value between 0x0 and 0x7f newtype Char7 = Char7 { toByte :: Word8 } @@ -44,14 +45,14 @@ newtype Char7 = Char7 { toByte :: Word8 } -- | Convert a 'Char7' to a unicode code point 'Char' toChar :: Char7 -> Char -toChar !(Char7 (W8# w)) = C# (chr# (word2Int# w)) +toChar !(Char7 (W8# w)) = C# (chr# (word2Int# (word8ToWordCompat# w))) -- | Try to convert a 'Char' to a 'Char7' --- +-- -- If the code point is non ascii, then Nothing is returned. fromChar :: Char -> Maybe Char7 fromChar !(C# c#) - | bool# (ltChar# c# (chr# 0x80#)) = Just $ Char7 $ W8# (int2Word# (ord# c#)) + | bool# (ltChar# c# (chr# 0x80#)) = Just $ Char7 $ W8# (wordToWord8Compat# (int2Word# (ord# c#))) | otherwise = Nothing -- | Try to convert 'Word8' to a 'Char7' @@ -64,11 +65,11 @@ fromByte !w -- | Convert a 'Char' to a 'Char7' ignoring all higher bits fromCharMask :: Char -> Char7 -fromCharMask !(C# c#) = Char7 $ W8# (and# (int2Word# (ord# c#)) 0x7f##) +fromCharMask !(C# c#) = Char7 $ W8# (wordToWord8Compat# (and# (int2Word# (ord# c#)) 0x7f##)) -- | Convert a 'Byte' to a 'Char7' ignoring the higher bit fromByteMask :: Word8 -> Char7 -fromByteMask !(W8# w#) = Char7 $ W8# (and# w# 0x7f##) +fromByteMask !(W8# w#) = Char7 $ W8# (wordToWord8Compat# (and# (word8ToWordCompat# w#) 0x7f##)) c7_LF :: Char7 c7_LF = Char7 0xa diff --git a/Basement/UArray.hs b/Basement/UArray.hs index 5d78a4e..63a320d 100644 --- a/Basement/UArray.hs +++ b/Basement/UArray.hs @@ -138,6 +138,7 @@ import qualified Basement.Base16 as Base16 import qualified Basement.Alg.Mutable as Alg import qualified Basement.Alg.Class as Alg import qualified Basement.Alg.PrimArray as Alg +import Basement.HeadHackageUtils -- | Return the element at a specific index from an array. -- @@ -847,9 +848,9 @@ toHexadecimal ba | sIdx == endOfs = pure () | otherwise = do let !(W8# !w) = getAt sIdx - !(# wHi, wLo #) = Base16.unsafeConvertByte w - unsafeWrite ma dIdx (W8# wHi) - unsafeWrite ma (dIdx+1) (W8# wLo) + !(# wHi, wLo #) = Base16.unsafeConvertByte (word8ToWordCompat# w) + unsafeWrite ma dIdx (W8# (wordToWord8Compat# wHi)) + unsafeWrite ma (dIdx+1) (W8# (wordToWord8Compat# wLo)) loop (dIdx + 2) (sIdx+1) toBase64Internal :: PrimType ty => Addr# -> UArray ty -> Bool -> UArray Word8 @@ -913,10 +914,10 @@ outputLengthBase64 padding (CountOf inputLenInt) = outputLength convert3 :: Addr# -> Word8 -> Word8 -> Word8 -> (Word8, Word8, Word8, Word8) convert3 table (W8# a) (W8# b) (W8# c) = - let !w = narrow8Word# (uncheckedShiftRL# a 2#) - !x = or# (and# (uncheckedShiftL# a 4#) 0x30##) (uncheckedShiftRL# b 4#) - !y = or# (and# (uncheckedShiftL# b 2#) 0x3c##) (uncheckedShiftRL# c 6#) - !z = and# c 0x3f## + let !w = narrow8Word# (uncheckedShiftRL# (word8ToWordCompat# a) 2#) + !x = or# (and# (uncheckedShiftL# (word8ToWordCompat# a) 4#) 0x30##) (uncheckedShiftRL# (word8ToWordCompat# b) 4#) + !y = or# (and# (uncheckedShiftL# (word8ToWordCompat# b) 2#) 0x3c##) (uncheckedShiftRL# (word8ToWordCompat# c) 6#) + !z = and# (word8ToWordCompat# c) 0x3f## in (idx w, idx x, idx y, idx z) where idx :: Word# -> Word8 diff --git a/Basement/UArray/Base.hs b/Basement/UArray/Base.hs index ecd2375..3f7a919 100644 --- a/Basement/UArray/Base.hs +++ b/Basement/UArray/Base.hs @@ -54,7 +54,7 @@ module Basement.UArray.Base , pureST ) where -import GHC.Prim +import GHC.Exts import GHC.Types import GHC.Ptr import GHC.ST diff --git a/Basement/UArray/Mutable.hs b/Basement/UArray/Mutable.hs index 86f1147..c9a2075 100644 --- a/Basement/UArray/Mutable.hs +++ b/Basement/UArray/Mutable.hs @@ -40,7 +40,7 @@ module Basement.UArray.Mutable , withMutablePtrHint ) where -import GHC.Prim +import GHC.Exts import GHC.Types import GHC.Ptr import Basement.Compat.Base diff --git a/Basement/UTF8/Helper.hs b/Basement/UTF8/Helper.hs index 0290272..80b25c2 100644 --- a/Basement/UTF8/Helper.hs +++ b/Basement/UTF8/Helper.hs @@ -18,6 +18,7 @@ module Basement.UTF8.Helper import Basement.Compat.Base import Basement.Compat.Primitive +import Basement.HeadHackageUtils import Basement.Types.OffsetSize import Basement.UTF8.Types import GHC.Prim @@ -57,25 +58,25 @@ toChar# w = C# (chr# (word2Int# w)) {-# INLINE toChar# #-} toChar1 :: StepASCII -> Char -toChar1 (StepASCII (W8# w)) = toChar# w +toChar1 (StepASCII (W8# w)) = toChar# (word8ToWordCompat# w) toChar2 :: StepASCII -> Word8 -> Char toChar2 (StepASCII (W8# w1)) (W8# w2) = - toChar# (or# (uncheckedShiftL# (maskHeader2# w1) 6#) (maskContinuation# w2)) + toChar# (or# (uncheckedShiftL# (maskHeader2# (word8ToWordCompat# w1)) 6#) (maskContinuation# (word8ToWordCompat# w2))) toChar3 :: StepASCII -> Word8 -> Word8 -> Char toChar3 (StepASCII (W8# w1)) (W8# w2) (W8# w3) = - toChar# (or3# (uncheckedShiftL# (maskHeader3# w1) 12#) - (uncheckedShiftL# (maskContinuation# w2) 6#) - (maskContinuation# w3) + toChar# (or3# (uncheckedShiftL# (maskHeader3# (word8ToWordCompat# w1)) 12#) + (uncheckedShiftL# (maskContinuation# (word8ToWordCompat# w2)) 6#) + (maskContinuation# (word8ToWordCompat# w3)) ) toChar4 :: StepASCII -> Word8 -> Word8 -> Word8 -> Char toChar4 (StepASCII (W8# w1)) (W8# w2) (W8# w3) (W8# w4) = - toChar# (or4# (uncheckedShiftL# (maskHeader4# w1) 18#) - (uncheckedShiftL# (maskContinuation# w2) 12#) - (uncheckedShiftL# (maskContinuation# w3) 6#) - (maskContinuation# w4) + toChar# (or4# (uncheckedShiftL# (maskHeader4# (word8ToWordCompat# w1)) 18#) + (uncheckedShiftL# (maskContinuation# (word8ToWordCompat# w2)) 12#) + (uncheckedShiftL# (maskContinuation# (word8ToWordCompat# w3)) 6#) + (maskContinuation# (word8ToWordCompat# w4)) ) -- | Different way to encode a Character in UTF8 represented as an ADT @@ -98,25 +99,25 @@ asUTF8Char !(C# c) where !x = int2Word# (ord# c) - encode1 = UTF8_1 (W8# x) + encode1 = UTF8_1 (W8# (wordToWord8Compat# x)) encode2 = - let !x1 = W8# (or# (uncheckedShiftRL# x 6#) 0xc0##) + let !x1 = W8# (wordToWord8Compat# (or# (uncheckedShiftRL# x 6#) 0xc0##)) !x2 = toContinuation x in UTF8_2 x1 x2 encode3 = - let !x1 = W8# (or# (uncheckedShiftRL# x 12#) 0xe0##) + let !x1 = W8# (wordToWord8Compat# (or# (uncheckedShiftRL# x 12#) 0xe0##)) !x2 = toContinuation (uncheckedShiftRL# x 6#) !x3 = toContinuation x in UTF8_3 x1 x2 x3 encode4 = - let !x1 = W8# (or# (uncheckedShiftRL# x 18#) 0xf0##) + let !x1 = W8# (wordToWord8Compat# (or# (uncheckedShiftRL# x 18#) 0xf0##)) !x2 = toContinuation (uncheckedShiftRL# x 12#) !x3 = toContinuation (uncheckedShiftRL# x 6#) !x4 = toContinuation x in UTF8_4 x1 x2 x3 x4 toContinuation :: Word# -> Word8 - toContinuation w = W8# (or# (and# w 0x3f##) 0x80##) + toContinuation w = W8# (wordToWord8Compat# (or# (and# w 0x3f##) 0x80##)) {-# INLINE toContinuation #-} -- given the encoding of UTF8 Char, get the number of bytes of this sequence @@ -149,7 +150,7 @@ charToBytes c -- | Encode a Char into a CharUTF8 encodeCharUTF8 :: Char -> CharUTF8 encodeCharUTF8 !(C# c) - | bool# (ltWord# x 0x80## ) = CharUTF8 (W32# x) + | bool# (ltWord# x 0x80## ) = CharUTF8 (W32# (wordToWord32Compat# x)) | bool# (ltWord# x 0x800## ) = CharUTF8 encode2 | bool# (ltWord# x 0x10000##) = CharUTF8 encode3 | otherwise = CharUTF8 encode4 @@ -166,22 +167,22 @@ encodeCharUTF8 !(C# c) set3 = 0x008080e0## -- 10xxxxxx * 2 1110xxxx set4 = 0x808080f0## -- 10xxxxxx * 3 11111xxx - encode2 = W32# (and# mask2 (or3# set2 + encode2 = W32# (wordToWord32Compat# (and# mask2 (or3# set2 (uncheckedShiftRL# x 6#) -- 5 bits to 1st byte (uncheckedShiftL# x 8# ) -- move lowest bits to the 2nd byte - )) - encode3 = W32# (and# mask3 (or4# set3 + ))) + encode3 = W32# (wordToWord32Compat# (and# mask3 (or4# set3 (uncheckedShiftRL# x 12#) -- 4 bits to 1st byte (and# 0x3f00## (uncheckedShiftL# x 2#)) -- 6 bits to the 2nd byte (uncheckedShiftL# x 16# ) -- move lowest bits to the 3rd byte - )) - encode4 = W32# (and# mask4 (or4# set4 + ))) + encode4 = W32# (wordToWord32Compat# (and# mask4 (or4# set4 (uncheckedShiftRL# x 18#) -- 3 bits to 1st byte (or# (and# 0x3f00## (uncheckedShiftRL# x 4#)) -- 6 bits to the 2nd byte (and# 0x3f0000## (uncheckedShiftL# x 10#)) -- 6 bits to the 3nd byte ) (uncheckedShiftL# x 24# ) -- move lowest bits to the 4rd byte - )) + ))) -- | decode a CharUTF8 into a Char -- @@ -189,25 +190,25 @@ encodeCharUTF8 !(C# c) -- of the Char invariants decodeCharUTF8 :: CharUTF8 -> Char decodeCharUTF8 c@(CharUTF8 !(W32# w)) - | isCharUTF8Case1 c = toChar# w + | isCharUTF8Case1 c = toChar# (word32ToWordCompat# w) | isCharUTF8Case2 c = encode2 | isCharUTF8Case3 c = encode3 | otherwise = encode4 where encode2 = - toChar# (or# (uncheckedShiftL# (maskHeader2# w) 6#) - (maskContinuation# (uncheckedShiftRL# w 8#)) + toChar# (or# (uncheckedShiftL# (maskHeader2# (word32ToWordCompat# w)) 6#) + (maskContinuation# (uncheckedShiftRL# (word32ToWordCompat# w) 8#)) ) encode3 = - toChar# (or3# (uncheckedShiftL# (maskHeader3# w) 12#) - (uncheckedShiftRL# (and# 0x3f00## w) 8#) - (maskContinuation# (uncheckedShiftRL# w 16#)) + toChar# (or3# (uncheckedShiftL# (maskHeader3# (word32ToWordCompat# w)) 12#) + (uncheckedShiftRL# (and# 0x3f00## (word32ToWordCompat# w)) 8#) + (maskContinuation# (uncheckedShiftRL# (word32ToWordCompat# w) 16#)) ) encode4 = - toChar# (or4# (uncheckedShiftL# (maskHeader4# w) 18#) - (uncheckedShiftRL# (and# 0x3f00## w) 10#) - (uncheckedShiftL# (and# 0x3f0000## w) 4#) - (maskContinuation# (uncheckedShiftRL# w 24#)) + toChar# (or4# (uncheckedShiftL# (maskHeader4# (word32ToWordCompat# w)) 18#) + (uncheckedShiftRL# (and# 0x3f00## (word32ToWordCompat# w)) 10#) + (uncheckedShiftL# (and# 0x3f0000## (word32ToWordCompat# w)) 4#) + (maskContinuation# (uncheckedShiftRL# (word32ToWordCompat# w) 24#)) ) -- clearing mask, removing all UTF8 metadata and keeping only signal (content) @@ -216,17 +217,17 @@ decodeCharUTF8 c@(CharUTF8 !(W32# w)) --maskContent4 = 0x3f3f3f07## -- 3 continuations, 3 bits header isCharUTF8Case1 :: CharUTF8 -> Bool -isCharUTF8Case1 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# w 0x80##) 0##) +isCharUTF8Case1 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# (word32ToWordCompat# w) 0x80##) 0##) {-# INLINE isCharUTF8Case1 #-} isCharUTF8Case2 :: CharUTF8 -> Bool -isCharUTF8Case2 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# w 0x20##) 0##) +isCharUTF8Case2 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# (word32ToWordCompat# w) 0x20##) 0##) {-# INLINE isCharUTF8Case2 #-} isCharUTF8Case3 :: CharUTF8 -> Bool -isCharUTF8Case3 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# w 0x10##) 0##) +isCharUTF8Case3 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# (word32ToWordCompat# w) 0x10##) 0##) {-# INLINE isCharUTF8Case3 #-} isCharUTF8Case4 :: CharUTF8 -> Bool -isCharUTF8Case4 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# w 0x08##) 0##) +isCharUTF8Case4 (CharUTF8 !(W32# w)) = bool# (eqWord# (and# (word32ToWordCompat# w) 0x08##) 0##) {-# INLINE isCharUTF8Case4 #-} diff --git a/Basement/UTF8/Table.hs b/Basement/UTF8/Table.hs index 6d59102..ffeef82 100644 --- a/Basement/UTF8/Table.hs +++ b/Basement/UTF8/Table.hs @@ -21,23 +21,24 @@ import GHC.Types import GHC.Word import Basement.Compat.Base import Basement.Compat.Primitive +import Basement.HeadHackageUtils import Basement.UTF8.Types (StepASCII(..)) -- | Check if the byte is a continuation byte isContinuation :: Word8 -> Bool -isContinuation (W8# w) = isContinuation# w +isContinuation (W8# w) = isContinuation# (word8ToWordCompat# w) {-# INLINE isContinuation #-} isContinuation2 :: Word8 -> Word8 -> Bool isContinuation2 (W8# w1) (W8# w2) = - bool# (mask w1 `andI#` mask w2) + bool# (mask (word8ToWordCompat# w1) `andI#` mask (word8ToWordCompat# w2)) where mask v = (and# 0xC0## v) `eqWord#` 0x80## {-# INLINE isContinuation2 #-} isContinuation3 :: Word8 -> Word8 -> Word8 -> Bool isContinuation3 (W8# w1) (W8# w2) (W8# w3) = - bool# (mask w1) && bool# (mask w2) && bool# (mask w3) + bool# (mask (word8ToWordCompat# w1)) && bool# (mask (word8ToWordCompat# w2)) && bool# (mask (word8ToWordCompat# w3)) where mask v = (and# 0xC0## v) `eqWord#` 0x80## {-# INLINE isContinuation3 #-} @@ -54,17 +55,17 @@ data NbBytesCont_ = NbBytesCont0_ | NbBytesCont1_ | NbBytesCont2_ | NbBytesCont3 -- | Get the number of following bytes given the first byte of a UTF8 sequence. getNbBytes :: StepASCII -> Int -getNbBytes (StepASCII (W8# w)) = I# (getNbBytes# w) +getNbBytes (StepASCII (W8# w)) = I# (getNbBytes# (word8ToWordCompat# w)) {-# INLINE getNbBytes #-} -- | Check if the byte is a continuation byte isContinuation# :: Word# -> Bool -isContinuation# w = W# (indexWord8OffAddr# (unTable contTable) (word2Int# w)) == W# 0## +isContinuation# w = W# (word8ToWordCompat# (indexWord8OffAddr# (unTable contTable) (word2Int# w))) == W# 0## {-# INLINE isContinuation# #-} -- | Get the number of following bytes given the first byte of a UTF8 sequence. getNbBytes# :: Word# -> Int# -getNbBytes# w = word2Int# (indexWord8OffAddr# (unTable headTable) (word2Int# w)) +getNbBytes# w = word2Int# (word8ToWordCompat# (indexWord8OffAddr# (unTable headTable) (word2Int# w))) {-# INLINE getNbBytes# #-} data Table = Table { unTable :: !Addr# } diff --git a/basement.cabal b/basement.cabal index 89b2794..05b8d9b 100644 --- a/basement.cabal +++ b/basement.cabal @@ -136,6 +136,8 @@ library Basement.Terminal.Size + Basement.HeadHackageUtils + -- support and dependencies if impl(ghc < 8.0) buildable: False