public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/102014] New: [missed optimization] __uint128_t % uint64_t emits a call to __umodti3 instead of div instruction
@ 2021-08-22 16:56 kamkaz at windowslive dot com
  2021-08-22 17:18 ` [Bug c/102014] " jakub at gcc dot gnu.org
  2021-08-22 18:41 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: kamkaz at windowslive dot com @ 2021-08-22 16:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102014

            Bug ID: 102014
           Summary: [missed optimization] __uint128_t % uint64_t emits a
                    call to __umodti3 instead of div instruction
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kamkaz at windowslive dot com
  Target Milestone: ---

The following code:

    #include <stdint.h>
    extern u64 safe_mul(uint64_t a, uint64_t b, uint64_t n) {
        return (((__uint128_t)a)*b)%n;
    }

compiled with -O2 for x86_64 architecture generates following assembly:

    safe_mul(unsigned long, unsigned long, unsigned long):
        mov     rax, rdi
        mov     r8, rdx
        sub     rsp, 8
        xor     ecx, ecx
        mul     rsi
        mov     rsi, rdx
        mov     rdi, rax
        mov     rdx, r8
        call    __umodti3
        add     rsp, 8
        ret

With call to __umodti3, while it could compiled to:

    safe_mul(unsigned long, unsigned long, unsigned long):
        mov     rax, rdx
        mul     rcx
        div     r8
        mov     rax, rdx
        ret

The same thing happens with division __uint128_t / uint64_t and unnecessary
call to __udivti3 instead of div instruction.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/102014] [missed optimization] __uint128_t % uint64_t emits a call to __umodti3 instead of div instruction
  2021-08-22 16:56 [Bug c/102014] New: [missed optimization] __uint128_t % uint64_t emits a call to __umodti3 instead of div instruction kamkaz at windowslive dot com
@ 2021-08-22 17:18 ` jakub at gcc dot gnu.org
  2021-08-22 18:41 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-08-22 17:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102014

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
No, that is not a safe optimization.  The x86 128-bit by 64-bit DIV instruction
will #DE if the quotient is larger than ~(uint64_t) 0, you won't get a modulo
in that case even when the modulo is guaranteed to be representable.
Consider e.g. a == b == 0x3fffffffffffffffULL and n == 3.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/102014] [missed optimization] __uint128_t % uint64_t emits a call to __umodti3 instead of div instruction
  2021-08-22 16:56 [Bug c/102014] New: [missed optimization] __uint128_t % uint64_t emits a call to __umodti3 instead of div instruction kamkaz at windowslive dot com
  2021-08-22 17:18 ` [Bug c/102014] " jakub at gcc dot gnu.org
@ 2021-08-22 18:41 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-22 18:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102014

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 58897.

*** This bug has been marked as a duplicate of bug 58897 ***

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-22 18:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-22 16:56 [Bug c/102014] New: [missed optimization] __uint128_t % uint64_t emits a call to __umodti3 instead of div instruction kamkaz at windowslive dot com
2021-08-22 17:18 ` [Bug c/102014] " jakub at gcc dot gnu.org
2021-08-22 18:41 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).