Skip to content

Commit 77f2d86

Browse files
committed
share sqrt implemention across float types
1 parent f19b560 commit 77f2d86

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

src/tools/miri/src/intrinsics/math.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
use rand::Rng;
2-
use rustc_apfloat::{self, Float, Round};
2+
use rustc_apfloat::{self, Float, FloatConvert, Round};
33
use rustc_middle::mir;
44
use rustc_middle::ty::{self, FloatTy};
55

66
use self::helpers::{ToHost, ToSoft};
77
use super::check_intrinsic_arg_count;
88
use crate::*;
99

10+
fn sqrt<'tcx, F: Float + FloatConvert<F> + Into<Scalar>>(
11+
this: &mut MiriInterpCx<'tcx>,
12+
args: &[OpTy<'tcx>],
13+
dest: &MPlaceTy<'tcx>,
14+
) -> InterpResult<'tcx> {
15+
let [f] = check_intrinsic_arg_count(args)?;
16+
let f = this.read_scalar(f)?;
17+
let f: F = f.to_float()?;
18+
// Sqrt is specified to be fully precise.
19+
let res = math::sqrt(f);
20+
let res = this.adjust_nan(res, &[f]);
21+
this.write_scalar(res, dest)
22+
}
23+
1024
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
1125
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1226
fn emulate_math_intrinsic(
@@ -20,38 +34,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2034

2135
match intrinsic_name {
2236
// Operations we can do with soft-floats.
23-
"sqrtf16" => {
24-
let [f] = check_intrinsic_arg_count(args)?;
25-
let f = this.read_scalar(f)?.to_f16()?;
26-
// Sqrt is specified to be fully precise.
27-
let res = math::sqrt(f);
28-
let res = this.adjust_nan(res, &[f]);
29-
this.write_scalar(res, dest)?;
30-
}
31-
"sqrtf32" => {
32-
let [f] = check_intrinsic_arg_count(args)?;
33-
let f = this.read_scalar(f)?.to_f32()?;
34-
// Sqrt is specified to be fully precise.
35-
let res = math::sqrt(f);
36-
let res = this.adjust_nan(res, &[f]);
37-
this.write_scalar(res, dest)?;
38-
}
39-
"sqrtf64" => {
40-
let [f] = check_intrinsic_arg_count(args)?;
41-
let f = this.read_scalar(f)?.to_f64()?;
42-
// Sqrt is specified to be fully precise.
43-
let res = math::sqrt(f);
44-
let res = this.adjust_nan(res, &[f]);
45-
this.write_scalar(res, dest)?;
46-
}
47-
"sqrtf128" => {
48-
let [f] = check_intrinsic_arg_count(args)?;
49-
let f = this.read_scalar(f)?.to_f128()?;
50-
// Sqrt is specified to be fully precise.
51-
let res = math::sqrt(f);
52-
let res = this.adjust_nan(res, &[f]);
53-
this.write_scalar(res, dest)?;
54-
}
37+
"sqrtf16" => sqrt::<rustc_apfloat::ieee::Half>(this, args, dest)?,
38+
"sqrtf32" => sqrt::<rustc_apfloat::ieee::Single>(this, args, dest)?,
39+
"sqrtf64" => sqrt::<rustc_apfloat::ieee::Double>(this, args, dest)?,
40+
"sqrtf128" => sqrt::<rustc_apfloat::ieee::Quad>(this, args, dest)?,
5541

5642
"fmaf32" => {
5743
let [a, b, c] = check_intrinsic_arg_count(args)?;

src/tools/miri/src/math.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::Neg;
22
use std::{f32, f64};
33

44
use rand::Rng as _;
5-
use rustc_apfloat::Float as _;
5+
use rustc_apfloat::Float;
66
use rustc_apfloat::ieee::{DoubleS, IeeeFloat, Semantics, SingleS};
77
use rustc_middle::ty::{self, FloatTy, ScalarInt};
88

@@ -317,19 +317,19 @@ where
317317
}
318318
}
319319

320-
pub(crate) fn sqrt<S: rustc_apfloat::ieee::Semantics>(x: IeeeFloat<S>) -> IeeeFloat<S> {
320+
pub(crate) fn sqrt<F: Float>(x: F) -> F {
321321
match x.category() {
322322
// preserve zero sign
323323
rustc_apfloat::Category::Zero => x,
324324
// propagate NaN
325325
rustc_apfloat::Category::NaN => x,
326326
// sqrt of negative number is NaN
327-
_ if x.is_negative() => IeeeFloat::NAN,
327+
_ if x.is_negative() => F::NAN,
328328
// sqrt(∞) = ∞
329-
rustc_apfloat::Category::Infinity => IeeeFloat::INFINITY,
329+
rustc_apfloat::Category::Infinity => F::INFINITY,
330330
rustc_apfloat::Category::Normal => {
331331
// Floating point precision, excluding the integer bit
332-
let prec = i32::try_from(S::PRECISION).unwrap() - 1;
332+
let prec = i32::try_from(F::PRECISION).unwrap() - 1;
333333

334334
// x = 2^(exp - prec) * mant
335335
// where mant is an integer with prec+1 bits
@@ -394,7 +394,7 @@ pub(crate) fn sqrt<S: rustc_apfloat::ieee::Semantics>(x: IeeeFloat<S>) -> IeeeFl
394394
res = (res + 1) >> 1;
395395

396396
// Build resulting value with res as mantissa and exp/2 as exponent
397-
IeeeFloat::from_u128(res).value.scalbn(exp / 2 - prec)
397+
F::from_u128(res).value.scalbn(exp / 2 - prec)
398398
}
399399
}
400400
}

0 commit comments

Comments
 (0)