Crypto++ 8.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 // C1189: error: This header is specific to ARM targets
18 #if (CRYPTOPP_ARM_NEON_AVAILABLE) && !defined(_M_ARM64)
19 # include <arm_neon.h>
20 #endif
21
22 #if (CRYPTOPP_ARM_ACLE_AVAILABLE)
23 # include <stdint.h>
24 # include <arm_acle.h>
25 #endif
26
27 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
28 # include <signal.h>
29 # include <setjmp.h>
30 #endif
31
32 #ifndef EXCEPTION_EXECUTE_HANDLER
33 # define EXCEPTION_EXECUTE_HANDLER 1
34 #endif
35
36 // Squash MS LNK4221 and libtool warnings
37 extern const char CRC_SIMD_FNAME[] = __FILE__;
38
39 NAMESPACE_BEGIN(CryptoPP)
40
41 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
42 extern "C" {
43 typedef void (*SigHandler)(int);
44
45 static jmp_buf s_jmpSIGILL;
46 static void SigIllHandler(int)
47 {
48 longjmp(s_jmpSIGILL, 1);
49 }
50 }
51 #endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
52
53 #if (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8)
54
55 bool CPU_ProbeCRC32()
56 {
57 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
58 return false;
59 #elif (CRYPTOPP_ARM_CRC32_AVAILABLE)
60 # if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY)
61 volatile bool result = true;
62 __try
63 {
64 word32 w=0, x=1; word16 y=2; byte z=3;
65 w = __crc32w(w,x);
66 w = __crc32h(w,y);
67 w = __crc32b(w,z);
68 w = __crc32cw(w,x);
69 w = __crc32ch(w,y);
70 w = __crc32cb(w,z);
71
72 result = !!w;
73 }
74 __except (EXCEPTION_EXECUTE_HANDLER)
75 {
76 return false;
77 }
78 return result;
79 #else
80
81 // longjmp and clobber warnings. Volatile is required.
82 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
83 volatile bool result = true;
84
85 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
86 if (oldHandler == SIG_ERR)
87 return false;
88
89 volatile sigset_t oldMask;
90 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
91 return false;
92
93 if (setjmp(s_jmpSIGILL))
94 result = false;
95 else
96 {
97 word32 w=0, x=1; word16 y=2; byte z=3;
98 w = __crc32w(w,x);
99 w = __crc32h(w,y);
100 w = __crc32b(w,z);
101 w = __crc32cw(w,x);
102 w = __crc32ch(w,y);
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 > 4; s+=4, n-=4)
126 c = __crc32w(c, *(const word32 *)(void*)s);
127
128 for(; n > 0; s++, n--)
129 c = __crc32b(c, *s);
130 }
131
132 void CRC32C_Update_ARMV8(const byte *s, size_t n, word32& c)
133 {
134 for(; !IsAligned<word32>(s) && n > 0; s++, n--)
135 c = __crc32cb(c, *s);
136
137 for(; n > 4; s+=4, n-=4)
138 c = __crc32cw(c, *(const word32 *)(void*)s);
139
140 for(; n > 0; s++, n--)
141 c = __crc32cb(c, *s);
142 }
143 #endif
144
145 #if (CRYPTOPP_SSE42_AVAILABLE)
146 void CRC32C_Update_SSE42(const byte *s, size_t n, word32& c)
147 {
148 for(; !IsAligned<word32>(s) && n > 0; s++, n--)
149 c = _mm_crc32_u8(c, *s);
150
151 for(; n > 4; s+=4, n-=4)
152 c = _mm_crc32_u32(c, *(const word32 *)(void*)s);
153
154 for(; n > 0; s++, n--)
155 c = _mm_crc32_u8(c, *s);
156 }
157 #endif
158
159 NAMESPACE_END
misc.h
Utility functions for the Crypto++ library.
config.h
Library configuration file.
pch.h
Precompiled header file.
CryptoPP
Crypto++ library namespace.