From f267352eecba63a75b7958639a737a0ba9f0e43e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 10 Oct 2015 19:23:39 +0200 Subject: [PATCH] Use SIMD-specific compiler flags only for files needing them --- configure.ac | 37 ++++++++++++++++--- src/libsodium/Makefile.am | 19 +++++++++- .../aes256gcm/aesni/aead_aes256gcm_aesni.c | 15 -------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/configure.ac b/configure.ac index a307c135..3c838b92 100644 --- a/configure.ac +++ b/configure.ac @@ -271,7 +271,8 @@ AS_IF([test "x$EMSCRIPTEN" = "x"],[ #include ]], [[ __m64 x = _mm_setzero_si64(); ]])], [AC_MSG_RESULT(yes) - AC_DEFINE([HAVE_MMINTRIN_H], [1], [mmx is available])], + AC_DEFINE([HAVE_MMINTRIN_H], [1], [mmx is available]) + AX_CHECK_COMPILE_FLAG([-mmmx], [CFLAGS_MMX="-mmmx"])], [AC_MSG_RESULT(no)]) AC_MSG_CHECKING(for SSE2 instructions set) @@ -283,7 +284,8 @@ AS_IF([test "x$EMSCRIPTEN" = "x"],[ #include ]], [[ __m128d x = _mm_setzero_pd(); ]])], [AC_MSG_RESULT(yes) - AC_DEFINE([HAVE_EMMINTRIN_H], [1], [sse2 is available])], + AC_DEFINE([HAVE_EMMINTRIN_H], [1], [sse2 is available]) + AX_CHECK_COMPILE_FLAG([-msse2], [CFLAGS_SSE2="-msse2"])], [AC_MSG_RESULT(no)]) AC_MSG_CHECKING(for SSE3 instructions set) @@ -296,7 +298,8 @@ AS_IF([test "x$EMSCRIPTEN" = "x"],[ ]], [[ __m128 x = _mm_addsub_ps(_mm_cvtpd_ps(_mm_setzero_pd()), _mm_cvtpd_ps(_mm_setzero_pd())); ]])], [AC_MSG_RESULT(yes) - AC_DEFINE([HAVE_PMMINTRIN_H], [1], [sse3 is available])], + AC_DEFINE([HAVE_PMMINTRIN_H], [1], [sse3 is available]) + AX_CHECK_COMPILE_FLAG([-msse3], [CFLAGS_SSE3="-msse3"])], [AC_MSG_RESULT(no)]) AC_MSG_CHECKING(for SSSE3 instructions set) @@ -308,7 +311,21 @@ AS_IF([test "x$EMSCRIPTEN" = "x"],[ #include ]], [[ __m64 x = _mm_abs_pi32(_m_from_int(0)); ]])], [AC_MSG_RESULT(yes) - AC_DEFINE([HAVE_TMMINTRIN_H], [1], [ssse3 is available])], + AC_DEFINE([HAVE_TMMINTRIN_H], [1], [ssse3 is available]) + AX_CHECK_COMPILE_FLAG([-mssse3], [CFLAGS_SSSE3="-mssse3"])], + [AC_MSG_RESULT(no)]) + + AC_MSG_CHECKING(for SSE4.1 instructions set) + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ +#pragma GCC target("sse4.1") +#ifndef __SSE4_1__ +# define __SSE4_1__ +#endif +#include +]], [[ __m128i x = _mm_minpos_epu16(_mm_setzero_si128()); ]])], + [AC_MSG_RESULT(yes) + AC_DEFINE([HAVE_SMMINTRIN_H], [1], [sse4.1 is available]) + AX_CHECK_COMPILE_FLAG([-msse4.1], [CFLAGS_SSE4_1="-msse4.1"])], [AC_MSG_RESULT(no)]) AC_MSG_CHECKING(for AESNI instructions set and PCLMULQDQ) @@ -326,12 +343,20 @@ AS_IF([test "x$EMSCRIPTEN" = "x"],[ __m128i y = _mm_clmulepi64_si128(_mm_setzero_si128(), _mm_setzero_si128(), 0);]])], [AC_MSG_RESULT(yes) AC_DEFINE([HAVE_WMMINTRIN_H], [1], [aesni is available]) - AX_CHECK_COMPILE_FLAG([-maes], [CFLAGS="$CFLAGS -maes"]) - AX_CHECK_COMPILE_FLAG([-mpclmul], [CFLAGS="$CFLAGS -mpclmul"]) + AX_CHECK_COMPILE_FLAG([-maes], [CFLAGS_AESNI="-maes"]) + AX_CHECK_COMPILE_FLAG([-mpclmul], [CFLAGS_PCLMUL="-mpclmul"]) ], [AC_MSG_RESULT(no)]) ]) +AC_SUBST(CFLAGS_MMX) +AC_SUBST(CFLAGS_SSE2) +AC_SUBST(CFLAGS_SSE3) +AC_SUBST(CFLAGS_SSSE3) +AC_SUBST(CFLAGS_SSE4_1) +AC_SUBST(CFLAGS_AESNI) +AC_SUBST(CFLAGS_PCLMUL) + AC_CHECK_HEADERS([sys/mman.h]) dnl Checks for typedefs, structures, and compiler characteristics. diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index c7748234..201c0bd3 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -2,7 +2,6 @@ lib_LTLIBRARIES = \ libsodium.la libsodium_la_SOURCES = \ - crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c \ crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c \ crypto_auth/crypto_auth.c \ crypto_auth/hmacsha256/auth_hmacsha256_api.c \ @@ -63,7 +62,6 @@ libsodium_la_SOURCES = \ crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c \ crypto_pwhash/scryptsalsa208sha256/sysendian.h \ crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c \ - crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c \ crypto_scalarmult/crypto_scalarmult.c \ crypto_scalarmult/curve25519/scalarmult_curve25519_api.c \ crypto_secretbox/crypto_secretbox.c \ @@ -269,3 +267,20 @@ endif SUBDIRS = \ include + +libsodium_la_LIBADD = libaesni.la libsse2.la +noinst_LTLIBRARIES = libaesni.la libsse2.la + +libaesni_la_LDFLAGS = $(libsodium_la_LDFLAGS) +libaesni_la_CPPFLAGS = $(libsodium_la_CPPFLAGS) \ + @CFLAGS_SSE4_1@ @CFLAGS_AESNI@ @CFLAGS_PCLMUL@ + +libaesni_la_SOURCES = \ + crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c + +libsse2_la_LDFLAGS = $(libsodium_la_LDFLAGS) +libsse2_la_CPPFLAGS = $(libsodium_la_CPPFLAGS) \ + @CFLAGS_SSE2@ + +libsse2_la_SOURCES = \ + crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c 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 37beab47..5cb17fbc 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -13,25 +13,10 @@ #ifdef HAVE_WMMINTRIN_H -#pragma GCC target("sse") -#pragma GCC target("sse2") -#pragma GCC target("ssse3") #pragma GCC target("sse4.1") #pragma GCC target("aes") #pragma GCC target("pclmul") -#ifndef __SSSE3__ -# define __SSSE3__ -#endif -#ifndef __SSE4_1__ -# define __SSE4_1__ -#endif -#ifndef __AES__ -# define __AES__ -#endif -#ifndef __PCLMUL__ -# define __PCLMUL__ -#endif #include #if defined(__INTEL_COMPILER) || defined(_bswap64)