From aa043b38a7930725c31a0cd7016986d1c581c573 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 20 Jan 2019 22:10:36 -0500 Subject: [PATCH] Use OpenMP 2.0 for MSC compilers (GH #787) --- cryptest.nmake | 1 + salsa.cpp | 5 +++++ scrypt.cpp | 16 ++++++++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cryptest.nmake b/cryptest.nmake index fa71635a..1c547ed3 100644 --- a/cryptest.nmake +++ b/cryptest.nmake @@ -155,6 +155,7 @@ LDLIBS = # CXXFLAGS = $(CXXFLAGS) /DDEBUG /D_DEBUG /Oi /Oy- /Od /MTd # Release build. Add /OPT:REF to linker CXXFLAGS = $(CXXFLAGS) /DNDEBUG /D_NDEBUG /Oi /Oy /O2 /MT +# Linker flags. LDFLAGS = $(LDFLAGS) /OPT:REF # Attempt to detect when and are available diff --git a/salsa.cpp b/salsa.cpp index 148d970a..fb2dc03e 100644 --- a/salsa.cpp +++ b/salsa.cpp @@ -90,9 +90,14 @@ void Salsa20_Core(word32* data, unsigned int rounds) x[15] ^= rotlConstant<18>(x[14]+x[13]); } +#ifdef _MSC_VER + for (size_t i = 0; i < 16; ++i) + data[i] += x[i]; +#else #pragma omp simd for (size_t i = 0; i < 16; ++i) data[i] += x[i]; +#endif } std::string Salsa20_Policy::AlgorithmProvider() const diff --git a/scrypt.cpp b/scrypt.cpp index f63a020e..0098d71a 100644 --- a/scrypt.cpp +++ b/scrypt.cpp @@ -60,9 +60,14 @@ static inline void BlockCopy(byte* dest, byte* src, size_t len) static inline void BlockXOR(byte* dest, byte* src, size_t len) { +#ifdef _MSC_VER + for (size_t i = 0; i < len; ++i) + dest[i] ^= src[i]; +#else #pragma omp simd for (size_t i = 0; i < len; ++i) dest[i] ^= src[i]; +#endif } static inline void PBKDF2_SHA256(byte* buf, size_t dkLen, @@ -252,10 +257,13 @@ size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen, const byte*secret, siz // 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) PBKDF2_SHA256(B, B.size(), secret, secretLen, salt, saltLen, 1); + // Visual Studio and OpenMP 2.0 fixup. We must use int, not size_t. + int maxParallel=0; + if (!SafeConvert(parallel, maxParallel)) + maxParallel = std::numeric_limits::max(); + #ifdef _OPENMP - int threads = STDMIN(omp_get_max_threads(), - static_cast(STDMIN(static_cast(parallel), - static_cast(std::numeric_limits::max())))); + int threads = STDMIN(omp_get_max_threads(), maxParallel); #endif // http://stackoverflow.com/q/49604260/608639 @@ -267,7 +275,7 @@ size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen, const byte*secret, siz // 2: for i = 0 to p - 1 do #pragma omp for - for (size_t i = 0; i < static_cast(parallel); ++i) + for (int i = 0; i < maxParallel; ++i) { // 3: B_i <-- MF(B_i, N) const ptrdiff_t offset = static_cast(blockSize*i*128);