Skip to content

Commit e0e59f6

Browse files
committed
Simplify finiteness checking
1 parent 68d2955 commit e0e59f6

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/shims/intrinsics.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,35 +173,31 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
173173
"frem_fast" => mir::BinOp::Rem,
174174
_ => bug!(),
175175
};
176-
let a_valid = match a.layout.ty.kind() {
177-
ty::Float(FloatTy::F32) => a.to_scalar()?.to_f32()?.is_finite(),
178-
ty::Float(FloatTy::F64) => a.to_scalar()?.to_f64()?.is_finite(),
179-
_ => bug!(
180-
"`{}` called with non-float input type {:?}",
176+
let float_finite = |x: ImmTy<'tcx, _>| -> InterpResult<'tcx, bool> {
177+
Ok(match x.layout.ty.kind() {
178+
ty::Float(FloatTy::F32) => x.to_scalar()?.to_f32()?.is_finite(),
179+
ty::Float(FloatTy::F64) => x.to_scalar()?.to_f64()?.is_finite(),
180+
_ => bug!(
181+
"`{}` called with non-float input type {:?}",
182+
intrinsic_name,
183+
x.layout.ty
184+
),
185+
})
186+
};
187+
match (float_finite(a)?, float_finite(b)?) {
188+
(false, false) => throw_ub_format!(
189+
"`{}` intrinsic called with non-finite value as both parameters",
181190
intrinsic_name,
182-
a.layout.ty
183191
),
184-
};
185-
if !a_valid {
186-
throw_ub_format!(
192+
(false, _) => throw_ub_format!(
187193
"`{}` intrinsic called with non-finite value as first parameter",
188194
intrinsic_name,
189-
);
190-
}
191-
let b_valid = match b.layout.ty.kind() {
192-
ty::Float(FloatTy::F32) => b.to_scalar()?.to_f32()?.is_finite(),
193-
ty::Float(FloatTy::F64) => b.to_scalar()?.to_f64()?.is_finite(),
194-
_ => bug!(
195-
"`{}` called with non-float input type {:?}",
196-
intrinsic_name,
197-
b.layout.ty
198195
),
199-
};
200-
if !b_valid {
201-
throw_ub_format!(
196+
(_, false) => throw_ub_format!(
202197
"`{}` intrinsic called with non-finite value as second parameter",
203198
intrinsic_name,
204-
);
199+
),
200+
_ => {}
205201
}
206202
this.binop_ignore_overflow(op, &a, &b, dest)?;
207203
}

tests/compile-fail/fast_math_both.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
fn main() {
44
unsafe {
5-
let _x: f32 = core::intrinsics::frem_fast(f32::NAN, 3.2); //~ ERROR `frem_fast` intrinsic called with non-finite value as first parameter
5+
let _x: f32 = core::intrinsics::fsub_fast(f32::NAN, f32::NAN); //~ ERROR `fsub_fast` intrinsic called with non-finite value as both parameters
66
}
77
}

tests/compile-fail/fast_math_first.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
fn main() {
44
unsafe {
5-
let _x: f32 = core::intrinsics::fsub_fast(f32::NAN, f32::NAN); //~ ERROR `fsub_fast` intrinsic called with non-finite value as first parameter
5+
let _x: f32 = core::intrinsics::frem_fast(f32::NAN, 3.2); //~ ERROR `frem_fast` intrinsic called with non-finite value as first parameter
66
}
77
}

0 commit comments

Comments
 (0)