+//! Various number casting functions
+
use crate::prelude::*;
pub(crate) fn clif_intcast(
- fx: &mut FunctionCx<'_, '_, impl Backend>,
+ fx: &mut FunctionCx<'_, '_, impl Module>,
val: Value,
to: Type,
signed: bool,
}
pub(crate) fn clif_int_or_float_cast(
- fx: &mut FunctionCx<'_, '_, impl Backend>,
+ fx: &mut FunctionCx<'_, '_, impl Module>,
from: Value,
from_signed: bool,
to_ty: Type,
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