Skip to content

Commit c906d2e

Browse files
committed
Enable const evaluation for f16 and f128
This excludes casting, which needs more tests.
1 parent 921645c commit c906d2e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,18 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
362362
let left = left.to_scalar();
363363
let right = right.to_scalar();
364364
Ok(match fty {
365-
FloatTy::F16 => unimplemented!("f16_f128"),
365+
FloatTy::F16 => {
366+
self.binary_float_op(bin_op, layout, left.to_f16()?, right.to_f16()?)
367+
}
366368
FloatTy::F32 => {
367369
self.binary_float_op(bin_op, layout, left.to_f32()?, right.to_f32()?)
368370
}
369371
FloatTy::F64 => {
370372
self.binary_float_op(bin_op, layout, left.to_f64()?, right.to_f64()?)
371373
}
372-
FloatTy::F128 => unimplemented!("f16_f128"),
374+
FloatTy::F128 => {
375+
self.binary_float_op(bin_op, layout, left.to_f128()?, right.to_f128()?)
376+
}
373377
})
374378
}
375379
_ if left.layout.ty.is_integral() => {
@@ -431,8 +435,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
431435
let val = val.to_scalar();
432436
// No NaN adjustment here, `-` is a bitwise operation!
433437
let res = match (un_op, fty) {
438+
(Neg, FloatTy::F16) => Scalar::from_f16(-val.to_f16()?),
434439
(Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?),
435440
(Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?),
441+
(Neg, FloatTy::F128) => Scalar::from_f128(-val.to_f128()?),
436442
_ => span_bug!(self.cur_span(), "Invalid float op {:?}", un_op),
437443
};
438444
Ok(ImmTy::from_scalar(res, layout))

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
6969
}
7070
}
7171

72+
impl<Prov> From<Half> for Scalar<Prov> {
73+
#[inline(always)]
74+
fn from(f: Half) -> Self {
75+
Scalar::from_f16(f)
76+
}
77+
}
78+
7279
impl<Prov> From<Single> for Scalar<Prov> {
7380
#[inline(always)]
7481
fn from(f: Single) -> Self {
@@ -83,6 +90,13 @@ impl<Prov> From<Double> for Scalar<Prov> {
8390
}
8491
}
8592

93+
impl<Prov> From<Quad> for Scalar<Prov> {
94+
#[inline(always)]
95+
fn from(f: Quad) -> Self {
96+
Scalar::from_f128(f)
97+
}
98+
}
99+
86100
impl<Prov> From<ScalarInt> for Scalar<Prov> {
87101
#[inline(always)]
88102
fn from(ptr: ScalarInt) -> Self {

0 commit comments

Comments
 (0)