public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/103345] New: missed optimization: add/xor individual bytes to form a word
@ 2021-11-21 11:17 gcc at rjk dot terraraq.uk
  2021-11-21 12:44 ` [Bug tree-optimization/103345] " roger at nextmovesoftware dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: gcc at rjk dot terraraq.uk @ 2021-11-21 11:17 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103345
           Summary: missed optimization: add/xor individual bytes to form
                    a word
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at rjk dot terraraq.uk
  Target Milestone: ---

All code generated with godbolt's idea of 'trunk'. See
https://godbolt.org/z/Wcj61PKKG

Source:

#include <stdint.h>

uint32_t load_le_32_or(const uint8_t *ptr)
{
  return ((uint32_t)ptr[0]) | ((uint32_t)ptr[1] << 8) | ((uint32_t)ptr[2] <<
16) | ((uint32_t)ptr[3] << 24);
}

uint32_t load_le_32_add(const uint8_t *ptr)
{
  return ((uint32_t)ptr[0]) + ((uint32_t)ptr[1] << 8) + ((uint32_t)ptr[2] <<
16) + ((uint32_t)ptr[3] << 24);
}


uint32_t load_le_32_xor(const uint8_t *ptr)
{
  return ((uint32_t)ptr[0]) ^ ((uint32_t)ptr[1] << 8) ^ ((uint32_t)ptr[2] <<
16) ^ ((uint32_t)ptr[3] << 24);
}

The ^ version is admittedly a bit of an odd choice but the + version is a
reasonably natural way to write the code.


Code on gcc -O2:

load_le_32_or:
        mov     eax, DWORD PTR [rdi]
        ret
load_le_32_add:
        movzx   eax, BYTE PTR [rdi+1]
        movzx   edx, BYTE PTR [rdi+2]
        sal     eax, 8
        sal     edx, 16
        add     eax, edx
        movzx   edx, BYTE PTR [rdi]
        add     eax, edx
        movzx   edx, BYTE PTR [rdi+3]
        sal     edx, 24
        add     eax, edx
        ret
load_le_32_xor:
        movzx   eax, BYTE PTR [rdi+1]
        movzx   edx, BYTE PTR [rdi+2]
        sal     eax, 8
        sal     edx, 16
        xor     eax, edx
        movzx   edx, BYTE PTR [rdi]
        xor     eax, edx
        movzx   edx, BYTE PTR [rdi+3]
        sal     edx, 24
        xor     eax, edx
        ret


Code on clang -O2:

load_le_32_or:                          # @load_le_32_or
        mov     eax, dword ptr [rdi]
        ret
load_le_32_add:                         # @load_le_32_add
        mov     eax, dword ptr [rdi]
        ret
load_le_32_xor:                         # @load_le_32_xor
        mov     eax, dword ptr [rdi]
        ret

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

end of thread, other threads:[~2021-11-30 10:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-21 11:17 [Bug other/103345] New: missed optimization: add/xor individual bytes to form a word gcc at rjk dot terraraq.uk
2021-11-21 12:44 ` [Bug tree-optimization/103345] " roger at nextmovesoftware dot com
2021-11-22  8:41 ` roger at nextmovesoftware dot com
2021-11-22 18:17 ` cvs-commit at gcc dot gnu.org
2021-11-24  8:55 ` cvs-commit at gcc dot gnu.org
2021-11-25 19:47 ` roger at nextmovesoftware dot com
2021-11-30 10:37 ` cvs-commit 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).