Skip to content

Commit f5a641a

Browse files
committed
Replace integer-gmp with ghc-bignum
1 parent c4ecc71 commit f5a641a

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

json-syntax.cabal

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ library
3333
build-depends:
3434
, array-builder >=0.1 && <0.2
3535
, array-chunks >=0.1.3 && <0.2
36-
, base >=4.12 && <5
36+
, base >=4.15 && <5
3737
, bytebuild >=0.3.10 && <0.4
3838
, byteslice >=0.2.9 && <0.3
3939
, bytesmith >=0.3.8 && <0.4
4040
, bytestring >=0.10.8 && <0.12
41-
, integer-gmp >=1.0 && <1.2
4241
, natural-arithmetic >=0.1.2 && <0.2
4342
, contiguous >=0.6 && <0.7
44-
, primitive >=0.7 && <0.8
43+
, primitive >=0.7 && <0.10
4544
, run-st >=0.1.1 && <0.2
4645
, scientific-notation >=0.1.5 && <0.2
4746
, text-short >=0.1.3 && <0.2

src/Json/Smile.hs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE NamedFieldPuns #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
77
{-# LANGUAGE TypeApplications #-}
8+
{-# LANGUAGE UnboxedTuples #-}
89

910
module Json.Smile
1011
( -- * Encode JSON Document
@@ -35,6 +36,8 @@ import Data.Primitive (readByteArray,copyMutableByteArray)
3536
import Data.Text.Short (ShortText)
3637
import Data.Word (Word8,Word32,Word64)
3738
import Data.Word.Zigzag (toZigzag32,toZigzag64)
39+
import GHC.Exts (RealWorld,Word#,State#)
40+
import GHC.IO (IO(IO))
3841
import GHC.Word (Word(..))
3942
import Json (Value(..), Member(..))
4043
import Numeric.Natural (Natural)
@@ -49,7 +52,8 @@ import qualified Data.ByteString.Short as SBS
4952
import qualified Data.Number.Scientific as Sci
5053
import qualified Data.Text.Short as TS
5154
import 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
5357
import qualified Prelude
5458

5559
-- | Encode a Json 'Value' to the Smile binary format.
@@ -118,11 +122,11 @@ integerToBase256ByteArray :: Integer -> ByteArray
118122
integerToBase256ByteArray 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
143154
twosComplementBigNat 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

Comments
 (0)