55{-# LANGUAGE NamedFieldPuns #-}
66{-# LANGUAGE ScopedTypeVariables #-}
77{-# LANGUAGE TypeApplications #-}
8+ {-# LANGUAGE UnboxedTuples #-}
89
910module Json.Smile
1011 ( -- * Encode JSON Document
@@ -35,6 +36,8 @@ import Data.Primitive (readByteArray,copyMutableByteArray)
3536import Data.Text.Short (ShortText )
3637import Data.Word (Word8 ,Word32 ,Word64 )
3738import Data.Word.Zigzag (toZigzag32 ,toZigzag64 )
39+ import GHC.Exts (RealWorld ,Word #,State #)
40+ import GHC.IO (IO (IO ))
3841import GHC.Word (Word (.. ))
3942import Json (Value (.. ), Member (.. ))
4043import Numeric.Natural (Natural )
@@ -49,7 +52,8 @@ import qualified Data.ByteString.Short as SBS
4952import qualified Data.Number.Scientific as Sci
5053import qualified Data.Text.Short as TS
5154import qualified GHC.Exts as Exts
52- import qualified GHC.Integer.GMP.Internals as GMP
55+ import qualified GHC.Num.BigNat as BN
56+ import qualified GHC.Num.Integer as Integer
5357import qualified Prelude
5458
5559-- | Encode a Json 'Value' to the Smile binary format.
@@ -118,11 +122,11 @@ integerToBase256ByteArray :: Integer -> ByteArray
118122integerToBase256ByteArray c = if c == 0
119123 then byteArrayFromListN 1 [0 :: Word8 ]
120124 else case c of
121- GMP. Jp # bn -> unsafeDupablePerformIO $ do
122- let nDigits256 = fromIntegral @ Word @ Int (W # (GMP. sizeInBaseBigNat bn 256 # ))
125+ Integer. IP bn -> unsafeDupablePerformIO $ do
126+ let nDigits256 = fromIntegral @ Word @ Int (W # (BN. bigNatSizeInBase # 256 ## bn ))
123127 mut <- newByteArray nDigits256
124128 let ! (MutableByteArray mut# ) = mut
125- ! _ <- GMP. exportBigNatToMutableByteArray bn mut# 0 ## 1 #
129+ ! _ <- liftWordIO ( BN. bigNatToMutableByteArray # bn mut# 0 ## 1 # )
126130 -- This is safe because Jp cannot have zero inside it.
127131 w0 :: Word8 <- readByteArray mut 0
128132 if testBit w0 7
@@ -134,17 +138,24 @@ integerToBase256ByteArray c = if c == 0
134138 copyMutableByteArray dst 1 mut 0 nDigits256
135139 unsafeFreezeByteArray dst
136140 else unsafeFreezeByteArray mut
137- GMP. Jn # bn -> twosComplementBigNat bn
138- GMP. S # i -> case i Exts. ># 0 # of
141+ Integer. IN bn -> twosComplementBigNat bn
142+ Integer. IS i -> case i Exts. ># 0 # of
139143 1 # -> encodePosWordBase256 (W # (Exts. int2Word# i))
140144 _ -> encodeNegWordBase256 (W # (Exts. int2Word# i))
141145
142- twosComplementBigNat :: GMP. BigNat -> ByteArray
146+ liftWordIO :: (State # RealWorld -> (# State # RealWorld , Word # # )) -> IO Word
147+ {-# inline liftWordIO #-}
148+ liftWordIO f = IO
149+ (\ s -> case f s of
150+ (# s', w # ) -> (# s', W # w # )
151+ )
152+
153+ twosComplementBigNat :: BN. BigNat# -> ByteArray
143154twosComplementBigNat bn = unsafeDupablePerformIO $ do
144- let nDigits256 = fromIntegral @ Word @ Int (W # (GMP. sizeInBaseBigNat bn 256 # ))
155+ let nDigits256 = fromIntegral @ Word @ Int (W # (BN. bigNatSizeInBase # 256 ## bn ))
145156 mut <- newByteArray nDigits256
146157 let ! (MutableByteArray mut# ) = mut
147- ! _ <- GMP. exportBigNatToMutableByteArray bn mut# 0 ## 1 #
158+ ! _ <- liftWordIO ( BN. bigNatToMutableByteArray # bn mut# 0 ## 1 # )
148159 -- First, complement
149160 let goComplement ! ix = if ix >= 0
150161 then do
0 commit comments