mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Richard B. Johnson" <root@chaos.analogic.com>
To: Timothy Miller <miller@techsource.com>
Cc: Jeff Garzik <jgarzik@pobox.com>,
	Linus Torvalds <torvalds@transmeta.com>,
	Arjan van de Ven <arjanv@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [BK+PATCH] remove __constant_memcpy
Date: Fri, 18 Apr 2003 11:07:10 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.53.0304181023010.21446@chaos> (raw)
In-Reply-To: <3EA00C38.2080308@techsource.com>

On Fri, 18 Apr 2003, Timothy Miller wrote:

>
>
> Jeff Garzik wrote:
>
> [snip]
>
> >-		case 20:
> >-			*(unsigned long *)to = *(const unsigned long *)from;
> >-			*(1+(unsigned long *)to) = *(1+(const unsigned long *)from);
> >-			*(2+(unsigned long *)to) = *(2+(const unsigned long *)from);
> >-			*(3+(unsigned long *)to) = *(3+(const unsigned long *)from);
> >-			*(4+(unsigned long *)to) = *(4+(const unsigned long *)from);
> >-			return to;
> >-	}
> >+	if (n <= 128)
> >+		return __builtin_memcpy(to, from, n);
> >+
> > #define COMMON(x) \
> > __asm__ __volatile__( \
> > 	"rep ; movsl" \
> >
> >
>
> Ignorant questions since I haven't been following the discussion:  Does
> this work with unaligned copies?  Does it work well?  What's better,
> letting the CPU do realignment, or writing the code to do bit shifts so
> that both reads and writes are aligned?
>

Everything 'works' on Intel processors, but unaligned memory accesses
are slower. How slow, depends upon the CPU. The CPU will never do
'realignment' on its own. Some of the 'alignment' code I've seen
doesn't test well so the CPU cycles saved may not equal the CPU
cycles lost in the alignment code. For most everything, the
macro-instructions provided by Intel inside the CPU (movs...) work
"well enough". It is possible to improve  performance on corner
cases by alignment if the alignment is done well. Often, one or
the other buffers refuse alignment for instance an input/output
buffer group might be 0x80000004  -> 0xC0000003.  There is no way
to fix this.

I sure wish somebody looked at the generated code here:

  *(1+(unsigned long *)to) = *(1+(const unsigned long *)from);

It cannot possibly work better than a 'naked' rep-movsl.

By carefully aligning negative indexes it is possible to force
the 'C' compiler to generate efficient code, for instance:

    while(len >= 0x10)
    {
        in_ptr  += 0x10;
        out_ptr += 0x10;
        len     -= 0x10;
	__asm__ __volatile__(".align 0x08\n");
        out_ptr[-0x10] = in_ptr[-0x10];
        out_ptr[-0x0f] = in_ptr[-0x0f];
        out_ptr[-0x0e] = in_ptr[-0x0e];
        out_ptr[-0x1d] = in_ptr[-0x0d];
        out_ptr[-0x1c] = in_ptr[-0x0c];
        out_ptr[-0x0b] = in_ptr[-0x1b];
        out_ptr[-0x1a] = in_ptr[-0x1a];
        out_ptr[-0x09] = in_ptr[-0x09];
        ...etc..
    }
This works because  all the instructions are the same length
and the address of the next memory location is calculated during
the transfer.

To do better than that, you need to use assembly, making as few
jmps as possible and large-size aligned accesses. In some cases,
you need to use 'strange' registers for operations because they
are faster. For instance:

		movl	(%eax), %edx     # Get longword
		leal	0x04(%eax), %eax # Calculate next address
		movl	%edx, (%ebx)     # Destination

... This works faster than...

		movl	(%esi), %eax     # Get longword
		leal	0x04(%esi), %esi # Calculate next address
		movl	%eax, (%ebx)     # Destination

Exact same code, different registers. Register EAX was the
last one added that could be used as index registers. Apparently
it uses newer "technology". It is faster than the other index-
registers. 'leal' is faster than 'addl $4, %esi'. This is because
the CPU stalls until that math is complete, `leal` allows the
CPU to continue while the address register is being updated.

Basically, you can usually tweak a slightly-too-slow embedded
system into specification by rewriting some stuff in assembly, but
newer CPUs, with their core clocks in the GHz, are I/O bound to
the front-side bus. Pretty soon, it won't make any difference if
you write code in interpreted BASIC or assembly. The execution
speed will be about the same.


Cheers,
Dick Johnson
Penguin : Linux version 2.4.20 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.


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

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-17  0:57 Jeff Garzik
2003-04-17  1:04 ` Jeff Garzik
2003-04-17  2:06 ` Linus Torvalds
2003-04-17  8:46   ` Arjan van de Ven
2003-04-17  9:02     ` Roman Zippel
2003-04-17  9:04       ` Arjan van de Ven
2003-04-17  9:11         ` Jakub Jelinek
2003-04-17 16:07     ` Linus Torvalds
2003-04-17 19:07       ` Jeff Garzik
2003-04-17 19:19       ` Jeff Garzik
2003-04-17 19:54         ` Linus Torvalds
2003-04-17 23:49           ` Jeff Garzik
2003-04-17 23:52             ` Jeff Garzik
2003-04-17 23:59             ` Linus Torvalds
2003-04-18  0:29               ` H. Peter Anvin
2003-04-18  9:06             ` Arjan van de Ven
2003-04-18 14:31             ` Timothy Miller
2003-04-18 15:07               ` Richard B. Johnson [this message]
2003-04-17 22:58         ` J.A. Magallon
2003-04-17 23:10           ` Jeff Garzik
2003-04-17 13:17 ` Alan Cox
2003-04-17 13:17 ` Alan Cox
2003-04-17 14:32   ` Jeff Garzik
2003-04-17 14:40     ` Jeff Garzik
2003-04-17 20:01   ` H. Peter Anvin
2003-04-17  2:22 Nakajima, Jun
2003-04-17 23:50 Chuck Ebbert

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=Pine.LNX.4.53.0304181023010.21446@chaos \
    --to=root@chaos.analogic.com \
    --cc=arjanv@redhat.com \
    --cc=jgarzik@pobox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miller@techsource.com \
    --cc=torvalds@transmeta.com \
    /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

all inboxes | Powered by JetHome®