make memcpy and memset 32-bit copies
cover make memcpy and memset 32-bit copies
I've added 32-bit lcd to the Nomadik (not submitted yet), and I found the scroll to be very slow, as the screen is big.
Instead of activating the "if 0" stanza for 32-bit scroll in lcd.c, I'd better have a faster memcpy/memset globally. So this patch set adds 32-bit memcpy and memset and removes the "#if 0" part in the scroll function. For me it's 4 times faster ("help" from 14s to 3.5s).
I agree I should use 8-bits in u-boot, but the speedup of 32bit memcpy/memset is there regardless, as most users are 32-bit aligned anyways.
Alessandro Rubini (3):
memcpy: use 32-bit copies if possible
memset: use 32-bit copies if possible
lcd: remove '#if 0' 32-bit scroll, now memcpy does it
common/lcd.c | 21 --------------------- lib_generic/string.c | 27 +++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 23 deletions(-)
PATCH 1/3 memcpy: use 32-bit copies if possible
From: Alessandro Rubini <[email protected]>
--- lib_generic/string.c | 11 ++++++++++- 1 files changed, 10 insertions(+), 1 deletions(-)
lib_generic/string.c +9 -0
@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count)void *memcpy (void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src; +/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) { + count /=4 ; +while (count--) + *d32++ = *s32++; +return dest; + } +/* else, use 1-byte copy */ while (count--) *tmp++ = *s++;
--
1.5.6.5
PATCH 1/3 memcpy: use 32-bit copies if possible
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 455
Quoted 10 lines · On Wednesday 07 October 2009 04:44:26 Alessandro Rubini wrote:
lib_generic/string.c +3
-0@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count)void *memcpy (void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src; +/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) {
while 64bit isnt in today, might as well avoid unclean code from the start when possible. in other words, used "unsigned int" rather than "u32" and cast to "unsigned long" rather than "int".
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 456
Quoted 1 line
lib_generic/string.c +1
-0+ count /=4 ;
count >>= 2 ? although gcc probably optimizes this properly.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20091007/05b69532/attachment.pgp
PATCH 1/3 memcpy: use 32-bit copies if possible
Quoted 3 lines
while 64bit isnt in today, might as well avoid unclean code from the start when possible. in other words, used "unsigned int" rather than "u32" and cast to "unsigned long" rather than "int".
You are right. Will do.
Quoted 3 lines
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 456
Quoted 1 line
lib_generic/string.c +1
-0+ count /=4 ;count >>= 2 ? although gcc probably optimizes this properly.
gcc-4.0 for arm already turns it into a shift. I'll use "sizeof(unsigned long)" in version 2.
Thanks
/alessandro
PATCH 1/3 memcpy: use 32-bit copies if possible
In reply to Mike FrysingerDear Mike Frysinger,
Quoted 16 lines · In message <[email protected]> you wrote:
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 455
Quoted 11 lines
lib_generic/string.c +3
-0@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count)void *memcpy (void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src; +/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) {while 64bit isnt in today, might as well avoid unclean code from the start when possible. in other words, used "unsigned int" rather than "u32" and cast to "unsigned long" rather than "int".
Quoted 3 lines
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 456
Quoted 1 line
lib_generic/string.c +1
-0+ count /=4 ;count >>= 2 ? although gcc probably optimizes this properly.
Neither one nor the other.
This should become
count /= sizof(int);
then.
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 When all is said and done, more is said than done.
PATCH 1/3 memcpy: use 32-bit copies if possible
In reply to Mike FrysingerI was making my v2, and I found a problem wrt:
Quoted 3 lines
while 64bit isnt in today, might as well avoid unclean code from the start when possible. in other words, used "unsigned int" rather than "u32" and cast to "unsigned long" rather than "int".
Since int is 32 also on 64bit systems, I used unsigned long. For memcpy all is well, for memset I have this problem:
void * memset(void * s,int c,size_t count)
{
char *xs = (char *) s;
unsigned long *sl = (unsigned long *) s;
unsigned long cl;
/* do it one word at a time (32 bits or 64 bits) if possible */
if ( ((count | (int)s) & (sizeof(long) - 1)) == 0) {
count /= sizeof(long);
cl = (c & 0xff) | ((c & 0xff) << 8);
cl |= cl << 16;
if (sizeof(long) > 4)
cl |= cl << 32;
while (count--)
*sl++ = cl;
return s;
}
/* else, fill 8 bits@a time */
while (count--)
*xs++ = c;
return s;
}string.c:416: warning: left shift count >= width of type
(obviously there is no such shift in the generated code, since the condition is false at compile time).
I think I'll stick to u32 for memset, unless I get better suggestions.
/alessandro
PATCH 1/3 memcpy: use 32-bit copies if possible
Dear Alessandro Rubini,
Quoted 2 lines · In message <[email protected]> you wrote:
Since int is 32 also on 64bit systems, I used unsigned long.
Note that this is not guaranteed, though. It could be 64 bit as well.
Quoted 7 lines
/* do it one word at a time (32 bits or 64 bits) if possible */
if ( ((count | (int)s) & (sizeof(long) - 1)) == 0) {
count /= sizeof(long);
cl = (c & 0xff) | ((c & 0xff) << 8);
cl |= cl << 16;
if (sizeof(long) > 4)
cl |= cl << 32;How about:
cl = 0;
for (i=0; i<sizeof(long); ++i) {
cl <<= 8;
cl |= c & 0xff;
}GCC optimization will do the rest...
Best regards,
Wolfgang Denk
Signature · 6 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 I don't document something, it's usually either for a good reason,
or a bad reason. In this case it's a good reason. :-)
- Larry Wall in <[email protected]>PATCH 1/3 memcpy: use 32-bit copies if possible
Quoted 20 lines
Dear Alessandro Rubini,
Quoted 2 lines · In message <[email protected]> you wrote:
Since int is 32 also on 64bit systems, I used unsigned long.
Note that this is not guaranteed, though. It could be 64 bit as well.
Quoted 7 lines
/* do it one word at a time (32 bits or 64 bits) if possible */ if ( ((count | (int)s) & (sizeof(long) - 1)) == 0) { count /= sizeof(long); cl = (c & 0xff) | ((c & 0xff) << 8); cl |= cl << 16; if (sizeof(long) > 4) cl |= cl << 32;How about:
Quoted 7 lines
cl = 0; for (i=0; i<sizeof(long); ++i) { cl <<= 8; cl |= c & 0xff; }GCC optimization will do the rest...
If you want gcc to optimise well, make it easy to do so. Changing the for loop into:
for (i=sizeof(long); i; --i)
makes it easier for gcc and more likely to result in optimal code.
Similarly for copy ops
for(--top,--fromp, i = len/4; i; --i)
*++top = *++fromp; /* pre incr can be one op on some archs */
Jocke
PATCH 1/3 memcpy: use 32-bit copies if possible
Dear Joakim Tjernlund,
Quoted 9 lines · In message <OF26FEA2CB.36E102EE-ONC1257649.0031301D-C1257649.0031EDAF@transmode…
Quoted 9 lines
How about:
cl = 0; for (i=0; i<sizeof(long); ++i) { cl <<= 8; cl |= c & 0xff; }
Quoted 6 lines
Quoted 1 line
GCC optimization will do the rest...
If you want gcc to optimise well, make it easy to do so. Changing the for loop into:
for (i=sizeof(long); i; --i)makes it easier for gcc and more likely to result in optimal code.
Did you actually _check_ the code? (I did).
It does not matter. The generated code is identical.
What matters might be compiler options - "-Os" generates a small loop, and "-O3" and higher will unroll the loop, which is way more code.
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 When you die, the first thing you lose is your life. The next thing is the illusions. - Terry Pratchett, _Pyramids_
PATCH 1/3 memcpy: use 32-bit copies if possible
Wolfgang Denk <[email protected]> wrote on 08/10/2009 14:49:15:
Quoted 14 lines
Dear Joakim Tjernlund,
In message <OF26FEA2CB.36E102EE-ONC1257649.0031301D-C1257649.
Quoted 9 lines · 0031EDAF at transmode.se> you wrote:
Quoted 9 lines
How about:
cl = 0; for (i=0; i<sizeof(long); ++i) { cl <<= 8; cl |= c & 0xff; }
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
GCC optimization will do the rest...
If you want gcc to optimise well, make it easy to do so. Changing the for loop into:
for (i=sizeof(long); i; --i)makes it easier for gcc and more likely to result in optimal code.
Did you actually _check_ the code? (I did).
No, but I have done this in the past and found that gcc does stupid things sometimes, especially loops, and it depends on arch and gcc version.
Quoted 2 lines
It does not matter. The generated code is identical.
So my question is: Did you check all gcc versions and arches?
Jocke
PATCH 1/3 memcpy: use 32-bit copies if possible
Dear Joakim Tjernlund,
Quoted 3 lines · In message <OF15EDFD3F.769F0248-ONC1257649.0049C8AE-C1257649.004A1776@transmode…
So my question is: Did you check all gcc versions and arches?
Of course not :-)
Best regards,
Wolfgang Denk
Signature · 6 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 Even if you can deceive people about a product through misleading statements, sooner or later the product will speak for itself. - Hajime Karatsu
PATCH 1/3 memcpy: use 32-bit copies if possible
In reply to Alessandro RubiniQuoted 11 lines · Alessandro Rubini
For memcpy all is well, for memset I have this problem:
if (sizeof(long) > 4) cl |= cl << 32;string.c:416: warning: left shift count >= width of type
(obviously there is no such shift in the generated code, since the condition is false at compile time).
I think I'll stick to u32 for memset, unless I get better suggestions.
What about
if (sizeof(long) > 4)
cl |= (unsigned long long)cl << 32;?
Eric
PATCH 1/3 memcpy: use 32-bit copies if possible
In reply to Alessandro RubiniHi Alessandro,
Quoted 8 lines
lib_generic/string.c +1
-0@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count)void *memcpy (void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src;
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 463
Quoted 10 lines
lib_generic/string.c +8
-0+/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) { + count /=4 ; +while (count--) + *d32++ = *s32++; +return dest; + } +/* else, use 1-byte copy */ while (count--) *tmp++ = *s++;
If we're adding this logic, what about adding it such that:
if (src/dest are 32-bit aligned and count > 3) {
perform 32-bit copies till count <= 3
}
perform remaining 8-bit copies till count == 0
You'd still get the performance boost but not have the requirement that count is evenly divisible by 4. You could do byte copies before the 32-bit copies to align the src/dest in some cases, but that might be overkill...
Same comment goes for the memset implementation.
Best,
Peter
PATCH 1/3 memcpy: use 32-bit copies if possible
Quoted 26 lines · On Wednesday 07 October 2009 10:35:19 Peter Tyser wrote:
Quoted 8 lines
lib_generic/string.c +1
-0@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count) void * memcpy(void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src;lib_generic/string.c char * bcopy(const char * src, char * dest, int · line 463
Quoted 10 lines
lib_generic/string.c +8
-0+/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) { + count /=4 ; +while (count--) + *d32++ = *s32++; +return dest; + } +/* else, use 1-byte copy */ while (count--) *tmp++ = *s++;If we're adding this logic, what about adding it such that:
if (src/dest are 32-bit aligned and count > 3) {
perform 32-bit copies till count <= 3
}
perform remaining 8-bit copies till count == 0
Quoted 4 lines
You'd still get the performance boost but not have the requirement that count is evenly divisible by 4. You could do byte copies before the 32-bit copies to align the src/dest in some cases, but that might be overkill...
i thought the same but didnt feel like sending out another e-mail ;)
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20091007/10ed4a62/attachment.pgp
PATCH 2/3 memset: use 32-bit copies if possible
In reply to Alessandro RubiniFrom: Alessandro Rubini <[email protected]>
--- lib_generic/string.c | 16 +++++++++++++++- 1 files changed, 15 insertions(+), 1 deletions(-)
lib_generic/string.c +14 -0
@@ -404,7 +404,21 @@ char *strswab(const char *s)void *memset (void * s,int c,size_t count) {char *xs = (char *) s; +u32 *s32 = (u32 *) s; +int c32 =0 ;/* most common case */ +/* do it 32 bits at a time if possible */ +if ( ((count &3 ) | ((int )s &3 )) ==0 ) { + count /=4 ; +if (c) {/* not 0: build 32-bit value */ + c32 = c | (c<<8 ); + c32 |= c32 <<16 ; + } +while (count--) + *s32++ = c32; +return s; + } +/* else, fill 8 bits@a time */ while (count--) *xs++ = c;
Signature · 1 line
1.5.6.5
PATCH 2/3 memset: use 32-bit copies if possible
Dear Alessandro Rubini,
Quoted 4 lines · In message <d10837d7edf963ce9b0e87773c9771e451f9f9ac.1254904388.git.rubini@…
From: Alessandro Rubini <[email protected]>
Signed-off-by Alessandro Rubini <[email protected]>Acked-by Andrea Gallo <[email protected]>
Please fix the Subject: memset() does not copy data.
Quoted 10 lines
lib_generic/string.c | 16 +++++++++++++++- 1 files changed, 15 insertions(+), 1 deletions(-)lib_generic/string.c +0
-0@@ -404,7 +404,21 @@ char *strswab(const char *s)void *memset (void * s,int c,size_t count) {
lib_generic/string.c char *strswab(const char *s) · line 415
Quoted 10 lines
lib_generic/string.c +8
-0char *xs = (char *) s; +u32 *s32 = (u32 *) s; +int c32 =0 ;/* most common case */ +/* do it 32 bits at a time if possible */ +if ( ((count &3 ) | ((int )s &3 )) ==0 ) { + count /=4 ; +if (c) {/* not 0: build 32-bit value */ + c32 = c | (c<<8 ); + c32 |= c32 <<16 ;
I reject this code, as it changes behaviour. You may argument that it's only for special cases, but anyway:
Fill pattern for standard memset() for your code:
c = -2 0xFEFEFEFE 0xFFFFFFFE
c = -3 0xFDFDFDFD 0xFFFFFFFD
...
c = -2147483648 0x00000000 0x80000000
lib_generic/string.c char *strswab(const char *s) · line 416
Quoted 1 line
lib_generic/string.c +1
-0+ }
I suggest to omit the special case for zero and handle it just the same. This results in smaller code size and is easier to read.
lib_generic/string.c char *strswab(const char *s)
Quoted 5 lines
lib_generic/string.c +5
-0+while (count--) + *s32++ = c32; +return s; + } +/* else, fill 8 bits at a time */
Blank line here, please.
lib_generic/string.c char *strswab(const char *s) · line 423
Quoted 2 lines
lib_generic/string.c +0
-0while (count--) *xs++ = c;
Please fix and resubmit.
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 God made machine language; all the rest is the work of man.
PATCH 3/3 lcd: remove '#if 0' 32-bit scroll, now memcpy does it
In reply to Alessandro RubiniFrom: Alessandro Rubini <[email protected]>
--- common/lcd.c | 21 --------------------- 1 files changed, 0 insertions(+), 21 deletions(-)
common/lcd.c +0 -21
@@ -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.5.6.5
A message that is not in the archive (sent off-list or not yet received): b03f28d4c4afa0c5c9f2421b7d260a0f3890cdcd.1254904388.git.rubini@ unipv.it
PATCH 1/3 memcpy: use 32-bit copies if possible
Alessandro Rubini a ?crit :
Quoted 13 lines
From: Alessandro Rubini <[email protected]>
Signed-off-by Alessandro Rubini <[email protected]>Acked-by Andrea Gallo <[email protected]>--- lib_generic/string.c | 11 ++++++++++- 1 files changed, 10 insertions(+), 1 deletions(-)lib_generic/string.c +0
-0
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 456
Quoted 8 lines
lib_generic/string.c +3
-0@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count)void *memcpy (void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src; +/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) {
This can be factorized as (a & 3) | (b & 3) | (c & 3) is equivalent to (a | b | c) & 3.
GCC is pretty smart but I doubt that it will pick this up :(
lib_generic/string.c char * bcopy(const char * src, char * dest, int count) · line 464
Quoted 10 lines
lib_generic/string.c +6
-0+ count /=4 ; +while (count--) + *d32++ = *s32++; +return dest; + } +/* else, use 1-byte copy */ while (count--) *tmp++ = *s++;
Cheers,
Chris
PATCH 1/3 memcpy: use 32-bit copies if possible
Quoted 9 lines · On Thursday 08 October 2009 01:23:05 Chris Moore wrote:
Alessandro Rubini a ?crit :
Quoted 8 lines
lib_generic/string.c +1
-0@@ -449,7 +449,16 @@ char * bcopy(const char * src, char * dest, int count) void * memcpy(void * dest,const void *src,size_t count) {char *tmp = (char *) dest, *s = (char *) src; +u32 *d32 = (u32 *)dest, *s32 = (u32 *) src;
Quoted 6 lines
lib_generic/string.c char * bcopy(const char * src, char * dest, int · line 455
Quoted 2 lines
lib_generic/string.c +2
-0+/* if both are aligned, use 32-bit copy */ +if ( (((int )dest &3 ) | ((int )src &3 ) | (count &3 )) ==0 ) {This can be factorized as (a & 3) | (b & 3) | (c & 3) is equivalent to (a | b | c) & 3.
GCC is pretty smart but I doubt that it will pick this up :(
last time i looked (gcc-4.1 and gcc-4.3), it worked fine
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20091008/6b8468e2/attachment.pgp