Skip to contentSkip to conversation

lib_generic memcpy: copy one word at a time if possible

PATCH New Alessandro Rubini 4 participants 8 messages Oct 10, 2009 – Oct 18, 2009 4 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): OF174ADC43.E238E9B3-ONC125764B.0039E83E-C125764B.003AEEB3@trans mode.se

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

PATCH v4 1/3 lib_generic memcpy: copy one word at a time if possible

From: Alessandro Rubini <[email protected]>

If source and destination are aligned, this copies ulong values until possible, trailing part is copied by byte. Thanks for the details to Wolfgang Denk, Mike Frysinger, Peter Tyser, Chris Moore.

---
 lib_generic/string.c |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
lib_generic/string.c +15 -4
index 181eda6..61a45dc 100644
@@ -446,12 +446,23 @@ char * bcopy(const char * src, char * dest, int count)
  * You should not use this function to access IO space, use memcpy_toio()
  * or memcpy_fromio() instead.
  */
-void * memcpy(void * dest,const void *src,size_t count)
+void * memcpy(void *dest, const void *src, size_t count)
 {
-	char *tmp = (char *) dest, *s = (char *) src;
-
+	unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
+	char *d8, *s8;
+
+	/* while all data is aligned (common case), copy a word at a time */
+	if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
+		while (count >= sizeof(*dl)) {
+			*dl++ = *sl++;
+			count -= sizeof(*dl);
+		}
+	}
+	/* copy the reset one byte at a time */
+	d8 = (char *)dl;
+	s8 = (char *)sl;
 	while (count--)
-		*tmp++ = *s++;
+		*d8++ = *s8++;
 
 	return dest;
 }
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH v4 1/3 lib_generic memcpy: copy one word at a time if possible

Dear Alessandro Rubini,

Quoted 6 lines · In message <cceb84d6368125b2d1003ebced494b45ca2cf999.1255168028.git.rubini@…

From: Alessandro Rubini <[email protected]>

If source and destination are aligned, this copies ulong values until possible, trailing part is copied by byte. Thanks for the details to Wolfgang Denk, Mike Frysinger, Peter Tyser, Chris Moore.

Quoted 6 lines
---
 lib_generic/string.c |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Madness has no purpose.  Or reason.  But it may have a goal.
	-- Spock, "The Alternative Factor", stardate 3088.7
#4 lore raw

PATCH v4 2/3 lib_generic memset: fill one word at a time if possible

In reply to a message not in the archive

From: Alessandro Rubini <[email protected]>

If the destination is aligned, fill ulong values until possible. Then fill remaining part by byte.

---
 lib_generic/string.c |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)
lib_generic/string.c +19 -3
index 61a45dc..b375b81 100644
@@ -403,10 +403,26 @@ char *strswab(const char *s)
  */
 void * memset(void * s,int c,size_t count)
 {
-	char *xs = (char *) s;
-
+	unsigned long *sl = (unsigned long *) s;
+	unsigned long cl = 0;
+	char *s8;
+	int i;
+
+	/* do it one word at a time (32 bits or 64 bits) while possible */
+	if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
+		for (i = 0; i < sizeof(*sl); i++) {
+			cl <<= 8;
+			cl |= c & 0xff;
+		}
+		while (count >= sizeof(*sl)) {
+			*sl++ = cl;
+			count -= sizeof(*sl);
+		}
+	}
+	/* fill 8 bits at a time */
+	s8 = (char *)sl;
 	while (count--)
-		*xs++ = c;
+		*s8++ = c;
 
 	return s;
 }
Signature · 1 line
1.6.0.2

A message that is not in the archive (sent off-list or not yet received): ab1169840f2a99906a81d8512922f5911dc9c0f1.1255168028.git.rubini@ unipv.it

#6 lore raw

PATCH v4 2/3 lib_generic memset: fill one word at a time if possible

u-boot-bounces at lists.denx.de wrote on 10/10/2009 11:51:16:

Quoted 51 lines

From:

Alessandro Rubini <[email protected]>

To:

u-boot at lists.denx.de

Cc:

STEricsson_nomadik_linux at list.st.com, andrea.gallo at stericsson.com

Date:

10/10/2009 11:51

Subject:

[U-Boot] [PATCH V4 2/3] lib_generic memset: fill one word at a time if possible

Sent by:

u-boot-bounces at lists.denx.de

From: Alessandro Rubini <[email protected]>

If the destination is aligned, fill ulong values until possible. Then fill remaining part by byte.

---
 lib_generic/string.c |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)
lib_generic/string.c +5 -2
index 61a45dc..b375b81 100644
@@ -403,10 +403,26 @@ char *strswab(const char *s)
  */
 void * memset(void * s,int c,size_t count)
 {
-   char *xs = (char *) s;
-
+   unsigned long *sl = (unsigned long *) s;
+   unsigned long cl = 0;
+   char *s8;
+   int i;
+

lib_generic/string.c char *strswab(const char *s) · line 420

Quoted 10 lines
lib_generic/string.c +10 -0
+   /* do it one word at a time (32 bits or 64 bits) while possible */
+   if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
+      for (i = 0; i < sizeof(*sl); i++) {
+         cl <<= 8;
+         cl |= c & 0xff;
+      }
+      while (count >= sizeof(*sl)) {
+         *sl++ = cl;
+         count -= sizeof(*sl);
+      }

The above while can be slow if not the complier manages to turn into:

  for(wc = count / sizeof(*sl); wc; --wc) {
         *sl++ = cl;

}
count = count & (sizeof(*sl)-1);

even better would be:

  for(--sl, wc = count / sizeof(*sl); wc; --wc) {
         *++sl = cl;

}
++sl;
count = count & (sizeof(*sl)-1);
for those arch's that can do pre inc and load/store in one instruction.

Just figured I should mention it.

A message that is not in the archive (sent off-list or not yet received): OF174ADC43.E238E9B3-ONC125764B.0039E83E-C125764B.003AEEB3@tran smode.se

#8 lore raw

PATCH v4 2/3 lib_generic memset: fill one word at a time if possible

Quoted 1 line

The above while can be slow if not the complier manages to turn into:

But the compiler is able, and Wolfgang already told that in another thread. Yes, I checked my assembly. No, not all platforms and all versions. like Wolfgang.

I think you may have a point, but unless you have a measured misworking case to show, we'd better stop here.

/alessandro, who'll lift linux/arch/arm/lib/memcpy.S next time

#9 lore raw

PATCH v4 2/3 lib_generic memset: fill one word at a time if possible

In reply to Alessandro Rubini

Dear Alessandro Rubini,

Quoted 11 lines · In message <ab1169840f2a99906a81d8512922f5911dc9c0f1.1255168028.git.rubini@…

From: Alessandro Rubini <[email protected]>

If the destination is aligned, fill ulong values until possible. Then fill remaining part by byte.

---
 lib_generic/string.c |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"A witty saying proves nothing."                           - Voltaire
#10 lore raw

PATCH v4 3/3 lcd: remove '#if 0' 32-bit scroll, now memcpy does it

In reply to a message not in the archive

From: Alessandro Rubini <[email protected]>

---
 common/lcd.c |   21 ---------------------
 1 files changed, 0 insertions(+), 21 deletions(-)
common/lcd.c +0 -21
index dc8fea6..4e31618 100644
@@ -99,32 +99,11 @@ static int lcd_getfgcolor (void);
 
 static void console_scrollup (void)
 {
-#if 1
 	/* Copy up rows ignoring the first one */
 	memcpy (CONSOLE_ROW_FIRST, CONSOLE_ROW_SECOND, CONSOLE_SCROLL_SIZE);
 
 	/* Clear the last one */
 	memset (CONSOLE_ROW_LAST, COLOR_MASK(lcd_color_bg), CONSOLE_ROW_SIZE);
-#else
-	/*
-	 * Poor attempt to optimize speed by moving "long"s.
-	 * But the code is ugly, and not a bit faster :-(
-	 */
-	ulong *t = (ulong *)CONSOLE_ROW_FIRST;
-	ulong *s = (ulong *)CONSOLE_ROW_SECOND;
-	ulong    l = CONSOLE_SCROLL_SIZE / sizeof(ulong);
-	uchar  c = lcd_color_bg & 0xFF;
-	ulong val= (c<<24) | (c<<16) | (c<<8) | c;
-
-	while (l--)
-		*t++ = *s++;
-
-	t = (ulong *)CONSOLE_ROW_LAST;
-	l = CONSOLE_ROW_SIZE / sizeof(ulong);
-
-	while (l-- > 0)
-		*t++ = val;
-#endif
 }
 
 /*----------------------------------------------------------------------*/
Signature · 1 line
1.6.0.2
#11 lore raw

PATCH v4 3/3 lcd: remove '#if 0' 32-bit scroll, now memcpy does it

Dear Alessandro Rubini,

Quoted 7 lines · In message <a492794e122bec9e622a27c0b5cf895602ef1ffd.1255168028.git.rubini@…

From: Alessandro Rubini <[email protected]>

---
 common/lcd.c |   21 ---------------------
 1 files changed, 0 insertions(+), 21 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
If in any problem you find yourself doing an immense amount of  work,
the answer can be obtained by simple inspection.