Skip to content

Commit 69a2513

Browse files
committed
Add f16-f128 support for SIMD intrinsics
- refactor float rounding intrinsics a bit
1 parent c1878f3 commit 69a2513

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10101010
interp_ok(())
10111011
}
10121012

1013+
fn float_round<F>(
1014+
&mut self,
1015+
x: Scalar<M::Provenance>,
1016+
mode: rustc_apfloat::Round,
1017+
) -> InterpResult<'tcx, F>
1018+
where
1019+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F>,
1020+
{
1021+
let x: F = x.to_float()?;
1022+
let res = x.round_to_integral(mode).value;
1023+
let res = self.adjust_nan(res, &[x]);
1024+
interp_ok(res)
1025+
}
1026+
10131027
fn float_round_intrinsic<F>(
10141028
&mut self,
10151029
args: &[OpTy<'tcx, M::Provenance>],
@@ -1019,9 +1033,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10191033
where
10201034
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
10211035
{
1022-
let x: F = self.read_scalar(&args[0])?.to_float()?;
1023-
let res = x.round_to_integral(mode).value;
1024-
let res = self.adjust_nan(res, &[x]);
1036+
let res = self.float_round::<F>(self.read_scalar(&args[0])?, mode)?;
10251037
self.write_scalar(res, dest)?;
10261038
interp_ok(())
10271039
}

compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
119119
let op = op.to_scalar();
120120
// "Bitwise" operation, no NaN adjustments
121121
match float_ty {
122-
FloatTy::F16 => unimplemented!("f16_f128"),
122+
FloatTy::F16 => Scalar::from_f16(op.to_f16()?.abs()),
123123
FloatTy::F32 => Scalar::from_f32(op.to_f32()?.abs()),
124124
FloatTy::F64 => Scalar::from_f64(op.to_f64()?.abs()),
125-
FloatTy::F128 => unimplemented!("f16_f128"),
125+
FloatTy::F128 => Scalar::from_f128(op.to_f128()?.abs()),
126126
}
127127
}
128128
Op::Round(rounding) => {
@@ -133,21 +133,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
133133
intrinsic_name
134134
)
135135
};
136+
let op = op.to_scalar();
136137
match float_ty {
137-
FloatTy::F16 => unimplemented!("f16_f128"),
138-
FloatTy::F32 => {
139-
let f = op.to_scalar().to_f32()?;
140-
let res = f.round_to_integral(rounding).value;
141-
let res = self.adjust_nan(res, &[f]);
142-
Scalar::from_f32(res)
143-
}
144-
FloatTy::F64 => {
145-
let f = op.to_scalar().to_f64()?;
146-
let res = f.round_to_integral(rounding).value;
147-
let res = self.adjust_nan(res, &[f]);
148-
Scalar::from_f64(res)
149-
}
150-
FloatTy::F128 => unimplemented!("f16_f128"),
138+
FloatTy::F16 => Scalar::from_f16(self.float_round(op, rounding)?),
139+
FloatTy::F32 => Scalar::from_f32(self.float_round(op, rounding)?),
140+
FloatTy::F64 => Scalar::from_f64(self.float_round(op, rounding)?),
141+
FloatTy::F128 => Scalar::from_f128(self.float_round(op, rounding)?),
151142
}
152143
}
153144
Op::Numeric(name) => {
@@ -724,10 +715,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
724715
};
725716

726717
let val = match float_ty {
727-
FloatTy::F16 => unimplemented!("f16_f128"),
718+
FloatTy::F16 => Scalar::from_f16(self.float_muladd(a, b, c, typ)?),
728719
FloatTy::F32 => Scalar::from_f32(self.float_muladd(a, b, c, typ)?),
729720
FloatTy::F64 => Scalar::from_f64(self.float_muladd(a, b, c, typ)?),
730-
FloatTy::F128 => unimplemented!("f16_f128"),
721+
FloatTy::F128 => Scalar::from_f128(self.float_muladd(a, b, c, typ)?),
731722
};
732723
self.write_scalar(val, &dest)?;
733724
}

0 commit comments

Comments
 (0)