Skip to content

Commit 3aa0f9a

Browse files
committed
Keep size as Word64 instead
1 parent e4473b3 commit 3aa0f9a

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

src/Data/Binary/Generic.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Data.Binary.Get
3030
import Data.Binary.Put
3131
import Data.Bits
3232
import Data.Word
33+
import Data.Proxy
3334
#if !MIN_VERSION_base(4,11,0)
3435
import Data.Monoid ((<>))
3536
#endif
@@ -84,8 +85,8 @@ instance Binary a => GBinaryGet (K1 i a) where
8485
-- use two bytes, and so on till 2^64-1.
8586

8687
#define GUARD(WORD) (size - 1) <= fromIntegral (maxBound :: WORD)
87-
#define PUTSUM(WORD) GUARD(WORD) = putSum (0 :: WORD) (fromIntegral (size - 1))
88-
#define GETSUM(WORD) GUARD(WORD) = (get :: Get WORD) >>= checkGetSum (fromIntegral (size - 1))
88+
#define PUTSUM(WORD) GUARD(WORD) = putSum (Proxy :: Proxy WORD) 0 size
89+
#define GETSUM(WORD) GUARD(WORD) = (get :: Get WORD) >>= checkGetSum size . fromIntegral
8990

9091
instance ( GSumPut a, GSumPut b
9192
, SumSize a, SumSize b) => GBinaryPut (a :+: b) where
@@ -109,41 +110,40 @@ sizeError s size =
109110

110111
------------------------------------------------------------------------
111112

112-
checkGetSum :: (Ord word, Num word, Bits word, GSumGet f)
113-
=> word -> word -> Get (f a)
114-
checkGetSum maxCode code
115-
| code <= maxCode = getSum code maxCode
116-
| otherwise = fail "Unknown encoding for constructor"
113+
checkGetSum :: (GSumGet f) => Word64 -> Word64 -> Get (f a)
114+
checkGetSum size code
115+
| code < size = getSum code size
116+
| otherwise = fail "Unknown encoding for constructor"
117117
{-# INLINE checkGetSum #-}
118118

119119
class GSumGet f where
120-
getSum :: (Ord word, Num word, Bits word) => word -> word -> Get (f a)
120+
getSum :: Word64 -> Word64 -> Get (f a)
121121

122122
class GSumPut f where
123-
putSum :: (Num w, Bits w, Binary w) => w -> w -> f a -> Put
123+
putSum :: (Binary word, Num word) => Proxy word -> Word64 -> Word64 -> f a -> Put
124124

125125
instance (GSumGet a, GSumGet b) => GSumGet (a :+: b) where
126-
getSum !code !maxCode
127-
| code <= maxCodeL = L1 <$> getSum code maxCodeL
128-
| otherwise = R1 <$> getSum (code - maxCodeL - 1) maxCodeR
126+
getSum !code !size
127+
| code < sizeL = L1 <$> getSum code sizeL
128+
| otherwise = R1 <$> getSum (code - sizeL) sizeR
129129
where
130-
maxCodeL = (maxCode - 1) `shiftR` 1
131-
maxCodeR = maxCode - maxCodeL - 1
130+
sizeL = size `shiftR` 1
131+
sizeR = size - sizeL
132132
{-# INLINE getSum #-}
133133

134134
instance (GSumPut a, GSumPut b) => GSumPut (a :+: b) where
135-
putSum !code !maxCode s = case s of
136-
L1 x -> putSum code maxCodeL x
137-
R1 x -> putSum (code + maxCodeL + 1) maxCodeR x
135+
putSum p !code !size s = case s of
136+
L1 x -> putSum p code sizeL x
137+
R1 x -> putSum p (code + sizeL) sizeR x
138138
where
139-
maxCodeL = (maxCode - 1) `shiftR` 1
140-
maxCodeR = maxCode - maxCodeL - 1
139+
sizeL = size `shiftR` 1
140+
sizeR = size - sizeL
141141

142142
instance GBinaryGet a => GSumGet (C1 c a) where
143143
getSum _ _ = gget
144144

145145
instance GBinaryPut a => GSumPut (C1 c a) where
146-
putSum !code _ x = put code <> gput x
146+
putSum (_ :: Proxy word) !code _ x = put (fromIntegral code :: word) <> gput x
147147

148148
------------------------------------------------------------------------
149149

0 commit comments

Comments
 (0)