From: PHO Date: Sat, 19 Feb 2011 12:45:24 +0000 (+0900) Subject: new function: countBits X-Git-Url: https://git.cielonegro.org/gitweb.cgi?a=commitdiff_plain;h=db3548d79ebddbc0927c06ece435d61d241c1644;p=wavpack.git new function: countBits --- diff --git a/Codec/Audio/WavPack/Internal.hs b/Codec/Audio/WavPack/Internal.hs index bb4cd37..5769a3f 100644 --- a/Codec/Audio/WavPack/Internal.hs +++ b/Codec/Audio/WavPack/Internal.hs @@ -12,6 +12,8 @@ module Codec.Audio.WavPack.Internal , exp2s , exp2 + + , countBits ) where import Data.Int @@ -35,7 +37,7 @@ packWeight !w fromIntegral w''' -- | Convert a packed weight to internal weight (+/-1024). Note that --- the following equation might not hold because 'packWeight' is +-- the following equation /might not hold/ because 'packWeight' is -- lossy. -- -- > unpackWeight . packWeight = id @@ -123,6 +125,15 @@ exp2 !l else exp2l `shiftL` fromIntegral (l' - 9) +-- | 'countBits' @av@ returns the number of bits that is required to +-- represent @av@. +countBits ∷ Word32 → Word8 +countBits av + | av < (1 `shiftL` 8) = getNBits (fromIntegral av) + | av < (1 `shiftL` 16) = getNBits (fromIntegral (av `shiftR` 8)) + 8 + | av < (1 `shiftL` 24) = getNBits (fromIntegral (av `shiftR` 16)) + 16 + | otherwise = getNBits (fromIntegral (av `shiftR` 24)) + 24 + getNBits ∷ Word8 → Word8 getNBits = UV.unsafeIndex nbitsTable ∘ fromIntegral where