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 GHC 9.4 #69

Closed
wants to merge 12 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
ghc: ['8.0', '8.2', '8.4', '8.6', '8.8', '8.10', '9.0', '9.2']
ghc: ['8.0', '8.2', '8.4', '8.6', '8.8', '8.10', '9.0', '9.2', '9.4.1']
steps:
- uses: actions/checkout@v2
- uses: haskell/actions/setup@v1
Expand Down
139 changes: 139 additions & 0 deletions src/Data/WideWord/Compat.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE UnboxedTuples #-}

-- | This module exists to centralize compatibility shims for GHC 9.4.
module Data.WideWord.Compat
( plusWord2#
, timesWord2#
, int2Word#
, minusWord#
, subWordC#
, not#
, isZeroWord#
, or#
, and#
, xor#
, timesWord#
, plusWord#
, word2Int#
, quotRemWord2#
, compatWordLiteral#
, compatIntLiteral#
, compatCaseOnWordLiteral#
, compatCaseOnIntLiteral#
) where

#if MIN_VERSION_base(4,17,0)
import qualified GHC.Base
import GHC.Prim (Word64#, wordToWord64#, word64ToWord#, Int64#)
#else
import qualified GHC.Base
import GHC.Base (Int#(..), Word#, quotRemWord2#, int2Word#, subWordC#, plusWord2#, or#, minusWord#, timesWord2#, word2Int#, xor#, and#, not#, plusWord#, timesWord#)
#endif

#if MIN_VERSION_base(4,17,0)
plusWord2# :: Word64# -> Word64# -> (# Word64#, Word64# #)
plusWord2# a b =
case GHC.Base.plusWord2# (word64ToWord# a) (word64ToWord# b) of
(# a', b' #) ->
(# wordToWord64# a', wordToWord64# b' #)

timesWord2# :: Word64# -> Word64# -> (# Word64#, Word64# #)
timesWord2# a b =
case GHC.Base.timesWord2# (word64ToWord# a) (word64ToWord# b) of
(# a', b' #) ->
(# wordToWord64# a', wordToWord64# b' #)

int2Word# :: Int64# -> Word64#
int2Word# = GHC.Base.int64ToWord64#

minusWord# :: Word64# -> Word64# -> Word64#
minusWord# a b =
wordToWord64# (GHC.Base.minusWord# (word64ToWord# a) (word64ToWord# b))

subWordC# :: Word64# -> Word64# -> (# Word64#, Int64# #)
subWordC# a b =
case GHC.Base.subWordC# (word64ToWord# a) (word64ToWord# b) of
(# a', b' #) ->
(# wordToWord64# a', GHC.Base.intToInt64# b' #)

not# :: Word64# -> Word64#
not# = GHC.Base.not64#

or# :: Word64# -> Word64# -> Word64#
or# = GHC.Base.or64#

xor# :: Word64# -> Word64# -> Word64#
xor# = GHC.Base.xor64#

and# :: Word64# -> Word64# -> Word64#
and# = GHC.Base.and64#

timesWord# :: Word64# -> Word64# -> Word64#
timesWord# = GHC.Base.timesWord64#

plusWord# :: Word64# -> Word64# -> Word64#
plusWord# = GHC.Base.plusWord64#

word2Int# :: Word64# -> Int64#
word2Int# = GHC.Base.word64ToInt64#

quotRemWord2# :: Word64# -> Word64# -> Word64# -> (# Word64#, Word64# #)
quotRemWord2# a b c =
case GHC.Base.quotRemWord2# (word64ToWord# a) (word64ToWord# b) (word64ToWord# c) of
(# x, y #) ->
(# wordToWord64# x, wordToWord64# y #)
#endif

isZeroWord#
#if MIN_VERSION_base(4,17,0)
:: Word64# -> Bool
isZeroWord# a = GHC.Base.isTrue# (GHC.Base.eqWord# (word64ToWord# a) 0##)
#else
:: Word# -> Bool
isZeroWord# 0## = True
isZeroWord# _ = False
#endif

compatWordLiteral#
#if MIN_VERSION_base(4,17,0)
:: GHC.Base.Word# -> Word64#
compatWordLiteral# = wordToWord64#
#else
:: Word# -> Word#
compatWordLiteral# a = a
#endif

compatIntLiteral#
#if MIN_VERSION_base(4,17,0)
:: GHC.Base.Int# -> Int64#
compatIntLiteral# = GHC.Base.intToInt64#
#else
:: Int# -> Int#
compatIntLiteral# a = a
#endif

compatCaseOnWordLiteral#
#if MIN_VERSION_base(4,17,0)
:: Word64# -> GHC.Base.Word#
compatCaseOnWordLiteral# = word64ToWord#
#else
:: Word# -> Word#
compatCaseOnWordLiteral# a = a
#endif

compatCaseOnIntLiteral#
#if MIN_VERSION_base(4,17,0)
:: Int64# -> GHC.Base.Int#
compatCaseOnIntLiteral# = GHC.Base.int64ToInt#
#else
:: Int# -> Int#
compatCaseOnIntLiteral# a = a
#endif

7 changes: 4 additions & 3 deletions src/Data/WideWord/Int128.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ import Numeric
import Foreign.Ptr (Ptr, castPtr)
import Foreign.Storable (Storable (..))

import GHC.Base (Int (..), and#, int2Word#, minusWord#, not#, or#, plusWord#, plusWord2#
, subWordC#, timesWord#, timesWord2#, word2Int#, xor#)
import GHC.Base (Int (..))
import GHC.Enum (predError, succError)
import GHC.Exts ((+#), (*#), State#, Int#, Addr#, ByteArray#, MutableByteArray#)
import GHC.Generics
Expand All @@ -65,6 +64,8 @@ import Data.Primitive.Types (Prim (..), defaultSetByteArray#, defaultSetOffAddr#

import Data.Hashable (Hashable,hashWithSalt)

import Data.WideWord.Compat
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@erikd I've consolidated the compat stuff to minimize noise here. Thoughts?

Copy link
Owner

Choose a reason for hiding this comment

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

I like it. That is a significant improvement.

Copy link
Owner

Choose a reason for hiding this comment

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

The only thing I would still like to address is the CPP and ViewPatterns use in things like say Data.WideWord.Word256.signum256. I am sure there is a neater way to do that (in say that Compat module).

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe something like:

signum256 :: Word256 -> Word256
signum256 (Word256 a b c d) =
  if isZeroWord64 a && isZeroWord64 b && isZeroWord64 c && isZeroWord64 d
    then zeroWord256
    else oneWord256

With the isZeroWord64 being defined in the Compat module.

Copy link
Owner

Choose a reason for hiding this comment

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

Also wondering if some of the other CPPisms like this:

#if MIN_VERSION_base(4,17,0)
  Word256 (W64# (or64# a3 b3)) (W64# (or64# a2 b2))
          (W64# (or64# a1 b1)) (W64# (or64# a0 b0))
#else
  Word256 (W64# (or# a3 b3)) (W64# (or# a2 b2))
          (W64# (or# a1 b1)) (W64# (or# a0 b0))
#endif

could be lifted into the Compat module.

Copy link
Owner

Choose a reason for hiding this comment

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

This is all made more difficult because ghc-9.4 insists on being built with the Hadrian build system which complete breaks the way I used to manage installing multiple GHC versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, totally - I only did the Int128 to start since I didn't want to do a bunch of work if it wasn't to your tastes 😅 I'll port the rest over.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also tbh ghcup does work very well, you may consider adopting it? i was pretty skeptical but it is legitimately very good

Copy link
Owner

Choose a reason for hiding this comment

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

Can ghcup produce a package I can install on several machines? Can I specify where it installs stuff like /usr/lib/ghc-X.Y ?


data Int128 = Int128
{ int128Hi64 :: !Word64
, int128Lo64 :: !Word64
Expand Down Expand Up @@ -269,7 +270,7 @@ times128 (Int128 (W64# a1) (W64# a0)) (Int128 (W64# b1) (W64# b0)) =
{-# INLINABLE negate128 #-}
negate128 :: Int128 -> Int128
negate128 (Int128 (W64# a1) (W64# a0)) =
case plusWord2# (not# a0) 1## of
case plusWord2# (not# a0) (compatWordLiteral# 1##) of
(# c, s #) -> Int128 (W64# (plusWord# (not# a1) c)) (W64# s)

{-# INLINABLE abs128 #-}
Expand Down
14 changes: 8 additions & 6 deletions src/Data/WideWord/Word128.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import Data.Semigroup ((<>))
import Foreign.Ptr (Ptr, castPtr)
import Foreign.Storable (Storable (..))

import GHC.Base (Int (..), and#, int2Word#, minusWord#, not#, or#, plusWord#, plusWord2#
, quotRemWord2#, subWordC#, timesWord#, timesWord2#, xor#)
import GHC.Base (Int (..))
import GHC.Enum (predError, succError)
import GHC.Exts ((*#), (+#), Int#, State#, ByteArray#, MutableByteArray#, Addr#)
import GHC.Generics
Expand All @@ -55,6 +54,7 @@ import GHC.Word (Word64 (..), Word32, byteSwap64)
import GHC.IntWord64
#endif

import Data.WideWord.Compat
import Numeric (showHex)

import Data.Primitive.Types (Prim (..), defaultSetByteArray#, defaultSetOffAddr#)
Expand Down Expand Up @@ -266,13 +266,16 @@ times128 (Word128 (W64# a1) (W64# a0)) (Word128 (W64# b1) (W64# b0)) =
{-# INLINABLE negate128 #-}
negate128 :: Word128 -> Word128
negate128 (Word128 (W64# a1) (W64# a0)) =
case plusWord2# (not# a0) 1## of
case plusWord2# (not# a0) (compatWordLiteral# 1##) of
(# c, s #) -> Word128 (W64# (plusWord# (not# a1) c)) (W64# s)

{-# INLINABLE signum128 #-}
signum128 :: Word128 -> Word128
signum128 (Word128 (W64# 0##) (W64# 0##)) = zeroWord128
signum128 _ = oneWord128
signum128 (Word128 (W64# a) (W64# b))
| isZeroWord# a
, isZeroWord# b
= zeroWord128
| otherwise = oneWord128

fromInteger128 :: Integer -> Word128
fromInteger128 i =
Expand Down Expand Up @@ -441,7 +444,6 @@ quotRemWord64 (W64# n1) (W64# n0) (W64# d) =
case quotRemWord2# n1 n0 d of
(# q, r #) -> (W64# q, W64# r)


{-# INLINE quotRemTwo #-}
quotRemTwo :: Word64 -> Word64 -> (Word128, Word128)
quotRemTwo n0 d0 =
Expand Down
36 changes: 21 additions & 15 deletions src/Data/WideWord/Word256.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ import Data.Semigroup ((<>))
import Foreign.Ptr (Ptr, castPtr)
import Foreign.Storable (Storable (..))

import GHC.Base (Int (..), and#, minusWord#, not#, or#, plusWord#, plusWord2#
, subWordC#, timesWord#, timesWord2#, xor#)
import GHC.Base (Int (..))
import GHC.Enum (predError, succError)
import GHC.Exts ((*#), (+#), Int#, State#, ByteArray#, MutableByteArray#, Addr#)
import GHC.Generics
import GHC.Real ((%))
import GHC.Word (Word64 (..), Word32)

import Data.WideWord.Compat

#if WORD_SIZE_IN_BITS < 64
import GHC.IntWord64
#endif
Expand Down Expand Up @@ -274,23 +275,23 @@ minus256 (Word256 (W64# a3) (W64# a2) (W64# a1) (W64# a0))
where
!(# s0, v1 #) = subWordC# a0 b0
!(# s1, v2 #) =
case v1 of
case compatCaseOnIntLiteral# v1 of
0# -> subWordC# a1 b1
_ ->
case a1 of
0## -> (# minusWord# 0xFFFFFFFFFFFFFFFF## b1, 1# #)
_ -> subWordC# (minusWord# a1 1##) b1
case compatCaseOnWordLiteral# a1 of
0## -> (# minusWord# (compatWordLiteral# 0xFFFFFFFFFFFFFFFF##) b1, (compatIntLiteral# 1#) #)
_ -> subWordC# (minusWord# a1 (compatWordLiteral# 1##)) b1
!(# s2, v3 #) =
case v2 of
case compatCaseOnIntLiteral# v2 of
0# -> subWordC# a2 b2
_ ->
case a2 of
0## -> (# minusWord# 0xFFFFFFFFFFFFFFFF## b2, 1# #)
_ -> subWordC# (minusWord# a2 1##) b2
case compatCaseOnWordLiteral# a2 of
0## -> (# minusWord# (compatWordLiteral# 0xFFFFFFFFFFFFFFFF##) b2, (compatIntLiteral# 1#) #)
_ -> subWordC# (minusWord# a2 (compatWordLiteral# 1##)) b2
!s3 =
case v3 of
case compatCaseOnIntLiteral# v3 of
0# -> minusWord# a3 b3
_ -> minusWord# (minusWord# a3 1##) b3
_ -> minusWord# (minusWord# a3 (compatWordLiteral# 1##)) b3

times256 :: Word256 -> Word256 -> Word256
times256 (Word256 (W64# a3) (W64# a2) (W64# a1) (W64# a0))
Expand Down Expand Up @@ -328,16 +329,21 @@ times256 (Word256 (W64# a3) (W64# a2) (W64# a1) (W64# a0))
{-# INLINABLE negate256 #-}
negate256 :: Word256 -> Word256
negate256 (Word256 (W64# a3) (W64# a2) (W64# a1) (W64# a0)) =
case plusWord2# (not# a0) 1## of
case plusWord2# (not# a0) (compatWordLiteral# 1##) of
(# c1, s0 #) -> case plusWord2# (not# a1) c1 of
(# c2, s1 #) -> case plusWord2# (not# a2) c2 of
(# c3, s2 #) -> case plusWord# (not# a3) c3 of
s3 -> Word256 (W64# s3) (W64# s2) (W64# s1) (W64# s0)

{-# INLINABLE signum256 #-}
signum256 :: Word256 -> Word256
signum256 (Word256 (W64# 0##) (W64# 0##) (W64# 0##) (W64# 0##)) = zeroWord256
signum256 _ = oneWord256
signum256 (Word256 (W64# a) (W64# b) (W64# c) (W64# d))
| isZeroWord# a
, isZeroWord# b
, isZeroWord# c
, isZeroWord# d
= zeroWord256
| otherwise = oneWord256

fromInteger256 :: Integer -> Word256
fromInteger256 i = Word256
Expand Down
6 changes: 4 additions & 2 deletions wide-word.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ library
Data.WideWord.Word256
Data.WideWord.Int128

build-depends: base >= 4.9 && < 4.17
other-modules: Data.WideWord.Compat

build-depends: base >= 4.9 && < 4.18
, deepseq >= 1.3 && < 1.5
-- Required so that GHC.IntWord64 is available on 32 bit systems
, ghc-prim
Expand All @@ -61,7 +63,7 @@ test-suite test
build-depends: base
, bytestring >= 0.10
, ghc-prim
, hedgehog == 1.0.*
, hedgehog >= 1.0 && < 1.2
, primitive
, wide-word

Expand Down