#5615: ghc produces poor code for `div` with constant powers of 2.
-------------------------+--------------------------------------------------
    Reporter:  Lennart   |       Owner:  daniel.is.fischer
        Type:  bug       |      Status:  new              
    Priority:  normal    |   Milestone:  7.6.1            
   Component:  Compiler  |     Version:  7.2.1            
    Keywords:            |          Os:  Unknown/Multiple 
Architecture:  x86       |     Failure:  None/Unknown     
  Difficulty:  Unknown   |    Testcase:                   
   Blockedby:            |    Blocking:                   
     Related:            |  
-------------------------+--------------------------------------------------

Comment(by simonmar):

 Well, that's a good point.  Integer right shift is equivalent to division
 truncated to negative infinity (i.e. `div`), so the test on the numerator
 would not be necessary.  That's a good argument for having a built in
 rule.

 For `quot` some additional checks would be necessary in the rule.  I just
 checked what gcc does, and for `x / 2` it generates the clever sequence

 {{{
         movl    %edi, %eax
         shrl    $31, %eax
         addl    %edi, %eax
         sarl    %eax
 }}}

 which is basically the same as `x < 0 ? (x+1) >> 1 : x >> 1`.  Dividing by
 4 instead gives this:

 {{{
         leal    3(%rdi), %eax
         testl   %edi, %edi
         cmovns  %edi, %eax
         sarl    $2, %eax
 }}}

 which is `x < 0 ? (x + 3) >> 2 : x >> 2`.

 We could do the same tricks with a built-in rule for `quot`.

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

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to