Skip to content

Commit b1316ec

Browse files
committed
support and test some more math functions
1 parent 5a4ac1e commit b1316ec

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed

src/shims/foreign_items.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
575575
this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
576576
}
577577

578-
// math functions
578+
// math functions (note that there are also intrinsics for some other functions)
579579
#[rustfmt::skip]
580580
| "cbrtf"
581581
| "coshf"
582582
| "sinhf"
583583
| "tanf"
584+
| "tanhf"
584585
| "acosf"
585586
| "asinf"
586587
| "atanf"
588+
| "log1pf"
589+
| "expm1f"
587590
=> {
588591
let [f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
589592
// FIXME: Using host floats.
@@ -593,9 +596,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
593596
"coshf" => f.cosh(),
594597
"sinhf" => f.sinh(),
595598
"tanf" => f.tan(),
599+
"tanhf" => f.tanh(),
596600
"acosf" => f.acos(),
597601
"asinf" => f.asin(),
598602
"atanf" => f.atan(),
603+
"log1pf" => f.ln_1p(),
604+
"expm1f" => f.exp_m1(),
599605
_ => bug!(),
600606
};
601607
this.write_scalar(Scalar::from_u32(res.to_bits()), dest)?;
@@ -604,6 +610,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
604610
| "_hypotf"
605611
| "hypotf"
606612
| "atan2f"
613+
| "fdimf"
607614
=> {
608615
let [f1, f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
609616
// underscore case for windows, here and below
@@ -614,6 +621,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
614621
let res = match link_name.as_str() {
615622
"_hypotf" | "hypotf" => f1.hypot(f2),
616623
"atan2f" => f1.atan2(f2),
624+
#[allow(deprecated)]
625+
"fdimf" => f1.abs_sub(f2),
617626
_ => bug!(),
618627
};
619628
this.write_scalar(Scalar::from_u32(res.to_bits()), dest)?;
@@ -623,9 +632,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
623632
| "cosh"
624633
| "sinh"
625634
| "tan"
635+
| "tanh"
626636
| "acos"
627637
| "asin"
628638
| "atan"
639+
| "log1p"
640+
| "expm1"
629641
=> {
630642
let [f] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
631643
// FIXME: Using host floats.
@@ -635,9 +647,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
635647
"cosh" => f.cosh(),
636648
"sinh" => f.sinh(),
637649
"tan" => f.tan(),
650+
"tanh" => f.tanh(),
638651
"acos" => f.acos(),
639652
"asin" => f.asin(),
640653
"atan" => f.atan(),
654+
"log1p" => f.ln_1p(),
655+
"expm1" => f.exp_m1(),
641656
_ => bug!(),
642657
};
643658
this.write_scalar(Scalar::from_u64(res.to_bits()), dest)?;
@@ -646,6 +661,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
646661
| "_hypot"
647662
| "hypot"
648663
| "atan2"
664+
| "fdim"
649665
=> {
650666
let [f1, f2] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
651667
// FIXME: Using host floats.
@@ -654,6 +670,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
654670
let res = match link_name.as_str() {
655671
"_hypot" | "hypot" => f1.hypot(f2),
656672
"atan2" => f1.atan2(f2),
673+
#[allow(deprecated)]
674+
"fdim" => f1.abs_sub(f2),
657675
_ => bug!(),
658676
};
659677
this.write_scalar(Scalar::from_u64(res.to_bits()), dest)?;

tests/pass/intrinsics-math.rs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ pub fn main() {
3232
assert_approx_eq!(25f32.powi(-2), 0.0016f32);
3333
assert_approx_eq!(23.2f64.powi(2), 538.24f64);
3434

35-
assert_approx_eq!(0f32.sin(), 0f32);
36-
assert_approx_eq!((f64::consts::PI / 2f64).sin(), 1f64);
37-
38-
assert_approx_eq!(0f32.cos(), 1f32);
39-
assert_approx_eq!((f64::consts::PI * 2f64).cos(), 1f64);
40-
4135
assert_approx_eq!(25f32.powf(-2f32), 0.0016f32);
4236
assert_approx_eq!(400f64.powf(0.5f64), 20f64);
4337

44-
assert_approx_eq!((1f32.exp() - f32::consts::E).abs(), 0f32);
38+
assert_approx_eq!(1f32.exp(), f32::consts::E);
4539
assert_approx_eq!(1f64.exp(), f64::consts::E);
4640

41+
assert_approx_eq!(1f32.exp_m1(), f32::consts::E - 1.0);
42+
assert_approx_eq!(1f64.exp_m1(), f64::consts::E - 1.0);
43+
4744
assert_approx_eq!(10f32.exp2(), 1024f32);
4845
assert_approx_eq!(50f64.exp2(), 1125899906842624f64);
4946

50-
assert_approx_eq!((f32::consts::E.ln() - 1f32).abs(), 0f32);
47+
assert_approx_eq!(f32::consts::E.ln(), 1f32);
5148
assert_approx_eq!(1f64.ln(), 0f64);
5249

50+
assert_approx_eq!(0f32.ln_1p(), 0f32);
51+
assert_approx_eq!(0f64.ln_1p(), 0f64);
52+
5353
assert_approx_eq!(10f32.log10(), 1f32);
5454
assert_approx_eq!(f64::consts::E.log10(), f64::consts::LOG10_E);
5555

@@ -66,6 +66,12 @@ pub fn main() {
6666
assert_approx_eq!((-1.0f32).abs(), 1.0f32);
6767
assert_approx_eq!(34.2f64.abs(), 34.2f64);
6868

69+
#[allow(deprecated)]
70+
{
71+
assert_approx_eq!(5.0f32.abs_sub(3.0), 2.0);
72+
assert_approx_eq!(3.0f64.abs_sub(5.0), 0.0);
73+
}
74+
6975
assert_approx_eq!(3.8f32.floor(), 3.0f32);
7076
assert_approx_eq!((-1.1f64).floor(), -2.0f64);
7177

@@ -81,31 +87,54 @@ pub fn main() {
8187
assert_approx_eq!(3.0f32.hypot(4.0f32), 5.0f32);
8288
assert_approx_eq!(3.0f64.hypot(4.0f64), 5.0f64);
8389

84-
assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32);
85-
assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32);
90+
assert_eq!(3.3_f32.round(), 3.0);
91+
assert_eq!(3.3_f64.round(), 3.0);
8692

87-
assert_approx_eq!(1.0f32.cosh(), 1.54308f32);
88-
assert_approx_eq!(1.0f64.cosh(), 1.54308f64);
93+
assert_eq!(ldexp(0.65f64, 3i32), 5.2f64);
94+
assert_eq!(ldexp(1.42, 0xFFFF), f64::INFINITY);
95+
assert_eq!(ldexp(1.42, -0xFFFF), 0f64);
96+
97+
// Trigonometric functions.
98+
99+
assert_approx_eq!(0f32.sin(), 0f32);
100+
assert_approx_eq!((f64::consts::PI / 2f64).sin(), 1f64);
101+
assert_approx_eq!(f32::consts::FRAC_PI_6.sin(), 0.5);
102+
assert_approx_eq!(f64::consts::FRAC_PI_6.sin(), 0.5);
103+
assert_approx_eq!(f32::consts::FRAC_PI_4.sin().asin(), f32::consts::FRAC_PI_4);
104+
assert_approx_eq!(f64::consts::FRAC_PI_4.sin().asin(), f64::consts::FRAC_PI_4);
89105

90106
assert_approx_eq!(1.0f32.sinh(), 1.1752012f32);
91107
assert_approx_eq!(1.0f64.sinh(), 1.1752012f64);
108+
assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
109+
assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
92110

93-
assert_approx_eq!(1.0f32.tan(), 1.557408f32);
94-
assert_approx_eq!(1.0f64.tan(), 1.557408f64);
95-
111+
assert_approx_eq!(0f32.cos(), 1f32);
112+
assert_approx_eq!((f64::consts::PI * 2f64).cos(), 1f64);
113+
assert_approx_eq!(f32::consts::FRAC_PI_3.cos(), 0.5);
114+
assert_approx_eq!(f64::consts::FRAC_PI_3.cos(), 0.5);
96115
assert_approx_eq!(f32::consts::FRAC_PI_4.cos().acos(), f32::consts::FRAC_PI_4);
97116
assert_approx_eq!(f64::consts::FRAC_PI_4.cos().acos(), f64::consts::FRAC_PI_4);
98117

99-
assert_approx_eq!(f32::consts::FRAC_PI_4.sin().asin(), f32::consts::FRAC_PI_4);
100-
assert_approx_eq!(f64::consts::FRAC_PI_4.sin().asin(), f64::consts::FRAC_PI_4);
118+
assert_approx_eq!(1.0f32.cosh(), 1.54308f32);
119+
assert_approx_eq!(1.0f64.cosh(), 1.54308f64);
120+
assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
121+
assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
101122

123+
assert_approx_eq!(1.0f32.tan(), 1.557408f32);
124+
assert_approx_eq!(1.0f64.tan(), 1.557408f64);
102125
assert_approx_eq!(1.0_f32, 1.0_f32.tan().atan());
103126
assert_approx_eq!(1.0_f64, 1.0_f64.tan().atan());
127+
assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32);
128+
assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32);
104129

105-
assert_eq!(3.3_f32.round(), 3.0);
106-
assert_eq!(3.3_f64.round(), 3.0);
107-
108-
assert_eq!(ldexp(0.65f64, 3i32), 5.2f64);
109-
assert_eq!(ldexp(1.42, 0xFFFF), f64::INFINITY);
110-
assert_eq!(ldexp(1.42, -0xFFFF), 0f64);
130+
assert_approx_eq!(
131+
1.0f32.tanh(),
132+
(1.0 - f32::consts::E.powi(-2)) / (1.0 + f32::consts::E.powi(-2))
133+
);
134+
assert_approx_eq!(
135+
1.0f64.tanh(),
136+
(1.0 - f64::consts::E.powi(-2)) / (1.0 + f64::consts::E.powi(-2))
137+
);
138+
assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
139+
assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
111140
}

0 commit comments

Comments
 (0)