Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor out RISCV support from the guts_api branch #375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rust/guts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ documentation = "https://docs.rs/blake3_guts"
readme = "readme.md"
edition = "2021"

[dependencies]
cfg-if = "1.0.0"

[dev-dependencies]
hex = "0.4.3"
reference_impl = { path = "../../reference_impl" }

[features]
default = ["std"]
std = []

[build-dependencies]
cc = "1.0.79"
59 changes: 59 additions & 0 deletions rust/guts/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::env;

fn defined(var: &str) -> bool {
println!("cargo:rerun-if-env-changed={}", var);
env::var_os(var).is_some()
}

fn is_pure() -> bool {
defined("CARGO_FEATURE_PURE")
}

fn target_components() -> Vec<String> {
let target = env::var("TARGET").unwrap();
target.split("-").map(|s| s.to_string()).collect()
}

fn is_riscv64gc() -> bool {
target_components()[0] == "riscv64gc"
}

fn new_build() -> cc::Build {
let build = cc::Build::new();
build
}

fn build_riscv_rva23u64_assembly() {
println!("cargo:rustc-cfg=blake3_riscv_rva23u64_ffi");
let mut build = new_build();
let asm_path = "src/riscv_rva23u64.S";
build.file(asm_path);
build.flag("--target=riscv64");
build.flag("-march=rv64gcv_zbb_zvbb1p0");
build.flag("-menable-experimental-extensions");
build.compile("blake3_riscv_rva23u64_assembly");
println!("cargo:rerun-if-changed={asm_path}");
}

fn main() {
// TODO: This implementation assumes some bleeding-edge extensions, and it should probably be
// gated by a Cargo feature.
if is_riscv64gc() && !is_pure() {
build_riscv_rva23u64_assembly();
}

// The `cc` crate doesn't automatically emit rerun-if directives for the
// environment variables it supports, in particular for $CC. We expect to
// do a lot of benchmarking across different compilers, so we explicitly
// add the variables that we're likely to need.
println!("cargo:rerun-if-env-changed=CC");
println!("cargo:rerun-if-env-changed=CFLAGS");

// Ditto for source files, though these shouldn't change as often.
for file in std::fs::read_dir("../../c").unwrap() {
println!(
"cargo:rerun-if-changed={}",
file.unwrap().path().to_str().expect("utf-8")
);
}
}
17 changes: 15 additions & 2 deletions rust/guts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ use core::ptr;
use core::sync::atomic::{AtomicPtr, Ordering::Relaxed};

pub mod portable;
#[cfg(any(target_arch = "riscv64"))]
pub mod riscv_rva23u64;

#[cfg(test)]
mod test;
Expand Down Expand Up @@ -82,8 +84,14 @@ pub const MSG_SCHEDULE: [[usize; 16]; 7] = [
[11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13],
];

// never less than 2
pub const MAX_SIMD_DEGREE: usize = 2;
cfg_if::cfg_if! {
if #[cfg(target_arch = "riscv64")] {
pub const MAX_SIMD_DEGREE: usize = riscv_rva23u64::MAX_SIMD_DEGREE;
} else {
// never less than 2
pub const MAX_SIMD_DEGREE: usize = 2;
}
}

pub type CVBytes = [u8; 32];
pub type CVWords = [u32; 8];
Expand All @@ -101,6 +109,11 @@ pub static DETECTED_IMPL: Implementation = Implementation::new(
);

fn detect() -> Implementation {
#[cfg(target_arch = "riscv64")]
{
return riscv_rva23u64::implementation();
}
#[allow(unreachable_code)]
portable::implementation()
}

Expand Down
Loading