module Data.ProtoBuf.ZigZag
( decode32
, decode64
, encode32
, encode64
) where
import Data.Bits ((.&.), shiftL, shiftR, xor)
import Data.Int (Int32, Int64)
import Data.Word (Word32, Word64)
decode32 :: Word32 -> Int32
decode32 x = xor (fromIntegral $ shiftR x 1) (negate (fromIntegral $ x .&. 1))
decode64 :: Word64 -> Int64
decode64 x = xor (fromIntegral $ shiftR x 1) (negate (fromIntegral $ x .&. 1))
encode32 :: Int32 -> Word32
encode32 x = fromIntegral $ xor (shiftL x 1) (shiftR x 31)
encode64 :: Int64 -> Word64
encode64 x = fromIntegral $ xor (shiftL x 1) (shiftR x 63)