Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 88f6b83

Browse files
committed
Vendor cfg_if::cfg_if!
`cfg_if` is helpful for applying `cfg` attributes to groups of items, like we will need to do with architecture-specific modules of `f16` and `f128`. However, `libm` can't have dependencies. The `cfg_if` macro is complex but small, so just vendor it here.
1 parent 0575358 commit 88f6b83

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

src/math/support/macros.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
1-
/// Choose among using an intrinsic (if available) and falling back to the default function body.
2-
/// Returns directly if the intrinsic version is used, otherwise continues to the rest of the
3-
/// function.
1+
/// `libm` cannot have dependencies, so this is vendored directly from the `cfg-if` crate
2+
/// (with some comments stripped for compactness).
3+
macro_rules! cfg_if {
4+
// match if/else chains with a final `else`
5+
($(
6+
if #[cfg($meta:meta)] { $($tokens:tt)* }
7+
) else * else {
8+
$($tokens2:tt)*
9+
}) => {
10+
cfg_if! { @__items () ; $( ( ($meta) ($($tokens)*) ), )* ( () ($($tokens2)*) ), }
11+
};
12+
13+
// match if/else chains lacking a final `else`
14+
(
15+
if #[cfg($i_met:meta)] { $($i_tokens:tt)* }
16+
$( else if #[cfg($e_met:meta)] { $($e_tokens:tt)* } )*
17+
) => {
18+
cfg_if! {
19+
@__items
20+
() ;
21+
( ($i_met) ($($i_tokens)*) ),
22+
$( ( ($e_met) ($($e_tokens)*) ), )*
23+
( () () ),
24+
}
25+
};
26+
27+
// Internal and recursive macro to emit all the items
28+
//
29+
// Collects all the negated cfgs in a list at the beginning and after the
30+
// semicolon is all the remaining items
31+
(@__items ($($not:meta,)*) ; ) => {};
32+
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($tokens:tt)*) ), $($rest:tt)*) => {
33+
#[cfg(all($($m,)* not(any($($not),*))))] cfg_if! { @__identity $($tokens)* }
34+
cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
35+
};
36+
37+
// Internal macro to make __apply work out right for different match types,
38+
// because of how macros matching/expand stuff.
39+
(@__identity $($tokens:tt)*) => { $($tokens)* };
40+
}
41+
42+
/// Choose between using an intrinsic (if available) and the function body. Returns directly if
43+
/// the intrinsic is used, otherwise the rest of the function body is used.
444
///
545
/// Use this if the intrinsic is likely to be more performant on the platform(s) specified
646
/// in `intrinsic_available`.

0 commit comments

Comments
 (0)