Skip to content

Commit 14c4192

Browse files
authored
Merge pull request #771 from kennethbgoodin/foreign_math_functions
Add more missing math functions
2 parents 7fc182c + 32a2cf0 commit 14c4192

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

src/fn_call.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,18 +560,56 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
560560
let n = this.memory().get(ptr.alloc_id)?.read_c_str(tcx, ptr)?.len();
561561
this.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
562562
}
563-
"cbrt" => {
563+
564+
// math functions
565+
566+
"cbrtf" | "coshf" | "sinhf" |"tanf" => {
567+
// FIXME: Using host floats.
568+
let f = f32::from_bits(this.read_scalar(args[0])?.to_u32()?);
569+
let f = match link_name {
570+
"cbrtf" => f.cbrt(),
571+
"coshf" => f.cosh(),
572+
"sinhf" => f.sinh(),
573+
"tanf" => f.tan(),
574+
_ => bug!(),
575+
};
576+
this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
577+
}
578+
// underscore case for windows
579+
"_hypotf" | "hypotf" | "atan2f" => {
580+
// FIXME: Using host floats.
581+
let f1 = f32::from_bits(this.read_scalar(args[0])?.to_u32()?);
582+
let f2 = f32::from_bits(this.read_scalar(args[1])?.to_u32()?);
583+
let n = match link_name {
584+
"_hypotf" | "hypotf" => f1.hypot(f2),
585+
"atan2f" => f1.atan2(f2),
586+
_ => bug!(),
587+
};
588+
this.write_scalar(Scalar::from_u32(n.to_bits()), dest)?;
589+
}
590+
591+
"cbrt" | "cosh" | "sinh" | "tan" => {
564592
// FIXME: Using host floats.
565593
let f = f64::from_bits(this.read_scalar(args[0])?.to_u64()?);
566-
let n = f.cbrt();
567-
this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
594+
let f = match link_name {
595+
"cbrt" => f.cbrt(),
596+
"cosh" => f.cosh(),
597+
"sinh" => f.sinh(),
598+
"tan" => f.tan(),
599+
_ => bug!(),
600+
};
601+
this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
568602
}
569603
// underscore case for windows
570-
"_hypot" | "hypot" => {
604+
"_hypot" | "hypot" | "atan2" => {
571605
// FIXME: Using host floats.
572606
let f1 = f64::from_bits(this.read_scalar(args[0])?.to_u64()?);
573607
let f2 = f64::from_bits(this.read_scalar(args[1])?.to_u64()?);
574-
let n = f1.hypot(f2);
608+
let n = match link_name {
609+
"_hypot" | "hypot" => f1.hypot(f2),
610+
"atan2" => f1.atan2(f2),
611+
_ => bug!(),
612+
};
575613
this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
576614
}
577615

tests/run-pass/intrinsics-math.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ pub fn main() {
6767
assert_approx_eq!(0.1f32.trunc(), 0.0f32);
6868
assert_approx_eq!((-0.1f64).trunc(), 0.0f64);
6969

70-
assert_approx_eq!(27f64.cbrt(), 3.0f64);
71-
assert_approx_eq!(3f64.hypot(4f64), 5.0f64);
70+
assert_approx_eq!(27.0f32.cbrt(), 3.0f32);
71+
assert_approx_eq!(27.0f64.cbrt(), 3.0f64);
72+
73+
assert_approx_eq!(3.0f32.hypot(4.0f32), 5.0f32);
74+
assert_approx_eq!(3.0f64.hypot(4.0f64), 5.0f64);
75+
76+
assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32);
77+
assert_approx_eq!(1.0f32.atan2(2.0f32), 0.46364761f32);
78+
79+
assert_approx_eq!(1.0f32.cosh(), 1.54308f32);
80+
assert_approx_eq!(1.0f64.cosh(), 1.54308f64);
81+
82+
assert_approx_eq!(1.0f32.sinh(), 1.1752012f32);
83+
assert_approx_eq!(1.0f64.sinh(), 1.1752012f64);
84+
85+
assert_approx_eq!(1.0f32.tan(), 1.557408f32);
86+
assert_approx_eq!(1.0f64.tan(), 1.557408f64);
87+
7288
}

0 commit comments

Comments
 (0)