Skip to content

Commit 045a79e

Browse files
committed
Gate AVX-512 behind chacha20_avx512 cfg
1 parent 9f4b9c9 commit 045a79e

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

chacha20/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "chacha20"
33
version = "0.10.0-rc.2"
44
authors = ["RustCrypto Developers"]
55
edition = "2024"
6-
rust-version = "1.89"
6+
rust-version = "1.85"
77
documentation = "https://docs.rs/chacha20"
88
readme = "README.md"
99
repository = "https://github.com/RustCrypto/stream-ciphers"
@@ -49,6 +49,7 @@ rustdoc-args = ["--cfg", "docsrs"]
4949
[lints.rust.unexpected_cfgs]
5050
level = "warn"
5151
check-cfg = [
52+
'cfg(chacha20_avx512)',
5253
'cfg(chacha20_force_soft)',
5354
'cfg(chacha20_force_sse2)',
5455
'cfg(chacha20_force_avx2)',

chacha20/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ work on stable Rust with the following `RUSTFLAGS`:
3232
- `x86` / `x86_64`
3333
- `avx2`: (~1.4cpb) `-Ctarget-cpu=haswell -Ctarget-feature=+avx2`
3434
- `sse2`: (~1.6cpb) `-Ctarget-feature=+sse2` (on by default on x86 CPUs)
35+
- `avx512`: `-Ctarget-feature=+avx512f,+avx512vl --cfg chacha20_avx512` requires Rust 1.89+
3536
- `aarch64`
3637
- `neon` (~2-3x faster than `soft`) requires Rust 1.61+ and the `neon` feature enabled
3738
- Portable

chacha20/src/backends.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ cfg_if! {
77
pub(crate) mod soft;
88
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
99
cfg_if! {
10-
if #[cfg(chacha20_force_avx512)] {
10+
if #[cfg(all(chacha20_avx512, chacha20_force_avx512))] {
1111
pub(crate) mod avx512;
1212
} else if #[cfg(chacha20_force_avx2)] {
1313
pub(crate) mod avx2;
1414
} else if #[cfg(chacha20_force_sse2)] {
1515
pub(crate) mod sse2;
1616
} else {
1717
pub(crate) mod soft;
18+
#[cfg(chacha20_avx512)]
1819
pub(crate) mod avx512;
1920
pub(crate) mod avx2;
2021
pub(crate) mod sse2;

chacha20/src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ cfg_if! {
185185
type Tokens = ();
186186
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
187187
cfg_if! {
188-
if #[cfg(chacha20_force_avx512)] {
188+
if #[cfg(all(chacha20_avx512, chacha20_force_avx512))] {
189189
#[cfg(not(all(target_feature = "avx512f", target_feature = "avx512vl")))]
190-
compile_error!("You must enable `avx512f` target feature with \
190+
compile_error!("You must enable `avx512f` and `avx512vl` target features with \
191191
`chacha20_force_avx512` configuration option");
192192
type Tokens = ();
193193
} else if #[cfg(chacha20_force_avx2)] {
@@ -201,10 +201,14 @@ cfg_if! {
201201
`chacha20_force_sse2` configuration option");
202202
type Tokens = ();
203203
} else {
204+
#[cfg(chacha20_avx512)]
204205
cpufeatures::new!(avx512_cpuid, "avx512f", "avx512vl");
205206
cpufeatures::new!(avx2_cpuid, "avx2");
206207
cpufeatures::new!(sse2_cpuid, "sse2");
208+
#[cfg(chacha20_avx512)]
207209
type Tokens = (avx512_cpuid::InitToken, avx2_cpuid::InitToken, sse2_cpuid::InitToken);
210+
#[cfg(not(chacha20_avx512))]
211+
type Tokens = (avx2_cpuid::InitToken, sse2_cpuid::InitToken);
208212
}
209213
}
210214
} else {
@@ -259,8 +263,10 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
259263
let tokens = ();
260264
} else if #[cfg(chacha20_force_sse2)] {
261265
let tokens = ();
262-
} else {
266+
} else if #[cfg(chacha20_avx512)] {
263267
let tokens = (avx512_cpuid::init(), avx2_cpuid::init(), sse2_cpuid::init());
268+
} else {
269+
let tokens = (avx2_cpuid::init(), sse2_cpuid::init());
264270
}
265271
}
266272
} else {
@@ -306,7 +312,7 @@ impl<R: Rounds, V: Variant> StreamCipherCore for ChaChaCore<R, V> {
306312
f.call(&mut backends::soft::Backend(self));
307313
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
308314
cfg_if! {
309-
if #[cfg(chacha20_force_avx512)] {
315+
if #[cfg(all(chacha20_avx512, chacha20_force_avx512))] {
310316
unsafe {
311317
backends::avx512::inner::<R, _, V>(&mut self.state, f);
312318
}
@@ -319,12 +325,19 @@ impl<R: Rounds, V: Variant> StreamCipherCore for ChaChaCore<R, V> {
319325
backends::sse2::inner::<R, _, V>(&mut self.state, f);
320326
}
321327
} else {
328+
#[cfg(chacha20_avx512)]
322329
let (avx512_token, avx2_token, sse2_token) = self.tokens;
330+
#[cfg(not(chacha20_avx512))]
331+
let (avx2_token, sse2_token) = self.tokens;
332+
333+
#[cfg(chacha20_avx512)]
323334
if avx512_token.get() {
324335
unsafe {
325336
backends::avx512::inner::<R, _, V>(&mut self.state, f);
326337
}
327-
} else if avx2_token.get() {
338+
return;
339+
}
340+
if avx2_token.get() {
328341
unsafe {
329342
backends::avx2::inner::<R, _, V>(&mut self.state, f);
330343
}

0 commit comments

Comments
 (0)