Crypto++  6.1
Free C++ class library of cryptographic schemes
crc-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 SSE4.2 and
5 // ARMv8a CRC-32 and CRC-32C instructions. A separate source file
6 // is needed because additional CXXFLAGS are required to enable
7 // the appropriate instructions sets in some build configurations.
8 
9 #include "pch.h"
10 #include "config.h"
11 #include "misc.h"
12 
13 #if (CRYPTOPP_SSE42_AVAILABLE)
14 # include <nmmintrin.h>
15 #endif
16 
17 // Use ARMv8 rather than NEON due to compiler inconsistencies
18 #if (CRYPTOPP_ARM_CRC32_AVAILABLE)
19 # include <arm_neon.h>
20 #endif
21 
22 // Can't use CRYPTOPP_ARM_XXX_AVAILABLE because too many
23 // compilers don't follow ACLE conventions for the include.
24 #if defined(CRYPTOPP_ARM_ACLE_AVAILABLE)
25 # include <stdint.h>
26 # include <arm_acle.h>
27 #endif
28 
29 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
30 # include <signal.h>
31 # include <setjmp.h>
32 #endif
33 
34 #ifndef EXCEPTION_EXECUTE_HANDLER
35 # define EXCEPTION_EXECUTE_HANDLER 1
36 #endif
37 
38 NAMESPACE_BEGIN(CryptoPP)
39 
40 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
41 extern "C" {
42  typedef void (*SigHandler)(int);
43 
44  static jmp_buf s_jmpSIGILL;
45  static void SigIllHandler(int)
46  {
47  longjmp(s_jmpSIGILL, 1);
48  }
49 };
50 #endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
51 
52 #if (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64)
53 
54 bool CPU_ProbeCRC32()
55 {
56 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
57  return false;
58 #elif (CRYPTOPP_ARM_CRC32_AVAILABLE)
59 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
60  volatile bool result = true;
61  __try
62  {
63  word32 w=0, x=1; word16 y=2; byte z=3;
64  w = __crc32w(w,x);
65  w = __crc32h(w,y);
66  w = __crc32b(w,z);
67  w = __crc32cw(w,x);
68  w = __crc32ch(w,y);
69  w = __crc32cb(w,z);
70 
71  result = !!w;
72  }
73  __except (EXCEPTION_EXECUTE_HANDLER)
74  {
75  return false;
76  }
77  return result;
78 #else
79 
80  // longjmp and clobber warnings. Volatile is required.
81  // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
82  volatile bool result = true;
83 
84  volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
85  if (oldHandler == SIG_ERR)
86  return false;
87 
88  volatile sigset_t oldMask;
89  if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
90  return false;
91 
92  if (setjmp(s_jmpSIGILL))
93  result = false;
94  else
95  {
96  word32 w=0, x=1; word16 y=2; byte z=3;
97  w = __crc32w(w,x);
98  w = __crc32h(w,y);
99  w = __crc32b(w,z);
100  w = __crc32cw(w,x);
101  w = __crc32ch(w,y);
102  w = __crc32cb(w,z);
103 
104  // Hack... GCC optimizes away the code and returns true
105  result = !!w;
106  }
107 
108  sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
109  signal(SIGILL, oldHandler);
110  return result;
111 # endif
112 #else
113  return false;
114 #endif // CRYPTOPP_ARM_CRC32_AVAILABLE
115 }
116 #endif // ARM32 or ARM64
117 
118 #if (CRYPTOPP_ARM_CRC32_AVAILABLE)
119 void CRC32_Update_ARMV8(const byte *s, size_t n, word32& c)
120 {
121  for(; !IsAligned<word32>(s) && n > 0; s++, n--)
122  c = __crc32b(c, *s);
123 
124  for(; n > 4; s+=4, n-=4)
125  c = __crc32w(c, *(const word32 *)(void*)s);
126 
127  for(; n > 0; s++, n--)
128  c = __crc32b(c, *s);
129 }
130 
131 void CRC32C_Update_ARMV8(const byte *s, size_t n, word32& c)
132 {
133  for(; !IsAligned<word32>(s) && n > 0; s++, n--)
134  c = __crc32cb(c, *s);
135 
136  for(; n > 4; s+=4, n-=4)
137  c = __crc32cw(c, *(const word32 *)(void*)s);
138 
139  for(; n > 0; s++, n--)
140  c = __crc32cb(c, *s);
141 }
142 #endif
143 
144 #if (CRYPTOPP_SSE42_AVAILABLE)
145 void CRC32C_Update_SSE42(const byte *s, size_t n, word32& c)
146 {
147  for(; !IsAligned<word32>(s) && n > 0; s++, n--)
148  c = _mm_crc32_u8(c, *s);
149 
150  for(; n > 4; s+=4, n-=4)
151  c = _mm_crc32_u32(c, *(const word32 *)(void*)s);
152 
153  for(; n > 0; s++, n--)
154  c = _mm_crc32_u8(c, *s);
155 }
156 #endif
157 
158 NAMESPACE_END
Utility functions for the Crypto++ library.
Library configuration file.
Precompiled header file.
Crypto++ library namespace.