Commit 82d356de authored by Bodigrim's avatar Bodigrim Committed by Xia Li-yao
Browse files

Replace foreign calls with primops in Data.Text.Array

parent 321c10a4
......@@ -16,14 +16,8 @@
#include "text_cbits.h"
void _hs_text_memcpy(void *dest, size_t doff, const void *src, size_t soff,
size_t n)
{
memcpy(dest + (doff<<1), src + (soff<<1), n<<1);
}
int _hs_text_memcmp(const void *a, size_t aoff, const void *b, size_t boff,
size_t n)
size_t n)
{
return memcmp(a + (aoff<<1), b + (boff<<1), n<<1);
}
......@@ -196,7 +190,7 @@ _hs_text_decode_utf8_int(uint16_t *const dest, size_t *destoff,
_mm_storeu_si128((__m128i *)d, eight_utf16_chars);
d += 8;
}
#else
#else
while (s < srcend - 4) {
codepoint = *((uint32_t *) s);
if ((codepoint & 0x80808080) != 0)
......
......@@ -42,16 +42,13 @@ module Data.Text.Array
#if defined(ASSERTS)
import Control.Exception (assert)
import GHC.Base (sizeofByteArray#, getSizeofMutableByteArray#)
import GHC.Stack (HasCallStack)
#endif
import Control.Monad.ST.Unsafe (unsafeIOToST)
import Data.Bits ((.&.), xor, shiftL, shiftR)
#if !MIN_VERSION_base(4,11,0)
import Data.Text.Internal.Unsafe (inlinePerformIO)
import Foreign.C.Types (CInt(CInt), CSize(CSize))
import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
indexWord16Array#, newByteArray#,
unsafeFreezeByteArray#, writeWord16Array#)
#endif
import GHC.Exts hiding (toList)
import GHC.ST (ST(..), runST)
import GHC.Word (Word16(..))
import Prelude hiding (length, read)
......@@ -172,18 +169,17 @@ copyM :: MArray s -- ^ Destination
-> Int -- ^ Source offset
-> Int -- ^ Count
-> ST s ()
copyM dest didx src sidx count
| count <= 0 = return ()
copyM dst@(MArray dst#) dstOff@(I# dstOff#) src@(MArray src#) srcOff@(I# srcOff#) count@(I# count#)
| I# count# <= 0 = return ()
| otherwise = do
#if defined(ASSERTS)
srcLen <- getSizeofMArray src
destLen <- getSizeofMArray dest
assert (sidx + count <= srcLen `quot` 2) .
assert (didx + count <= destLen `quot` 2) .
srcLen <- getSizeofMArray src
dstLen <- getSizeofMArray dst
assert (srcOff + count <= srcLen `quot` 2) .
assert (dstOff + count <= dstLen `quot` 2) .
#endif
unsafeIOToST $ memcpyM (maBA dest) (intToCSize didx)
(maBA src) (intToCSize sidx)
(intToCSize count)
ST $ \s1# -> case copyMutableByteArray# src# (2# *# srcOff#) dst# (2# *# dstOff#) (2# *# count#) s1# of
s2# -> (# s2#, () #)
{-# INLINE copyM #-}
-- | Copy some elements of an immutable array.
......@@ -194,12 +190,11 @@ copyI :: MArray s -- ^ Destination
-> Int -- ^ First offset in destination /not/ to
-- copy (i.e. /not/ length)
-> ST s ()
copyI dest i0 src j0 top
| i0 >= top = return ()
| otherwise = unsafeIOToST $
memcpyI (maBA dest) (intToCSize i0)
(aBA src) (intToCSize j0)
(intToCSize (top-i0))
copyI (MArray dst#) dstOff@(I# dstOff#) (Array src#) (I# srcOff#) top@(I# top#)
| dstOff >= top = return ()
| otherwise = ST $ \s1# ->
case copyByteArray# src# (2# *# srcOff#) dst# (2# *# dstOff#) (2# *# (top# -# dstOff#)) s1# of
s2# -> (# s2#, () #)
{-# INLINE copyI #-}
-- | Compare portions of two arrays for equality. No bounds checking
......@@ -210,21 +205,14 @@ equal :: Array -- ^ First
-> Int -- ^ Offset into second
-> Int -- ^ Count
-> Bool
equal arrA offA arrB offB count = inlinePerformIO $ do
i <- memcmp (aBA arrA) (intToCSize offA)
(aBA arrB) (intToCSize offB) (intToCSize count)
return $! i == 0
{-# INLINE equal #-}
intToCSize :: Int -> CSize
intToCSize = fromIntegral
foreign import ccall unsafe "_hs_text_memcpy" memcpyI
:: MutableByteArray# s -> CSize -> ByteArray# -> CSize -> CSize -> IO ()
equal (Array src1#) (I# off1#) (Array src2#) (I# off2#) (I# count#) = i == 0
where
#if MIN_VERSION_base(4,11,0)
i = I# (compareByteArrays# src1# (2# *# off1#) src2# (2# *# off2#) (2# *# count#))
#else
i = inlinePerformIO (memcmp src1# off1# src2# off2# count#)
foreign import ccall unsafe "_hs_text_memcmp" memcmp
:: ByteArray# -> CSize -> ByteArray# -> CSize -> CSize -> IO CInt
foreign import ccall unsafe "_hs_text_memcpy" memcpyM
:: MutableByteArray# s -> CSize -> MutableByteArray# s -> CSize -> CSize
-> IO ()
:: ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> IO Int
#endif
{-# INLINE equal #-}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment