This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug math/19974] New: optimize div() so the asm insn that calcs quotient and remainder is used when available


https://sourceware.org/bugzilla/show_bug.cgi?id=19974

            Bug ID: 19974
           Summary: optimize div() so the asm insn that calcs quotient and
                    remainder is used when available
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: math
          Assignee: unassigned at sourceware dot org
          Reporter: daniel.gutson at tallertechnologies dot com
  Target Milestone: ---

Created attachment 9209
  --> https://sourceware.org/bugzilla/attachment.cgi?id=9209&action=edit
example with disassembly listing

div() should be optimized so, if available, an instruction that calculates the
quotient and the remainder is expanded (rather than a function call).

For example, in x86, consider:

int addqr(int x, int y)
{
    div_t q = div(x, y);
    return q.quot + q.rem;
}

The result is

  400550:       48 83 ec 08             sub    $0x8,%rsp
  400554:       e8 d7 fe ff ff          callq  400430 <div@plt>
  400559:       48 89 c2                mov    %rax,%rdx
  40055c:       48 83 c4 08             add    $0x8,%rsp
  400560:       48 c1 fa 20             sar    $0x20,%rdx
  400564:       01 d0                   add    %edx,%eax
  400566:       c3                      retq   
  400567:       66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)

whereas the following code

int addqr2(int x, int y)
{
    const int quot = x / y;
    const int rem = x % y;
    return quot + rem;
}

results in 

  400570:       89 f8                   mov    %edi,%eax
  400572:       99                      cltd   
  400573:       f7 fe                   idiv   %esi
  400575:       01 d0                   add    %edx,%eax
  400577:       c3                      retq   
  400578:       0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)

using gcc 4.8.4 with -O3.

I think that div() should be implemented similarly to what gcc optimizes when
recognizes the pattern. IMHO, the programmer should not rely on particular
compiler optimizations but on writing standard C or C++ code.

Please assign this to marcos.diaz@tallertechnologies.com

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]