Skip to content

Commit

Permalink
Detect when CPU supports AVX(2), but feature not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernauer committed Jun 17, 2023
1 parent caa118b commit 47e56ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use breakwater::{
};
use clap::Parser;
use env_logger::Env;
use log::warn;
use std::sync::Arc;
use tokio::sync::{broadcast, mpsc};
#[cfg(feature = "vnc")]
Expand All @@ -22,14 +21,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let args = Args::parse();

#[cfg(target_arch = "x86_64")]
{
if !is_x86_feature_detected!("avx2") {
warn!("Your CPU does not support AVX2. Consider using a CPU supporting AVX2 for best performance");
} else if !is_x86_feature_detected!("avx") {
warn!("Your CPU does not support AVX. Consider using a CPU supporting AVX2 (or at least AVX) for best performance");
}
}
breakwater::parser::check_cpu_support();

let fb = Arc::new(FrameBuffer::new(args.width, args.height));

Expand Down
20 changes: 20 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::framebuffer::FrameBuffer;
use const_format::formatcp;
use log::{info, warn};
use std::simd::{u32x8, Simd, SimdUint};
use std::sync::Arc;
use tokio::io::AsyncWriteExt;
Expand Down Expand Up @@ -356,6 +357,25 @@ fn simd_unhex(value: &[u8]) -> u32 {
shifted.reduce_or()
}

pub fn check_cpu_support() {
#[cfg(target_arch = "x86_64")]
{
if !is_x86_feature_detected!("avx2") {
warn!("Your CPU does not support AVX2. Consider using a CPU supporting AVX2 for best performance");
} else if !is_x86_feature_detected!("avx") {
warn!("Your CPU does not support AVX. Consider using a CPU supporting AVX2 (or at least AVX) for best performance");
} else {
// At this point the CPU should support AVX und AVX2
// Warn the user when he has compiled breakwater without the needed target features
if cfg!(all(target_feature = "avx", target_feature = "avx2")) {
info!("Using AVX and AVX2 support");
} else {
warn!("Your CPU does support AVX and AVX2, but you have not enabled avx and avx2 support. Please re-compile using RUSTFLAGS='-C target-cpu=native' cargo build --release`");
}
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 47e56ad

Please sign in to comment.