Used CRYPTOPP_UNLIKELY on gf2n.cpp hotspot

pull/326/head
Jeffrey Walton 2016-09-27 20:58:17 -04:00
parent 7ae1267673
commit 6bf0d32279
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
2 changed files with 11 additions and 5 deletions

View File

@ -689,7 +689,13 @@ const GF2NT::Element& GF2NT::MultiplicativeInverse(const Element &a) const
// temp ^= ((temp >> j) & 1) << ((t1 + j) & (sizeof(temp)*8-1)); // temp ^= ((temp >> j) & 1) << ((t1 + j) & (sizeof(temp)*8-1));
if (t1 < WORD_BITS) if (t1 < WORD_BITS)
for (unsigned int j=0; j<WORD_BITS-t1; j++) for (unsigned int j=0; j<WORD_BITS-t1; j++)
temp ^= ((temp >> j) & 1) << (t1 + j); {
// Coverity finding on shift amount of 'word x << (t1+j)'.
// temp ^= ((temp >> j) & 1) << (t1 + j);
const unsigned int shift = t1 + j;
CRYPTOPP_ASSERT(shift < WORD_BITS);
temp ^= (CRYPTOPP_UNLIKELY(shift >= WORD_BITS) ? 0 : ((temp >> j) & 1) << shift);
}
else else
b[t1/WORD_BITS-1] ^= temp << t1%WORD_BITS; b[t1/WORD_BITS-1] ^= temp << t1%WORD_BITS;
@ -718,10 +724,9 @@ const GF2NT::Element& GF2NT::MultiplicativeInverse(const Element &a) const
{ {
// Coverity finding on shift amount of 'word x << (t1+j)'. // Coverity finding on shift amount of 'word x << (t1+j)'.
// temp ^= ((temp >> j) & 1) << (t1 + j); // temp ^= ((temp >> j) & 1) << (t1 + j);
CRYPTOPP_ASSERT(t1+j < WORD_BITS);
const unsigned int shift = t1 + j; const unsigned int shift = t1 + j;
temp ^= ((shift >= WORD_BITS) ? 0 : ((temp >> j) & 1) << shift); CRYPTOPP_ASSERT(shift < WORD_BITS);
temp ^= (CRYPTOPP_UNLIKELY(shift >= WORD_BITS) ? 0 : ((temp >> j) & 1) << shift);
} }
} }
else else

1
misc.h
View File

@ -118,6 +118,7 @@ class Integer;
// Micro-optimization, use juditiously. Be sure you find a hotspot // Micro-optimization, use juditiously. Be sure you find a hotspot
// using 'make coverage', and its in a tight loop. Otherwise, DFW. // using 'make coverage', and its in a tight loop. Otherwise, DFW.
// Also see http://www.akkadia.org/drepper/cpumemory.pdf
#if defined(__GNUC__) #if defined(__GNUC__)
# define CRYPTOPP_LIKELY(x) __builtin_expect(!!(x), 1) # define CRYPTOPP_LIKELY(x) __builtin_expect(!!(x), 1)
# define CRYPTOPP_UNLIKELY(x) __builtin_expect(!!(x), 0) # define CRYPTOPP_UNLIKELY(x) __builtin_expect(!!(x), 0)