Skip to content

Commit a8f66ef

Browse files
committed
Implement cbrt and hypot function calls
Test cases are added to `tests/run-pass/intrinsics-math.rs`
1 parent f090362 commit a8f66ef

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/fn_call.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,17 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
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" => {
564+
let f = this.read_scalar(args[0])?.to_f64()?;
565+
let n = f.cbrt();
566+
this.write_scalar(Scalar::from_f64(n), dest)?;
567+
}
568+
"hypot" => {
569+
let f1 = this.read_scalar(args[0])?.to_f64()?;
570+
let f2 = this.read_scalar(args[1])?.to_f64()?;
571+
let n = f1.hypot(f2);
572+
this.write_scalar(Scalar::from_f64(n), dest)?;
573+
}
563574

564575
// Some things needed for `sys::thread` initialization to go through.
565576
"signal" | "sigaction" | "sigaltstack" => {

tests/run-pass/intrinsics-math.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@ pub fn main() {
6464

6565
assert_approx_eq!(0.1f32.trunc(), 0.0f32);
6666
assert_approx_eq!((-0.1f64).trunc(), 0.0f64);
67+
68+
assert_approx_eq!(27f64.cbrt(), 3.0f64);
69+
assert_approx_eq!(3f64.hypot(4f64), 5.0f64);
6770
}

0 commit comments

Comments
 (0)