Define the HAVE_* macros for SIMD instructions on Visual Studio

This avoids a lot of redundant preprocessor checks
This commit is contained in:
Frank Denis
2017-02-26 18:22:14 +01:00
parent d781ec3cdf
commit 45160a0f78
16 changed files with 141 additions and 153 deletions
@@ -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")
@@ -6,10 +6,11 @@
#include <stdint.h>
#include <string.h>
#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 <smmintrin.h>
#include <tmmintrin.h>
#include "blake2.h"
#include "blake2b-compress-avx2.h"
#include "private/common.h"
CRYPTO_ALIGN(64)
static const uint64_t blake2b_IV[8] = {
@@ -5,10 +5,11 @@
#include <stdint.h>
#include <string.h>
#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 <smmintrin.h>
#include <tmmintrin.h>
#include "blake2.h"
#include "blake2b-compress-sse41.h"
#include "private/common.h"
CRYPTO_ALIGN(64)
static const uint64_t blake2b_IV[8] = {
@@ -2,21 +2,21 @@
#include <stdint.h>
#include <string.h>
#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 <intrin.h> /* for _mm_set_epi64x */
# include <intrin.h> /* for _mm_set_epi64x */
#endif
#include <emmintrin.h>
#include <tmmintrin.h>
#include "blake2.h"
#include "blake2b-compress-ssse3.h"
#include "private/common.h"
CRYPTO_ALIGN(64)
static const uint64_t blake2b_IV[8] = {
@@ -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;
@@ -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"
@@ -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"
@@ -10,27 +10,27 @@
#if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)
#pragma GCC target("sse2")
# pragma GCC target("sse2")
#include <emmintrin.h>
# include <emmintrin.h>
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 */
@@ -11,8 +11,9 @@
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <sys/types.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
# include <sys/mman.h>
#endif
#include <errno.h>
#include <stdint.h>
@@ -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;
@@ -15,24 +15,23 @@
#include <stdlib.h>
#include <string.h>
#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 <intrin.h> /* for _mm_set_epi64x */
#endif
#include <emmintrin.h>
#include <tmmintrin.h>
#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 <intrin.h> /* for _mm_set_epi64x */
# endif
# include <emmintrin.h>
# include <tmmintrin.h>
# include "blamka-round-ssse3.h"
static void
fill_block(__m128i *state, const uint8_t *ref_block, uint8_t *next_block)
{
@@ -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
@@ -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 <emmintrin.h>
#if defined(__XOP__) && defined(DISABLED)
#include <x86intrin.h>
#endif
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#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 <emmintrin.h>
# if defined(__XOP__) && defined(DISABLED)
# include <x86intrin.h>
# 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;
@@ -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;
}
@@ -3,22 +3,21 @@
#include <stdlib.h>
#include <string.h>
#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 <emmintrin.h>
#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 <emmintrin.h>
# include "../stream_salsa20.h"
# include "stream_salsa20_xmm6int.h"
# define ROUNDS 20
typedef struct salsa_ctx {
uint32_t input[16];
@@ -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
+8 -21
View File
@@ -8,6 +8,7 @@
# include <intrin.h>
#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