Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!seismo!mcvax!ukc!hrc63!miduet!adam From: adam@miduet.UUCP Newsgroups: comp.arch,comp.lang.c Subject: Re: String Handling -- Incompetence of run-time libraries Message-ID: <531@gec-mi-at.co.uk> Date: Fri, 10-Apr-87 04:43:48 EST Article-I.D.: gec-mi-a.531 Posted: Fri Apr 10 04:43:48 1987 Date-Received: Wed, 15-Apr-87 00:48:53 EST References: <15292@amdcad.UUCP> <978@ames.UUCP> <15694@sun.uucp> Reply-To: adam@gec-mi-at.co.uk (Adam Quantrill) Organization: Marconi Instruments Ltd., St. Albans, Herts, UK Lines: 35 Keywords: instruction set architectures, strcpy Xref: utgpu comp.arch:854 comp.lang.c:1592 In article <9996@sri-spam.istc.sri.com> robert@sri-spam.UUCP (Robert Allen) writes: > >To further speed this up you could write it as: > > strcpy2(ss1,ss2) > register char *ss1, *ss2; > { > while (*s1++ = *s2++) > ; > } > >thus eliminating two assignments and two local variables. > If your machine doesn't complain at char-int alignment, try strcmp2(s1, s2) register short int *s1, *s2; { while (*s1 == *s2++) if (!((*s1++ & 255) && (*(char *)s1))) return(0); return(*s1 - *--s2); } This relies on the fact that it doesn't matter if you compare the NULLs at the end of two equal strings or not. We go along the string twice as fast because of the 16 bit operations, although there is a slight penalty due to the extra test. The alignment problem only comes in if either of the strings begins on an odd boundary, which is fairly unusual anyway. Also watch the last part of the 'if' condition - this won't work on some machines, you may have to add 1 to the pointer. -- -Adam. /* If at first it don't compile, kludge, kludge again.*/