Crypto++ 7.0 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_AVAILABLE)
18 # include <arm_neon.h>
19 #endif
20
21 #if (CRYPTOPP_ARM_ACLE_AVAILABLE)
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 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
misc.h
Utility functions for the Crypto++ library.
config.h
Library configuration file.
pch.h
Precompiled header file.
CryptoPP
Crypto++ library namespace.