Crypto++  8.6
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 #if (CRYPTOPP_ARM_ACLE_HEADER)
18 # include <stdint.h>
19 # include <arm_acle.h>
20 #endif
21 
22 #if (CRYPTOPP_ARM_CRC32_AVAILABLE)
23 # include "arm_simd.h"
24 #endif
25 
26 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
27 # include <signal.h>
28 # include <setjmp.h>
29 #endif
30 
31 #ifndef EXCEPTION_EXECUTE_HANDLER
32 # define EXCEPTION_EXECUTE_HANDLER 1
33 #endif
34 
35 #define CONST_WORD32_CAST(x) ((const word32 *)(void*)(x))
36 
37 // Squash MS LNK4221 and libtool warnings
38 extern const char CRC_SIMD_FNAME[] = __FILE__;
39 
40 NAMESPACE_BEGIN(CryptoPP)
41 
42 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
43 extern "C" {
44  typedef void (*SigHandler)(int);
45 
46  static jmp_buf s_jmpSIGILL;
47  static void SigIllHandler(int)
48  {
49  longjmp(s_jmpSIGILL, 1);
50  }
51 }
52 #endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
53 
54 #if (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8)
55 
56 bool CPU_ProbeCRC32()
57 {
58 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
59  return false;
60 #elif (CRYPTOPP_ARM_CRC32_AVAILABLE)
61 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
62  volatile bool result = true;
63  __try
64  {
65  word32 w=0, x=1; byte z=3;
66  w = CRC32W(w,x);
67  w = CRC32B(w,z);
68  w = CRC32CW(w,x);
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  {
91  signal(SIGILL, oldHandler);
92  return false;
93  }
94 
95  if (setjmp(s_jmpSIGILL))
96  result = false;
97  else
98  {
99  word32 w=0, x=1; byte z=3;
100  w = CRC32W(w,x);
101  w = CRC32B(w,z);
102  w = CRC32CW(w,x);
103  w = CRC32CB(w,z);
104 
105  // Hack... GCC optimizes away the code and returns true
106  result = !!w;
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_CRC32_AVAILABLE
116 }
117 #endif // ARM32 or ARM64
118 
119 #if (CRYPTOPP_ARM_CRC32_AVAILABLE)
120 void CRC32_Update_ARMV8(const byte *s, size_t n, word32& c)
121 {
122  for(; !IsAligned<word32>(s) && n > 0; s++, n--)
123  c = CRC32B(c, *s);
124 
125  for(; n >= 16; s+=16, n-=16)
126  c = CRC32Wx4(c, CONST_WORD32_CAST(s));
127 
128  for(; n >= 4; s+=4, n-=4)
129  c = CRC32W(c, *CONST_WORD32_CAST(s));
130 
131  for(; n > 0; s++, n--)
132  c = CRC32B(c, *s);
133 }
134 
135 void CRC32C_Update_ARMV8(const byte *s, size_t n, word32& c)
136 {
137  for(; !IsAligned<word32>(s) && n > 0; s++, n--)
138  c = CRC32CB(c, *s);
139 
140  for(; n >= 16; s+=16, n-=16)
141  c = CRC32CWx4(c, CONST_WORD32_CAST(s));
142 
143  for(; n >= 4; s+=4, n-=4)
144  c = CRC32CW(c, *CONST_WORD32_CAST(s));
145 
146  for(; n > 0; s++, n--)
147  c = CRC32CB(c, *s);
148 }
149 #endif
150 
151 #if (CRYPTOPP_SSE42_AVAILABLE)
152 void CRC32C_Update_SSE42(const byte *s, size_t n, word32& c)
153 {
154  for(; !IsAligned<word32>(s) && n > 0; s++, n--)
155  c = _mm_crc32_u8(c, *s);
156 
157  for(; n >= 16; s+=16, n-=16)
158  {
159  c = _mm_crc32_u32(_mm_crc32_u32(_mm_crc32_u32(_mm_crc32_u32(c,
160  *CONST_WORD32_CAST(s+ 0)), *CONST_WORD32_CAST(s+ 4)),
161  *CONST_WORD32_CAST(s+ 8)), *CONST_WORD32_CAST(s+12));
162  }
163 
164  for(; n >= 4; s+=4, n-=4)
165  c = _mm_crc32_u32(c, *CONST_WORD32_CAST(s));
166 
167  for(; n > 0; s++, n--)
168  c = _mm_crc32_u8(c, *s);
169 }
170 #endif
171 
172 NAMESPACE_END
Utility functions for the Crypto++ library.
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
Library configuration file.
uint32_t CRC32W(uint32_t crc, uint32_t val)
CRC32 checksum.
Definition: arm_simd.h:46
uint32_t CRC32B(uint32_t crc, uint8_t val)
CRC32 checksum.
Definition: arm_simd.h:30
Precompiled header file.
uint32_t CRC32CWx4(uint32_t crc, const uint32_t vals[4])
CRC32-C checksum.
Definition: arm_simd.h:118
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:56
Crypto++ library namespace.
uint32_t CRC32CB(uint32_t crc, uint8_t val)
CRC32-C checksum.
Definition: arm_simd.h:86
uint32_t CRC32Wx4(uint32_t crc, const uint32_t vals[4])
CRC32 checksum.
Definition: arm_simd.h:62
uint32_t CRC32CW(uint32_t crc, uint32_t val)
CRC32-C checksum.
Definition: arm_simd.h:102
Support functions for ARM and vector operations.