public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gcc at rjk dot terraraq.uk" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug other/103345] New: missed optimization: add/xor individual bytes to form a word
Date: Sun, 21 Nov 2021 11:17:03 +0000	[thread overview]
Message-ID: <bug-103345-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2021-11-21 11:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-21 11:17 gcc at rjk dot terraraq.uk [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-103345-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).