On 02/27/2014 11:37 AM, Paul Sandoz wrote:
/* * We want to compare only the first index where left[index] != right[index]. * This corresponds to the least significant nonzero byte in lw ^ rw, since lw * and rw are little-endian. Long.numberOfTrailingZeros(diff) tells us the least * significant nonzero bit, and zeroing out the first three bits of L.nTZ gives us the * shift to get that least significant nonzero byte. */ int n = Long.numberOfTrailingZeros(lw ^ rw) & ~0x7; return (int) (((lw >>> n) & UNSIGNED_MASK) - ((rw >>> n) & UNSIGNED_MASK));
This really depends on the microarchitecture, but for many current CPUs, conversion to big endian with Long::reverseBytes(long) will be faster (assuming that it's intrinsified).
This is not just a performance optimization, it can also be used to plug an alleged timing oracle:
<https://sourceware.org/ml/libc-alpha/2014-01/msg00763.html> -- Florian Weimer / Red Hat Product Security Team