2019-07-31 02:45:11 -05:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn clif_intcast(
|
|
|
|
fx: &mut FunctionCx<'_, '_, impl Backend>,
|
2019-07-31 02:45:11 -05:00
|
|
|
val: Value,
|
|
|
|
to: Type,
|
|
|
|
signed: bool,
|
|
|
|
) -> Value {
|
|
|
|
let from = fx.bcx.func.dfg.value_type(val);
|
|
|
|
match (from, to) {
|
|
|
|
// equal
|
|
|
|
(_, _) if from == to => val,
|
|
|
|
|
|
|
|
// extend
|
|
|
|
(_, types::I128) => {
|
2019-11-16 09:44:26 -06:00
|
|
|
let lo = if from == types::I64 {
|
2019-07-31 02:45:11 -05:00
|
|
|
val
|
|
|
|
} else if signed {
|
|
|
|
fx.bcx.ins().sextend(types::I64, val)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().uextend(types::I64, val)
|
|
|
|
};
|
2019-11-16 09:44:26 -06:00
|
|
|
let hi = if signed {
|
|
|
|
fx.bcx.ins().sshr_imm(lo, 63)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().iconst(types::I64, 0)
|
|
|
|
};
|
|
|
|
fx.bcx.ins().iconcat(lo, hi)
|
2019-07-31 02:45:11 -05:00
|
|
|
}
|
|
|
|
(_, _) if to.wider_or_equal(from) => {
|
|
|
|
if signed {
|
|
|
|
fx.bcx.ins().sextend(to, val)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().uextend(to, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reduce
|
|
|
|
(types::I128, _) => {
|
|
|
|
let (lsb, _msb) = fx.bcx.ins().isplit(val);
|
|
|
|
if to == types::I64 {
|
|
|
|
lsb
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().ireduce(to, lsb)
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 12:28:09 -05:00
|
|
|
(_, _) => fx.bcx.ins().ireduce(to, val),
|
2019-07-31 02:45:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clif_int_or_float_cast(
|
|
|
|
fx: &mut FunctionCx<'_, '_, impl Backend>,
|
|
|
|
from: Value,
|
2019-08-12 10:25:16 -05:00
|
|
|
from_signed: bool,
|
2019-07-31 02:45:11 -05:00
|
|
|
to_ty: Type,
|
2019-08-12 10:25:16 -05:00
|
|
|
to_signed: bool,
|
2019-07-31 02:45:11 -05:00
|
|
|
) -> Value {
|
|
|
|
let from_ty = fx.bcx.func.dfg.value_type(from);
|
|
|
|
|
|
|
|
if from_ty.is_int() && to_ty.is_int() {
|
|
|
|
// int-like -> int-like
|
|
|
|
clif_intcast(
|
|
|
|
fx,
|
|
|
|
from,
|
|
|
|
to_ty,
|
2019-11-16 09:54:47 -06:00
|
|
|
// This is correct as either from_signed == to_signed (=> this is trivially correct)
|
|
|
|
// Or from_clif_ty == to_clif_ty, which means this is a no-op.
|
|
|
|
from_signed,
|
2019-07-31 02:45:11 -05:00
|
|
|
)
|
|
|
|
} else if from_ty.is_int() && to_ty.is_float() {
|
2019-08-19 09:43:25 -05:00
|
|
|
if from_ty == types::I128 {
|
2019-08-21 07:01:29 -05:00
|
|
|
// _______ss__f_
|
|
|
|
// __float tisf: i128 -> f32
|
|
|
|
// __float tidf: i128 -> f64
|
|
|
|
// __floatuntisf: u128 -> f32
|
|
|
|
// __floatuntidf: u128 -> f64
|
2019-08-21 07:35:48 -05:00
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
let name = format!(
|
|
|
|
"__float{sign}ti{flt}f",
|
|
|
|
sign = if from_signed { "" } else { "un" },
|
|
|
|
flt = match to_ty {
|
2019-08-21 07:35:48 -05:00
|
|
|
types::F32 => "s",
|
|
|
|
types::F64 => "d",
|
|
|
|
_ => unreachable!("{:?}", to_ty),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let from_rust_ty = if from_signed {
|
2019-08-21 07:01:29 -05:00
|
|
|
fx.tcx.types.i128
|
|
|
|
} else {
|
|
|
|
fx.tcx.types.u128
|
2019-08-21 07:35:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let to_rust_ty = match to_ty {
|
2019-08-21 07:01:29 -05:00
|
|
|
types::F32 => fx.tcx.types.f32,
|
|
|
|
types::F64 => fx.tcx.types.f64,
|
|
|
|
_ => unreachable!(),
|
2019-08-21 07:35:48 -05:00
|
|
|
};
|
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
return fx
|
|
|
|
.easy_call(
|
|
|
|
&name,
|
|
|
|
&[CValue::by_val(from, fx.layout_of(from_rust_ty))],
|
|
|
|
to_rust_ty,
|
|
|
|
)
|
|
|
|
.load_scalar(fx);
|
2019-08-19 09:43:25 -05:00
|
|
|
}
|
|
|
|
|
2019-07-31 02:45:11 -05:00
|
|
|
// int-like -> float
|
2019-08-12 10:25:16 -05:00
|
|
|
if from_signed {
|
2019-07-31 02:45:11 -05:00
|
|
|
fx.bcx.ins().fcvt_from_sint(to_ty, from)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().fcvt_from_uint(to_ty, from)
|
|
|
|
}
|
|
|
|
} else if from_ty.is_float() && to_ty.is_int() {
|
2019-08-19 09:43:25 -05:00
|
|
|
if to_ty == types::I128 {
|
2019-08-21 07:01:29 -05:00
|
|
|
// _____sssf___
|
|
|
|
// __fix sfti: f32 -> i128
|
|
|
|
// __fix dfti: f64 -> i128
|
|
|
|
// __fixunssfti: f32 -> u128
|
|
|
|
// __fixunsdfti: f64 -> u128
|
2019-08-21 07:35:48 -05:00
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
let name = format!(
|
|
|
|
"__fix{sign}{flt}fti",
|
|
|
|
sign = if to_signed { "" } else { "uns" },
|
|
|
|
flt = match from_ty {
|
2019-08-21 07:35:48 -05:00
|
|
|
types::F32 => "s",
|
|
|
|
types::F64 => "d",
|
|
|
|
_ => unreachable!("{:?}", to_ty),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let from_rust_ty = match from_ty {
|
2019-08-21 07:01:29 -05:00
|
|
|
types::F32 => fx.tcx.types.f32,
|
|
|
|
types::F64 => fx.tcx.types.f64,
|
|
|
|
_ => unreachable!(),
|
2019-08-21 07:35:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let to_rust_ty = if to_signed {
|
2019-08-21 07:01:29 -05:00
|
|
|
fx.tcx.types.i128
|
|
|
|
} else {
|
|
|
|
fx.tcx.types.u128
|
2019-08-21 07:35:48 -05:00
|
|
|
};
|
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
return fx
|
|
|
|
.easy_call(
|
|
|
|
&name,
|
|
|
|
&[CValue::by_val(from, fx.layout_of(from_rust_ty))],
|
|
|
|
to_rust_ty,
|
|
|
|
)
|
|
|
|
.load_scalar(fx);
|
2019-08-19 09:43:25 -05:00
|
|
|
}
|
|
|
|
|
2019-07-31 02:45:11 -05:00
|
|
|
// float -> int-like
|
2019-08-12 10:25:16 -05:00
|
|
|
if to_ty == types::I8 || to_ty == types::I16 {
|
2019-08-18 09:52:07 -05:00
|
|
|
// FIXME implement fcvt_to_*int_sat.i8/i16
|
2019-08-12 10:25:16 -05:00
|
|
|
let val = if to_signed {
|
|
|
|
fx.bcx.ins().fcvt_to_sint_sat(types::I32, from)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().fcvt_to_uint_sat(types::I32, from)
|
|
|
|
};
|
|
|
|
let (min, max) = type_min_max_value(to_ty, to_signed);
|
|
|
|
let min_val = fx.bcx.ins().iconst(types::I32, min);
|
|
|
|
let max_val = fx.bcx.ins().iconst(types::I32, max);
|
|
|
|
|
|
|
|
let val = if to_signed {
|
2019-08-31 12:28:09 -05:00
|
|
|
let has_underflow = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, val, min);
|
|
|
|
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, val, max);
|
2019-08-12 10:25:16 -05:00
|
|
|
let bottom_capped = fx.bcx.ins().select(has_underflow, min_val, val);
|
|
|
|
fx.bcx.ins().select(has_overflow, max_val, bottom_capped)
|
|
|
|
} else {
|
2019-08-31 12:28:09 -05:00
|
|
|
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, val, max);
|
2019-08-12 10:25:16 -05:00
|
|
|
fx.bcx.ins().select(has_overflow, max_val, val)
|
|
|
|
};
|
|
|
|
fx.bcx.ins().ireduce(to_ty, val)
|
2019-07-31 02:45:11 -05:00
|
|
|
} else {
|
2019-08-12 10:25:16 -05:00
|
|
|
if to_signed {
|
|
|
|
fx.bcx.ins().fcvt_to_sint_sat(to_ty, from)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().fcvt_to_uint_sat(to_ty, from)
|
|
|
|
}
|
2019-07-31 02:45:11 -05:00
|
|
|
}
|
|
|
|
} else if from_ty.is_float() && to_ty.is_float() {
|
|
|
|
// float -> float
|
|
|
|
match (from_ty, to_ty) {
|
2019-08-31 12:28:09 -05:00
|
|
|
(types::F32, types::F64) => fx.bcx.ins().fpromote(types::F64, from),
|
|
|
|
(types::F64, types::F32) => fx.bcx.ins().fdemote(types::F32, from),
|
2019-07-31 02:45:11 -05:00
|
|
|
_ => from,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!("cast value from {:?} to {:?}", from_ty, to_ty);
|
|
|
|
}
|
|
|
|
}
|