public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/107859] New: Fail to optimize rot13
@ 2022-11-24 13:32 denis.campredon at gmail dot com
  0 siblings, 0 replies; only message in thread
From: denis.campredon at gmail dot com @ 2022-11-24 13:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107859
           Summary: Fail to optimize rot13
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: denis.campredon at gmail dot com
  Target Milestone: ---

Compiled with -O2, the following functions produce different assembly although
they compute the same things:

----------------------------

unsigned rot13_1(unsigned c) {
  if(c >= 'A' && c <= 'Z') return 'A' + ((c -'A') + 13)%26;
  __builtin_unreachable();
}

unsigned rot13_2(unsigned c) {
  if (c >= 'A' && c <= 'M' ) return c + 13;
  else if (c >= 'N' && c <= 'Z' ) return c - 13;
  __builtin_unreachable();
}

unsigned rot13_3(unsigned c) {
  if(c >= 'A' && c <= 'Z') return  c + (c > 'Z' - 13 ? -13 : 13);
  __builtin_unreachable();
}

unsigned rot13_4(unsigned c) {
  if(c >= 'A' && c <= 'Z') return  c + 13 + (c > 'Z' - 13 ? -26 : 0);
  __builtin_unreachable();
}

------------------------------

rot13_1(unsigned int):
        lea     edx, [rdi-52]
        mov     rax, rdx
        imul    rdx, rdx, 1321528399
        shr     rdx, 35
        imul    edx, edx, 26
        sub     eax, edx
        add     eax, 65
        ret
rot13_2(unsigned int):
        lea     edx, [rdi-65]
        lea     eax, [rdi+13]
        sub     edi, 13
        cmp     edx, 12
        cmova   eax, edi
        ret
rot13_3(unsigned int):
        cmp     edi, 78
        sbb     eax, eax
        and     eax, 26
        lea     eax, [rax-13+rdi]
        ret
rot13_4(unsigned int):
        cmp     edi, 78
        sbb     eax, eax
        not     eax
        and     eax, -26
        lea     eax, [rax+13+rdi]
        ret

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-24 13:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-24 13:32 [Bug tree-optimization/107859] New: Fail to optimize rot13 denis.campredon 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).