]> git.lizzy.rs Git - rust.git/blob - src/codegen_i128.rs
Implement 128bit shl and shr binops
[rust.git] / src / codegen_i128.rs
1 //! Replaces 128-bit operators with lang item calls
2
3 use crate::prelude::*;
4
5 pub fn maybe_codegen<'a, 'tcx>(
6     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
7     bin_op: BinOp,
8     checked: bool,
9     is_signed: bool,
10     lhs: CValue<'tcx>,
11     rhs: CValue<'tcx>,
12     out_ty: Ty<'tcx>,
13 ) -> Option<CValue<'tcx>> {
14     if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 {
15         return None;
16     }
17
18     let lhs_val = lhs.load_scalar(fx);
19     let rhs_val = rhs.load_scalar(fx);
20
21     match bin_op {
22         BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
23             assert!(!checked);
24             return None;
25         }
26         BinOp::Add | BinOp::Sub => {
27             return None; // FIXME implement checked versions
28         }
29         BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
30         BinOp::Mul => {
31             let res = if checked {
32                 if is_signed {
33                     let oflow_place = CPlace::new_stack_slot(fx, fx.tcx.types.i32);
34                     let oflow_addr = oflow_place.to_addr(fx);
35                     let oflow_addr = CValue::by_val(oflow_addr, fx.layout_of(fx.tcx.mk_mut_ptr(fx.tcx.types.i32)));
36                     let val = fx.easy_call("__muloti4", &[lhs, rhs, oflow_addr], fx.tcx.types.i128);
37                     let val = val.load_scalar(fx);
38                     let oflow = oflow_place.to_cvalue(fx).load_scalar(fx);
39                     let oflow = fx.bcx.ins().icmp_imm(IntCC::NotEqual, oflow, 0);
40                     let oflow = fx.bcx.ins().bint(types::I8, oflow);
41                     CValue::by_val_pair(val, oflow, fx.layout_of(out_ty))
42                 } else {
43                     // FIXME implement it
44                 let out_layout = fx.layout_of(out_ty);
45                     return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit checked binop unsigned mul")));
46                 }
47             } else {
48                 let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 };
49                 fx.easy_call("__multi3", &[lhs, rhs], val_ty)
50             };
51             return Some(res);
52         }
53         BinOp::Div => {
54             let res = if checked {
55                 // FIXME implement it
56                 let out_layout = fx.layout_of(out_ty);
57                 return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit checked binop div")));
58             } else {
59                 if is_signed {
60                     fx.easy_call("__divti3", &[lhs, rhs], fx.tcx.types.i128)
61                 } else {
62                     fx.easy_call("__udivti3", &[lhs, rhs], fx.tcx.types.u128)
63                 }
64             };
65             return Some(res);
66         }
67         BinOp::Rem => {
68             let res = if checked {
69                 // FIXME implement it
70                 let out_layout = fx.layout_of(out_ty);
71                 return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit checked binop rem")));
72             } else {
73                 if is_signed {
74                     fx.easy_call("__modti3", &[lhs, rhs], fx.tcx.types.i128)
75                 } else {
76                     fx.easy_call("__umodti3", &[lhs, rhs], fx.tcx.types.u128)
77                 }
78             };
79             return Some(res);
80         }
81         BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => {
82             assert!(!checked);
83             let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
84             let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
85             let res = match (bin_op, is_signed) {
86                 (BinOp::Eq, _) => {
87                     let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
88                     let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
89                     fx.bcx.ins().band(lsb_eq, msb_eq)
90                 }
91                 (BinOp::Ne, _) => {
92                     let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
93                     let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
94                     fx.bcx.ins().bor(lsb_ne, msb_ne)
95                 }
96                 _ => {
97                     // FIXME implement it
98                     let out_layout = fx.layout_of(out_ty);
99                     return Some(crate::trap::trap_unreachable_ret_value(fx, out_layout, format!("unimplemented 128bit binop {:?}", bin_op)));
100                 },
101             };
102
103             let res = fx.bcx.ins().bint(types::I8, res);
104             let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.bool));
105             return Some(res);
106         }
107         BinOp::Shl | BinOp::Shr => {
108             let is_overflow = if checked {
109                 // rhs >= 128
110
111                 // FIXME support non 128bit rhs
112                 /*let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs_val);
113                 let rhs_msb_gt_0 = fx.bcx.ins().icmp_imm(IntCC::NotEqual, rhs_msb, 0);
114                 let rhs_lsb_ge_128 = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThan, rhs_lsb, 127);
115                 let is_overflow = fx.bcx.ins().bor(rhs_msb_gt_0, rhs_lsb_ge_128);*/
116                 let is_overflow = fx.bcx.ins().bconst(types::B1, false);
117
118                 Some(fx.bcx.ins().bint(types::I8, is_overflow))
119             } else {
120                 None
121             };
122
123             // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit
124             // integer into its lsb and msb.
125             // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217
126             if let Some(64) = resolve_value_imm(fx.bcx.func, rhs_val) {
127                 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val);
128                 let all_zeros = fx.bcx.ins().iconst(types::I64, 0);
129                 let val = match (bin_op, is_signed) {
130                     (BinOp::Shr, false) => {
131                         let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros);
132                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128)))
133                     }
134                     (BinOp::Shr, true) => {
135                         let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0);
136                         let all_ones = fx.bcx.ins().iconst(types::I64, u64::max_value() as i64);
137                         let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones);
138
139                         let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits);
140                         Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128)))
141                     }
142                     (BinOp::Shl, _) => {
143                         let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb);
144                         Some(CValue::by_val(val, fx.layout_of(out_ty)))
145                     }
146                     _ => None
147                 };
148                 if let Some(val) = val {
149                     if let Some(is_overflow) = is_overflow {
150                         let val = val.load_scalar(fx);
151                         return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
152                     } else {
153                         return Some(val);
154                     }
155                 }
156             }
157
158             let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false);
159             let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32));
160             let val = match (bin_op, is_signed) {
161                 (BinOp::Shl, false) => {
162                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
163                 }
164                 (BinOp::Shl, true) => {
165                     fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
166                 }
167                 (BinOp::Shr, false) => {
168                     fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128)
169                 }
170                 (BinOp::Shr, true) => {
171                     fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128)
172                 }
173                 (_, _) => unreachable!(),
174             };
175             if let Some(is_overflow) = is_overflow {
176                 let val = val.load_scalar(fx);
177                 Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty)))
178             } else {
179                 Some(val)
180             }
181         }
182     }
183 }