Skip to content

Commit 5e07ac3

Browse files
committed
implement min and max floating point intrinsics
1 parent 6eab94a commit 5e07ac3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/intrinsic.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_apfloat::Float;
12
use rustc::mir;
23
use rustc::mir::interpret::{InterpResult, PointerArithmetic};
34
use rustc::ty::layout::{self, LayoutOf, Size};
@@ -242,6 +243,28 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
242243
this.binop_ignore_overflow(op, a, b, dest)?;
243244
}
244245

246+
"minnumf32" | "maxnumf32" => {
247+
let a = this.read_scalar(args[0])?.to_f32()?;
248+
let b = this.read_scalar(args[1])?.to_f32()?;
249+
let res = if intrinsic_name.get().starts_with("min") {
250+
a.min(b)
251+
} else {
252+
a.max(b)
253+
};
254+
this.write_scalar(Scalar::from_f32(res), dest)?;
255+
}
256+
257+
"minnumf64" | "maxnumf64" => {
258+
let a = this.read_scalar(args[0])?.to_f64()?;
259+
let b = this.read_scalar(args[1])?.to_f64()?;
260+
let res = if intrinsic_name.get().starts_with("min") {
261+
a.min(b)
262+
} else {
263+
a.max(b)
264+
};
265+
this.write_scalar(Scalar::from_f64(res), dest)?;
266+
}
267+
245268
"exact_div" => {
246269
// Performs an exact division, resulting in undefined behavior where
247270
// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
extern crate log;
77
// From rustc.
88
extern crate syntax;
9-
#[macro_use]
10-
extern crate rustc;
9+
extern crate rustc_apfloat;
10+
#[macro_use] extern crate rustc;
1111
extern crate rustc_data_structures;
1212
extern crate rustc_mir;
1313
extern crate rustc_target;

tests/run-pass/floats.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
fn main() {
32
assert_eq!(6.0_f32*6.0_f32, 36.0_f32);
43
assert_eq!(6.0_f64*6.0_f64, 36.0_f64);
@@ -12,4 +11,18 @@ fn main() {
1211
assert_eq!(5.0f32 as u32, 5);
1312
assert_eq!(5.0f32 as i32, 5);
1413
assert_eq!(-5.0f32 as i32, -5);
14+
15+
assert_eq!((1.0 as f32).max(-1.0), 1.0);
16+
assert_eq!((1.0 as f32).min(-1.0), -1.0);
17+
assert_eq!(std::f32::NAN.min(9.0), 9.0);
18+
assert_eq!(std::f32::NAN.max(-9.0), -9.0);
19+
assert_eq!((9.0 as f32).min(std::f32::NAN), 9.0);
20+
assert_eq!((-9.0 as f32).max(std::f32::NAN), -9.0);
21+
22+
assert_eq!((1.0 as f64).max(-1.0), 1.0);
23+
assert_eq!((1.0 as f64).min(-1.0), -1.0);
24+
assert_eq!(std::f64::NAN.min(9.0), 9.0);
25+
assert_eq!(std::f64::NAN.max(-9.0), -9.0);
26+
assert_eq!((9.0 as f64).min(std::f64::NAN), 9.0);
27+
assert_eq!((-9.0 as f64).max(std::f64::NAN), -9.0);
1528
}

0 commit comments

Comments
 (0)