Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building against integer-simple #147

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ flags:
default: false
manual: true

integer-simple:
description: >-
Use the [simple integer library](http://hackage.haskell.org/package/integer-simple)
instead of [integer-gmp](http://hackage.haskell.org/package/integer-gmp)
default: False
manual: False

ghc-options: -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns -fwarn-incomplete-record-updates -O2

dependencies:
Expand Down Expand Up @@ -54,7 +61,6 @@ dependencies:
- hashable >=1.2.3.1
- hspec >=2.1.2
- hspec-smallcheck >=0.3.0
- integer-gmp >=0.5.1.0
- lifted-base >=0.2.3.3
- monad-control >=0.3.3.0
- mono-traversable >=0.7.0
Expand All @@ -78,6 +84,13 @@ dependencies:
- async >=2.0.2
- contravariant >=1.3
- bifunctors >=4.0
when:
- condition: flag(integer-simple)
then:
dependencies: integer-simple >= 0.1.1.1
else:
dependencies: integer-gmp >= 0.5.1.0
cpp-options: -DINTEGER_GMP
library:
source-dirs: src
other-modules:
Expand Down
56 changes: 55 additions & 1 deletion src/Data/Store/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ import Foreign.C.Types ()
import Foreign.Ptr (plusPtr, minusPtr)
import Foreign.Storable (Storable, sizeOf)
import GHC.Generics (Generic)
#ifdef INTEGER_GMP
import qualified GHC.Integer.GMP.Internals as I
import GHC.Types (Int (I#))
#else
import GHC.Types (Word (W#))
import qualified GHC.Integer.Simple.Internals as I
#endif
import GHC.Real (Ratio(..))
import GHC.TypeLits
import GHC.Types (Int (I#))
import Instances.TH.Lift ()
import Language.Haskell.TH
import Language.Haskell.TH.Instances ()
Expand All @@ -132,9 +137,11 @@ import Prelude
import TH.Derive

-- Conditional import to avoid warning
#ifdef INTEGER_GMP
#if MIN_VERSION_integer_gmp(1,0,0)
import GHC.Prim (sizeofByteArray#)
#endif
#endif

------------------------------------------------------------------------
-- Utilities for defining list-like 'Store' instances in terms of 'IsSequence'
Expand Down Expand Up @@ -573,6 +580,7 @@ peekArray = do
{-# INLINE peekArray #-}

instance Store Integer where
#ifdef INTEGER_GMP
#if MIN_VERSION_integer_gmp(1,0,0)
size = VarSize $ \ x ->
sizeOf (undefined :: Word8) + case x of
Expand Down Expand Up @@ -640,6 +648,52 @@ instance Store Integer where
when (r /= 0) (peekException "Buffer size stored for encoded Integer not divisible by Word size (to get limb count).")
return (I.J# sz arr)
#endif
#else
-- May as well put in the extra effort to use the same encoding as
-- used for the newer integer-gmp.
size = VarSize $ \ x ->
sizeOf (undefined :: Word8) + case x of
I.Positive ds -> (1 + fromIntegral (numDigits ds)) * sizeOf (undefined :: Word)
I.Negative ds -> (1 + fromIntegral (numDigits ds)) * sizeOf (undefined :: Word)
I.Naught -> 0
where
poke x = case x of
I.Naught -> poke (0 :: Word8)
I.Positive ds -> do
poke (1 :: Word8)
poke (numDigits ds)
pokeDigits ds
I.Negative ds -> do
poke (2 :: Word8)
poke (numDigits ds)
pokeDigits ds
where
pokeDigits I.None = pure ()
pokeDigits (I.Some d ds) = poke (W# d) *> pokeDigits ds
peek = do
tag <- peek :: Peek Word8
case tag of
0 -> pure I.Naught
1 -> do
len <- peek :: Peek Word
I.Positive <$> peekDigits len
2 -> do
len <- peek :: Peek Word
I.Negative <$> peekDigits len
_ -> peekException "Invalid Integer tag"
where
peekDigits i
| i <= 0 = pure I.None
| otherwise = do
W# d <- peek
ds <- peekDigits (i - 1)
pure $! I.Some d ds

numDigits :: I.Digits -> Word
numDigits = go 0
where go !acc I.None = acc
go !acc (I.Some _ ds) = go (acc + 1) ds
#endif

-- instance Store GHC.Fingerprint.Types.Fingerprint where

Expand Down
8 changes: 6 additions & 2 deletions test/Data/StoreSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import Data.IntSet (IntSet)
import qualified Data.List.NonEmpty as NE
import Data.Map (Map)
import Data.Monoid
#if !MIN_VERSION_primitive(0,7,0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated change to fix the build against newer versions of primitive. It is still not sufficient to get the test suite working on recent GHCs and recent dependencies (I tried 8.6.5) so to test the instance, I’ve simply uncommented everything but Integer in the “Store on all monomorphic instances” tests.

import Data.Primitive.Types (Addr)
#endif
import Data.Proxy (Proxy(..))
import Data.Sequence (Seq)
import Data.Sequences (fromList)
Expand Down Expand Up @@ -285,8 +287,10 @@ spec = do
describe "Store on all monomorphic instances"
$(do insts <- getAllInstanceTypes1 ''Store
omitTys0 <- sequence
[ [t| Addr |]
, [t| CUIntPtr |]
#if !MIN_VERSION_primitive(0,7,0)
[t| Addr |] :
#endif
[ [t| CUIntPtr |]
, [t| CIntPtr |]
, [t| IntPtr |]
, [t| WordPtr |]
Expand Down