Crypto++ 8.2 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_NEON_HEADER)
18 # include <arm_neon.h>
19 #endif
20
21 #if (CRYPTOPP_ARM_ACLE_HEADER)
22 # include <stdint.h>
23 # include <arm_acle.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 // Squash MS LNK4221 and libtool warnings
36 extern const char CRC_SIMD_FNAME[] = __FILE__;
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_ARMV8)
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 {
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; word16 y=2; byte z=3;
100 w = __crc32w(w,x);
101 w = __crc32h(w,y);
102 w = __crc32b(w,z);
103 w = __crc32cw(w,x);
104 w = __crc32ch(w,y);
105 w = __crc32cb(w,z);
106
107 // Hack... GCC optimizes away the code and returns true
108 result = !!w;
109 }
110
111 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
112 signal(SIGILL, oldHandler);
113 return result;
114 # endif
115 #else
116 return false;
117 #endif // CRYPTOPP_ARM_CRC32_AVAILABLE
118 }
119 #endif // ARM32 or ARM64
120
121 #if (CRYPTOPP_ARM_CRC32_AVAILABLE)
122 void CRC32_Update_ARMV8(const byte *s, size_t n, word32& c)
123 {
124 for(; !IsAligned<word32>(s) && n > 0; s++, n--)
125 c = __crc32b(c, *s);
126
127 for(; n > 4; s+=4, n-=4)
128 c = __crc32w(c, *(const word32 *)(void*)s);
129
130 for(; n > 0; s++, n--)
131 c = __crc32b(c, *s);
132 }
133
134 void CRC32C_Update_ARMV8(const byte *s, size_t n, word32& c)
135 {
136 for(; !IsAligned<word32>(s) && n > 0; s++, n--)
137 c = __crc32cb(c, *s);
138
139 for(; n > 4; s+=4, n-=4)
140 c = __crc32cw(c, *(const word32 *)(void*)s);
141
142 for(; n > 0; s++, n--)
143 c = __crc32cb(c, *s);
144 }
145 #endif
146
147 #if (CRYPTOPP_SSE42_AVAILABLE)
148 void CRC32C_Update_SSE42(const byte *s, size_t n, word32& c)
149 {
150 for(; !IsAligned<word32>(s) && n > 0; s++, n--)
151 c = _mm_crc32_u8(c, *s);
152
153 for(; n > 4; s+=4, n-=4)
154 c = _mm_crc32_u32(c, *(const word32 *)(void*)s);
155
156 for(; n > 0; s++, n--)
157 c = _mm_crc32_u8(c, *s);
158 }
159 #endif
160
161 NAMESPACE_END
misc.h
Utility functions for the Crypto++ library.
config.h
Library configuration file.
pch.h
Precompiled header file.
CryptoPP
Crypto++ library namespace.