-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
230 lines (174 loc) · 7.42 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#![allow(dead_code)]
#![allow(unused)]
extern crate cc;
use cc::Build;
use std::env;
#[cfg(target_arch = "aarch64")]
use std::arch::is_aarch64_feature_detected;
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use std::arch::is_x86_feature_detected;
fn main() {
// Windows doesn't build the C bindings automatically, and since they're auto-generated from
// another project, I'm not inclined to fix it. The Rust implementation is still very fast.
#[cfg(target_os = "windows")]
return;
// build hardware optimized version
build_optimized();
}
/// Builds hardware-optimized versions of the CRC32 functions
fn build_optimized() {
// in build scripts, the target architecture is only available via an environment variable
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if "aarch64" == target_arch {
return build_optimized_aarch64();
}
if "x86_64" == target_arch || "x86" == target_arch {
build_optimized_x86()
}
// fall back to Rust implementation
}
fn build_optimized_target_crc32_iscsi(name: &str, flags: &[String]) {
build_optimized_target(name, flags);
println!("cargo:rustc-cfg=optimized_crc32_iscsi");
}
fn build_optimized_target_crc32_iso_hdlc(name: &str, flags: &[String]) {
build_optimized_target(name, flags);
println!("cargo:rustc-cfg=optimized_crc32_iso_hdlc");
}
fn build_optimized_target(name: &str, flags: &[String]) {
// Create a longer-lived binding as suggested by the error message
let mut binding = Build::new();
let mut build = binding.file(format!("include/{name}.c")).include("include");
// Apply each flag individually
for flag in flags {
build = build.flag(flag);
}
build.compile(name);
}
fn build_optimized_aarch64() {
// feature flag overrides to allow forcing a specific implementation
// NEON EOR3, which seems to be faster for larger payloads,
// but slower for smaller ones than v12e_v1
#[cfg(feature = "optimize_crc32_neon_eor3_v9s3x2e_s3")]
return build_neon_eor3_v9s3x2e_s3();
// NEON w/o EOR3, tuned for Apple M1, which is MUCH faster at smaller payloads, and slightly
// slower at larger ones, on my Apple M2 Ultra
#[cfg(feature = "optimize_crc32_neon_v12e_v1")]
return build_neon_v12e_v1();
// NEON w/o EOR3, tuned for Ampere Altra Arm (GCP Tau T2A)
#[cfg(feature = "optimize_crc32_neon_v3s4x2e_v2")]
return build_neon_v3s4x2e_v2();
// NEON w/EOR3 for large payloads (>1KiB), NEON w/o EOR3 for small ones
#[cfg(feature = "optimize_crc32_neon_blended")]
return build_neon_blended();
// no auto-optimize enabled, return and use the internal Rust implementation
#[cfg(feature = "optimize_crc32_auto")]
{
// for auto, default to NEON blended with EOR3 for large (>1KiB) payloads, w/o EOR3 for
// small ones
#[allow(unreachable_code)]
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
if is_aarch64_feature_detected!("crc") && is_aarch64_feature_detected!("sha3") {
return build_neon_blended();
}
// for auto, fallback to non-EOR3 if SHA3 is not available
#[allow(unreachable_code)]
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
if is_aarch64_feature_detected!("crc") {
build_neon_v12e_v1()
}
}
// fall through to internal Rust implementation
}
fn build_neon_blended() {
println!("Building NEON blended");
let flags = [String::from("-march=armv8.2-a+crypto+crc+sha3")];
build_optimized_target_crc32_iscsi("crc32_iscsi_neon_blended", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_neon_blended", &flags);
}
fn build_neon_eor3_v9s3x2e_s3() {
println!("Building NEON EOR3 v9s3x2e s3");
let flags = [String::from("-march=armv8.2-a+crypto+crc+sha3")];
build_optimized_target_crc32_iscsi("crc32_iscsi_neon_eor3_v9s3x2e_s3", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_neon_eor3_v9s3x2e_s3", &flags);
}
fn build_neon_v12e_v1() {
println!("Building NEON v12e v1");
let flags = [String::from("-march=armv8-a+crypto+crc")];
build_optimized_target_crc32_iscsi("crc32_iscsi_neon_v12e_v1", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_neon_v12e_v1", &flags);
}
fn build_neon_v3s4x2e_v2() {
println!("Building NEON v12e v1");
let flags = [String::from("-march=armv8-a+crypto+crc")];
build_optimized_target_crc32_iscsi("crc32_iscsi_neon_v3s4x2e_v2", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_neon_v3s4x2e_v2", &flags);
}
fn build_optimized_x86() {
// feature flag overrides to allow forcing a specific implementation
#[cfg(feature = "optimize_crc32_avx512_vpclmulqdq_v3x2")]
return build_avx512_vpclmulqdq_v3x2();
#[cfg(feature = "optimize_crc32_avx512_v4s3x3")]
return build_avx512_v4s3x3();
#[cfg(feature = "optimize_crc32_sse_v4s3x3")]
return build_sse_v4s3x3();
// no auto-optimize enabled, return and use the internal Rust implementation
#[cfg(feature = "optimize_crc32_auto")]
{
// for auto, default to the best available implementation based on CPU features
// in build scripts, the target architecture is only available via an environment variable
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if "x86" == target_arch {
// this is the only one supported on 32-bit x86 systems
crate::build_sse_v4s3x3()
}
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
if is_x86_feature_detected!("vpclmulqdq")
&& is_x86_feature_detected!("avx512vl")
&& is_x86_feature_detected!("avx512f")
{
return build_avx512_vpclmulqdq_v3x2();
}
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
if is_x86_feature_detected!("avx512vl")
&& is_x86_feature_detected!("avx512f")
&& is_x86_feature_detected!("pclmulqdq")
{
return crate::build_avx512_v4s3x3();
}
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
crate::build_sse_v4s3x3()
}
}
// fall through to internal Rust implementation
}
fn build_avx512_vpclmulqdq_v3x2() {
println!("Building AVX512 VPCLMULQDQ v3x2");
let flags = [
String::from("-msse4.2"),
String::from("-mpclmul"),
String::from("-mavx512f"),
String::from("-mavx512vl"),
String::from("-mvpclmulqdq"),
];
build_optimized_target_crc32_iscsi("crc32_iscsi_avx512_vpclmulqdq_v3x2", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_avx512_vpclmulqdq_v3x2", &flags);
}
fn build_avx512_v4s3x3() {
println!("Building AVX512 v4s3x3");
let flags = [
String::from("-msse4.2"),
String::from("-mpclmul"),
String::from("-mavx512f"),
String::from("-mavx512vl"),
];
build_optimized_target_crc32_iscsi("crc32_iscsi_avx512_v4s3x3", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_avx512_v4s3x3", &flags);
}
fn build_sse_v4s3x3() {
println!("Building SSE v4s3x3 for x86 / x86_64");
let flags = [String::from("-msse4.2"), String::from("-mpclmul")];
build_optimized_target_crc32_iscsi("crc32_iscsi_sse_v4s3x3", &flags);
build_optimized_target_crc32_iso_hdlc("crc32_iso_hdlc_sse_v4s3x3", &flags);
}