3 pub(crate) fn clif_intcast(
4 fx: &mut FunctionCx<'_, '_, impl Backend>,
9 let from = fx.bcx.func.dfg.value_type(val);
12 (_, _) if from == to => val,
16 let lo = if from == types::I64 {
19 fx.bcx.ins().sextend(types::I64, val)
21 fx.bcx.ins().uextend(types::I64, val)
24 fx.bcx.ins().sshr_imm(lo, 63)
26 fx.bcx.ins().iconst(types::I64, 0)
28 fx.bcx.ins().iconcat(lo, hi)
30 (_, _) if to.wider_or_equal(from) => {
32 fx.bcx.ins().sextend(to, val)
34 fx.bcx.ins().uextend(to, val)
40 let (lsb, _msb) = fx.bcx.ins().isplit(val);
44 fx.bcx.ins().ireduce(to, lsb)
47 (_, _) => fx.bcx.ins().ireduce(to, val),
51 pub(crate) fn clif_int_or_float_cast(
52 fx: &mut FunctionCx<'_, '_, impl Backend>,
58 let from_ty = fx.bcx.func.dfg.value_type(from);
60 if from_ty.is_int() && to_ty.is_int() {
61 // int-like -> int-like
66 // This is correct as either from_signed == to_signed (=> this is trivially correct)
67 // Or from_clif_ty == to_clif_ty, which means this is a no-op.
70 } else if from_ty.is_int() && to_ty.is_float() {
71 if from_ty == types::I128 {
73 // __float tisf: i128 -> f32
74 // __float tidf: i128 -> f64
75 // __floatuntisf: u128 -> f32
76 // __floatuntidf: u128 -> f64
79 "__float{sign}ti{flt}f",
80 sign = if from_signed { "" } else { "un" },
84 _ => unreachable!("{:?}", to_ty),
88 let from_rust_ty = if from_signed {
94 let to_rust_ty = match to_ty {
95 types::F32 => fx.tcx.types.f32,
96 types::F64 => fx.tcx.types.f64,
103 &[CValue::by_val(from, fx.layout_of(from_rust_ty))],
111 fx.bcx.ins().fcvt_from_sint(to_ty, from)
113 fx.bcx.ins().fcvt_from_uint(to_ty, from)
115 } else if from_ty.is_float() && to_ty.is_int() {
116 if to_ty == types::I128 {
118 // __fix sfti: f32 -> i128
119 // __fix dfti: f64 -> i128
120 // __fixunssfti: f32 -> u128
121 // __fixunsdfti: f64 -> u128
124 "__fix{sign}{flt}fti",
125 sign = if to_signed { "" } else { "uns" },
126 flt = match from_ty {
129 _ => unreachable!("{:?}", to_ty),
133 let from_rust_ty = match from_ty {
134 types::F32 => fx.tcx.types.f32,
135 types::F64 => fx.tcx.types.f64,
139 let to_rust_ty = if to_signed {
148 &[CValue::by_val(from, fx.layout_of(from_rust_ty))],
155 if to_ty == types::I8 || to_ty == types::I16 {
156 // FIXME implement fcvt_to_*int_sat.i8/i16
157 let val = if to_signed {
158 fx.bcx.ins().fcvt_to_sint_sat(types::I32, from)
160 fx.bcx.ins().fcvt_to_uint_sat(types::I32, from)
162 let (min, max) = match (to_ty, to_signed) {
163 (types::I8, false) => (0, u8::MAX as i64),
164 (types::I16, false) => (0, u16::MAX as i64),
165 (types::I8, true) => (i8::MIN as i64, i8::MAX as i64),
166 (types::I16, true) => (i16::MIN as i64, i16::MAX as i64),
169 let min_val = fx.bcx.ins().iconst(types::I32, min);
170 let max_val = fx.bcx.ins().iconst(types::I32, max);
172 let val = if to_signed {
173 let has_underflow = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, val, min);
174 let has_overflow = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, val, max);
175 let bottom_capped = fx.bcx.ins().select(has_underflow, min_val, val);
176 fx.bcx.ins().select(has_overflow, max_val, bottom_capped)
178 let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, val, max);
179 fx.bcx.ins().select(has_overflow, max_val, val)
181 fx.bcx.ins().ireduce(to_ty, val)
184 fx.bcx.ins().fcvt_to_sint_sat(to_ty, from)
186 fx.bcx.ins().fcvt_to_uint_sat(to_ty, from)
189 } else if from_ty.is_float() && to_ty.is_float() {
191 match (from_ty, to_ty) {
192 (types::F32, types::F64) => fx.bcx.ins().fpromote(types::F64, from),
193 (types::F64, types::F32) => fx.bcx.ins().fdemote(types::F32, from),
197 unreachable!("cast value from {:?} to {:?}", from_ty, to_ty);