{-# LANGUAGE CPP #-}

-- |Primitives to calculate the maximum size in bits of the encoding of a value
module Flat.Encoder.Size where

import           Data.Bits                      (Bits)
import qualified Data.ByteString                as B
import qualified Data.ByteString.Lazy           as L
import qualified Data.ByteString.Short.Internal as SBS
import           Data.Char                      (ord)
import qualified Data.Text                      as T
import qualified Data.Text.Internal             as TI
import           Data.ZigZag                    (ZigZag (zigZag))
import           Flat.Encoder.Prim              (w7l)
import           Flat.Types                     (Int16, Int32, Int64, Natural,
                                                 NumBits, Text, Word16, Word32,
                                                 Word64)
#include "MachDeps.h"
-- A filler can take anything from 1 to 8 bits
sFillerMax :: NumBits
sFillerMax :: NumBits
sFillerMax = NumBits
8

sBool :: NumBits
sBool :: NumBits
sBool = NumBits
1

sWord8 :: NumBits
sWord8 :: NumBits
sWord8 = NumBits
8

sInt8 :: NumBits
sInt8 :: NumBits
sInt8 = NumBits
8

sFloat :: NumBits
sFloat :: NumBits
sFloat = NumBits
32

sDouble :: NumBits
sDouble :: NumBits
sDouble = NumBits
64

{-# INLINE sChar #-}
sChar :: Char -> NumBits
sChar :: Char -> NumBits
sChar = Word32 -> NumBits
sWord32 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> NumBits
ord

sCharMax :: NumBits
sCharMax :: NumBits
sCharMax = NumBits
24

{-# INLINE sWord #-}
sWord :: Word -> NumBits
{-# INLINE sInt #-}
sInt :: Int -> NumBits
#if WORD_SIZE_IN_BITS == 64
sWord :: Word -> NumBits
sWord = Word64 -> NumBits
sWord64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

sInt :: NumBits -> NumBits
sInt = Int64 -> NumBits
sInt64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
#elif WORD_SIZE_IN_BITS == 32
sWord = sWord32 . fromIntegral

sInt = sInt32 . fromIntegral
#else
#error expected WORD_SIZE_IN_BITS to be 32 or 64
#endif
-- TODO: optimize ints sizes
{-# INLINE sInt16 #-}
sInt16 :: Int16 -> NumBits
sInt16 :: Int16 -> NumBits
sInt16 = Word16 -> NumBits
sWord16 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall signed unsigned.
ZigZag signed unsigned =>
signed -> unsigned
zigZag

{-# INLINE sInt32 #-}
sInt32 :: Int32 -> NumBits
sInt32 :: Int32 -> NumBits
sInt32 = Word32 -> NumBits
sWord32 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall signed unsigned.
ZigZag signed unsigned =>
signed -> unsigned
zigZag

{-# INLINE sInt64 #-}
sInt64 :: Int64 -> NumBits
sInt64 :: Int64 -> NumBits
sInt64 = Word64 -> NumBits
sWord64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall signed unsigned.
ZigZag signed unsigned =>
signed -> unsigned
zigZag

{-# INLINE sWord16 #-}
sWord16 :: Word16 -> NumBits
sWord16 :: Word16 -> NumBits
sWord16 Word16
w
  | Word16
w forall a. Ord a => a -> a -> Bool
< Word16
128 = NumBits
8
  | Word16
w forall a. Ord a => a -> a -> Bool
< Word16
16384 = NumBits
16
  | Bool
otherwise = NumBits
24

{-# INLINE sWord32 #-}
sWord32 :: Word32 -> NumBits
sWord32 :: Word32 -> NumBits
sWord32 Word32
w
  | Word32
w forall a. Ord a => a -> a -> Bool
< Word32
128 = NumBits
8
  | Word32
w forall a. Ord a => a -> a -> Bool
< Word32
16384 = NumBits
16
  | Word32
w forall a. Ord a => a -> a -> Bool
< Word32
2097152 = NumBits
24
  | Word32
w forall a. Ord a => a -> a -> Bool
< Word32
268435456 = NumBits
32
  | Bool
otherwise = NumBits
40

{-# INLINE sWord64 #-}
sWord64 :: Word64 -> NumBits
sWord64 :: Word64 -> NumBits
sWord64 Word64
w
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
128 = NumBits
8
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
16384 = NumBits
16
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
2097152 = NumBits
24
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
268435456 = NumBits
32
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
34359738368 = NumBits
40
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
4398046511104 = NumBits
48
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
562949953421312 = NumBits
56
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
72057594037927936 = NumBits
64
  | Word64
w forall a. Ord a => a -> a -> Bool
< Word64
9223372036854775808 = NumBits
72
  | Bool
otherwise = NumBits
80

{-# INLINE sInteger #-}
sInteger :: Integer -> NumBits
sInteger :: Integer -> NumBits
sInteger = forall t. (Bits t, Integral t) => t -> NumBits
sIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall signed unsigned.
ZigZag signed unsigned =>
signed -> unsigned
zigZag

{-# INLINE sNatural #-}
sNatural :: Natural -> NumBits
sNatural :: Natural -> NumBits
sNatural = forall t. (Bits t, Integral t) => t -> NumBits
sIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger

-- BAD: duplication of work with encoding
{-# INLINE sIntegral #-}
sIntegral :: (Bits t, Integral t) => t -> Int
sIntegral :: forall t. (Bits t, Integral t) => t -> NumBits
sIntegral t
t =
  let vs :: [Word8]
vs = forall t. (Bits t, Integral t) => t -> [Word8]
w7l t
t
   in forall (t :: * -> *) a. Foldable t => t a -> NumBits
length [Word8]
vs forall a. Num a => a -> a -> a
* NumBits
8

{-# INLINE sUTF8Max #-}
sUTF8Max :: Text -> NumBits
sUTF8Max :: Text -> NumBits
sUTF8Max (TI.Text Array
_ NumBits
_ NumBits
lenInUnits) =
  let len :: NumBits
len =
#if MIN_VERSION_text(2,0,0)
        -- UTF-8 encoding, units are bytes
        lenInUnits
#else
        -- UTF-16 encoding, units are 16 bits words
        -- worst case: a utf-16 unit becomes a 3 bytes utf-8 encoding
        NumBits
lenInUnits forall a. Num a => a -> a -> a
* NumBits
3
#endif
  in NumBits -> NumBits
blobBits NumBits
len

{-# INLINE sUTF16Max #-}
sUTF16Max :: T.Text -> NumBits
sUTF16Max :: Text -> NumBits
sUTF16Max (TI.Text Array
_ NumBits
_ NumBits
lenInUnits) =
  let len :: NumBits
len =
#if MIN_VERSION_text(2,0,0)
        -- UTF-8 encoding
        -- worst case, a 1 byte UTF-8 char becomes a 2 bytes UTF-16 (ascii)
        lenInUnits * 2
#else
        -- UTF-16 encoding
        NumBits
lenInUnits forall a. Num a => a -> a -> a
* NumBits
2
#endif
  in NumBits -> NumBits
blobBits NumBits
len

{-# INLINE sBytes #-}
sBytes :: B.ByteString -> NumBits
sBytes :: ByteString -> NumBits
sBytes = NumBits -> NumBits
blobBits forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> NumBits
B.length

{-# INLINE sLazyBytes #-}
sLazyBytes :: L.ByteString -> NumBits
sLazyBytes :: ByteString -> NumBits
sLazyBytes ByteString
bs = NumBits
16 forall a. Num a => a -> a -> a
+ forall a. (ByteString -> a -> a) -> a -> ByteString -> a
L.foldrChunks (\ByteString
b NumBits
l -> ByteString -> NumBits
blkBitsBS ByteString
b forall a. Num a => a -> a -> a
+ NumBits
l) NumBits
0 ByteString
bs

{-# INLINE sShortBytes #-}
sShortBytes :: SBS.ShortByteString -> NumBits
sShortBytes :: ShortByteString -> NumBits
sShortBytes = NumBits -> NumBits
blobBits forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> NumBits
SBS.length

{-# INLINE bitsToBytes #-}
bitsToBytes :: Int -> Int
bitsToBytes :: NumBits -> NumBits
bitsToBytes = forall t. Integral t => t -> t -> t
numBlks NumBits
8

{-# INLINE numBlks #-}
numBlks :: Integral t => t -> t -> t
numBlks :: forall t. Integral t => t -> t -> t
numBlks t
blkSize t
bits =
  let (t
d, t
m) = t
bits forall a. Integral a => a -> a -> (a, a)
`divMod` t
blkSize
   in t
d forall a. Num a => a -> a -> a
+
      (if t
m forall a. Eq a => a -> a -> Bool
== t
0
         then t
0
         else t
1)

{-# INLINE arrayBits #-}
arrayBits :: Int -> NumBits
arrayBits :: NumBits -> NumBits
arrayBits = (NumBits
8 forall a. Num a => a -> a -> a
*) forall b c a. (b -> c) -> (a -> b) -> a -> c
. NumBits -> NumBits
arrayChunks

{-# INLINE arrayChunks #-}
arrayChunks :: Int -> NumBits
arrayChunks :: NumBits -> NumBits
arrayChunks = (NumBits
1 forall a. Num a => a -> a -> a
+) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Integral t => t -> t -> t
numBlks NumBits
255

{-# INLINE blobBits #-}
blobBits :: Int -> NumBits
blobBits :: NumBits -> NumBits
blobBits NumBits
numBytes =
  NumBits
16 -- initial filler + final 0
   forall a. Num a => a -> a -> a
+
  NumBits -> NumBits
blksBits NumBits
numBytes

{-# INLINE blkBitsBS #-}
blkBitsBS :: B.ByteString -> NumBits
blkBitsBS :: ByteString -> NumBits
blkBitsBS = NumBits -> NumBits
blksBits forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> NumBits
B.length

{-# INLINE blksBits #-}
blksBits :: Int -> NumBits
blksBits :: NumBits -> NumBits
blksBits NumBits
numBytes = NumBits
8 forall a. Num a => a -> a -> a
* (NumBits
numBytes forall a. Num a => a -> a -> a
+ forall t. Integral t => t -> t -> t
numBlks NumBits
255 NumBits
numBytes)