+//! Various number casting functions
+
use crate::prelude::*;
pub(crate) fn clif_intcast(
- fx: &mut FunctionCx<'_, '_, impl Backend>,
+ fx: &mut FunctionCx<'_, '_, '_>,
val: Value,
to: Type,
signed: bool,
// reduce
(types::I128, _) => {
let (lsb, _msb) = fx.bcx.ins().isplit(val);
- if to == types::I64 {
- lsb
- } else {
- fx.bcx.ins().ireduce(to, lsb)
- }
+ if to == types::I64 { lsb } else { fx.bcx.ins().ireduce(to, lsb) }
}
(_, _) => fx.bcx.ins().ireduce(to, val),
}
}
pub(crate) fn clif_int_or_float_cast(
- fx: &mut FunctionCx<'_, '_, impl Backend>,
+ fx: &mut FunctionCx<'_, '_, '_>,
from: Value,
from_signed: bool,
to_ty: Type,
},
);
- let from_rust_ty = if from_signed {
- fx.tcx.types.i128
- } else {
- fx.tcx.types.u128
- };
+ let from_rust_ty = if from_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
let to_rust_ty = match to_ty {
types::F32 => fx.tcx.types.f32,
};
return fx
- .easy_call(
- &name,
- &[CValue::by_val(from, fx.layout_of(from_rust_ty))],
- to_rust_ty,
- )
+ .easy_call(&name, &[CValue::by_val(from, fx.layout_of(from_rust_ty))], to_rust_ty)
.load_scalar(fx);
}
_ => unreachable!(),
};
- let to_rust_ty = if to_signed {
- fx.tcx.types.i128
- } else {
- fx.tcx.types.u128
- };
+ let to_rust_ty = if to_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
return fx
- .easy_call(
- &name,
- &[CValue::by_val(from, fx.layout_of(from_rust_ty))],
- to_rust_ty,
- )
+ .easy_call(&name, &[CValue::by_val(from, fx.layout_of(from_rust_ty))], to_rust_ty)
.load_scalar(fx);
}
fx.bcx.ins().fcvt_to_uint_sat(types::I32, from)
};
let (min, max) = match (to_ty, to_signed) {
- (types::I8, false) => (0, u8::MAX as i64),
- (types::I16, false) => (0, u16::MAX as i64),
- (types::I8, true) => (i8::MIN as i64, i8::MAX as i64),
- (types::I16, true) => (i16::MIN as i64, i16::MAX as i64),
+ (types::I8, false) => (0, i64::from(u8::MAX)),
+ (types::I16, false) => (0, i64::from(u16::MAX)),
+ (types::I8, true) => (i64::from(i8::MIN), i64::from(i8::MAX)),
+ (types::I16, true) => (i64::from(i16::MIN), i64::from(i16::MAX)),
_ => unreachable!(),
};
let min_val = fx.bcx.ins().iconst(types::I32, min);
fx.bcx.ins().select(has_overflow, max_val, val)
};
fx.bcx.ins().ireduce(to_ty, val)
+ } else if to_signed {
+ fx.bcx.ins().fcvt_to_sint_sat(to_ty, from)
} else {
- if to_signed {
- fx.bcx.ins().fcvt_to_sint_sat(to_ty, from)
- } else {
- fx.bcx.ins().fcvt_to_uint_sat(to_ty, from)
- }
+ fx.bcx.ins().fcvt_to_uint_sat(to_ty, from)
}
} else if from_ty.is_float() && to_ty.is_float() {
// float -> float