2017-11-16 20:11:51 +00:00
|
|
|
// ppc-simd.cpp - written and placed in the public domain by
|
2017-09-11 07:05:04 +00:00
|
|
|
// Jeffrey Walton, Uri Blumenthal and Marcel Raad.
|
|
|
|
//
|
|
|
|
// This source file uses intrinsics to gain access to AltiVec,
|
|
|
|
// Power8 and in-core crypto 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"
|
|
|
|
#include "stdcpp.h"
|
|
|
|
|
2017-12-17 01:26:59 +00:00
|
|
|
#if defined(CRYPTOPP_ALTIVEC_AVAILABLE)
|
2017-12-12 12:15:59 +00:00
|
|
|
# include "ppc-simd.h"
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#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 PPC_SIMD_FNAME[] = __FILE__;
|
|
|
|
|
2017-09-11 07:05:04 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
|
|
|
|
extern "C" {
|
|
|
|
typedef void (*SigHandler)(int);
|
|
|
|
|
2017-12-17 01:26:59 +00:00
|
|
|
static jmp_buf s_jmpSIGILL;
|
|
|
|
static void SigIllHandler(int)
|
|
|
|
{
|
|
|
|
longjmp(s_jmpSIGILL, 1);
|
|
|
|
}
|
2018-03-31 17:06:44 +00:00
|
|
|
}
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
|
|
|
|
|
2017-09-12 09:49:38 +00:00
|
|
|
#if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
|
2017-09-12 02:39:59 +00:00
|
|
|
bool CPU_ProbeAltivec()
|
2017-09-11 07:05:04 +00:00
|
|
|
{
|
2017-09-20 01:08:37 +00:00
|
|
|
#if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
|
2017-12-17 01:26:59 +00:00
|
|
|
return false;
|
2018-08-05 09:39:42 +00:00
|
|
|
#elif (CRYPTOPP_ALTIVEC_AVAILABLE)
|
2017-09-11 07:05:04 +00:00
|
|
|
# if defined(CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY)
|
|
|
|
|
2017-12-17 01:26:59 +00:00
|
|
|
// longjmp and clobber warnings. Volatile is required.
|
|
|
|
// http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
|
|
|
|
volatile int 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
|
|
|
|
{
|
2018-08-03 06:54:50 +00:00
|
|
|
CRYPTOPP_ALIGN_DATA(16)
|
2017-12-17 01:26:59 +00:00
|
|
|
const byte b1[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
2018-08-03 06:54:50 +00:00
|
|
|
CRYPTOPP_ALIGN_DATA(16)
|
2017-12-17 01:26:59 +00:00
|
|
|
const byte b2[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
|
2018-08-03 06:54:50 +00:00
|
|
|
CRYPTOPP_ALIGN_DATA(16) byte b3[16];
|
2017-12-17 01:26:59 +00:00
|
|
|
|
2018-08-03 06:54:50 +00:00
|
|
|
// Specifically call the Altivec loads and stores
|
|
|
|
const uint8x16_p v1 = (uint8x16_p)vec_ld(0, (byte*)b1);
|
|
|
|
const uint8x16_p v2 = (uint8x16_p)vec_ld(0, (byte*)b2);
|
|
|
|
const uint8x16_p v3 = (uint8x16_p)vec_xor(v1, v2);
|
|
|
|
vec_st(v3, 0, b3);
|
2017-12-17 01:26:59 +00:00
|
|
|
|
|
|
|
result = (0 == std::memcmp(b2, b3, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
|
|
|
|
signal(SIGILL, oldHandler);
|
|
|
|
return result;
|
2017-09-11 07:05:04 +00:00
|
|
|
# endif
|
|
|
|
#else
|
2017-12-17 01:26:59 +00:00
|
|
|
return false;
|
2017-09-11 07:05:04 +00:00
|
|
|
#endif // CRYPTOPP_ALTIVEC_AVAILABLE
|
|
|
|
}
|
|
|
|
|
2017-09-12 09:49:38 +00:00
|
|
|
# endif // CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
|
2017-09-11 07:05:04 +00:00
|
|
|
NAMESPACE_END
|