{-|
Defines the 'Scalable' type class.
-}
module Data.CDAR.Classes where

import Data.Bits

-- | 'Scalable' allows scaling numerical data types by powers of 2.
class Scalable a where
  scale :: a -> Int -> a

-- | The 'Integer' instance.
instance Scalable Integer where
  scale :: Integer -> Int -> Integer
scale Integer
x Int
n
    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 = Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
unsafeShiftL Integer
x Int
n
    | Bool
otherwise = Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
unsafeShiftR (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Bits a => Int -> a
bit (-Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)) (-Int
n)