mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-21 11:23:03 +00:00
Implement float -> float and int -> float casts
This commit is contained in:
parent
6c86274943
commit
9a2b1d4ac3
@ -158,3 +158,11 @@ unsafe fn use_ctlz_nonzero(a: u16) -> u16 {
|
||||
fn ptr_as_usize(ptr: *const u8) -> usize {
|
||||
ptr as usize
|
||||
}
|
||||
|
||||
fn float_cast(a: f32, b: f64) -> (f64, f32) {
|
||||
(a as f64, b as f32)
|
||||
}
|
||||
|
||||
fn int_to_float(a: u8, b: i32) -> (f64, f32) {
|
||||
(a as f64, b as f32)
|
||||
}
|
||||
|
25
src/base.rs
25
src/base.rs
@ -395,6 +395,31 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, cur_ebb: Ebb, stmt: &
|
||||
let res = crate::common::cton_intcast(fx, from, from_ty, to_ty, true);
|
||||
lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
|
||||
}
|
||||
(TypeVariants::TyFloat(from_flt), TypeVariants::TyFloat(to_flt)) => {
|
||||
let from = operand.load_value(fx);
|
||||
let res = match (from_flt, to_flt) {
|
||||
(FloatTy::F32, FloatTy::F64) => {
|
||||
fx.bcx.ins().fpromote(types::F64, from)
|
||||
}
|
||||
(FloatTy::F64, FloatTy::F32) => {
|
||||
fx.bcx.ins().fdemote(types::F32, from)
|
||||
}
|
||||
_ => from,
|
||||
};
|
||||
lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
|
||||
}
|
||||
(TypeVariants::TyInt(_), TypeVariants::TyFloat(_)) => {
|
||||
let from = operand.load_value(fx);
|
||||
let f_type = fx.cton_type(to_ty).unwrap();
|
||||
let res = fx.bcx.ins().fcvt_from_sint(f_type, from);
|
||||
lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
|
||||
}
|
||||
(TypeVariants::TyUint(_), TypeVariants::TyFloat(_)) => {
|
||||
let from = operand.load_value(fx);
|
||||
let f_type = fx.cton_type(to_ty).unwrap();
|
||||
let res = fx.bcx.ins().fcvt_from_uint(f_type, from);
|
||||
lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
|
||||
}
|
||||
_ => unimpl!("rval misc {:?} {:?}", from_ty, to_ty),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user