-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathmain.rs
361 lines (310 loc) · 10.5 KB
/
main.rs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Helper CLI utility for common tasks.
#![cfg_attr(f16_enabled, feature(f16))]
#![cfg_attr(f128_enabled, feature(f128))]
use std::any::type_name;
use std::env;
use std::num::ParseIntError;
use std::str::FromStr;
use libm::support::{Hexf, hf32, hf64};
#[cfg(feature = "build-mpfr")]
use libm_test::mpfloat::MpOp;
use libm_test::{MathOp, TupleCall};
#[cfg(feature = "build-mpfr")]
use rug::az::{self, Az};
const USAGE: &str = "\
usage:
cargo run -p util -- <SUBCOMMAND>
SUBCOMMAND:
eval <BASIS> <OP> inputs...
Evaulate the expression with a given basis. This can be useful for
running routines with a debugger, or quickly checking input. Examples:
* eval musl sinf 1.234 # print the results of musl sinf(1.234f32)
* eval mpfr pow 1.234 2.432 # print the results of mpfr pow(1.234, 2.432)
";
fn main() {
let args = env::args().collect::<Vec<_>>();
let str_args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
match &str_args.as_slice()[1..] {
["eval", basis, op, inputs @ ..] => do_eval(basis, op, inputs),
_ => {
println!("{USAGE}\nunrecognized input `{str_args:?}`");
std::process::exit(1);
}
}
}
macro_rules! handle_call {
(
fn_name: $fn_name:ident,
CFn: $CFn:ty,
RustFn: $RustFn:ty,
RustArgs: $RustArgs:ty,
attrs: [$($attr:meta),*],
extra: ($basis:ident, $op:ident, $inputs:ident),
fn_extra: $musl_fn:expr,
) => {
$(#[$attr])*
if $op == stringify!($fn_name) {
type Op = libm_test::op::$fn_name::Routine;
let input = <$RustArgs>::parse($inputs);
let libm_fn: <Op as MathOp>::RustFn = libm::$fn_name;
let output = match $basis {
"libm" => input.call(libm_fn),
#[cfg(feature = "build-musl")]
"musl" => {
let musl_fn: <Op as MathOp>::CFn =
$musl_fn.unwrap_or_else(|| panic!("no musl function for {}", $op));
input.call(musl_fn)
}
#[cfg(feature = "build-mpfr")]
"mpfr" => {
let mut mp = <Op as MpOp>::new_mp();
Op::run(&mut mp, input)
}
_ => panic!("unrecognized or disabled basis '{}'", $basis),
};
println!("{output:?} {:x}", Hexf(output));
return;
}
};
}
/// Evaluate the specified operation with a given basis.
fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
libm_macros::for_each_function! {
callback: handle_call,
emit_types: [CFn, RustFn, RustArgs],
extra: (basis, op, inputs),
fn_extra: match MACRO_FN_NAME {
ceilf128
| ceilf16
| copysignf128
| copysignf16
| fabsf128
| fabsf16
| fdimf128
| fdimf16
| floorf128
| floorf16
| fmaxf128
| fmaxf16
| fminf128
| fminf16
| fmodf128
| fmodf16
| frexpf128
| frexpf16
| ilogbf128
| ilogbf16
| rintf128
| rintf16
| roundf128
| roundf16
| sqrtf128
| sqrtf16
| truncf128
| truncf16 => None,
_ => Some(musl_math_sys::MACRO_FN_NAME)
}
}
panic!("no operation matching {op}");
}
/// Parse a tuple from a space-delimited string.
trait ParseTuple {
fn parse(input: &[&str]) -> Self;
}
macro_rules! impl_parse_tuple {
($ty:ty) => {
impl ParseTuple for ($ty,) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 1, "expected a single argument, got {input:?}");
(parse(input, 0),)
}
}
impl ParseTuple for ($ty, $ty) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 2, "expected two arguments, got {input:?}");
(parse(input, 0), parse(input, 1))
}
}
impl ParseTuple for ($ty, i32) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 2, "expected two arguments, got {input:?}");
(parse(input, 0), parse(input, 1))
}
}
impl ParseTuple for (i32, $ty) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 2, "expected two arguments, got {input:?}");
(parse(input, 0), parse(input, 1))
}
}
impl ParseTuple for ($ty, $ty, $ty) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 3, "expected three arguments, got {input:?}");
(parse(input, 0), parse(input, 1), parse(input, 2))
}
}
};
}
#[allow(unused_macros)]
#[cfg(feature = "build-mpfr")]
macro_rules! impl_parse_tuple_via_rug {
($ty:ty) => {
impl ParseTuple for ($ty,) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 1, "expected a single argument, got {input:?}");
(parse_rug(input, 0),)
}
}
impl ParseTuple for ($ty, $ty) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 2, "expected two arguments, got {input:?}");
(parse_rug(input, 0), parse_rug(input, 1))
}
}
impl ParseTuple for ($ty, i32) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 2, "expected two arguments, got {input:?}");
(parse_rug(input, 0), parse(input, 1))
}
}
impl ParseTuple for (i32, $ty) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 2, "expected two arguments, got {input:?}");
(parse(input, 0), parse_rug(input, 1))
}
}
impl ParseTuple for ($ty, $ty, $ty) {
fn parse(input: &[&str]) -> Self {
assert_eq!(input.len(), 3, "expected three arguments, got {input:?}");
(parse_rug(input, 0), parse_rug(input, 1), parse_rug(input, 2))
}
}
};
}
// Fallback for when Rug is not built.
#[allow(unused_macros)]
#[cfg(not(feature = "build-mpfr"))]
macro_rules! impl_parse_tuple_via_rug {
($ty:ty) => {
impl ParseTuple for ($ty,) {
fn parse(_input: &[&str]) -> Self {
panic!("parsing this type requires the `build-mpfr` feature")
}
}
impl ParseTuple for ($ty, $ty) {
fn parse(_input: &[&str]) -> Self {
panic!("parsing this type requires the `build-mpfr` feature")
}
}
impl ParseTuple for ($ty, i32) {
fn parse(_input: &[&str]) -> Self {
panic!("parsing this type requires the `build-mpfr` feature")
}
}
impl ParseTuple for (i32, $ty) {
fn parse(_input: &[&str]) -> Self {
panic!("parsing this type requires the `build-mpfr` feature")
}
}
impl ParseTuple for ($ty, $ty, $ty) {
fn parse(_input: &[&str]) -> Self {
panic!("parsing this type requires the `build-mpfr` feature")
}
}
};
}
impl_parse_tuple!(f32);
impl_parse_tuple!(f64);
#[cfg(f16_enabled)]
impl_parse_tuple_via_rug!(f16);
#[cfg(f128_enabled)]
impl_parse_tuple_via_rug!(f128);
/// Try to parse the number, printing a nice message on failure.
fn parse<T: FromStr + FromStrRadix>(input: &[&str], idx: usize) -> T {
let s = input[idx];
let msg = || format!("invalid {} input '{s}'", type_name::<T>());
if s.starts_with("0x") {
return T::from_str_radix(s, 16).unwrap_or_else(|_| panic!("{}", msg()));
}
if s.starts_with("0b") {
return T::from_str_radix(s, 2).unwrap_or_else(|_| panic!("{}", msg()));
}
s.parse().unwrap_or_else(|_| panic!("{}", msg()))
}
/// Try to parse the float type going via `rug`, for `f16` and `f128` which don't yet implement
/// `FromStr`.
#[cfg(feature = "build-mpfr")]
fn parse_rug<F>(input: &[&str], idx: usize) -> F
where
F: libm_test::Float + FromStrRadix,
rug::Float: az::Cast<F>,
{
let s = input[idx];
let msg = || format!("invalid {} input '{s}'", type_name::<F>());
if s.starts_with("0x") {
return F::from_str_radix(s, 16).unwrap_or_else(|_| panic!("{}", msg()));
}
if s.starts_with("0b") {
return F::from_str_radix(s, 2).unwrap_or_else(|_| panic!("{}", msg()));
}
let x = rug::Float::parse(s).unwrap_or_else(|_| panic!("{}", msg()));
let x = rug::Float::with_val(F::BITS, x);
x.az()
}
trait FromStrRadix: Sized {
fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError>;
}
impl FromStrRadix for i32 {
fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
let s = strip_radix_prefix(s, radix);
i32::from_str_radix(s, radix)
}
}
#[cfg(f16_enabled)]
impl FromStrRadix for f16 {
fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
if radix == 16 && s.contains("p") {
return Ok(libm::support::hf16(s));
}
let s = strip_radix_prefix(s, radix);
u16::from_str_radix(s, radix).map(Self::from_bits)
}
}
impl FromStrRadix for f32 {
fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
if radix == 16 && s.contains("p") {
// Parse as hex float
return Ok(hf32(s));
}
let s = strip_radix_prefix(s, radix);
u32::from_str_radix(s, radix).map(Self::from_bits)
}
}
impl FromStrRadix for f64 {
fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
if s.contains("p") {
return Ok(hf64(s));
}
let s = strip_radix_prefix(s, radix);
u64::from_str_radix(s, radix).map(Self::from_bits)
}
}
#[cfg(f128_enabled)]
impl FromStrRadix for f128 {
fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseIntError> {
if radix == 16 && s.contains("p") {
return Ok(libm::support::hf128(s));
}
let s = strip_radix_prefix(s, radix);
u128::from_str_radix(s, radix).map(Self::from_bits)
}
}
fn strip_radix_prefix(s: &str, radix: u32) -> &str {
if radix == 16 {
s.strip_prefix("0x").unwrap()
} else if radix == 2 {
s.strip_prefix("0b").unwrap()
} else {
s
}
}