Crypto++  8.6
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 // For Linux and Unix additional flags are not required.
10 
11 #include "pch.h"
12 #include "config.h"
13 #include "stdcpp.h"
14 
15 #if (CRYPTOPP_ARM_NEON_HEADER)
16 # include <arm_neon.h>
17 #endif
18 
19 #if (CRYPTOPP_ARM_ACLE_HEADER)
20 # include <stdint.h>
21 # include <arm_acle.h>
22 #endif
23 
24 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
25 # include <signal.h>
26 # include <setjmp.h>
27 #endif
28 
29 #ifndef EXCEPTION_EXECUTE_HANDLER
30 # define EXCEPTION_EXECUTE_HANDLER 1
31 #endif
32 
33 // Squash MS LNK4221 and libtool warnings
34 extern const char NEON_SIMD_FNAME[] = __FILE__;
35 
36 NAMESPACE_BEGIN(CryptoPP)
37 
38 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
39 extern "C" {
40  typedef void (*SigHandler)(int);
41 
42  static jmp_buf s_jmpSIGILL;
43  static void SigIllHandler(int)
44  {
45  longjmp(s_jmpSIGILL, 1);
46  }
47 }
48 #endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
49 
50 bool CPU_ProbeARMv7()
51 {
52 #if defined(__aarch32__) || defined(__aarch64__) || defined(_M_ARM64)
53  return false;
54 #elif defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
55  return false;
56 #elif CRYPTOPP_ARM_NEON_AVAILABLE
57 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
58  volatile bool result = true;
59  __try
60  {
61  // Modern MS hardware is ARMv7
62  result = true;
63  }
64  __except (EXCEPTION_EXECUTE_HANDLER)
65  {
66  return false;
67  }
68  return result;
69 # else
70  // longjmp and clobber warnings. Volatile is required.
71  // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
72  volatile bool result = true;
73 
74  volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
75  if (oldHandler == SIG_ERR)
76  return false;
77 
78  volatile sigset_t oldMask;
79  if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
80  {
81  signal(SIGILL, oldHandler);
82  return false;
83  }
84 
85  if (setjmp(s_jmpSIGILL))
86  result = false;
87  else
88  {
89 
90 #if 0
91  // ARMv7 added movt and movw
92  int a;
93  asm volatile("movw %0,%1 \n"
94  "movt %0,%1 \n"
95  : "=r"(a) : "i"(0x1234));
96 
97 00000010 <_Z5test2v>: // ARM
98  10: e3010234 movw r0, #4660 ; 0x1234
99  14: e3410234 movt r0, #4660 ; 0x1234
100  18: e12fff1e bx lr
101 
102 0000001c <_Z5test3v>: // Thumb
103  1c: f241 2034 movw r0, #4660 ; 0x1234
104  20: f2c1 2034 movt r0, #4660 ; 0x1234
105  24: e12fff1e bx lr
106 #endif
107 
108  unsigned int a;
109  asm volatile (
110 #if defined(__thumb__)
111  ".inst.n 0xf241, 0x2034 \n\t" // movw r0, 0x1234
112  ".inst.n 0xf2c1, 0x2034 \n\t" // movt r0, 0x1234
113  "mov %0, r0 \n\t" // mov [a], r0
114 #else
115  ".inst 0xe3010234 \n\t" // movw r0, 0x1234
116  ".inst 0xe3410234 \n\t" // movt r0, 0x1234
117  "mov %0, r0 \n\t" // mov [a], r0
118 #endif
119  : "=r" (a) : : "r0");
120 
121  result = (a == 0x12341234);
122  }
123 
124  sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
125  signal(SIGILL, oldHandler);
126  return result;
127 # endif
128 #else
129  return false;
130 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
131 }
132 
133 bool CPU_ProbeNEON()
134 {
135 #if defined(__aarch32__) || defined(__aarch64__) || defined(_M_ARM64)
136  return true;
137 #elif defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
138  return false;
139 #elif CRYPTOPP_ARM_NEON_AVAILABLE
140 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
141  volatile bool result = true;
142  __try
143  {
144  uint32x4_t x = vdupq_n_u32(1);
145  uint32x4_t y = vshlq_n_u32(x, 4);
146 
147  word32 z[4]; vst1q_u32(z, y);
148  return (z[0] & z[1] & z[2] & z[3]) == 16;
149  }
150  __except (EXCEPTION_EXECUTE_HANDLER)
151  {
152  return false;
153  }
154  return result;
155 # else
156  // longjmp and clobber warnings. Volatile is required.
157  // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
158  volatile bool result = true;
159 
160  volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
161  if (oldHandler == SIG_ERR)
162  return false;
163 
164  volatile sigset_t oldMask;
165  if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
166  {
167  signal(SIGILL, oldHandler);
168  return false;
169  }
170 
171  if (setjmp(s_jmpSIGILL))
172  result = false;
173  else
174  {
175  // This is risky... When we hand encode the instructions
176  // for vmov.u32 and vshl.u32 we get a SIGILL. Apparently
177  // we need more than just the instructions. Using
178  // intrinsics introduces the risk because the whole
179  // file gets built with ISA options, and the higher ISA
180  // may escape the try block with the SIGILL guard.
181  uint32x4_t x = vdupq_n_u32(1);
182  uint32x4_t y = vshlq_n_u32(x, 4);
183 
184  word32 z[4]; vst1q_u32(z, y);
185  result = (z[0] & z[1] & z[2] & z[3]) == 16;
186  }
187 
188  sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
189  signal(SIGILL, oldHandler);
190  return result;
191 # endif
192 #else
193  return false;
194 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
195 }
196 
197 NAMESPACE_END
pch.h
Precompiled header file.
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
stdcpp.h
Common C++ header files.
CryptoPP
Crypto++ library namespace.
config.h
Library configuration file.