From mboxrd@z Thu Jan 1 00:00:00 1970 From: Chris Moore Date: Mon, 12 Oct 2009 06:37:12 +0200 Subject: [U-Boot] [PATCH V3 1/3] lib_generic memcpy: copy one word at a time if possible In-Reply-To: <20091010074431.GA3732@mail.gnudd.com> References: <4AD0261B.1060501@free.fr> <20091009091220.GA3801@mail.gnudd.com> <20091010074431.GA3732@mail.gnudd.com> Message-ID: <4AD2B278.4010103@free.fr> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Hi Alessandro, Alessandro Rubini a ?crit : >>> + unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src; >>> > > >> Nitpick: Are you sure the casts are necessary here ? >> > > Without the one on src it complains because of "const". So I write > both for symetry. > Yes, of course, you and the compiler are absolutely right. Silly me, I completely overlooked the const :( To me casting away const is a cardinal sin (it rather defeats the object) and I find that the ease with which it can be done in C is frightening. So I feel obliged to (hopefully) correct my submission: void *memcpy(void *dest, const void *src, size_t count) { char *d8; const char *s8; unsigned long *dl = dest; const unsigned long *sl = src; /* while all data is aligned (common case), copy multiple bytes at a time */ if ( (((int)(long)dest | (int)(long)src) & (sizeof(*dl) - 1)) == 0) { while (count >= sizeof(*dl)) { *dl++ = *sl++; count -= sizeof(*dl); } } d8 = (char *)dl; s8 = (const char *)sl; /* copy any remaining data byte by byte */ while (count--) *d8++ = *s8++; return dest; } But this is still not even compile tested :( I never learn :( Cheers, Chris