]> git.lizzy.rs Git - rust.git/blobdiff - src/cast.rs
Fold `vtable_trait_upcasting_coercion_new_vptr_slot` logic into obligation processing.
[rust.git] / src / cast.rs
index db465a581275a871247b1f00058fe8c2a615d396..e7e6afeb865bb005bf436b7ec6e2802930dc7fe2 100644 (file)
@@ -1,7 +1,9 @@
+//! 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,
@@ -12,21 +14,6 @@ pub(crate) fn clif_intcast(
         (_, _) if from == to => val,
 
         // extend
-        (_, types::I128) => {
-            let lo = if from == types::I64 {
-                val
-            } else if signed {
-                fx.bcx.ins().sextend(types::I64, val)
-            } else {
-                fx.bcx.ins().uextend(types::I64, val)
-            };
-            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)
-        }
         (_, _) if to.wider_or_equal(from) => {
             if signed {
                 fx.bcx.ins().sextend(to, val)
@@ -36,20 +23,12 @@ pub(crate) fn clif_intcast(
         }
 
         // reduce
-        (types::I128, _) => {
-            let (lsb, _msb) = fx.bcx.ins().isplit(val);
-            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,
@@ -85,24 +64,16 @@ pub(crate) fn clif_int_or_float_cast(
                 },
             );
 
-            let from_rust_ty = if from_signed {
-                fx.codegen_cx.tcx.types.i128
-            } else {
-                fx.codegen_cx.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.codegen_cx.tcx.types.f32,
-                types::F64 => fx.codegen_cx.tcx.types.f64,
+                types::F32 => fx.tcx.types.f32,
+                types::F64 => fx.tcx.types.f64,
                 _ => unreachable!(),
             };
 
             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);
         }
 
@@ -131,23 +102,15 @@ pub(crate) fn clif_int_or_float_cast(
             );
 
             let from_rust_ty = match from_ty {
-                types::F32 => fx.codegen_cx.tcx.types.f32,
-                types::F64 => fx.codegen_cx.tcx.types.f64,
+                types::F32 => fx.tcx.types.f32,
+                types::F64 => fx.tcx.types.f64,
                 _ => unreachable!(),
             };
 
-            let to_rust_ty = if to_signed {
-                fx.codegen_cx.tcx.types.i128
-            } else {
-                fx.codegen_cx.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);
         }
 
@@ -179,12 +142,10 @@ pub(crate) fn clif_int_or_float_cast(
                 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