[GHC] #7041: GHC.Real.gcdInt is no longer optimized.

GHC cvs-ghc at haskell.org
Sat Jul 14 00:12:14 CEST 2012


#7041: GHC.Real.gcdInt is no longer optimized.
-------------------------------+--------------------------------------------
  Reporter:  int-e             |          Owner:  igloo           
      Type:  bug               |         Status:  closed          
  Priority:  high              |      Milestone:  7.6.1           
 Component:  libraries/base    |        Version:  7.5             
Resolution:  fixed             |       Keywords:                  
        Os:  Unknown/Multiple  |   Architecture:  Unknown/Multiple
   Failure:  None/Unknown      |     Difficulty:  Unknown         
  Testcase:                    |      Blockedby:                  
  Blocking:                    |        Related:                  
-------------------------------+--------------------------------------------

Comment(by simonpj):

 Here's are the commits for this ticket
 {{{
 commit a07f118c08ac5fd5923b6e0b2c994f4185121604
 Author: Ian Lynagh <igloo at earth.li>
 Date:   Fri Jul 13 19:51:08 2012 +0100

     Tweak RULEs; fixes #7041

     In particular, the gcd rule now uses smallInteger rather than S#,
 which
     means that it actually fires.

     Also fixed a bug when the result is minBound :: Int.

 >---------------------------------------------------------------

  GHC/Integer/Type.lhs |    9 +++------
  1 files changed, 3 insertions(+), 6 deletions(-)

 diff --git a/GHC/Integer/Type.lhs b/GHC/Integer/Type.lhs index
 be3ebea..a402f37 100644
 --- a/GHC/Integer/Type.lhs
 +++ b/GHC/Integer/Type.lhs
 @@ -135,11 +135,6 @@ int64ToInteger i = if ((i `leInt64#` intToInt64#
 0x7FFFFFFF#) &&

  integerToInt :: Integer -> Int#
  {-# NOINLINE integerToInt #-}
 -{-# RULES "integerToInt" forall i. integerToInt (S# i) = i #-}
 --- Don't inline integerToInt, because it can't do much unless
 --- it sees a (S# i), and inlining just creates fruitless
 --- join points.  But we do need a RULE to get the constants
 --- to work right:  1::Int had better optimise to (I# 1)!
  integerToInt (S# i)   = i
  integerToInt (J# s d) = integer2Int# s d

 @@ -287,8 +282,10 @@ lcmInteger a b =      if a `eqInteger` S# 0# then S#
 0#
    where aa = absInteger a
          ab = absInteger b

 +-- This rule needs to use absInteger so that it works correctly when
 +-- the result is minBound :: Int
  {-# RULES "gcdInteger/Int" forall a b.
 -            gcdInteger (S# a) (S# b) = S# (gcdInt a b)
 +    gcdInteger (smallInteger a) (smallInteger b) = absInteger
 + (smallInteger (gcdInt a b))
    #-}
  gcdInt :: Int# -> Int# -> Int#
  gcdInt 0# y  = absInt y
 }}}
 and
 {{{
 commit 4b780b93beac863b7945d7e050ffa6cdaf256eb7
 Author: Ian Lynagh <igloo at earth.li>
 Date:   Fri Jul 13 20:04:51 2012 +0100

     Add another gcdInteger rule

     This one is better when the result is converted to an Int

 >---------------------------------------------------------------

  GHC/Integer/Type.lhs |   11 ++++++++---
  1 files changed, 8 insertions(+), 3 deletions(-)

 diff --git a/GHC/Integer/Type.lhs b/GHC/Integer/Type.lhs index
 a402f37..b9d9c3c 100644
 --- a/GHC/Integer/Type.lhs
 +++ b/GHC/Integer/Type.lhs
 @@ -283,9 +283,14 @@ lcmInteger a b =      if a `eqInteger` S# 0# then S#
 0#
          ab = absInteger b

  -- This rule needs to use absInteger so that it works correctly when
 --- the result is minBound :: Int
 -{-# RULES "gcdInteger/Int" forall a b.
 -    gcdInteger (smallInteger a) (smallInteger b) = absInteger
 (smallInteger (gcdInt a b))
 +-- the result is minBound :: Int. But that isn't necessary when the
 +-- result is converted to an Int.
 +{-# RULES
 +"gcdInteger/Int" forall a b.
 +    gcdInteger (smallInteger a) (smallInteger b)
 +        = absInteger (smallInteger (gcdInt a b))
 +"integerToInt/gcdInteger/Int" forall a b.
 +    integerToInt (gcdInteger (smallInteger a) (smallInteger b)) =
 +gcdInt a b
    #-}
  gcdInt :: Int# -> Int# -> Int#
  gcdInt 0# y  = absInt y
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/7041#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler



More information about the Glasgow-haskell-bugs mailing list