{-# LANGUAGE MagicHash #-}

module Data.Queue.Numeric where

import GHC.Exts

intLog :: Int -> Int
intLog 0 = 0
intLog 1 = 0
intLog (I# x) = I# (word2Int# (intLog1 (int2Word# x))) where
	zeroq x = x `eqWord#` int2Word# 0#
	intLog1 x = let ans = uncheckedShiftRL# x 16# in if zeroq ans then intLog2 x else int2Word# 16# `or#` intLog2 ans
	intLog2 x = let ans = uncheckedShiftRL# x 8# in if zeroq ans then intLog3 x else int2Word# 8# `or#` intLog3 ans
	intLog3 x = let ans = uncheckedShiftRL# x 4# in if zeroq ans then intLog4 x else int2Word# 4# `or#` intLog4 ans
	intLog4 x = let ans = uncheckedShiftRL# x 2# in if zeroq ans then intLog5 x else int2Word# 2# `or#` intLog5 ans
	intLog5 x = if x `leWord#` int2Word# 1# then int2Word# 0# else int2Word# 1#

ceilLog :: Int -> Int
ceilLog x = intLog (2 * x - 1)