From 6bf0d322794b2d09a9caf71430af64c81f96a1fd Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 27 Sep 2016 20:58:17 -0400 Subject: [PATCH] Used CRYPTOPP_UNLIKELY on gf2n.cpp hotspot --- gf2n.cpp | 15 ++++++++++----- misc.h | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/gf2n.cpp b/gf2n.cpp index 6c3461c0..f7c7b8b5 100644 --- a/gf2n.cpp +++ b/gf2n.cpp @@ -689,7 +689,13 @@ const GF2NT::Element& GF2NT::MultiplicativeInverse(const Element &a) const // temp ^= ((temp >> j) & 1) << ((t1 + j) & (sizeof(temp)*8-1)); if (t1 < WORD_BITS) for (unsigned int j=0; j> 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 b[t1/WORD_BITS-1] ^= temp << t1%WORD_BITS; @@ -717,11 +723,10 @@ const GF2NT::Element& GF2NT::MultiplicativeInverse(const Element &a) const for (unsigned int j=0; j> j) & 1) << (t1 + j); - - CRYPTOPP_ASSERT(t1+j < WORD_BITS); + // temp ^= ((temp >> j) & 1) << (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 diff --git a/misc.h b/misc.h index 24e441e7..72d14be1 100644 --- a/misc.h +++ b/misc.h @@ -118,6 +118,7 @@ class Integer; // Micro-optimization, use juditiously. Be sure you find a hotspot // using 'make coverage', and its in a tight loop. Otherwise, DFW. +// Also see http://www.akkadia.org/drepper/cpumemory.pdf #if defined(__GNUC__) # define CRYPTOPP_LIKELY(x) __builtin_expect(!!(x), 1) # define CRYPTOPP_UNLIKELY(x) __builtin_expect(!!(x), 0)