1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#![feature(f16)]
#![feature(f128)]
// `STATUS_DLL_NOT_FOUND` on i686 MinGW, not worth looking into.
#![cfg(not(all(target_arch = "x86", target_os = "windows", target_env = "gnu")))]
macro_rules! basic {
(
fn_name: $fn_name:ident,
FTy: $FTy:ty,
CFn: $CFn:ty,
CArgs: $CArgs:ty,
CRet: $CRet:ty,
RustFn: $RustFn:ty,
RustArgs: $RustArgs:ty,
RustRet: $RustRet:ty,
public: $public:expr,
attrs: [$($attr:meta),*],
extra: [$($extra_tt:tt)*],
fn_extra: $fn_extra:expr,
) => {
$(#[$attr])*
#[allow(dead_code)]
pub mod $fn_name {
type FTy= $FTy;
type CFnTy<'a> = $CFn;
type RustFnTy = $RustFn;
type RustArgsTy = $RustArgs;
type RustRetTy = $RustRet;
const PUBLIC: bool = $public;
const A: &[&str] = &[$($extra_tt)*];
fn foo(a: f32) -> f32 {
$fn_extra(a)
}
}
};
}
mod test_basic {
libm_macros::for_each_function! {
callback: basic,
emit_types: all,
skip: [sin, cos],
attributes: [
// just some random attributes
#[allow(clippy::pedantic)]
#[allow(dead_code)]
[sinf, cosf]
],
extra: ["foo", "bar"],
fn_extra: match MACRO_FN_NAME {
sin => |x| x + 2.0,
cos | cosf => |x: f32| x.MACRO_FN_NAME_NORMALIZED(),
_ => |_x| 100.0
}
}
}
macro_rules! basic_no_extra {
(
fn_name: $fn_name:ident,
attrs: [$($attr:meta),*],
) => {
$(#[$attr])*
mod $fn_name {}
};
}
mod test_basic_no_extra {
// Test with no extra, no skip, and no attributes
libm_macros::for_each_function! {
callback: basic_no_extra,
}
}
mod test_only {
// Test that only works
libm_macros::for_each_function! {
callback: basic_no_extra,
only: [sin, sinf],
}
}
macro_rules! specified_types {
(
fn_name: $fn_name:ident,
RustFn: $RustFn:ty,
RustArgs: $RustArgs:ty,
attrs: [$($attr:meta),*],
) => {
$(#[$attr])*
#[allow(dead_code)]
mod $fn_name {
type RustFnTy = $RustFn;
type RustArgsTy = $RustArgs;
}
};
}
mod test_emit_types {
// Test that we can specify a couple types to emit
libm_macros::for_each_function! {
callback: specified_types,
emit_types: [RustFn, RustArgs],
}
}
#[test]
fn test_skip_f16_f128() {
macro_rules! skip_f16_f128 {
(
fn_name: $fn_name:ident,
attrs: [$($attr:meta),*],
extra: $vec:ident,
) => {
$vec.push(stringify!($fn_name));
};
}
let mut v = Vec::new();
// Test with no extra, no skip, and no attributes
libm_macros::for_each_function! {
callback: skip_f16_f128,
skip_f16_f128: true,
extra: v,
}
for name in v {
assert!(!name.contains("f16"), "{name}");
assert!(!name.contains("f128"), "{name}");
}
}
#[test]
fn test_fn_extra_expansion() {
macro_rules! fn_extra_expansion {
(
fn_name: $fn_name:ident,
attrs: [$($attr:meta),*],
fn_extra: $vec:expr,
) => {
$vec.push(stringify!($fn_name));
};
}
let mut vf16 = Vec::new();
let mut vf32 = Vec::new();
let mut vf64 = Vec::new();
let mut vf128 = Vec::new();
// Test with no extra, no skip, and no attributes
libm_macros::for_each_function! {
callback: fn_extra_expansion,
fn_extra: match MACRO_FN_NAME {
ALL_F16 => vf16,
ALL_F32 => vf32,
ALL_F64 => vf64,
ALL_F128 => vf128,
}
}
// Skip functions with a suffix after the type spec
vf16.retain(|name| !name.ends_with("_r"));
vf32.retain(|name| !name.ends_with("_r"));
vf64.retain(|name| !name.ends_with("_r"));
vf128.retain(|name| !name.ends_with("_r"));
for name in vf16 {
assert!(name.ends_with("f16"), "{name}");
}
for name in vf32 {
assert!(name.ends_with("f"), "{name}");
}
let _ = vf64;
for name in vf128 {
assert!(name.ends_with("f128"), "{name}");
}
}
|