Skip to content

use apfloat for ldexp #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::convert::TryInto;

use rustc_apfloat::Float;
use rustc::ty::layout::{Align, LayoutOf, Size};
use rustc::hir::def_id::DefId;
use rustc::mir;
Expand Down Expand Up @@ -577,7 +580,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
};
this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
}
// underscore case for windows
// underscore case for windows, here and below
// (see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/floating-point-primitives?view=vs-2019)
"_hypot" | "hypot" | "atan2" => {
// FIXME: Using host floats.
let f1 = f64::from_bits(this.read_scalar(args[0])?.to_u64()?);
Expand All @@ -589,16 +593,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
};
this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;
}
// underscore case for windows
"_ldexp" | "ldexp" => {
// FIXME: Using host floats.
let x = f64::from_bits(this.read_scalar(args[0])?.to_u64()?);
// For radix-2 (binary) systems, `ldexp` and `scalbn` are the same.
"_ldexp" | "ldexp" | "scalbn" => {
let x = this.read_scalar(args[0])?.to_f64()?;
let exp = this.read_scalar(args[1])?.to_i32()?;
extern {
fn ldexp(x: f64, n: i32) -> f64;
}
let n = unsafe { ldexp(x, exp) };
this.write_scalar(Scalar::from_u64(n.to_bits()), dest)?;

// Saturating cast to i16. Even those are outside the valid exponent range to
// `scalbn` below will do its over/underflow handling.
let exp = if exp > i16::max_value() as i32 {
i16::max_value()
} else if exp < i16::min_value() as i32 {
i16::min_value()
} else {
exp.try_into().unwrap()
};

let res = x.scalbn(exp);
this.write_scalar(Scalar::from_f64(res), dest)?;
}

// Some things needed for `sys::thread` initialization to go through.
Expand Down
14 changes: 10 additions & 4 deletions tests/run-pass/intrinsics-math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ macro_rules! assert_approx_eq {
})
}

fn ldexp(a: f64, b: i32) -> f64 {
extern {
fn ldexp(x: f64, n: i32) -> f64;
}
unsafe { ldexp(a, b) }
}

pub fn main() {
use std::f32;
use std::f64;
Expand Down Expand Up @@ -88,8 +95,7 @@ pub fn main() {
assert_eq!(3.3_f32.round(), 3.0);
assert_eq!(3.3_f64.round(), 3.0);

extern {
fn ldexp(x: f64, n: i32) -> f64;
}
unsafe { assert_approx_eq!(ldexp(0.65f64, 3i32), 5.2f64); }
assert_eq!(ldexp(0.65f64, 3i32), 5.2f64);
assert_eq!(ldexp(1.42, 0xFFFF), f64::INFINITY);
assert_eq!(ldexp(1.42, -0xFFFF), 0f64);
}