From 45160a0f7867f0422e8b77dc8dabfd301d5f85ca Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 26 Feb 2017 18:21:04 +0100 Subject: [PATCH] Define the HAVE_* macros for SIMD instructions on Visual Studio This avoids a lot of redundant preprocessor checks --- .../aes256gcm/aesni/aead_aes256gcm_aesni.c | 3 +- .../blake2b/ref/blake2b-compress-avx2.c | 11 ++-- .../blake2b/ref/blake2b-compress-sse41.c | 11 ++-- .../blake2b/ref/blake2b-compress-ssse3.c | 10 +-- .../blake2b/ref/blake2b-ref.c | 26 ++++---- .../poly1305/donna/poly1305_donna32.h | 8 +-- .../poly1305/donna/poly1305_donna64.h | 8 +-- .../poly1305/sse2/poly1305_sse2.c | 30 ++++----- .../crypto_pwhash/argon2/argon2-core.c | 9 ++- .../argon2/argon2-fill-block-ssse3.c | 27 ++++---- .../crypto_scrypt-common.c | 5 +- .../sse/pwhash_scryptsalsa208sha256_sse.c | 62 +++++++++---------- .../crypto_stream/salsa20/stream_salsa20.c | 18 +++--- .../salsa20/xmm6int/stream_salsa20_xmm6int.c | 21 +++---- src/libsodium/include/sodium/private/common.h | 16 +++++ src/libsodium/sodium/runtime.c | 29 +++------ 16 files changed, 141 insertions(+), 153 deletions(-) diff --git a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c index 2f2f084f..4baae622 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -17,8 +17,7 @@ #include "runtime.h" #include "utils.h" -#if (defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H)) || \ - (defined(_MSC_VER) && _MSC_VER >= 1600 && (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#if defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H) #pragma GCC target("ssse3") #pragma GCC target("aes") diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c index 99614c16..4b01b170 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c @@ -6,10 +6,11 @@ #include #include -#if (defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ - defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)) || \ - (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64)) && \ - _MSC_VER >= 1700) +#include "blake2.h" +#include "private/common.h" + +#if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ + defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) #pragma GCC target("sse2") #pragma GCC target("ssse3") @@ -21,9 +22,7 @@ #include #include -#include "blake2.h" #include "blake2b-compress-avx2.h" -#include "private/common.h" CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] = { diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c index 1cc0a158..6043f21f 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c @@ -5,10 +5,11 @@ #include #include -#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ - defined(HAVE_SMMINTRIN_H)) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#include "blake2.h" +#include "private/common.h" + +#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ + defined(HAVE_SMMINTRIN_H) #pragma GCC target("sse2") #pragma GCC target("ssse3") @@ -18,9 +19,7 @@ #include #include -#include "blake2.h" #include "blake2b-compress-sse41.h" -#include "private/common.h" CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] = { diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c index bc5531b1..0470901e 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c @@ -2,21 +2,21 @@ #include #include -#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)) || \ - (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64))) +#include "blake2.h" +#include "private/common.h" + +#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) #pragma GCC target("sse2") #pragma GCC target("ssse3") #ifdef _MSC_VER -#include /* for _mm_set_epi64x */ +# include /* for _mm_set_epi64x */ #endif #include #include -#include "blake2.h" #include "blake2b-compress-ssse3.h" -#include "private/common.h" CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] = { diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c index 15d783a4..25536ef9 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c @@ -161,9 +161,9 @@ blake2b_init0(blake2b_state *S) int i; memset(S, 0, sizeof(blake2b_state)); - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; i++) { S->h[i] = blake2b_IV[i]; - + } return 0; } @@ -179,9 +179,9 @@ blake2b_init_param(blake2b_state *S, const blake2b_param *P) p = (const uint8_t *) (P); /* IV XOR ParamBlock */ - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; i++) { S->h[i] ^= LOAD64_LE(p + sizeof(S->h[i]) * i); - + } return 0; } @@ -381,8 +381,9 @@ blake2b_final(blake2b_state *S, uint8_t *out, uint8_t outlen) uint8_t buffer[BLAKE2B_OUTBYTES]; int i; - for (i = 0; i < 8; ++i) /* Output full hash to temp buffer */ + for (i = 0; i < 8; i++) { /* Output full hash to temp buffer */ STORE64_LE(buffer + sizeof(S->h[i]) * i, S->h[i]); + } memcpy(out, buffer, outlen); } #endif @@ -466,26 +467,21 @@ int blake2b_pick_best_implementation(void) { /* LCOV_EXCL_START */ -#if (defined(HAVE_AVX2INTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ - defined(HAVE_SMMINTRIN_H)) || \ - (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64)) && \ - _MSC_VER >= 1700) +#if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ + defined(HAVE_SMMINTRIN_H) if (sodium_runtime_has_avx2()) { blake2b_compress = blake2b_compress_avx2; return 0; } #endif -#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ - defined(HAVE_SMMINTRIN_H)) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && \ + defined(HAVE_SMMINTRIN_H) if (sodium_runtime_has_sse41()) { blake2b_compress = blake2b_compress_sse41; return 0; } #endif -#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)) || \ - (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64))) +#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) if (sodium_runtime_has_ssse3()) { blake2b_compress = blake2b_compress_ssse3; return 0; diff --git a/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h b/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h index c9883afc..ef57e1f1 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h +++ b/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h @@ -1,14 +1,14 @@ /* - poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication + poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication and 64 bit addition */ #if defined(_MSC_VER) -#define POLY1305_NOINLINE __declspec(noinline) +# define POLY1305_NOINLINE __declspec(noinline) #elif defined(__GNUC__) -#define POLY1305_NOINLINE __attribute__((noinline)) +# define POLY1305_NOINLINE __attribute__((noinline)) #else -#define POLY1305_NOINLINE +# define POLY1305_NOINLINE #endif #include "private/common.h" diff --git a/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h b/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h index 3126d457..695ad28f 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h +++ b/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h @@ -1,5 +1,5 @@ /* - poly1305 implementation using 64 bit * 64 bit = 128 bit multiplication + poly1305 implementation using 64 bit * 64 bit = 128 bit multiplication and 128 bit addition */ @@ -16,11 +16,11 @@ typedef unsigned uint128_t __attribute__((mode(TI))); #define LO(in) (unsigned long long) (in) #if defined(_MSC_VER) -#define POLY1305_NOINLINE __declspec(noinline) +# define POLY1305_NOINLINE __declspec(noinline) #elif defined(__GNUC__) -#define POLY1305_NOINLINE __attribute__((noinline)) +# define POLY1305_NOINLINE __attribute__((noinline)) #else -#define POLY1305_NOINLINE +# define POLY1305_NOINLINE #endif #include "private/common.h" diff --git a/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c b/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c index c4631e30..f3e5f3bd 100644 --- a/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c +++ b/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c @@ -10,27 +10,27 @@ #if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H) -#pragma GCC target("sse2") +# pragma GCC target("sse2") -#include +# include typedef __m128i xmmi; -#if defined(__SIZEOF_INT128__) +# if defined(__SIZEOF_INT128__) typedef unsigned __int128 uint128_t; -#else +# else typedef unsigned uint128_t __attribute__((mode(TI))); -#endif +# endif -#if defined(_MSC_VER) -#define POLY1305_NOINLINE __declspec(noinline) -#elif defined(__GNUC__) -#define POLY1305_NOINLINE __attribute__((noinline)) -#else -#define POLY1305_NOINLINE -#endif +# if defined(_MSC_VER) +# define POLY1305_NOINLINE __declspec(noinline) +# elif defined(__GNUC__) +# define POLY1305_NOINLINE __attribute__((noinline)) +# else +# define POLY1305_NOINLINE +# endif -#define poly1305_block_size 32 +# define poly1305_block_size 32 enum poly1305_state_flags_t { poly1305_started = 1, @@ -59,7 +59,7 @@ typedef struct poly1305_state_internal_t { * totally fine, even though this intrinsic requires a __m128i* input. * This confuses dynamic analysis, so force alignment, only in debug mode. */ -#ifdef DEBUG +# ifdef DEBUG static xmmi _fakealign_mm_loadl_epi64(const void *m) { @@ -68,7 +68,7 @@ _fakealign_mm_loadl_epi64(const void *m) return _mm_loadl_epi64(&tmp); } -#define _mm_loadl_epi64(X) _fakealign_mm_loadl_epi64(X) +# define _mm_loadl_epi64(X) _fakealign_mm_loadl_epi64(X) #endif /* copy 0-31 bytes */ diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-core.c b/src/libsodium/crypto_pwhash/argon2/argon2-core.c index 1f168b6d..465715ba 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-core.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-core.c @@ -11,8 +11,9 @@ * . */ +#include #ifdef HAVE_SYS_MMAN_H -#include +# include #endif #include #include @@ -29,7 +30,7 @@ #include "blake2b-long.h" #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS) -#define MAP_ANON MAP_ANONYMOUS +# define MAP_ANON MAP_ANONYMOUS #endif static fill_segment_fn fill_segment = fill_segment_ref; @@ -589,9 +590,7 @@ int argon2_pick_best_implementation(void) { /* LCOV_EXCL_START */ -#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) if (sodium_runtime_has_ssse3()) { fill_segment = fill_segment_ssse3; return 0; diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c index 8b2452c1..77d0adb9 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c @@ -15,24 +15,23 @@ #include #include -#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) - -#pragma GCC target("sse2") -#pragma GCC target("ssse3") - -#ifdef _MSC_VER -#include /* for _mm_set_epi64x */ -#endif -#include -#include - #include "argon2-core.h" #include "argon2.h" -#include "blamka-round-ssse3.h" #include "private/common.h" +#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) + +# pragma GCC target("sse2") +# pragma GCC target("ssse3") + +# ifdef _MSC_VER +# include /* for _mm_set_epi64x */ +# endif +# include +# include + +# include "blamka-round-ssse3.h" + static void fill_block(__m128i *state, const uint8_t *ref_block, uint8_t *next_block) { diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c index f9fb742b..39aa4916 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c @@ -23,6 +23,7 @@ #include "crypto_pwhash_scryptsalsa208sha256.h" #include "crypto_scrypt.h" +#include "private/common.h" #include "runtime.h" #include "utils.h" @@ -154,9 +155,7 @@ escrypt_r(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen, if (need > buflen || need < saltlen) { return NULL; } -#if defined(HAVE_EMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_EMMINTRIN_H escrypt_kdf = sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse; #else diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c index 2668ab5a..309546ef 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c @@ -28,40 +28,40 @@ * online backup system. */ -#if defined(HAVE_EMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) -#ifdef __GNUC__ -#pragma GCC target("sse2") -#endif -#include -#if defined(__XOP__) && defined(DISABLED) -#include -#endif - #include #include #include #include #include -#include "../crypto_scrypt.h" -#include "../pbkdf2-sha256.h" #include "private/common.h" -#if defined(__XOP__) && defined(DISABLED) -#define ARX(out, in1, in2, s) \ +#ifdef HAVE_EMMINTRIN_H + +# ifdef __GNUC__ +# pragma GCC target("sse2") +# endif +# include +# if defined(__XOP__) && defined(DISABLED) +# include +# endif + +# include "../crypto_scrypt.h" +# include "../pbkdf2-sha256.h" + +# if defined(__XOP__) && defined(DISABLED) +# define ARX(out, in1, in2, s) \ out = _mm_xor_si128(out, _mm_roti_epi32(_mm_add_epi32(in1, in2), s)); -#else -#define ARX(out, in1, in2, s) \ +# else +# define ARX(out, in1, in2, s) \ { \ __m128i T = _mm_add_epi32(in1, in2); \ out = _mm_xor_si128(out, _mm_slli_epi32(T, s)); \ out = _mm_xor_si128(out, _mm_srli_epi32(T, 32 - s)); \ } -#endif +# endif -#define SALSA20_2ROUNDS \ +# define SALSA20_2ROUNDS \ /* Operate on "columns". */ \ ARX(X1, X0, X3, 7) \ ARX(X2, X1, X0, 9) \ @@ -87,7 +87,7 @@ /** * Apply the salsa20/8 core to the block provided in (X0 ... X3) ^ (Z0 ... Z3). */ -#define SALSA20_8_XOR(in, out) \ +# define SALSA20_8_XOR(in, out) \ { \ __m128i Y0 = X0 = _mm_xor_si128(X0, (in)[0]); \ __m128i Y1 = X1 = _mm_xor_si128(X1, (in)[1]); \ @@ -146,13 +146,13 @@ blockmix_salsa8(const __m128i *Bin, __m128i *Bout, size_t r) SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4]) } -#define XOR4(in) \ +# define XOR4(in) \ X0 = _mm_xor_si128(X0, (in)[0]); \ X1 = _mm_xor_si128(X1, (in)[1]); \ X2 = _mm_xor_si128(X2, (in)[2]); \ X3 = _mm_xor_si128(X3, (in)[3]); -#define XOR4_2(in1, in2) \ +# define XOR4_2(in1, in2) \ X0 = _mm_xor_si128((in1)[0], (in2)[0]); \ X1 = _mm_xor_si128((in1)[1], (in2)[1]); \ X2 = _mm_xor_si128((in1)[2], (in2)[2]); \ @@ -201,11 +201,11 @@ blockmix_salsa8_xor(const __m128i *Bin1, const __m128i *Bin2, __m128i *Bout, return _mm_cvtsi128_si32(X0); } -#undef ARX -#undef SALSA20_2ROUNDS -#undef SALSA20_8_XOR -#undef XOR4 -#undef XOR4_2 +# undef ARX +# undef SALSA20_2ROUNDS +# undef SALSA20_8_XOR +# undef XOR4 +# undef XOR4_2 /** * integerify(B, r): @@ -316,12 +316,12 @@ escrypt_kdf_sse(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen, uint32_t i; /* Sanity-check parameters. */ -#if SIZE_MAX > UINT32_MAX +# if SIZE_MAX > UINT32_MAX if (buflen > (((uint64_t)(1) << 32) - 1) * 32) { errno = EFBIG; return -1; } -#endif +# endif if ((uint64_t)(r) * (uint64_t)(p) >= ((uint64_t) 1 << 30)) { errno = EFBIG; return -1; @@ -339,9 +339,9 @@ escrypt_kdf_sse(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen, return -1; } if ((r > SIZE_MAX / 128 / p) || -#if SIZE_MAX / 256 <= UINT32_MAX +# if SIZE_MAX / 256 <= UINT32_MAX (r > SIZE_MAX / 256) || -#endif +# endif (N > SIZE_MAX / 128 / r)) { errno = ENOMEM; return -1; diff --git a/src/libsodium/crypto_stream/salsa20/stream_salsa20.c b/src/libsodium/crypto_stream/salsa20/stream_salsa20.c index e7bda88c..fae43f57 100644 --- a/src/libsodium/crypto_stream/salsa20/stream_salsa20.c +++ b/src/libsodium/crypto_stream/salsa20/stream_salsa20.c @@ -1,19 +1,15 @@ -#if defined(HAVE_EMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) -# define INCLUDE_SSE2_IMPL -#endif - #include "crypto_stream_salsa20.h" -#include "stream_salsa20.h" +#include "private/common.h" #include "randombytes.h" #include "runtime.h" +#include "stream_salsa20.h" + #include "ref/stream_salsa20_ref.h" -#ifdef INCLUDE_SSE2_IMPL +#ifdef HAVE_EMMINTRIN_H # include "xmm6int/stream_salsa20_xmm6int.h" #endif -#if defined(INCLUDE_SSE2_IMPL) && defined(__x86_64__) +#if defined(HAVE_EMMINTRIN_H) && defined(__x86_64__) static const crypto_stream_salsa20_implementation *implementation = &crypto_stream_salsa20_xmm6int_implementation; #else @@ -66,13 +62,13 @@ crypto_stream_salsa20_keygen(unsigned char k[crypto_stream_salsa20_KEYBYTES]) int _crypto_stream_salsa20_pick_best_implementation(void) { -#if defined(INCLUDE_SSE2_IMPL) && defined(__x86_64__) +#if defined(HAVE_EMMINTRIN_H) && defined(__x86_64__) implementation = &crypto_stream_salsa20_xmm6int_implementation; #else implementation = &crypto_stream_salsa20_ref_implementation; #endif -#ifdef INCLUDE_SSE2_IMPL +#ifdef HAVE_EMMINTRIN_H if (sodium_runtime_has_sse2()) { implementation = &crypto_stream_salsa20_xmm6int_implementation; } diff --git a/src/libsodium/crypto_stream/salsa20/xmm6int/stream_salsa20_xmm6int.c b/src/libsodium/crypto_stream/salsa20/xmm6int/stream_salsa20_xmm6int.c index bec46f3e..528baae1 100644 --- a/src/libsodium/crypto_stream/salsa20/xmm6int/stream_salsa20_xmm6int.c +++ b/src/libsodium/crypto_stream/salsa20/xmm6int/stream_salsa20_xmm6int.c @@ -3,22 +3,21 @@ #include #include -#if defined(HAVE_EMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) -#ifdef __GNUC__ -# pragma GCC target("sse2") -#endif -#include - #include "crypto_stream_salsa20.h" #include "private/common.h" #include "utils.h" -#include "../stream_salsa20.h" -#include "stream_salsa20_xmm6int.h" +#ifdef HAVE_EMMINTRIN_H -#define ROUNDS 20 +# ifdef __GNUC__ +# pragma GCC target("sse2") +# endif +# include + +# include "../stream_salsa20.h" +# include "stream_salsa20_xmm6int.h" + +# define ROUNDS 20 typedef struct salsa_ctx { uint32_t input[16]; diff --git a/src/libsodium/include/sodium/private/common.h b/src/libsodium/include/sodium/private/common.h index 3ee9dd85..c0c01142 100644 --- a/src/libsodium/include/sodium/private/common.h +++ b/src/libsodium/include/sodium/private/common.h @@ -192,4 +192,20 @@ store32_be(uint8_t dst[4], uint32_t w) # endif #endif +#if defined(_MSC_VER) && \ + (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86)) +# define HAVE_MMINTRIN_H 1 +# define HAVE_EMMINTRIN_H 1 +# define HAVE_PMMINTRIN_H 1 +# define HAVE_TMMINTRIN_H 1 +# define HAVE_SMMINTRIN_H 1 +# define HAVE_AVXINTRIN_H 1 +# if _MSC_VER >= 1600 +# define HAVE_WMMINTRIN_H 1 +# endif +# if _MSC_VER >= 1700 +# define HAVE_AVX2INTRIN_H 1 +# endif +#endif + #endif diff --git a/src/libsodium/sodium/runtime.c b/src/libsodium/sodium/runtime.c index 00d0a868..f46dcd23 100644 --- a/src/libsodium/sodium/runtime.c +++ b/src/libsodium/sodium/runtime.c @@ -8,6 +8,7 @@ # include #endif +#include "private/common.h" #include "runtime.h" typedef struct CPUFeatures_ { @@ -119,42 +120,32 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) return -1; /* LCOV_EXCL_LINE */ } _cpuid(cpu_info, 0x00000001); -#if defined(HAVE_EMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_EMMINTRIN_H cpu_features->has_sse2 = ((cpu_info[3] & CPUID_EDX_SSE2) != 0x0); #else cpu_features->has_sse2 = 0; #endif -#if defined(HAVE_PMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_PMMINTRIN_H cpu_features->has_sse3 = ((cpu_info[2] & CPUID_ECX_SSE3) != 0x0); #else cpu_features->has_sse3 = 0; #endif -#if defined(HAVE_TMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_TMMINTRIN_H cpu_features->has_ssse3 = ((cpu_info[2] & CPUID_ECX_SSSE3) != 0x0); #else cpu_features->has_ssse3 = 0; #endif -#if defined(HAVE_SMMINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_SMMINTRIN_H cpu_features->has_sse41 = ((cpu_info[2] & CPUID_ECX_SSE41) != 0x0); #else cpu_features->has_sse41 = 0; #endif cpu_features->has_avx = 0; -#if defined(HAVE_AVXINTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_AVXINTRIN_H if ((cpu_info[2] & (CPUID_ECX_AVX | CPUID_ECX_XSAVE | CPUID_ECX_OSXSAVE)) == (CPUID_ECX_AVX | CPUID_ECX_XSAVE | CPUID_ECX_OSXSAVE)) { uint32_t xcr0 = 0U; @@ -178,9 +169,7 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) #endif cpu_features->has_avx2 = 0; -#if defined(HAVE_AVX2INTRIN_H) || \ - (defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_AVX2INTRIN_H if (cpu_features->has_avx) { unsigned int cpu_info7[4]; @@ -189,9 +178,7 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) } #endif -#if defined(HAVE_WMMINTRIN_H) || \ - (defined(_MSC_VER) && _MSC_VER >= 1600 && \ - (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) +#ifdef HAVE_WMMINTRIN_H cpu_features->has_pclmul = ((cpu_info[2] & CPUID_ECX_PCLMUL) != 0x0); cpu_features->has_aesni = ((cpu_info[2] & CPUID_ECX_AESNI) != 0x0); #else