Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!microsoft!jangr From: jangr@microsoft.UUCP (Jan Gray) Newsgroups: comp.arch Subject: Re: Endian wars Message-ID: <7@microsoft.UUCP> Date: 28 Jan 89 00:04:24 GMT References: <6133@columbia.edu> <5124@aldebaran.UUCP> Reply-To: jangr@microsoft.UUCP (Jan Gray) Organization: Microsoft Corp., Redmond WA Lines: 25 In article <5124@aldebaran.UUCP> jimp@cognos.UUCP (Jim Patterson) writes: >If you don't know how long the string is (as for strcmp and strncmp), >then you have to scan the string to find how long it is. For a >word-oriented algorithm to be effective here, you need an algorithm >which detects which byte of a word (if any) contains a NUL. > >I contend that there's no simple way to do this with integer >instructions; it's more effective to use byte-oriented instructions. > > has_NUL = ! (i & 0xff && i & 0xff00 && i & 0xff0000 && i & 0xff000000); This went around comp.arch a while ago. has_NUL = (((i-0x01010101)&~i)&0x80808080) != 0, e.g. "test if there were any borrows as a result of the bytewise subtracts" Using this trick on the '386, strlen on long strings can be made about 30% faster than using the dedicated string instruction "rep scasb"! (Except this will cause many instruction fetches that will keep your bus busy.) The 80960 has the SCANBYTE instruction, and the 29000 has CPBYTE, for just this sort of thing. Hmm. "0x80808080". I knew the 8080 was good for something... :-) Jan Gray uunet!microsoft!jangr Microsoft Corp., Redmond Wash. 206-882-8080