public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/45980] New: Use not in stead of add to generate new constant
@ 2010-10-12  8:42 carrot at google dot com
  2010-10-14  2:14 ` [Bug target/45980] " ramana at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: carrot at google dot com @ 2010-10-12  8:42 UTC (permalink / raw)
  To: gcc-bugs

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


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

end of thread, other threads:[~2023-05-15  5:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-12  8:42 [Bug target/45980] New: Use not in stead of add to generate new constant carrot at google dot com
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

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