Crypto++ 8.2
Free C&
ppc_power7.cpp
1// ppc_power7.cpp - written and placed in the public domain by
2// Jeffrey Walton, Uri Blumenthal and Marcel Raad.
3//
4// This source file uses intrinsics and built-ins to gain access to
5// Power7 instructions. A separate source file is needed because
6// additional CXXFLAGS are required to enable the appropriate
7// instructions sets in some build configurations.
8
9#include "pch.h"
10#include "config.h"
11
12#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
13# include <signal.h>
14# include <setjmp.h>
15#endif
16
17#if defined(_ARCH_PWR7)
18# include "ppc_simd.h"
19#endif
20
21// Squash MS LNK4221 and libtool warnings
22extern const char PPC_POWER7_FNAME[] = __FILE__;
23
24NAMESPACE_BEGIN(CryptoPP)
25
26// ************************* Feature Probes ************************* //
27
28#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
29extern "C" {
30 typedef void (*SigHandler)(int);
31
32 static jmp_buf s_jmpSIGILL;
33 static void SigIllHandler(int)
34 {
35 longjmp(s_jmpSIGILL, 1);
36 }
37}
38#endif // CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
39
40#if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
41bool CPU_ProbePower7()
42{
43#if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
44 return false;
45#elif (_ARCH_PWR7) && defined(CRYPTOPP_POWER7_AVAILABLE)
46# if defined(CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY)
47
48 // longjmp and clobber warnings. Volatile is required.
49 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
50 volatile int result = false;
51
52 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
53 if (oldHandler == SIG_ERR)
54 return false;
55
56 volatile sigset_t oldMask;
57 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
58 return false;
59
60 if (setjmp(s_jmpSIGILL))
61 result = false;
62 else
63 {
64 // POWER7 added unaligned loads and store operations
65 byte b1[19] = {255, 255, 255, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, b2[17];
66
67 // Specifically call the VSX loads and stores
68 #if defined(__early_xlc__) || defined(__early_xlC__)
69 vec_xstw4(vec_xlw4(0, b1+3), 0, b2+1);
70 #elif defined(__xlc__) || defined(__xlC__) || defined(__clang__)
71 vec_xst(vec_xl(0, b1+3), 0, b2+1);
72 #else
73 vec_vsx_st(vec_vsx_ld(0, b1+3), 0, b2+1);
74 #endif
75
76 result = (0 == std::memcmp(b1+3, b2+1, 16));
77 }
78
79 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
80 signal(SIGILL, oldHandler);
81 return result;
82# endif
83#else
84 return false;
85#endif // _ARCH_PWR7
86}
87
88#endif // PPC32 or PPC64
89
90NAMESPACE_END
Library configuration file.
Crypto++ library namespace.
Precompiled header file.
Support functions for PowerPC and vector operations.