module Language.Lexer.Tlex.Data.Bits (
    maxBitSize,
) where

import           Language.Lexer.Tlex.Prelude

import qualified Data.Bits                   as Bits


maxBitSize :: Bits.FiniteBits a => Ord a => Num a => a -> Int
maxBitSize :: forall a. (FiniteBits a, Ord a, Num a) => 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 forall a. Ord a => a -> a -> Bool
< a
m                     = Int
i
        | Int
i forall a. Ord a => a -> a -> Bool
>= forall b. FiniteBits b => b -> Int
Bits.finiteBitSize a
n = Int
i
        | Bool
otherwise                 = Int -> a -> Int
go
            do Int
i forall a. Num a => a -> a -> a
+ Int
1
            do a
m forall a. Num a => a -> a -> a
* a
2