public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marcel Cox" <marcel_cox@hotmail.com>
To: gcc@gcc.gnu.org
Subject: Re: Slow memcmp for aligned strings on Pentium 3
Date: Fri, 04 Apr 2003 18:58:00 -0000	[thread overview]
Message-ID: <b6kht5$4pr$1@main.gmane.org> (raw)
In-Reply-To: <Pine.LNX.4.44.0304040923350.912-300000@kevin-pc.atkinson.dhs.org>


"Kevin Atkinson" <kevina@gnu.org> wrote in message
news:Pine.LNX.4.44.0304040923350.912-300000@kevin-pc.atkinson.dhs.org...
> Here you go.  Still assume the memory is alligned:
>
> This is what I did:
>
> int cmpa2(const unsigned int * x, const unsigned int * y, size_t size)
> {
>   int i = 0;
>   size_t s = size / 4;
>   while (i < s && x[i] == y[i]) ++i;
>   size -= i * 4;
>   if (size == 0) return 0;
>   // hopefully if this is inline expanded when size is known
>   // the compiler can eliminate many of these conditionals
>   else if (size >= 4) { // if original size % 4 == 0 this should
>                         // always be the case
>     unsigned int xx = x[i], yy = y[i];
>     asm("bswap %0" : "+r"(xx));
>     asm("bswap %0" : "+r"(yy));
>     return xx - yy;
>   } else {
>     const unsigned char * xb = (const unsigned char *)(x + i);
>     const unsigned char * yb = (const unsigned char *)(y + i);
>     // if size is known at compile time then the compiler should be
>     // able to select the correct choice at compile time
>     switch (size) {
>     case 1:
>       return *xb - *yb;
>     case 2:
>       return ((xb[0] - yb[0]) <<  8) + (xb[1] - yb[1]);
>     case 3:
>       return ((xb[0] - yb[0]) << 16) + ((xb[1] - yb[1]) << 8)
>         + xb[2] - yb[2];}
>   }
> }

There is still a flaw in that you assume that the difference of 2 unsigned
integers will return the correct signed result. This will not work if for
instance in your "return xx - yy" statement, xx for instance is 0xf0000000
and yy is 0x100000. In that case, xx is clearly greater than yy, but the
difference is 0xe0000000 which cast to signed integer will be a negative
number and will indicate that xx is smaller than yy which is clearly wrong.

Marcel



  reply	other threads:[~2003-04-04 18:12 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-04 13:18 Roger Sayle
2003-04-04 14:51 ` Kevin Atkinson
2003-04-04 17:08 ` Kevin Atkinson
2003-04-04 18:58   ` Marcel Cox [this message]
2003-04-04 19:27     ` Kevin Atkinson
  -- strict thread matches above, loose matches on Subject: below --
2003-04-04 17:13 Jerry Quinn
2003-04-04 17:29 ` Kevin Atkinson
2003-04-06  5:54   ` Jerry Quinn
2003-04-06 22:17     ` Kevin Atkinson
2003-04-07 17:30       ` Jerry Quinn
2003-04-04 14:33 Bonzini
2003-04-04  6:22 Kevin Atkinson
2003-04-04  8:52 ` Zack Weinberg
2003-04-04 15:16   ` Kevin Atkinson

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='b6kht5$4pr$1@main.gmane.org' \
    --to=marcel_cox@hotmail.com \
    --cc=gcc@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).