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

Commit 0cb855b

Browse files
committed
macros: Always emit f16_enabled and f128_enabled attributes
Once we start addinf `f16` and `f128` routines, we will need to have this cfg for almost all uses of `for_each_function`. Rather than needing to specify this each time, always emit `#[cfg(f16_enabled)]` or `#[cfg(f128_enabled)]` for each function that uses `f16` or `f128`, respectively.
1 parent f5d5cf8 commit 0cb855b

File tree

9 files changed

+57
-20
lines changed

9 files changed

+57
-20
lines changed

crates/libm-macros/src/lib.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn base_name_enum(attributes: pm::TokenStream, tokens: pm::TokenStream) -> p
7979
/// // The Rust version's return type (e.g. `(f32, f32)`)
8080
/// RustRet: $RustRet:ty,
8181
/// // Attributes for the current function, if any
82-
/// attrs: [$($meta:meta)*]
82+
/// attrs: [$($attr:meta),*],
8383
/// // Extra tokens passed directly (if any)
8484
/// extra: [$extra:ident],
8585
/// // Extra function-tokens passed directly (if any)
@@ -97,6 +97,9 @@ pub fn base_name_enum(attributes: pm::TokenStream, tokens: pm::TokenStream) -> p
9797
/// skip: [sin, cos],
9898
/// // Attributes passed as `attrs` for specific functions. For example, here the invocation
9999
/// // with `sinf` and that with `cosf` will both get `meta1` and `meta2`, but no others will.
100+
/// //
101+
/// // Note that `f16_enabled` and `f128_enabled` will always get emitted regardless of whether
102+
/// // or not this is specified.
100103
/// attributes: [
101104
/// #[meta1]
102105
/// #[meta2]
@@ -255,16 +258,28 @@ fn expand(input: StructuredInput, fn_list: &[&MathOpInfo]) -> syn::Result<pm2::T
255258
let fn_name = Ident::new(func.name, Span::call_site());
256259

257260
// Prepare attributes in an `attrs: ...` field
258-
let meta_field = match &input.attributes {
259-
Some(attrs) => {
260-
let meta = attrs
261-
.iter()
262-
.filter(|map| map.names.contains(&fn_name))
263-
.flat_map(|map| &map.meta);
264-
quote! { attrs: [ #( #meta )* ] }
265-
}
266-
None => pm2::TokenStream::new(),
267-
};
261+
let mut meta_fields = Vec::new();
262+
if let Some(attrs) = &input.attributes {
263+
let meta_iter = attrs
264+
.iter()
265+
.filter(|map| map.names.contains(&fn_name))
266+
.flat_map(|map| &map.meta)
267+
.map(|v| v.into_token_stream());
268+
269+
meta_fields.extend(meta_iter);
270+
}
271+
272+
// Always emit f16 and f128 meta so this doesn't need to be repeated everywhere
273+
if func.rust_sig.args.contains(&Ty::F16) || func.rust_sig.returns.contains(&Ty::F16) {
274+
let ts = quote! { cfg(f16_enabled) };
275+
meta_fields.push(ts);
276+
}
277+
if func.rust_sig.args.contains(&Ty::F128) || func.rust_sig.returns.contains(&Ty::F128) {
278+
let ts = quote! { cfg(f128_enabled) };
279+
meta_fields.push(ts);
280+
}
281+
282+
let meta_field = quote! { attrs: [ #( #meta_fields ),* ], };
268283

269284
// Prepare extra in an `extra: ...` field, running the replacer
270285
let extra_field = match input.extra.clone() {

crates/libm-macros/tests/basic.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(f16)]
2+
#![feature(f128)]
13
// `STATUS_DLL_NOT_FOUND` on i686 MinGW, not worth looking into.
24
#![cfg(not(all(target_arch = "x86", target_os = "windows", target_env = "gnu")))]
35

@@ -11,11 +13,11 @@ macro_rules! basic {
1113
RustFn: $RustFn:ty,
1214
RustArgs: $RustArgs:ty,
1315
RustRet: $RustRet:ty,
14-
attrs: [$($meta:meta)*]
16+
attrs: [$($attr:meta),*],
1517
extra: [$($extra_tt:tt)*],
1618
fn_extra: $fn_extra:expr,
1719
) => {
18-
$(#[$meta])*
20+
$(#[$attr])*
1921
mod $fn_name {
2022
#[allow(unused)]
2123
type FTy= $FTy;
@@ -60,7 +62,9 @@ mod test_basic {
6062
macro_rules! basic_no_extra {
6163
(
6264
fn_name: $fn_name:ident,
65+
attrs: [$($attr:meta),*],
6366
) => {
67+
$(#[$attr])*
6468
mod $fn_name {}
6569
};
6670
}
@@ -85,7 +89,9 @@ macro_rules! specified_types {
8589
fn_name: $fn_name:ident,
8690
RustFn: $RustFn:ty,
8791
RustArgs: $RustArgs:ty,
92+
attrs: [$($attr:meta),*],
8893
) => {
94+
$(#[$attr])*
8995
mod $fn_name {
9096
#[allow(unused)]
9197
type RustFnTy = $RustFn;

crates/libm-test/benches/random.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ struct MuslExtra<F> {
1818
macro_rules! musl_rand_benches {
1919
(
2020
fn_name: $fn_name:ident,
21+
attrs: [$($attr:meta),*],
2122
fn_extra: $skip_on_i586:expr,
2223
) => {
2324
paste::paste! {
25+
$(#[$attr])*
2426
fn [< musl_bench_ $fn_name >](c: &mut Criterion) {
2527
type Op = libm_test::op::$fn_name::Routine;
2628

@@ -113,9 +115,11 @@ libm_macros::for_each_function! {
113115
macro_rules! run_callback {
114116
(
115117
fn_name: $fn_name:ident,
118+
attrs: [$($attr:meta),*],
116119
extra: [$criterion:ident],
117120
) => {
118121
paste::paste! {
122+
$(#[$attr])*
119123
[< musl_bench_ $fn_name >](&mut $criterion)
120124
}
121125
};

crates/libm-test/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(f16_enabled, feature(f16))]
2+
#![cfg_attr(f128_enabled, feature(f128))]
13
#![allow(clippy::unusual_byte_groupings)] // sometimes we group by sign_exp_sig
24

35
pub mod domain;

crates/libm-test/src/mpfloat.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ macro_rules! impl_mp_op {
5050
(
5151
fn_name: $fn_name:ident,
5252
RustFn: fn($_fty:ty,) -> $_ret:ty,
53+
attrs: [$($attr:meta),*],
5354
fn_extra: $fn_name_normalized:expr,
5455
) => {
5556
paste::paste! {
57+
$(#[$attr])*
5658
impl MpOp for crate::op::$fn_name::Routine {
5759
type MpTy = MpFloat;
5860

@@ -72,9 +74,11 @@ macro_rules! impl_mp_op {
7274
(
7375
fn_name: $fn_name:ident,
7476
RustFn: fn($_fty:ty, $_fty2:ty,) -> $_ret:ty,
77+
attrs: [$($attr:meta),*],
7578
fn_extra: $fn_name_normalized:expr,
7679
) => {
7780
paste::paste! {
81+
$(#[$attr])*
7882
impl MpOp for crate::op::$fn_name::Routine {
7983
type MpTy = (MpFloat, MpFloat);
8084

@@ -95,9 +99,11 @@ macro_rules! impl_mp_op {
9599
(
96100
fn_name: $fn_name:ident,
97101
RustFn: fn($_fty:ty, $_fty2:ty, $_fty3:ty,) -> $_ret:ty,
102+
attrs: [$($attr:meta),*],
98103
fn_extra: $fn_name_normalized:expr,
99104
) => {
100105
paste::paste! {
106+
$(#[$attr])*
101107
impl MpOp for crate::op::$fn_name::Routine {
102108
type MpTy = (MpFloat, MpFloat, MpFloat);
103109

crates/libm-test/src/op.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ macro_rules! do_thing {
112112
RustFn: $RustFn:ty,
113113
RustArgs: $RustArgs:ty,
114114
RustRet: $RustRet:ty,
115+
attrs: [$($attr:meta),*],
116+
115117
) => {
116118
paste::paste! {
119+
$(#[$attr])*
117120
pub mod $fn_name {
118121
use super::*;
119122
pub struct Routine;

crates/libm-test/tests/check_coverage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::process::Command;
88
macro_rules! callback {
99
(
1010
fn_name: $name:ident,
11+
attrs: [$($attr:meta),*],
1112
extra: [$set:ident],
1213
) => {
1314
let name = stringify!($name);

crates/libm-test/tests/compare_built_musl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use libm_test::{CheckBasis, CheckCtx, CheckOutput, GenerateInput, MathOp, TupleC
1515
macro_rules! musl_rand_tests {
1616
(
1717
fn_name: $fn_name:ident,
18-
attrs: [$($meta:meta)*]
18+
attrs: [$($attr:meta),*],
1919
) => {
2020
paste::paste! {
2121
#[test]
22-
$(#[$meta])*
22+
$(#[$attr])*
2323
fn [< musl_random_ $fn_name >]() {
2424
test_one::<libm_test::op::$fn_name::Routine>(musl_math_sys::$fn_name);
2525
}

crates/libm-test/tests/multiprecision.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use libm_test::{
1313
macro_rules! mp_rand_tests {
1414
(
1515
fn_name: $fn_name:ident,
16-
attrs: [$($meta:meta)*]
16+
attrs: [$($attr:meta),*],
1717
) => {
1818
paste::paste! {
1919
#[test]
20-
$(#[$meta])*
20+
$(#[$attr])*
2121
fn [< mp_random_ $fn_name >]() {
2222
test_one_random::<libm_test::op::$fn_name::Routine>();
2323
}
@@ -76,18 +76,18 @@ libm_macros::for_each_function! {
7676
macro_rules! mp_domain_tests {
7777
(
7878
fn_name: $fn_name:ident,
79-
attrs: [$($meta:meta)*]
79+
attrs: [$($attr:meta),*],
8080
) => {
8181
paste::paste! {
8282
#[test]
83-
$(#[$meta])*
83+
$(#[$attr])*
8484
fn [< mp_edge_case_ $fn_name >]() {
8585
type Op = libm_test::op::$fn_name::Routine;
8686
domain_test_runner::<Op, _>(edge_cases::get_test_cases::<Op, _>);
8787
}
8888

8989
#[test]
90-
$(#[$meta])*
90+
$(#[$attr])*
9191
fn [< mp_logspace_ $fn_name >]() {
9292
type Op = libm_test::op::$fn_name::Routine;
9393
domain_test_runner::<Op, _>(domain_logspace::get_test_cases::<Op>);

0 commit comments

Comments
 (0)