Crypto++ 7.0 Free C++ class library of cryptographic schemes

neon_simd.cpp
1
2 // crc_simd.cpp - written and placed in the public domain by
3 // Jeffrey Walton, Uri Blumenthal and Marcel Raad.
4 //
5 // This source file uses intrinsics to gain access to ARMv7a and
6 // ARMv8a NEON instructions. A separate source file is needed
7 // because additional CXXFLAGS are required to enable the
8 // appropriate instructions sets in some build configurations.
9
10 #include "pch.h"
11 #include "config.h"
12 #include "stdcpp.h"
13
14 #if (CRYPTOPP_ARM_NEON_AVAILABLE)
15 # include <arm_neon.h>
16 #endif
17
18 // Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many
19 // compilers don't follow ACLE conventions for the include.
20 #if (CRYPTOPP_ARM_ACLE_AVAILABLE)
21 # include <stdint.h>
22 # include <arm_acle.h>
23 #endif
24
25 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
26 # include <signal.h>
27 # include <setjmp.h>
28 #endif
29
30 #ifndef EXCEPTION_EXECUTE_HANDLER
31 # define EXCEPTION_EXECUTE_HANDLER 1
32 #endif
33
34 // Squash MS LNK4221 and libtool warnings
35 extern const char NEON_SIMD_FNAME[] = __FILE__;
36
37 NAMESPACE_BEGIN(CryptoPP)
38
39 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
40 extern "C" {
41 typedef void (*SigHandler)(int);
42
43 static jmp_buf s_jmpSIGILL;
44 static void SigIllHandler(int)
45 {
46 longjmp(s_jmpSIGILL, 1);
47 }
48 }
49 #endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
50
51 bool CPU_ProbeARMv7()
52 {
53 #if defined(__aarch32__) || defined(__aarch64__)
54 return true;
55 #elif defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
56 return false;
57 #elif (CRYPTOPP_ARM_NEON_AVAILABLE)
58 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
59 volatile bool result = true;
60 __try
61 {
62 // Modern MS hardware is ARMv7
63 result = true;
64 }
65 __except (EXCEPTION_EXECUTE_HANDLER)
66 {
67 return false;
68 }
69 return result;
70 # elif defined(__arm__) && (__ARM_ARCH >= 7)
71 // longjmp and clobber warnings. Volatile is required.
72 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
73 volatile bool result = true;
74
75 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
76 if (oldHandler == SIG_ERR)
77 return false;
78
79 volatile sigset_t oldMask;
80 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
81 return false;
82
83 if (setjmp(s_jmpSIGILL))
84 result = false;
85 else
86 {
87 // ARMv7 added movt and movw
88 int a;
89 asm volatile("movw %0,%1 \n"
90 "movt %0,%1 \n"
91 : "=r"(a) : "i"(0x1234));
92 result = (a == 0x12341234);
93 }
94
95 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
96 signal(SIGILL, oldHandler);
97 return result;
98 # else
99 return false;
100 # endif
101 #else
102 return false;
103 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
104 }
105
106 bool CPU_ProbeNEON()
107 {
108 #if defined(__aarch32__) || defined(__aarch64__)
109 return true;
110 #elif defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
111 return false;
112 #elif (CRYPTOPP_ARM_NEON_AVAILABLE)
113 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
114 volatile bool result = true;
115 __try
116 {
117 uint32_t v1[4] = {1,1,1,1};
118 uint32x4_t x1 = vld1q_u32(v1);
119 uint64_t v2[2] = {1,1};
120 uint64x2_t x2 = vld1q_u64(v2);
121
122 uint32x4_t x3 = vdupq_n_u32(2);
123 x3 = vsetq_lane_u32(vgetq_lane_u32(x1,0),x3,0);
124 x3 = vsetq_lane_u32(vgetq_lane_u32(x1,3),x3,3);
125 uint64x2_t x4 = vdupq_n_u64(2);
126 x4 = vsetq_lane_u64(vgetq_lane_u64(x2,0),x4,0);
127 x4 = vsetq_lane_u64(vgetq_lane_u64(x2,1),x4,1);
128
129 result = !!(vgetq_lane_u32(x3,0) | vgetq_lane_u64(x4,1));
130 }
131 __except (EXCEPTION_EXECUTE_HANDLER)
132 {
133 return false;
134 }
135 return result;
136 # else
137
138 // longjmp and clobber warnings. Volatile is required.
139 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
140 volatile bool result = true;
141
142 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
143 if (oldHandler == SIG_ERR)
144 return false;
145
146 volatile sigset_t oldMask;
147 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
148 return false;
149
150 if (setjmp(s_jmpSIGILL))
151 result = false;
152 else
153 {
154 uint32_t v1[4] = {1,1,1,1};
155 uint32x4_t x1 = vld1q_u32(v1);
156 uint64_t v2[2] = {1,1};
157 uint64x2_t x2 = vld1q_u64(v2);
158
159 uint32x4_t x3 = {0,0,0,0};
160 x3 = vsetq_lane_u32(vgetq_lane_u32(x1,0),x3,0);
161 x3 = vsetq_lane_u32(vgetq_lane_u32(x1,3),x3,3);
162 uint64x2_t x4 = {0,0};
163 x4 = vsetq_lane_u64(vgetq_lane_u64(x2,0),x4,0);
164 x4 = vsetq_lane_u64(vgetq_lane_u64(x2,1),x4,1);
165
166 // Hack... GCC optimizes away the code and returns true
167 result = !!(vgetq_lane_u32(x3,0) | vgetq_lane_u64(x4,1));
168 }
169
170 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
171 signal(SIGILL, oldHandler);
172 return result;
173 # endif
174 #else
175 return false;
176 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
177 }
178
179 NAMESPACE_END
config.h
Library configuration file.
stdcpp.h
Common C++ header files.
pch.h
Precompiled header file.
CryptoPP
Crypto++ library namespace.