module Language.Parser.Ptera.TH.Data.Bits.MaxBit (
    maxBitSize,
) where

import           Language.Parser.Ptera.Prelude

import qualified Data.Bits                     as Bits


maxBitSize :: Bits.FiniteBits a => Ord a => Num a => a -> Int
maxBitSize :: a -> Int
maxBitSize a
n = Int -> a -> Int
go Int
1 a
2 where
    go :: Int -> a -> Int
go Int
i a
m
        | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
m                     = Int
i
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= a -> Int
forall b. FiniteBits b => b -> Int
Bits.finiteBitSize a
n = Int
i
        | Bool
otherwise                 = Int -> a -> Int
go
            do Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
            do a
m a -> a -> a
forall a. Num a => a -> a -> a
* a
2