public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/106484] New: Failure to optimize uint64_t/constant division on ARM32
@ 2022-07-30 18:38 rsaxvc at gmail dot com
  2022-07-30 18:39 ` [Bug target/106484] " rsaxvc at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: rsaxvc at gmail dot com @ 2022-07-30 18:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106484
           Summary: Failure to optimize uint64_t/constant division on
                    ARM32
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rsaxvc at gmail dot com
  Target Milestone: ---
            Target: arm

The following test function compiles into a call to __aeabi_uldivmod, even
though the divisor is a constant. Here's an example function:

    #include <stdint.h>
    uint64_t ns_to_s( uint64_t ns64 )
    {
    return ns64 / 1000000000ULL;
    }

CortexM4(-O3 -Wall -Wextra -mcpu=cortex-m4) assembly:

ns_to_s(unsigned long long):
        push    {r3, lr}
        adr     r3, .L4
        ldrd    r2, [r3]
        bl      __aeabi_uldivmod
        pop     {r3, pc}
.L4:
        .word   1000000000
        .word   0

Interestingly, gcc 12.1 for aarch64 compiles the above C function by
implementing division by a constant with scaled multiplication by the inverse
using the umulh instruction(not present on 32-bit ARM). (-O3 -Wall -Wextra):

ns_to_s(unsigned long):
        mov     x1, 23123
        lsr     x0, x0, 9
        movk    x1, 0xa09b, lsl 16
        movk    x1, 0xb82f, lsl 32
        movk    x1, 0x44, lsl 48
        umulh   x0, x0, x1
        lsr     x0, x0, 11
        ret

Instead, if something like __umulh could be added to libgcc, then GCC could use
the constants generated in the aarch64 logic to implement uint64_t/constant
division. Example umulh approach is attached. On Cortex-M4, the umulh-based
approach is significantly faster, although this depends on the specific libc
__aeabi_uldivmod linked against as well as the numerator.

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

end of thread, other threads:[~2023-07-08 18:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-30 18:38 [Bug target/106484] New: Failure to optimize uint64_t/constant division on ARM32 rsaxvc at gmail dot com
2022-07-30 18:39 ` [Bug target/106484] " rsaxvc at gmail dot com
2022-07-30 22:29 ` pinskia at gcc dot gnu.org
2022-08-01 10:50 ` rearnsha at gcc dot gnu.org
2022-08-01 12:12 ` rsaxvc at gmail dot com
2023-05-04 12:55 ` rsaxvc at gmail dot com
2023-07-08 18:10 ` rsaxvc at gmail dot com

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).