From: Ian Lynagh Date: Tue, 18 Mar 2008 00:30:17 +0000 (+0000) Subject: Replace (^) with a faster variant (from trac #1687) X-Git-Tag: 2008-05-28~30 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=fe3a8c9fa0c8a55e29a72e7f526984b201270c49;p=ghc-base.git Replace (^) with a faster variant (from trac #1687) --- diff --git a/GHC/Real.lhs b/GHC/Real.lhs index f7e2eb2..17faed9 100644 --- a/GHC/Real.lhs +++ b/GHC/Real.lhs @@ -404,13 +404,12 @@ odd = not . even Integer -> Int -> Integer, Int -> Int -> Int #-} (^) :: (Num a, Integral b) => a -> b -> a -_ ^ 0 = 1 -x ^ n | n > 0 = f x (n-1) x - where f _ 0 y = y - f a d y = g a d where - g b i | even i = g (b*b) (i `quot` 2) - | otherwise = f b (i-1) (b*y) -_ ^ _ = error "Prelude.^: negative exponent" +x ^ y | y < 0 = error "Negative exponent" + | y == 0 = 1 + | y == 1 = x + | odd y = x * (x ^ (y - 1)) + | otherwise = let x' = x ^ (y `div` 2) + in x' * x' -- | raise a number to an integral power {-# SPECIALISE (^^) ::