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

armv7l 884 testing #1

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions cborg/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision history for cborg

## 0.2.9.0 -- 2022-10-26

* building in ghc 8.8.4 on 64 bit machine

## 0.2.8.0 -- 2022-09-24

* Support GHC 9.4
Expand Down
2 changes: 1 addition & 1 deletion cborg/cborg.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cborg
version: 0.2.8.0
version: 0.2.9.0
synopsis: Concise Binary Object Representation (CBOR)
license: BSD3
license-file: LICENSE.txt
Expand Down
23 changes: 15 additions & 8 deletions cborg/src/Codec/CBOR/Decoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,14 @@ getDecodeAction (Decoder k) = k (\x -> return (Done x))
toInt8 :: Int# -> Int8
toInt16 :: Int# -> Int16
toInt32 :: Int# -> Int32
toInt64 :: Int# -> Int64
toWord8 :: Word# -> Word8
toWord16 :: Word# -> Word16
toWord32 :: Word# -> Word32
#if defined(ARCH_64bit)
toInt64 :: Int# -> Int64
toWord64 :: Word# -> Word64
#endif

#if MIN_VERSION_ghc_prim(0,8,0)
toInt8 n = I8# (intToInt8# n)
toInt16 n = I16# (intToInt16# n)
Expand All @@ -328,13 +331,15 @@ toWord8 n = W8# (wordToWord8# n)
toWord16 n = W16# (wordToWord16# n)
toWord32 n = W32# (wordToWord32# n)
#if WORD_SIZE_IN_BITS == 64
#if defined(ARCH_64bit)
#if MIN_VERSION_base(4,17,0)
toInt64 n = I64# (intToInt64# n)
toWord64 n = W64# (wordToWord64# n)
#else
toInt64 n = I64# n
toWord64 n = W64# n
#endif
#endif
#else
toInt64 n = I64# (intToInt64# n)
toWord64 n = W64# (wordToWord64# n)
Expand All @@ -343,12 +348,14 @@ toWord64 n = W64# (wordToWord64# n)
toInt8 n = I8# n
toInt16 n = I16# n
toInt32 n = I32# n
toInt64 n = I64# n
toWord8 n = W8# n
toWord16 n = W16# n
toWord32 n = W32# n
#if defined(ARCH_64bit)
toInt64 n = I64# n
toWord64 n = W64# n
#endif
#endif

-- $canonical
--
Expand Down Expand Up @@ -423,7 +430,7 @@ decodeWord64 =
#if defined(ARCH_64bit)
Decoder (\k -> return (ConsumeWord (\w# -> k (toWord64 w#))))
#else
Decoder (\k -> return (ConsumeWord64 (\w64# -> k (toWord64 w64#))))
Decoder (\k -> return (ConsumeWord64 (\w64# -> k (W64# w64#))))
#endif

-- | Decode a negative 'Word'.
Expand All @@ -442,7 +449,7 @@ decodeNegWord64 =
#if defined(ARCH_64bit)
Decoder (\k -> return (ConsumeNegWord (\w# -> k (toWord64 w#))))
#else
Decoder (\k -> return (ConsumeNegWord64 (\w64# -> k (toWord64 w64#))))
Decoder (\k -> return (ConsumeNegWord64 (\w64# -> k (W64# w64#))))
#endif

-- | Decode an 'Int'.
Expand Down Expand Up @@ -482,7 +489,7 @@ decodeInt64 =
#if defined(ARCH_64bit)
Decoder (\k -> return (ConsumeInt (\n# -> k (toInt64 n#))))
#else
Decoder (\k -> return (ConsumeInt64 (\n64# -> k (toInt64 n64#))))
Decoder (\k -> return (ConsumeInt64 (\n64# -> k (I64# n64#))))
#endif

-- | Decode canonical representation of a 'Word'.
Expand Down Expand Up @@ -522,7 +529,7 @@ decodeWord64Canonical =
#if defined(ARCH_64bit)
Decoder (\k -> return (ConsumeWordCanonical (\w# -> k (toWord64 w#))))
#else
Decoder (\k -> return (ConsumeWord64Canonical (\w64# -> k (toWord64 w64#))))
Decoder (\k -> return (ConsumeWord64Canonical (\w64# -> k (W64# w64#))))
#endif

-- | Decode canonical representation of a negative 'Word'.
Expand All @@ -541,7 +548,7 @@ decodeNegWord64Canonical =
#if defined(ARCH_64bit)
Decoder (\k -> return (ConsumeNegWordCanonical (\w# -> k (toWord64 w#))))
#else
Decoder (\k -> return (ConsumeNegWord64Canonical (\w64# -> k (toWord64 w64#))))
Decoder (\k -> return (ConsumeNegWord64Canonical (\w64# -> k (W64# w64#))))
#endif

-- | Decode canonical representation of an 'Int'.
Expand Down Expand Up @@ -581,7 +588,7 @@ decodeInt64Canonical =
#if defined(ARCH_64bit)
Decoder (\k -> return (ConsumeIntCanonical (\n# -> k (toInt64 n#))))
#else
Decoder (\k -> return (ConsumeInt64Canonical (\n64# -> k (toInt64 n64#))))
Decoder (\k -> return (ConsumeInt64Canonical (\n64# -> k (I64# n64#))))
#endif

-- | Decode an 'Integer'.
Expand Down
54 changes: 39 additions & 15 deletions cborg/src/Codec/CBOR/Magic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,21 @@ import qualified Numeric.Half as Half
import Data.Bits ((.|.), unsafeShiftL)
#endif

#if defined(ARCH_32bit)
import GHC.IntWord64 (wordToWord64#, word64ToWord#,
intToInt64#, int64ToInt#,
leWord64#, ltWord64#, word64ToInt64#)

#if MIN_VERSION_base(4,17,0)
import GHC.Exts as Compat (wordToWord64#, word64ToWord#,
intToInt64#, int64ToInt#,
leWord64#, ltWord64#, word64ToInt64#)

#endif
#if MIN_VERSION_base(4,14,0)
#else
#if WORD_SIZE_IN_BITS < 64
import GHC.IntWord64 as Compat (wordToWord64#, word64ToWord#,
intToInt64#, int64ToInt#,
leWord64#, ltWord64#, word64ToInt64#)
Copy link
Author

Choose a reason for hiding this comment

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

GHC.IntWord64, doesn't exist in base-4.16.3.0, so this is currently failing for GHC 9.2.4. Other alternatives need to be found for this version. GHC.IntWord64 is removed in ghc-prim-0.8.0

#endif
#endif


--------------------------------------------------------------------------------

Expand Down Expand Up @@ -168,7 +177,7 @@ grabWord32 (Ptr ip#) = W32# (narrow32Word# (byteSwap32# (indexWord32OffAddr# ip#
#endif
#if defined(ARCH_64bit)
#if MIN_VERSION_base(4,17,0)
grabWord64 (Ptr ip#) = W64# (wordToWord64# (byteSwap# (word64ToWord# (indexWord64OffAddr# ip# 0#))))
grabWord64 (Ptr ip#) = W64# (Compat.wordToWord64# (byteSwap# (word64ToWord# (indexWord64OffAddr# ip# 0#))))
#else
grabWord64 (Ptr ip#) = W64# (byteSwap# (indexWord64OffAddr# ip# 0#))
#endif
Expand Down Expand Up @@ -259,7 +268,7 @@ grabWord64 (Ptr ip#) =
#if WORD_SIZE_IN_BITS == 64
w64 w# = W64# (toWord w#)
#else
w64 w# = W64# (wordToWord64# (toWord w#))
w64 w# = W64# (Compat.wordToWord64# (toWord w#))
#endif

#endif
Expand Down Expand Up @@ -450,7 +459,7 @@ word64ToWord (W64# w#) = W# w#
#endif
#else
word64ToWord (W64# w64#) =
case isTrue# (w64# `leWord64#` wordToWord64# 0xffffffff##) of
case isTrue# (w64# `leWord64#` Compat.wordToWord64# 0xffffffff##) of
True -> Just (W# (word64ToWord# w64#))
False -> Nothing
#endif
Expand All @@ -462,7 +471,7 @@ word32ToWord (W32# w#) = W# w#
word64ToWord (W64# w#) = W# w#
#else
word64ToWord (W64# w64#) =
case isTrue# (w64# `leWord64#` wordToWord64# 0xffffffff##) of
case isTrue# (w64# `leWord64#` Compat.wordToWord64# 0xffffffff##) of
True -> Just (W# (word64ToWord# w64#))
False -> Nothing
#endif
Expand All @@ -480,7 +489,7 @@ word16ToInt (W16# w#) = I# (word2Int# (word16ToWord# w#))
word32ToInt (W32# w#) = I# (word2Int# (word32ToWord# w#))
#else
word32ToInt (W32# w#) =
case isTrue# (w# `ltWord#` 0x80000000##) of
case isTrue# (word32ToWord# w# `ltWord#` 0x80000000##) of
True -> Just (I# (word2Int# (word32ToWord# w#)))
False -> Nothing
#endif
Expand Down Expand Up @@ -515,7 +524,7 @@ word64ToInt (W64# w#) =
False -> Nothing
#else
word64ToInt (W64# w#) =
case isTrue# (w# `ltWord64#` wordToWord64# 0x80000000##) of
case isTrue# (w# `ltWord64#` Compat.wordToWord64# 0x80000000##) of
True -> Just (I# (int64ToInt# (word64ToInt64# w#)))
False -> Nothing
#endif
Expand All @@ -526,17 +535,32 @@ word64ToInt (W64# w#) =
{-# INLINE word64ToInt #-}

#if defined(ARCH_32bit)

#if MIN_VERSION_ghc_prim(0,8,0)
word8ToInt64 (W8# w#) = I64# (intToInt64# (word2Int# (word8ToWord# w#)))
word16ToInt64 (W16# w#) = I64# (intToInt64# (word2Int# (word16ToWord# w#)))
word32ToInt64 (W32# w#) = I64# (word64ToInt64# (Compat.wordToWord64# (word32ToWord# w#)))
#else
word8ToInt64 (W8# w#) = I64# (intToInt64# (word2Int# w#))
word16ToInt64 (W16# w#) = I64# (intToInt64# (word2Int# w#))
word32ToInt64 (W32# w#) = I64# (word64ToInt64# (wordToWord64# w#))
#endif


word64ToInt64 (W64# w#) =
case isTrue# (w# `ltWord64#` uncheckedShiftL64# (wordToWord64# 1##) 63#) of
case isTrue# (w# `ltWord64#` uncheckedShiftL64# (Compat.wordToWord64# 1##) 63#) of
True -> Just (I64# (word64ToInt64# w#))
False -> Nothing

word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
#if MIN_VERSION_ghc_prim(0,8,0)
word8ToWord64 (W8# w#) = W64# (Compat.wordToWord64# (word8ToWord# w#))
word16ToWord64 (W16# w#) = W64# (Compat.wordToWord64# (word16ToWord# w#))
word32ToWord64 (W32# w#) = W64# (Compat.wordToWord64# (word32ToWord# w#))
#else
word8ToWord64 (W8# w#) = W64# (Compat.wordToWord64# w#)
word16ToWord64 (W16# w#) = W64# (Compat.wordToWord64# w#)
word32ToWord64 (W32# w#) = W64# (Compat.wordToWord64# w#)
#endif

{-# INLINE word8ToInt64 #-}
{-# INLINE word16ToInt64 #-}
Expand Down
38 changes: 21 additions & 17 deletions cborg/src/Codec/CBOR/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Word
import GHC.Word
#if defined(ARCH_32bit)
import GHC.IntWord64
#endif
import GHC.Exts
import GHC.Float (float2Double)
import Data.Typeable
Expand All @@ -80,6 +77,11 @@ import Codec.CBOR.Decoding (DecodeAction)
import qualified Codec.CBOR.Decoding as D
import Codec.CBOR.Magic

#if !MIN_VERSION_ghc_prim(0,8,0)
import GHC.IntWord64
#endif


--------------------------------------------------------------------------------

-- | An exception type that may be returned (by pure functions) or
Expand Down Expand Up @@ -510,9 +512,9 @@ go_fast !bs da@(ConsumeNegWord64Canonical k) =
go_fast !bs da@(ConsumeInt64Canonical k) =
case tryConsumeInt64 (BS.unsafeHead bs) bs of
DecodeFailure -> go_fast_end bs da
DecodedToken sz i@(I64# i#)
| isInt64Canonical sz i -> k i# >>= go_fast (BS.unsafeDrop sz bs)
| otherwise -> go_fast_end bs da
DecodedToken sz (I64# i#)
| isInt64Canonical sz i# -> k i# >>= go_fast (BS.unsafeDrop sz bs)
| otherwise -> go_fast_end bs da

go_fast !bs da@(ConsumeListLen64Canonical k) =
case tryConsumeListLen64 (BS.unsafeHead bs) bs of
Expand Down Expand Up @@ -994,9 +996,9 @@ go_fast_end !bs (ConsumeNegWord64Canonical k) =
go_fast_end !bs (ConsumeInt64Canonical k) =
case tryConsumeInt64 (BS.unsafeHead bs) bs of
DecodeFailure -> return $! SlowFail bs "expected int64"
DecodedToken sz i@(I64# i#)
| isInt64Canonical sz i -> k i# >>= go_fast_end (BS.unsafeDrop sz bs)
| otherwise -> return $! SlowFail bs "non-canonical int64"
DecodedToken sz (I64# i#)
| isInt64Canonical sz i# -> k i# >>= go_fast_end (BS.unsafeDrop sz bs)
| otherwise -> return $! SlowFail bs "non-canonical int64"

go_fast_end !bs (ConsumeListLen64Canonical k) =
case tryConsumeListLen64 (BS.unsafeHead bs) bs of
Expand Down Expand Up @@ -1565,17 +1567,17 @@ isIntCanonical sz i
{-# INLINE isWord64Canonical #-}
isWord64Canonical :: Int -> Word64 -> Bool
isWord64Canonical sz w
| sz == 2 = w > 0x17)
| sz == 3 = w > 0xff)
| sz == 5 = w > 0xffff)
| sz == 9 = w > 0xffffffff)
| sz == 2 = (w > 0x17)
| sz == 3 = (w > 0xff)
| sz == 5 = (w > 0xffff)
| sz == 9 = (w > 0xffffffff)
| otherwise = True

{-# INLINE isInt64Canonical #-}
isInt64Canonical :: Int -> Int64# -> Bool
isInt64Canonical sz i#
| isTrue# (i# `ltInt64#` intToInt64# 0#) = isWord64Canonical sz (not64# w#)
| otherwise = isWord64Canonical sz w#
| isTrue# (i# `ltInt64#` intToInt64# 0#) = isWord64Canonical sz (W64# (not64# w#))
| otherwise = isWord64Canonical sz (W64# w#)
where
w# = int64ToWord64# i#
#endif
Expand Down Expand Up @@ -1796,7 +1798,8 @@ tryConsumeInteger hdr !bs = case word8ToWord hdr of
0x1b -> let !w = eatTailWord64 bs
sz = 9
#if defined(ARCH_32bit)
in DecodedToken sz (BigIntToken (isWord64Canonical sz (word64ToWord w)) $! toInteger w)
in DecodedToken sz (BigIntToken (maybe False (isWord64Canonical sz . wordToWord64) (word64ToWord w)) $! toInteger w)
where wordToWord64 (W# w#) = W64# (wordToWord64# w#)
#else
in DecodedToken sz (BigIntToken (isWordCanonical sz (word64ToWord w)) $! toInteger w)
#endif
Expand Down Expand Up @@ -1838,7 +1841,8 @@ tryConsumeInteger hdr !bs = case word8ToWord hdr of
0x3b -> let !w = eatTailWord64 bs
sz = 9
#if defined(ARCH_32bit)
in DecodedToken sz (BigIntToken (isWord64Canonical sz (word64ToWord w)) $! (-1 - toInteger w))
in DecodedToken sz (BigIntToken (maybe False (isWord64Canonical sz . wordToWord64) (word64ToWord w)) $! (-1 - toInteger w))
where wordToWord64 (W# w#) = W64# (wordToWord64# w#)
#else
in DecodedToken sz (BigIntToken (isWordCanonical sz (word64ToWord w)) $! (-1 - toInteger w))
#endif
Expand Down