public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "carrot at google dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/45980] New: Use not in stead of add to generate new constant
Date: Tue, 12 Oct 2010 08:42:00 -0000	[thread overview]
Message-ID: <bug-45980-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45980

           Summary: Use not in stead of add to generate new constant
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: carrot@google.com
                CC: carrot@google.com
              Host: i686-linux
            Target: arm-eabi
             Build: i686-linux


Compile the following code:

typedef struct {
      unsigned long state[5];
      unsigned long count[2];
} SHA1_CTX;

void SHA1Init(SHA1_CTX* context)
{
      /* SHA1 initialization constants */
      context->state[0] = 0x67452301;
      context->state[1] = 0xEFCDAB89;
      context->state[2] = 0x98BADCFE;
      context->state[3] = 0x10325476;
      context->state[4] = 0xC3D2E1F0;
      context->count[0] = context->count[1] = 0;
}

With options -march=armv7-a -mthumb -Os, gcc generates:

SHA1Init:
        ldr     r3, .L2
        str     r3, [r0, #0]
        add     r3, r3, #-2004318072    
        str     r3, [r0, #4]
        ldr     r3, .L2+4
        str     r3, [r0, #8]
        sub     r3, r3, #-2004318072     
        str     r3, [r0, #12]
        ldr     r3, .L2+8
        str     r3, [r0, #16]
        movs    r3, #0
        str     r3, [r0, #24]
        str     r3, [r0, #20]
        bx      lr
.L3:
        .align  2
.L2:
        .word   1732584193
        .word   -1732584194
        .word   -1009589776

This function needs to store 5 large constants to memory. Instead of load the 5
constants from constant pool, gcc found two of them can be computed out by a
single add/sub constant instruction. But we can do better, notice that

0x67452301 + 0x98BADCFE = 0xFFFFFFFF
0xEFCDAB89 + 0x10325476 = 0xFFFFFFFF

So if we have one such constant, the other one can be computed out by bitwise
not. So a shorter result could be:

SHA1Init:
        ldr     r3, .L2
        str     r3, [r0, #0]
        add     r2, r3, #-2004318072    
        str     r2, [r0, #4]
        movns     r3, r3
        str     r3, [r0, #8]
        movns     r2, r2
        str     r2, [r0, #12]
        ldr     r3, .L2+4
        str     r3, [r0, #16]
        movs    r3, #0
        str     r3, [r0, #24]
        str     r3, [r0, #20]
        bx      lr
.L3:
        .align  2
.L2:
        .word   1732584193
        .word   -1009589776


             reply	other threads:[~2010-10-12  8:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-12  8:42 carrot at google dot com [this message]
2010-10-14  2:14 ` [Bug target/45980] " ramana at gcc dot gnu.org
2010-10-18  6:24 ` carrot at google dot com
2011-03-05 14:40 ` rearnsha at gcc dot gnu.org
2023-05-15  5:10 ` pinskia 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-45980-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).