#1687: A faster (^)-function.
------------------------------------------+---------------------------------
    Reporter:  [EMAIL PROTECTED]  |        Owner:         
        Type:  bug                        |       Status:  new    
    Priority:  normal                     |    Milestone:  6.8    
   Component:  Prelude                    |      Version:  6.6.1  
    Severity:  normal                     |   Resolution:         
    Keywords:                             |   Difficulty:  Unknown
          Os:  Linux                      |     Testcase:         
Architecture:  x86                        |  
------------------------------------------+---------------------------------
Comment (by moonlite):

 Current version which tries to eliminate some unnecessary tests (don't
 test if we're odd when we just did).
 I can potentially see a slight speedup.

 {{{
 module Pow ((^)) where
 import Prelude hiding ((^))


 {-# SPECIALISE (^) ::
   Integer -> Integer -> Integer,
   Integer -> Int -> Integer,
   Int -> Int -> Int #-}

 (^) :: (Integral b, Num a) => a -> b -> a
 x ^ y | y < 0     = error "Negative exponent"
       | y == 0    = 1
       | otherwise = x `pow` y
       where
         pow :: (Integral b, Num a) => a -> b -> a
         x `pow` y
             | y == 1    = x
             | odd y     = x * (f x (y-1))
             | otherwise = f x y
             where
               f x y = let x' = x `pow` (y `div` 2)
                       in x' * x'
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/1687>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
Glasgow-haskell-bugs@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to