2020-03-07 01:30:27 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
{-|
|
|
|
|
Atom implementation with fast conversions between bytestrings
|
|
|
|
and atoms.
|
|
|
|
|
|
|
|
TODO Support Big Endian.
|
|
|
|
-}
|
|
|
|
|
2020-03-12 05:16:40 +03:00
|
|
|
module Urbit.Atom.Fast where
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2020-03-07 01:09:45 +03:00
|
|
|
import Control.Monad.Primitive (primitive_)
|
2020-01-23 12:22:30 +03:00
|
|
|
import Data.Bits (shiftL, shiftR, (.&.), (.|.))
|
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Data.Vector.Primitive (Vector(..))
|
|
|
|
import Data.Word (Word8)
|
|
|
|
import GHC.Exts (Ptr(Ptr), sizeofByteArray#)
|
2020-03-07 01:10:58 +03:00
|
|
|
import GHC.Exts (Int(..))
|
2020-01-23 12:22:30 +03:00
|
|
|
import GHC.Integer.GMP.Internals (BigNat(..), bigNatToWord, sizeofBigNat#)
|
2020-03-12 05:16:40 +03:00
|
|
|
import GHC.Integer.GMP.Internals (isZeroBigNat, indexBigNat#)
|
2020-01-23 12:22:30 +03:00
|
|
|
import GHC.Integer.GMP.Internals (byteArrayToBigNat#, wordToBigNat, zeroBigNat)
|
|
|
|
import GHC.Natural (Natural(..))
|
2020-03-07 01:10:58 +03:00
|
|
|
import GHC.Prim (Int#, clz#, minusWord#, plusWord#)
|
2020-01-23 12:22:30 +03:00
|
|
|
import GHC.Prim (Word#, int2Word#, subIntC#, timesWord#)
|
2020-03-07 01:09:45 +03:00
|
|
|
import GHC.Prim (copyByteArrayToAddr#)
|
2020-01-23 12:22:30 +03:00
|
|
|
import GHC.Word (Word(..))
|
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
|
|
|
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.ByteString.Internal as BS
|
2020-03-07 01:10:58 +03:00
|
|
|
import qualified Data.ByteString.Unsafe as BS
|
2020-01-23 12:22:30 +03:00
|
|
|
import qualified Data.Primitive.ByteArray as Prim
|
|
|
|
import qualified Data.Vector.Primitive as VP
|
|
|
|
import qualified Foreign.ForeignPtr.Unsafe as Ptr
|
|
|
|
|
|
|
|
|
2020-03-07 01:30:27 +03:00
|
|
|
-- Setup BIT and BYT macros. ---------------------------------------------------
|
|
|
|
|
|
|
|
#include <MachDeps.h>
|
|
|
|
|
|
|
|
#if WORD_SIZE_IN_BITS == 64
|
|
|
|
#define BIT 64
|
|
|
|
#define BYT 8
|
|
|
|
#elif WORD_SIZE_IN_BITS == 32
|
|
|
|
#define BIT 32
|
|
|
|
#define BYT 4
|
|
|
|
#else
|
|
|
|
#error WORD_SIZE_IN_BITS must be either 32 or 64
|
|
|
|
#endif
|
|
|
|
|
2020-03-07 05:12:53 +03:00
|
|
|
bit :: Word
|
|
|
|
bit = BIT
|
|
|
|
|
|
|
|
byt :: Word
|
|
|
|
byt = BYT
|
|
|
|
|
2020-03-07 01:30:27 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wordBitWidth# :: Word# -> Word#
|
2020-03-07 01:30:27 +03:00
|
|
|
wordBitWidth# w = minusWord# BIT## (clz# w)
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
wordBitWidth :: Word -> Word
|
|
|
|
wordBitWidth (W# w) = W# (wordBitWidth# w)
|
|
|
|
|
|
|
|
bigNatBitWidth# :: BigNat -> Word#
|
|
|
|
bigNatBitWidth# nat =
|
2020-03-07 01:30:27 +03:00
|
|
|
lswBits `plusWord#` ((int2Word# lastIdx) `timesWord#` BIT##)
|
2020-03-07 01:10:58 +03:00
|
|
|
where
|
|
|
|
(# lastIdx, _ #) = subIntC# (sizeofBigNat# nat) 1#
|
|
|
|
lswBits = wordBitWidth# (indexBigNat# nat lastIdx)
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
atomBitWidth# :: Natural -> Word#
|
|
|
|
atomBitWidth# (NatS# gl) = wordBitWidth# gl
|
|
|
|
atomBitWidth# (NatJ# bn) = bigNatBitWidth# bn
|
|
|
|
|
|
|
|
bitWidth :: Num a => Natural -> a
|
|
|
|
bitWidth a = fromIntegral (W# (atomBitWidth# a))
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE takeBitsWord #-}
|
|
|
|
takeBitsWord :: Int -> Word -> Word
|
|
|
|
takeBitsWord wid wor = wor .&. (shiftL 1 wid - 1)
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-
|
|
|
|
A `Pill` is a bytestring without trailing zeros.
|
|
|
|
-}
|
|
|
|
newtype Pill = Pill { unPill :: ByteString }
|
|
|
|
|
|
|
|
instance Eq Pill where
|
|
|
|
(==) x y = pillBytes x == pillBytes y
|
|
|
|
|
|
|
|
instance Show Pill where
|
|
|
|
show = show . pillBytes
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
strip :: ByteString -> ByteString
|
2020-01-23 12:22:30 +03:00
|
|
|
strip buf = BS.take (len - go 0 (len - 1)) buf
|
2020-03-07 01:10:58 +03:00
|
|
|
where
|
|
|
|
len = BS.length buf
|
|
|
|
go n i | i < 0 = n
|
|
|
|
| 0 == BS.unsafeIndex buf i = go (n + 1) (i - 1)
|
|
|
|
| otherwise = n
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
pillBytes :: Pill -> ByteString
|
|
|
|
pillBytes = strip . unPill
|
|
|
|
|
|
|
|
bytesPill :: ByteString -> Pill
|
|
|
|
bytesPill = Pill . strip
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-
|
|
|
|
Cast a BigNat to a vector without a copy.
|
|
|
|
-}
|
2020-03-07 01:10:58 +03:00
|
|
|
bigNatWords :: BigNat -> Vector Word
|
2020-03-12 05:16:40 +03:00
|
|
|
bigNatWords bn | isZeroBigNat bn = mempty
|
|
|
|
bigNatWords bn@(BN# bArr) =
|
2020-03-07 01:30:27 +03:00
|
|
|
Vector 0 (I# (sizeofByteArray# bArr) `div` BYT) (Prim.ByteArray bArr)
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Cast a vector to a BigNat. This will not copy.
|
|
|
|
|
|
|
|
TODO Don't crash if given a slice.
|
|
|
|
-}
|
2020-03-07 01:10:58 +03:00
|
|
|
wordsBigNat :: Vector Word -> BigNat
|
|
|
|
wordsBigNat v@(Vector off (I# len) (Prim.ByteArray buf)) = case VP.length v of
|
|
|
|
0 -> zeroBigNat
|
|
|
|
1 -> case VP.unsafeIndex v 0 of
|
|
|
|
W# w -> wordToBigNat w
|
|
|
|
n ->
|
|
|
|
if off /= 0 then error "words2Nat: bad-vec" else byteArrayToBigNat# buf len
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
More careful version of `wordsBigNat`, but not yet tested.
|
|
|
|
|
|
|
|
Cast a vector to a BigNat. This will not copy unless input is a slice.
|
|
|
|
|
|
|
|
Note that the length of the vector is in words, and the length passed
|
|
|
|
to `byteArrayToBigNat#` is also in words.
|
|
|
|
-}
|
2020-03-07 01:10:58 +03:00
|
|
|
wordsBigNat' :: Vector Word -> BigNat
|
|
|
|
wordsBigNat' v = case VP.length v of
|
|
|
|
0 -> zeroBigNat
|
|
|
|
1 -> wordToBigNat w where W# w = VP.unsafeIndex v 0
|
|
|
|
n -> if offset v == 0 then extract v else extract (VP.force v)
|
|
|
|
where
|
|
|
|
offset (Vector off _ _) = off
|
2020-01-23 12:22:30 +03:00
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
extract (Vector _ (I# len) (Prim.ByteArray buf)) = byteArrayToBigNat# buf len
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Cast a nat to a vector (no copy)
|
2020-03-07 01:10:58 +03:00
|
|
|
natWords :: Natural -> Vector Word
|
2020-01-23 12:22:30 +03:00
|
|
|
natWords = bigNatWords . natBigNat
|
|
|
|
|
|
|
|
-- | Cast a vector to a nat (no copy)
|
2020-03-07 01:10:58 +03:00
|
|
|
wordsNat :: Vector Word -> Natural
|
2020-01-23 12:22:30 +03:00
|
|
|
wordsNat = bigNatNat . wordsBigNat
|
|
|
|
|
|
|
|
-- | Cast a Nat to a BigNat (no copy).
|
2020-03-07 01:10:58 +03:00
|
|
|
natBigNat :: Natural -> BigNat
|
|
|
|
natBigNat (NatS# w ) = wordToBigNat w
|
2020-01-23 12:22:30 +03:00
|
|
|
natBigNat (NatJ# bn) = bn
|
|
|
|
|
|
|
|
-- | Cast a BigNat to a Nat (no copy).
|
2020-03-07 01:10:58 +03:00
|
|
|
bigNatNat :: BigNat -> Natural
|
|
|
|
bigNatNat bn = case sizeofBigNat# bn of
|
|
|
|
0# -> 0
|
|
|
|
1# -> NatS# (bigNatToWord bn)
|
|
|
|
_ -> NatJ# bn
|
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
wordBytes :: Word -> ByteString
|
|
|
|
wordBytes wor = BS.reverse $ BS.pack $ go 0 []
|
|
|
|
where
|
2020-03-07 01:30:27 +03:00
|
|
|
go i acc | i >= BYT = acc
|
|
|
|
go i acc | otherwise = go (i + 1) (fromIntegral (shiftR wor (i * BYT)) : acc)
|
2020-01-23 12:22:30 +03:00
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
bytesFirstWord :: ByteString -> Word
|
2020-01-23 12:22:30 +03:00
|
|
|
bytesFirstWord buf = go 0 0
|
2020-03-07 01:10:58 +03:00
|
|
|
where
|
2020-03-07 01:30:27 +03:00
|
|
|
top = min BYT (BS.length buf)
|
2020-03-07 01:10:58 +03:00
|
|
|
i idx off = shiftL (fromIntegral $ BS.index buf idx) off
|
|
|
|
go acc idx =
|
2020-03-07 01:30:27 +03:00
|
|
|
if idx >= top then acc else go (acc .|. i idx (BYT * idx)) (idx + 1)
|
2020-03-07 01:10:58 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
pillWords :: Pill -> Vector Word
|
2020-01-23 12:22:30 +03:00
|
|
|
pillWords = bsToWords . pillBytes
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
wordsPill :: Vector Word -> Pill
|
2020-01-23 12:22:30 +03:00
|
|
|
wordsPill = bytesPill . vecBytes . wordsToBytes
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wordsToBytes :: Vector Word -> Vector Word8
|
2020-03-07 01:30:27 +03:00
|
|
|
wordsToBytes (Vector off sz buf) = Vector (off * BYT) (sz * BYT) buf
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
bsToWords :: ByteString -> Vector Word
|
2020-03-07 01:30:27 +03:00
|
|
|
bsToWords bs = VP.generate (1 + BS.length bs `div` BYT)
|
|
|
|
$ \i -> bytesFirstWord (BS.drop (i * BYT) bs)
|
2020-03-07 01:10:58 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
vecBytes :: Vector Word8 -> ByteString
|
2020-03-07 01:09:45 +03:00
|
|
|
vecBytes (Vector off sz buf) = unsafePerformIO $ do
|
|
|
|
fp <- BS.mallocByteString sz
|
|
|
|
let Ptr a = Ptr.unsafeForeignPtrToPtr fp -- Safe b/c returning fp
|
|
|
|
copyByteArrayToAddr a buf 0 sz
|
|
|
|
pure (BS.PS fp off sz)
|
|
|
|
where
|
|
|
|
unI# :: Int -> Int#
|
|
|
|
unI# (I# n#) = n#
|
|
|
|
|
|
|
|
-- Hack to get GHCJS build working, since it has an old version of the
|
|
|
|
-- `primitive` library.
|
|
|
|
copyByteArrayToAddr dst# (Prim.ByteArray src#) soff sz =
|
|
|
|
primitive_ (copyByteArrayToAddr# src# (unI# soff) dst# (unI# sz))
|
2020-01-23 12:22:30 +03:00
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
bytesVec :: ByteString -> Vector Word8
|
2020-01-23 12:22:30 +03:00
|
|
|
bytesVec bs = VP.generate (BS.length bs) (BS.index bs)
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
natPill :: Natural -> Pill
|
2020-01-23 12:22:30 +03:00
|
|
|
natPill = wordsPill . natWords
|
|
|
|
|
2020-03-07 01:10:58 +03:00
|
|
|
pillNat :: Pill -> Natural
|
2020-01-23 12:22:30 +03:00
|
|
|
pillNat = wordsNat . bsToWords . pillBytes
|