Skip to content

Commit 2da3ede

Browse files
committed
chore: add comments for detect
Signed-off-by: usamoi <usamoi@outlook.com>
1 parent 470915b commit 2da3ede

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

crates/detect/src/lib.rs

+48
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1+
/// Function multiversioning attribute macros for `pgvecto.rs`.
2+
///
3+
/// ```rust
4+
/// #![feature(doc_cfg)]
5+
///
6+
/// #[cfg(any(target_arch = "x86_64", doc))]
7+
/// #[doc(cfg(target_arch = "x86_64"))]
8+
/// #[detect::target_cpu(enable = "v3")]
9+
/// unsafe fn g_v3(x: &[u32]) -> u32 {
10+
/// todo!()
11+
/// }
12+
///
13+
/// #[cfg(all(target_arch = "x86_64", test))]
14+
/// #[test]
15+
/// fn g_v3_test() {
16+
/// const EPSILON: F32 = F32(1e-5);
17+
/// detect::init();
18+
/// if !detect::v3::detect() {
19+
/// println!("test {} ... skipped (v3)", module_path!());
20+
/// return;
21+
/// }
22+
/// let x = vec![0u32; 400];
23+
/// x.fill_with(|| rand::random());
24+
/// let specialized = unsafe { g_v3(&x) };
25+
/// let fallback = unsafe { g_fallback(&x) };
26+
/// assert!(
27+
/// (specialized - fallback).abs() < EPSILON,
28+
/// "specialized = {specialized}, fallback = {fallback}."
29+
/// );
30+
/// }
31+
///
32+
/// // It generates x86_64/v3, x86_64/v2, aarch64/neon and fallback versions of this function.
33+
/// // It takes advantage of `g_v4` as x86_64/v4 version of this function.
34+
/// // It exposes the fallback version with the name "g_fallback".
35+
/// #[detect::multiversion(v3 = import, v2, neon, fallback = export)]
36+
/// fn g(x: &[u32]) -> u32 {
37+
/// let mut result = 0_u32;
38+
/// for v in x {
39+
/// result = result.wrapping_add(*v);
40+
/// }
41+
/// result
42+
/// }
43+
/// ```
44+
pub use detect_macros::multiversion;
45+
46+
/// This macros allows you to enable a set of features by target cpu names.
47+
pub use detect_macros::target_cpu;
48+
149
detect_macros::main!();

crates/detect_macros/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,6 @@ pub fn main(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
307307
});
308308
}
309309
quote::quote! {
310-
pub use detect_macros::multiversion;
311-
pub use detect_macros::target_cpu;
312310
#modules
313311
pub fn init() {
314312
#init

0 commit comments

Comments
 (0)