Skip to content

Commit 092309b

Browse files
committed
Fix global optimization bug for ChaCha AVX2 under VS2017 (GH #735)
Also see #649. The 649 issue is the one affecting AES. It appears to be the same problem.
1 parent af9fb9d commit 092309b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

chacha-avx.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,21 @@ extern const char CHACHA_AVX_FNAME[] = __FILE__;
3636
# define MAYBE_CONST const
3737
#endif
3838

39-
#if (CRYPTOPP_AVX2_AVAILABLE)
39+
// VS2017 and global optimization bug. TODO, figure out when
40+
// we can re-enable full optimizations for VS2017. Also see
41+
// https://github.com/weidai11/cryptopp/issues/649 and
42+
// https://github.com/weidai11/cryptopp/issues/735. The
43+
// 649 issue affects AES but it is the same here. The 735
44+
// issue is ChaCha AVX2 cut-in where it surfaced again.
45+
#if (_MSC_VER >= 1910) && defined(NDEBUG)
46+
# pragma optimize("", off)
47+
# pragma optimize("ts", on)
48+
#endif
4049

4150
ANONYMOUS_NAMESPACE_BEGIN
4251

52+
#if (CRYPTOPP_AVX2_AVAILABLE)
53+
4354
template <unsigned int R>
4455
inline __m256i RotateLeft(const __m256i val)
4556
{
@@ -62,10 +73,14 @@ inline __m256i RotateLeft<16>(const __m256i val)
6273
return _mm256_shuffle_epi8(val, mask);
6374
}
6475

76+
#endif CRYPTOPP_AVX2_AVAILABLE
77+
6578
ANONYMOUS_NAMESPACE_END
6679

6780
NAMESPACE_BEGIN(CryptoPP)
6881

82+
#if (CRYPTOPP_AVX2_AVAILABLE)
83+
6984
void ChaCha_OperateKeystream_AVX2(const word32 *state, const byte* input, byte *output, unsigned int rounds)
7085
{
7186
MAYBE_CONST __m128i* state_mm = (MAYBE_CONST __m128i*)(state);
@@ -358,6 +373,6 @@ void ChaCha_OperateKeystream_AVX2(const word32 *state, const byte* input, byte *
358373
}
359374
}
360375

361-
NAMESPACE_END
362-
363376
#endif // CRYPTOPP_AVX2_AVAILABLE
377+
378+
NAMESPACE_END

rijndael.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ being unloaded from L1 cache, until that round is finished.
8888
#include "misc.h"
8989
#include "cpu.h"
9090

91-
// MSVC bug, still don't know how to fix it. TODO, figure out
92-
// when we can re-enable optimizations for MSVC. Also see
91+
// VS2017 and global optimization bug. TODO, figure out when
92+
// we can re-enable full optimizations for VS2017. Also see
9393
// https://github.com/weidai11/cryptopp/issues/649
94-
#if defined(_MSC_VER) && (_MSC_VER >= 1910)
94+
#if (_MSC_VER >= 1910) && defined(NDEBUG)
9595
# pragma optimize("", off)
9696
# pragma optimize("ts", on)
9797
#endif

0 commit comments

Comments
 (0)