On 10/22/2015 09:47 AM, Bernhard Voelker wrote: >> Also I suspect the extra conditions involved in using longs >> for just the first 16 bytes would outweigh the benefits? >> I.E. the first simple loop probably breaks early, and if not >> has the added benefit of "priming the pumps" for the subsequent memcmp(). > > what about spending some 16 bytes of memory and do the memcmp on the whole > buffer? > > static unsigned char p[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; > return 0 == memcmp (p, buf, bufsize);
Won't work over the whole bufsize for anything larger than 16 unless you do repeated memcmp()s. Or are you suggesting that the first 16-byte head validation be done against a static buffer via one memcmp(), followed by the other overlap-self memcmp() for the rest of the buffer? But I suspect that for short lengths, it is more efficient to do an unrolled loop than to make a function call (where the function call itself will probably just do an unrolled loop on the short length). You want the short case to be fast, and the real speedup comes by delegating as much of the long case as possible to the system memcmp() optimizations. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature