2018-08-26 05:54:38 +00:00
|
|
|
|
2018-11-10 13:00:14 +00:00
|
|
|
// crc_simd.cpp - written and placed in the public domain by
|
2017-08-17 16:33:43 +00:00
|
|
|
// Jeffrey Walton, Uri Blumenthal and Marcel Raad.
|
|
|
|
//
|
|
|
|
// This source file uses intrinsics to gain access to ARMv7a and
|
|
|
|
// ARMv8a NEON instructions. A separate source file is needed
|
|
|
|
// because additional CXXFLAGS are required to enable the
|
|
|
|
// appropriate instructions sets in some build configurations.
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "config.h"
|
2017-08-17 18:24:51 +00:00
|
|
|
#include "stdcpp.h"
|
2017-08-17 16:33:43 +00:00
|
|
|
|
|
|
|
#if (CRYPTOPP_ARM_NEON_AVAILABLE)
|
2017-09-12 09:29:51 +00:00
|
|
|
# include <arm_neon.h>
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif
|
|
|
|
|
2018-01-20 18:23:41 +00:00
|
|
|
// Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many
|
|
|
|
// compilers don't follow ACLE conventions for the include.
|
2018-10-25 18:08:09 +00:00
|
|
|
#if (CRYPTOPP_ARM_ACLE_AVAILABLE)
|
2018-01-20 18:23:41 +00:00
|
|
|
# include <stdint.h>
|
2017-09-13 21:16:57 +00:00
|
|
|
# include <arm_acle.h>
|
|
|
|
#endif
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
|
|
|
|
# include <signal.h>
|
|
|
|
# include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EXCEPTION_EXECUTE_HANDLER
|
|
|
|
# define EXCEPTION_EXECUTE_HANDLER 1
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 05:22:38 +00:00
|
|
|
// Squash MS LNK4221 and libtool warnings
|
|
|
|
extern const char NEON_SIMD_FNAME[] = __FILE__;
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
|
|
|
|
extern "C" {
|
|
|
|
typedef void (*SigHandler)(int);
|
|
|
|
|
|
|
|
static jmp_buf s_jmpSIGILL;
|
|
|
|
static void SigIllHandler(int)
|
|
|
|
{
|
|
|
|
longjmp(s_jmpSIGILL, 1);
|
|
|
|
}
|
2018-03-31 17:07:30 +00:00
|
|
|
}
|
2017-08-17 16:33:43 +00:00
|
|
|
#endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
|
|
|
|
|
2018-07-08 06:49:21 +00:00
|
|
|
bool CPU_ProbeARMv7()
|
|
|
|
{
|
2018-08-26 05:54:38 +00:00
|
|
|
#if defined(__aarch32__) || defined(__aarch64__)
|
|
|
|
return true;
|
|
|
|
#elif defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
|
2018-07-08 06:49:21 +00:00
|
|
|
return false;
|
|
|
|
#elif (CRYPTOPP_ARM_NEON_AVAILABLE)
|
|
|
|
# if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
|
|
|
|
volatile bool result = true;
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
// Modern MS hardware is ARMv7
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return result;
|
Add ARMv8.4 cpu feature detection support (GH #685) (#687)
This PR adds ARMv8.4 cpu feature detection support. Previously we only needed ARMv8.1 and things were much easier. For example, ARMv8.1 `__ARM_FEATURE_CRYPTO` meant PMULL, AES, SHA-1 and SHA-256 were available. ARMv8.4 `__ARM_FEATURE_CRYPTO` means PMULL, AES, SHA-1, SHA-256, SHA-512, SHA-3, SM3 and SM4 are available.
We still use the same pattern as before. We make something available based on compiler version and/or preprocessor macros. But this time around we had to tighten things up a bit to ensure ARMv8.4 did not cross-pollinate down into ARMv8.1.
ARMv8.4 is largely untested at the moment. There is no hardware in the field and CI lacks QEMU with the relevant patches/support. We will probably have to revisit some of this stuff in the future.
Since this update applies to ARM gadgets we took the time to expand Android and iOS testing on Travis. Travis now tests more platforms, and includes Autotools and CMake builds, too.
2018-07-15 12:35:14 +00:00
|
|
|
# elif defined(__arm__) && (__ARM_ARCH >= 7)
|
2018-07-08 06:49:21 +00:00
|
|
|
// longjmp and clobber warnings. Volatile is required.
|
|
|
|
// http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
|
|
|
|
volatile bool result = true;
|
|
|
|
|
|
|
|
volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
|
|
|
|
if (oldHandler == SIG_ERR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
volatile sigset_t oldMask;
|
|
|
|
if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (setjmp(s_jmpSIGILL))
|
|
|
|
result = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ARMv7 added movt and movw
|
2018-07-10 09:08:27 +00:00
|
|
|
int a;
|
|
|
|
asm volatile("movw %0,%1 \n"
|
|
|
|
"movt %0,%1 \n"
|
|
|
|
: "=r"(a) : "i"(0x1234));
|
|
|
|
result = (a == 0x12341234);
|
2018-07-08 06:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
|
|
|
|
signal(SIGILL, oldHandler);
|
|
|
|
return result;
|
2018-08-26 05:54:38 +00:00
|
|
|
# else
|
|
|
|
return false;
|
2018-07-08 06:49:21 +00:00
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif // CRYPTOPP_ARM_NEON_AVAILABLE
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:33:43 +00:00
|
|
|
bool CPU_ProbeNEON()
|
|
|
|
{
|
2018-08-26 05:54:38 +00:00
|
|
|
#if defined(__aarch32__) || defined(__aarch64__)
|
|
|
|
return true;
|
|
|
|
#elif defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
|
2017-09-20 01:08:37 +00:00
|
|
|
return false;
|
|
|
|
#elif (CRYPTOPP_ARM_NEON_AVAILABLE)
|
2017-08-17 16:33:43 +00:00
|
|
|
# if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
|
|
|
|
volatile bool result = true;
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
uint32_t v1[4] = {1,1,1,1};
|
|
|
|
uint32x4_t x1 = vld1q_u32(v1);
|
|
|
|
uint64_t v2[2] = {1,1};
|
|
|
|
uint64x2_t x2 = vld1q_u64(v2);
|
|
|
|
|
|
|
|
uint32x4_t x3 = vdupq_n_u32(2);
|
|
|
|
x3 = vsetq_lane_u32(vgetq_lane_u32(x1,0),x3,0);
|
|
|
|
x3 = vsetq_lane_u32(vgetq_lane_u32(x1,3),x3,3);
|
|
|
|
uint64x2_t x4 = vdupq_n_u64(2);
|
|
|
|
x4 = vsetq_lane_u64(vgetq_lane_u64(x2,0),x4,0);
|
|
|
|
x4 = vsetq_lane_u64(vgetq_lane_u64(x2,1),x4,1);
|
|
|
|
|
|
|
|
result = !!(vgetq_lane_u32(x3,0) | vgetq_lane_u64(x4,1));
|
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
# else
|
|
|
|
|
|
|
|
// longjmp and clobber warnings. Volatile is required.
|
|
|
|
// http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
|
|
|
|
volatile bool result = true;
|
|
|
|
|
|
|
|
volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
|
|
|
|
if (oldHandler == SIG_ERR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
volatile sigset_t oldMask;
|
|
|
|
if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (setjmp(s_jmpSIGILL))
|
|
|
|
result = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t v1[4] = {1,1,1,1};
|
|
|
|
uint32x4_t x1 = vld1q_u32(v1);
|
|
|
|
uint64_t v2[2] = {1,1};
|
|
|
|
uint64x2_t x2 = vld1q_u64(v2);
|
|
|
|
|
|
|
|
uint32x4_t x3 = {0,0,0,0};
|
|
|
|
x3 = vsetq_lane_u32(vgetq_lane_u32(x1,0),x3,0);
|
|
|
|
x3 = vsetq_lane_u32(vgetq_lane_u32(x1,3),x3,3);
|
|
|
|
uint64x2_t x4 = {0,0};
|
|
|
|
x4 = vsetq_lane_u64(vgetq_lane_u64(x2,0),x4,0);
|
|
|
|
x4 = vsetq_lane_u64(vgetq_lane_u64(x2,1),x4,1);
|
|
|
|
|
|
|
|
// Hack... GCC optimizes away the code and returns true
|
|
|
|
result = !!(vgetq_lane_u32(x3,0) | vgetq_lane_u64(x4,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
|
|
|
|
signal(SIGILL, oldHandler);
|
|
|
|
return result;
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif // CRYPTOPP_ARM_NEON_AVAILABLE
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|