From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/3837 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: ARM memcpy post-0.9.12-release thread Date: Mon, 5 Aug 2013 17:24:27 -0400 Message-ID: <20130805212426.GN221@brightrain.aerifal.cx> References: <20130731022631.GA6655@brightrain.aerifal.cx> <20130802204146.GO221@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="neYutvxvOLaeuPCA" X-Trace: ger.gmane.org 1375737882 11241 80.91.229.3 (5 Aug 2013 21:24:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 5 Aug 2013 21:24:42 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-3841-gllmg-musl=m.gmane.org@lists.openwall.com Mon Aug 05 23:24:45 2013 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1V6SGe-0005Ht-U5 for gllmg-musl@plane.gmane.org; Mon, 05 Aug 2013 23:24:40 +0200 Original-Received: (qmail 13496 invoked by uid 550); 5 Aug 2013 21:24:40 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 13488 invoked from network); 5 Aug 2013 21:24:39 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:3837 Archived-At: --neYutvxvOLaeuPCA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Aug 03, 2013 at 10:03:14AM +1200, Andre Renaud wrote: > Hi Rich, > > On 3 August 2013 08:41, Rich Felker wrote: > > Andre, do you have any input on this? (Cc'ing) > > > > Rich > > Sorry, I've been reading the emails, but haven't had a chance to get > back to the code. I don't really have an opinion on the gcc memcpy > issue, however I was still hopeful that we could come up with a > relatively clean mixed C/asm solution for the misaligned/non-congruent > copy scenario. Having said that, I haven't done anything on it yet. > > To be honest, although a solution probably exists, I doubt it's ever > going to be much better than the bionic code (with the exception of > possibly being less to read). Attached is a "C version of the concept in the Bionic asm". Without spending any effort getting the compiler to optimize it better, it's only taking about 80% longer than the asm to run on misaligned input. I would say something like this is _potentially_ a candidate for replacing memcpy.c in musl, since it should do well on most RISC architectures (and does decently even on x86). Of course this is no replacement for the asm, as it's much slower, but it would allow us to get by longer without adding asm for new archs. The aligned case should probably be changed to use structure copies if we can be sure they won't generate calls to memcpy. Rich --neYutvxvOLaeuPCA Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="memcpy_risc.c" #include #include #include void *memcpy(void *restrict dest, const void *restrict src, size_t n) { unsigned char *d = dest; const unsigned char *s = src; uint32_t w, x; for (; (uintptr_t)s % 8 && n; n--) *d++ = *s++; if (!n) return dest; if (n>=4) switch ((uintptr_t)d % 4) { case 0: if (!(uintptr_t)d % 8) for (; n>=8; s+=8, d+=8, n-=8) *(uint64_t *)d = *(uint64_t *)s; else for (; n>=4; s+=4, d+=4, n-=4) *(uint32_t *)d = *(uint32_t *)s; break; case 1: if (!(union { int i; char c; }){1}.c) break; w = *(uint32_t *)s; *d++ = *s++; *d++ = *s++; *d++ = *s++; n -= 3; for (; n>=17; s+=16, d+=16, n-=16) { x = *(uint32_t *)(s+1); *(uint32_t *)(d+0) = (w>>24) | (x<<8); w = *(uint32_t *)(s+5); *(uint32_t *)(d+4) = (x>>24) | (w<<8); x = *(uint32_t *)(s+9); *(uint32_t *)(d+8) = (w>>24) | (x<<8); w = *(uint32_t *)(s+13); *(uint32_t *)(d+12) = (x>>24) | (w<<8); } break; case 2: if (!(union { int i; char c; }){1}.c) break; w = *(uint32_t *)s; *d++ = *s++; *d++ = *s++; n -= 2; for (; n>=18; s+=16, d+=16, n-=16) { x = *(uint32_t *)(s+2); *(uint32_t *)(d+0) = (w>>16) | (x<<16); w = *(uint32_t *)(s+6); *(uint32_t *)(d+4) = (x>>16) | (w<<16); x = *(uint32_t *)(s+10); *(uint32_t *)(d+8) = (w>>16) | (x<<16); w = *(uint32_t *)(s+14); *(uint32_t *)(d+12) = (x>>16) | (w<<16); } break; case 3: if (!(union { int i; char c; }){1}.c) break; w = *(uint32_t *)s; *d++ = *s++; n -= 1; for (; n>=19; s+=16, d+=16, n-=16) { x = *(uint32_t *)(s+3); *(uint32_t *)(d+0) = (w>>8) | (x<<24); w = *(uint32_t *)(s+7); *(uint32_t *)(d+4) = (x>>8) | (w<<24); x = *(uint32_t *)(s+11); *(uint32_t *)(d+8) = (w>>8) | (x<<24); w = *(uint32_t *)(s+15); *(uint32_t *)(d+12) = (x>>8) | (w<<24); } break; } for (; n; n--) *d++ = *s++; return dest; } --neYutvxvOLaeuPCA--