Skip to content

Commit

Permalink
Use OpenMP 2.0 for MSC compilers (GH weidai11#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Jan 21, 2019
1 parent 9280894 commit aa043b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions cryptest.nmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sdkddkver.h> and <winapifamily.h> are available
Expand Down
5 changes: 5 additions & 0 deletions salsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions scrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<int>::max();

#ifdef _OPENMP
int threads = STDMIN(omp_get_max_threads(),
static_cast<int>(STDMIN(static_cast<size_t>(parallel),
static_cast<size_t>(std::numeric_limits<int>::max()))));
int threads = STDMIN(omp_get_max_threads(), maxParallel);
#endif

// http://stackoverflow.com/q/49604260/608639
Expand All @@ -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<size_t>(parallel); ++i)
for (int i = 0; i < maxParallel; ++i)
{
// 3: B_i <-- MF(B_i, N)
const ptrdiff_t offset = static_cast<ptrdiff_t>(blockSize*i*128);
Expand Down

0 comments on commit aa043b3

Please sign in to comment.