Crypto++  7.0
Free C++ class library of cryptographic schemes
neon-simd.cpp
1 // crc-simd.cpp - written and placed in the public domain by
2 // Jeffrey Walton, Uri Blumenthal and Marcel Raad.
3 //
4 // This source file uses intrinsics to gain access to ARMv7a and
5 // ARMv8a NEON instructions. A separate source file is needed
6 // because additional CXXFLAGS are required to enable the
7 // appropriate instructions sets in some build configurations.
8 
9 #include "pch.h"
10 #include "config.h"
11 #include "stdcpp.h"
12 
13 #if (CRYPTOPP_ARM_NEON_AVAILABLE)
14 # include <arm_neon.h>
15 #endif
16 
17 // Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many
18 // compilers don't follow ACLE conventions for the include.
19 #if defined(CRYPTOPP_ARM_ACLE_AVAILABLE)
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 NAMESPACE_BEGIN(CryptoPP)
34 
35 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
36 extern "C" {
37  typedef void (*SigHandler)(int);
38 
39  static jmp_buf s_jmpSIGILL;
40  static void SigIllHandler(int)
41  {
42  longjmp(s_jmpSIGILL, 1);
43  }
44 }
45 #endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
46 
47 bool CPU_ProbeNEON()
48 {
49 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
50  return false;
51 #elif (CRYPTOPP_ARM_NEON_AVAILABLE)
52 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
53  volatile bool result = true;
54  __try
55  {
56  uint32_t v1[4] = {1,1,1,1};
57  uint32x4_t x1 = vld1q_u32(v1);
58  uint64_t v2[2] = {1,1};
59  uint64x2_t x2 = vld1q_u64(v2);
60 
61  uint32x4_t x3 = vdupq_n_u32(2);
62  x3 = vsetq_lane_u32(vgetq_lane_u32(x1,0),x3,0);
63  x3 = vsetq_lane_u32(vgetq_lane_u32(x1,3),x3,3);
64  uint64x2_t x4 = vdupq_n_u64(2);
65  x4 = vsetq_lane_u64(vgetq_lane_u64(x2,0),x4,0);
66  x4 = vsetq_lane_u64(vgetq_lane_u64(x2,1),x4,1);
67 
68  result = !!(vgetq_lane_u32(x3,0) | vgetq_lane_u64(x4,1));
69  }
70  __except (EXCEPTION_EXECUTE_HANDLER)
71  {
72  return false;
73  }
74  return result;
75 # else
76 
77  // longjmp and clobber warnings. Volatile is required.
78  // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
79  volatile bool result = true;
80 
81  volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
82  if (oldHandler == SIG_ERR)
83  return false;
84 
85  volatile sigset_t oldMask;
86  if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
87  return false;
88 
89  if (setjmp(s_jmpSIGILL))
90  result = false;
91  else
92  {
93  uint32_t v1[4] = {1,1,1,1};
94  uint32x4_t x1 = vld1q_u32(v1);
95  uint64_t v2[2] = {1,1};
96  uint64x2_t x2 = vld1q_u64(v2);
97 
98  uint32x4_t x3 = {0,0,0,0};
99  x3 = vsetq_lane_u32(vgetq_lane_u32(x1,0),x3,0);
100  x3 = vsetq_lane_u32(vgetq_lane_u32(x1,3),x3,3);
101  uint64x2_t x4 = {0,0};
102  x4 = vsetq_lane_u64(vgetq_lane_u64(x2,0),x4,0);
103  x4 = vsetq_lane_u64(vgetq_lane_u64(x2,1),x4,1);
104 
105  // Hack... GCC optimizes away the code and returns true
106  result = !!(vgetq_lane_u32(x3,0) | vgetq_lane_u64(x4,1));
107  }
108 
109  sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
110  signal(SIGILL, oldHandler);
111  return result;
112 # endif
113 #else
114  return false;
115 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
116 }
117 
118 NAMESPACE_END
Library configuration file.
Common C++ header files.
Precompiled header file.
Crypto++ library namespace.