Skip to content

Commit 99b6e9d

Browse files
committed
miri: reduce code duplication in SSE/SSE2 cvt{,t}s{s,d}2si{,64}
1 parent 4b3acaa commit 99b6e9d

File tree

2 files changed

+14
-62
lines changed

2 files changed

+14
-62
lines changed

src/shims/x86/sse.rs

+7-31
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
154154
};
155155
this.write_scalar(Scalar::from_i32(i32::from(res)), dest)?;
156156
}
157-
// Use to implement _mm_cvtss_si32 and _mm_cvttss_si32.
158-
// Converts the first component of `op` from f32 to i32.
159-
"cvtss2si" | "cvttss2si" => {
157+
// Use to implement the _mm_cvtss_si32, _mm_cvttss_si32,
158+
// _mm_cvtss_si64 and _mm_cvttss_si64 functions.
159+
// Converts the first component of `op` from f32 to i32/i64.
160+
"cvtss2si" | "cvttss2si" | "cvtss2si64" | "cvttss2si64" => {
160161
let [op] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
161162
let (op, _) = this.operand_to_simd(op)?;
162163

@@ -165,41 +166,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
165166
let rnd = match unprefixed_name {
166167
// "current SSE rounding mode", assume nearest
167168
// https://www.felixcloutier.com/x86/cvtss2si
168-
"cvtss2si" => rustc_apfloat::Round::NearestTiesToEven,
169+
"cvtss2si" | "cvtss2si64" => rustc_apfloat::Round::NearestTiesToEven,
169170
// always truncate
170171
// https://www.felixcloutier.com/x86/cvttss2si
171-
"cvttss2si" => rustc_apfloat::Round::TowardZero,
172+
"cvttss2si" | "cvttss2si64" => rustc_apfloat::Round::TowardZero,
172173
_ => unreachable!(),
173174
};
174175

175176
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
176177
// Fallback to minimum acording to SSE semantics.
177-
Scalar::from_i32(i32::MIN)
178-
});
179-
180-
this.write_scalar(res, dest)?;
181-
}
182-
// Use to implement _mm_cvtss_si64 and _mm_cvttss_si64.
183-
// Converts the first component of `op` from f32 to i64.
184-
"cvtss2si64" | "cvttss2si64" => {
185-
let [op] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
186-
let (op, _) = this.operand_to_simd(op)?;
187-
188-
let op = this.read_scalar(&this.project_index(&op, 0)?)?.to_f32()?;
189-
190-
let rnd = match unprefixed_name {
191-
// "current SSE rounding mode", assume nearest
192-
// https://www.felixcloutier.com/x86/cvtss2si
193-
"cvtss2si64" => rustc_apfloat::Round::NearestTiesToEven,
194-
// always truncate
195-
// https://www.felixcloutier.com/x86/cvttss2si
196-
"cvttss2si64" => rustc_apfloat::Round::TowardZero,
197-
_ => unreachable!(),
198-
};
199-
200-
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
201-
// Fallback to minimum acording to SSE semantics.
202-
Scalar::from_i64(i64::MIN)
178+
Scalar::from_int(dest.layout.size.signed_int_min(), dest.layout.size)
203179
});
204180

205181
this.write_scalar(res, dest)?;

src/shims/x86/sse2.rs

+7-31
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
722722
this.write_scalar(Scalar::from_i32(0), &dest)?;
723723
}
724724
}
725-
// Use to implement the _mm_cvtsd_si32 and _mm_cvttsd_si32 functions.
726-
// Converts the first component of `op` from f64 to i32.
727-
"cvtsd2si" | "cvttsd2si" => {
725+
// Use to implement the _mm_cvtsd_si32, _mm_cvttsd_si32,
726+
// _mm_cvtsd_si64 and _mm_cvttsd_si64 functions.
727+
// Converts the first component of `op` from f64 to i32/i64.
728+
"cvtsd2si" | "cvttsd2si" | "cvtsd2si64" | "cvttsd2si64" => {
728729
let [op] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
729730
let (op, _) = this.operand_to_simd(op)?;
730731

@@ -733,41 +734,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
733734
let rnd = match unprefixed_name {
734735
// "current SSE rounding mode", assume nearest
735736
// https://www.felixcloutier.com/x86/cvtsd2si
736-
"cvtsd2si" => rustc_apfloat::Round::NearestTiesToEven,
737+
"cvtsd2si" | "cvtsd2si64" => rustc_apfloat::Round::NearestTiesToEven,
737738
// always truncate
738739
// https://www.felixcloutier.com/x86/cvttsd2si
739-
"cvttsd2si" => rustc_apfloat::Round::TowardZero,
740+
"cvttsd2si" | "cvttsd2si64" => rustc_apfloat::Round::TowardZero,
740741
_ => unreachable!(),
741742
};
742743

743744
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
744745
// Fallback to minimum acording to SSE semantics.
745-
Scalar::from_i32(i32::MIN)
746-
});
747-
748-
this.write_scalar(res, dest)?;
749-
}
750-
// Use to implement the _mm_cvtsd_si64 and _mm_cvttsd_si64 functions.
751-
// Converts the first component of `op` from f64 to i64.
752-
"cvtsd2si64" | "cvttsd2si64" => {
753-
let [op] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
754-
let (op, _) = this.operand_to_simd(op)?;
755-
756-
let op = this.read_scalar(&this.project_index(&op, 0)?)?.to_f64()?;
757-
758-
let rnd = match unprefixed_name {
759-
// "current SSE rounding mode", assume nearest
760-
// https://www.felixcloutier.com/x86/cvtsd2si
761-
"cvtsd2si64" => rustc_apfloat::Round::NearestTiesToEven,
762-
// always truncate
763-
// https://www.felixcloutier.com/x86/cvttsd2si
764-
"cvttsd2si64" => rustc_apfloat::Round::TowardZero,
765-
_ => unreachable!(),
766-
};
767-
768-
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
769-
// Fallback to minimum acording to SSE semantics.
770-
Scalar::from_i64(i64::MIN)
746+
Scalar::from_int(dest.layout.size.signed_int_min(), dest.layout.size)
771747
});
772748

773749
this.write_scalar(res, dest)?;

0 commit comments

Comments
 (0)