]> git.lizzy.rs Git - rust.git/blob - src/shims/intrinsics.rs
implement simd_reduce_min/max
[rust.git] / src / shims / intrinsics.rs
1 use std::iter;
2
3 use log::trace;
4
5 use rustc_apfloat::{Float, Round};
6 use rustc_middle::ty::layout::{IntegerExt, LayoutOf};
7 use rustc_middle::{mir, mir::BinOp, ty, ty::FloatTy};
8 use rustc_target::abi::{Align, Integer};
9
10 use crate::*;
11 use helpers::{bool_to_simd_element, check_arg_count, simd_element_to_bool};
12
13 pub enum AtomicOp {
14     MirOp(mir::BinOp, bool),
15     Max,
16     Min,
17 }
18
19 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
20 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
21     fn call_intrinsic(
22         &mut self,
23         instance: ty::Instance<'tcx>,
24         args: &[OpTy<'tcx, Tag>],
25         ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
26         _unwind: StackPopUnwind,
27     ) -> InterpResult<'tcx> {
28         let this = self.eval_context_mut();
29
30         if this.emulate_intrinsic(instance, args, ret)? {
31             return Ok(());
32         }
33
34         // All supported intrinsics have a return place.
35         let intrinsic_name = this.tcx.item_name(instance.def_id());
36         let intrinsic_name = intrinsic_name.as_str();
37         let (dest, ret) = match ret {
38             None => throw_unsup_format!("unimplemented (diverging) intrinsic: {}", intrinsic_name),
39             Some(p) => p,
40         };
41
42         // Then handle terminating intrinsics.
43         match intrinsic_name {
44             // Miri overwriting CTFE intrinsics.
45             "ptr_guaranteed_eq" => {
46                 let &[ref left, ref right] = check_arg_count(args)?;
47                 let left = this.read_immediate(left)?;
48                 let right = this.read_immediate(right)?;
49                 this.binop_ignore_overflow(mir::BinOp::Eq, &left, &right, dest)?;
50             }
51             "ptr_guaranteed_ne" => {
52                 let &[ref left, ref right] = check_arg_count(args)?;
53                 let left = this.read_immediate(left)?;
54                 let right = this.read_immediate(right)?;
55                 this.binop_ignore_overflow(mir::BinOp::Ne, &left, &right, dest)?;
56             }
57             "const_allocate" => {
58                 // For now, for compatibility with the run-time implementation of this, we just return null.
59                 // See <https://github.com/rust-lang/rust/issues/93935>.
60                 this.write_null(dest)?;
61             }
62             "const_deallocate" => {
63                 // complete NOP
64             }
65
66             // Raw memory accesses
67             "volatile_load" => {
68                 let &[ref place] = check_arg_count(args)?;
69                 let place = this.deref_operand(place)?;
70                 this.copy_op(&place.into(), dest)?;
71             }
72             "volatile_store" => {
73                 let &[ref place, ref dest] = check_arg_count(args)?;
74                 let place = this.deref_operand(place)?;
75                 this.copy_op(dest, &place.into())?;
76             }
77
78             "write_bytes" | "volatile_set_memory" => {
79                 let &[ref ptr, ref val_byte, ref count] = check_arg_count(args)?;
80                 let ty = instance.substs.type_at(0);
81                 let ty_layout = this.layout_of(ty)?;
82                 let val_byte = this.read_scalar(val_byte)?.to_u8()?;
83                 let ptr = this.read_pointer(ptr)?;
84                 let count = this.read_scalar(count)?.to_machine_usize(this)?;
85                 let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
86                     err_ub_format!("overflow computing total size of `{}`", intrinsic_name)
87                 })?;
88                 this.memory
89                     .write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?;
90             }
91
92             // Floating-point operations
93             #[rustfmt::skip]
94             | "sinf32"
95             | "fabsf32"
96             | "cosf32"
97             | "sqrtf32"
98             | "expf32"
99             | "exp2f32"
100             | "logf32"
101             | "log10f32"
102             | "log2f32"
103             | "floorf32"
104             | "ceilf32"
105             | "truncf32"
106             | "roundf32"
107             => {
108                 let &[ref f] = check_arg_count(args)?;
109                 // FIXME: Using host floats.
110                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
111                 let f = match intrinsic_name {
112                     "sinf32" => f.sin(),
113                     "fabsf32" => f.abs(),
114                     "cosf32" => f.cos(),
115                     "sqrtf32" => f.sqrt(),
116                     "expf32" => f.exp(),
117                     "exp2f32" => f.exp2(),
118                     "logf32" => f.ln(),
119                     "log10f32" => f.log10(),
120                     "log2f32" => f.log2(),
121                     "floorf32" => f.floor(),
122                     "ceilf32" => f.ceil(),
123                     "truncf32" => f.trunc(),
124                     "roundf32" => f.round(),
125                     _ => bug!(),
126                 };
127                 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
128             }
129
130             #[rustfmt::skip]
131             | "sinf64"
132             | "fabsf64"
133             | "cosf64"
134             | "sqrtf64"
135             | "expf64"
136             | "exp2f64"
137             | "logf64"
138             | "log10f64"
139             | "log2f64"
140             | "floorf64"
141             | "ceilf64"
142             | "truncf64"
143             | "roundf64"
144             => {
145                 let &[ref f] = check_arg_count(args)?;
146                 // FIXME: Using host floats.
147                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
148                 let f = match intrinsic_name {
149                     "sinf64" => f.sin(),
150                     "fabsf64" => f.abs(),
151                     "cosf64" => f.cos(),
152                     "sqrtf64" => f.sqrt(),
153                     "expf64" => f.exp(),
154                     "exp2f64" => f.exp2(),
155                     "logf64" => f.ln(),
156                     "log10f64" => f.log10(),
157                     "log2f64" => f.log2(),
158                     "floorf64" => f.floor(),
159                     "ceilf64" => f.ceil(),
160                     "truncf64" => f.trunc(),
161                     "roundf64" => f.round(),
162                     _ => bug!(),
163                 };
164                 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
165             }
166
167             #[rustfmt::skip]
168             | "fadd_fast"
169             | "fsub_fast"
170             | "fmul_fast"
171             | "fdiv_fast"
172             | "frem_fast"
173             => {
174                 let &[ref a, ref b] = check_arg_count(args)?;
175                 let a = this.read_immediate(a)?;
176                 let b = this.read_immediate(b)?;
177                 let op = match intrinsic_name {
178                     "fadd_fast" => mir::BinOp::Add,
179                     "fsub_fast" => mir::BinOp::Sub,
180                     "fmul_fast" => mir::BinOp::Mul,
181                     "fdiv_fast" => mir::BinOp::Div,
182                     "frem_fast" => mir::BinOp::Rem,
183                     _ => bug!(),
184                 };
185                 let float_finite = |x: ImmTy<'tcx, _>| -> InterpResult<'tcx, bool> {
186                     Ok(match x.layout.ty.kind() {
187                         ty::Float(FloatTy::F32) => x.to_scalar()?.to_f32()?.is_finite(),
188                         ty::Float(FloatTy::F64) => x.to_scalar()?.to_f64()?.is_finite(),
189                         _ => bug!(
190                             "`{}` called with non-float input type {:?}",
191                             intrinsic_name,
192                             x.layout.ty
193                         ),
194                     })
195                 };
196                 match (float_finite(a)?, float_finite(b)?) {
197                     (false, false) => throw_ub_format!(
198                         "`{}` intrinsic called with non-finite value as both parameters",
199                         intrinsic_name,
200                     ),
201                     (false, _) => throw_ub_format!(
202                         "`{}` intrinsic called with non-finite value as first parameter",
203                         intrinsic_name,
204                     ),
205                     (_, false) => throw_ub_format!(
206                         "`{}` intrinsic called with non-finite value as second parameter",
207                         intrinsic_name,
208                     ),
209                     _ => {}
210                 }
211                 this.binop_ignore_overflow(op, &a, &b, dest)?;
212             }
213
214             #[rustfmt::skip]
215             | "minnumf32"
216             | "maxnumf32"
217             | "copysignf32"
218             => {
219                 let &[ref a, ref b] = check_arg_count(args)?;
220                 let a = this.read_scalar(a)?.to_f32()?;
221                 let b = this.read_scalar(b)?.to_f32()?;
222                 let res = match intrinsic_name {
223                     "minnumf32" => a.min(b),
224                     "maxnumf32" => a.max(b),
225                     "copysignf32" => a.copy_sign(b),
226                     _ => bug!(),
227                 };
228                 this.write_scalar(Scalar::from_f32(res), dest)?;
229             }
230
231             #[rustfmt::skip]
232             | "minnumf64"
233             | "maxnumf64"
234             | "copysignf64"
235             => {
236                 let &[ref a, ref b] = check_arg_count(args)?;
237                 let a = this.read_scalar(a)?.to_f64()?;
238                 let b = this.read_scalar(b)?.to_f64()?;
239                 let res = match intrinsic_name {
240                     "minnumf64" => a.min(b),
241                     "maxnumf64" => a.max(b),
242                     "copysignf64" => a.copy_sign(b),
243                     _ => bug!(),
244                 };
245                 this.write_scalar(Scalar::from_f64(res), dest)?;
246             }
247
248             "powf32" => {
249                 let &[ref f, ref f2] = check_arg_count(args)?;
250                 // FIXME: Using host floats.
251                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
252                 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
253                 this.write_scalar(Scalar::from_u32(f.powf(f2).to_bits()), dest)?;
254             }
255
256             "powf64" => {
257                 let &[ref f, ref f2] = check_arg_count(args)?;
258                 // FIXME: Using host floats.
259                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
260                 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
261                 this.write_scalar(Scalar::from_u64(f.powf(f2).to_bits()), dest)?;
262             }
263
264             "fmaf32" => {
265                 let &[ref a, ref b, ref c] = check_arg_count(args)?;
266                 let a = this.read_scalar(a)?.to_f32()?;
267                 let b = this.read_scalar(b)?.to_f32()?;
268                 let c = this.read_scalar(c)?.to_f32()?;
269                 let res = a.mul_add(b, c).value;
270                 this.write_scalar(Scalar::from_f32(res), dest)?;
271             }
272
273             "fmaf64" => {
274                 let &[ref a, ref b, ref c] = check_arg_count(args)?;
275                 let a = this.read_scalar(a)?.to_f64()?;
276                 let b = this.read_scalar(b)?.to_f64()?;
277                 let c = this.read_scalar(c)?.to_f64()?;
278                 let res = a.mul_add(b, c).value;
279                 this.write_scalar(Scalar::from_f64(res), dest)?;
280             }
281
282             "powif32" => {
283                 let &[ref f, ref i] = check_arg_count(args)?;
284                 // FIXME: Using host floats.
285                 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
286                 let i = this.read_scalar(i)?.to_i32()?;
287                 this.write_scalar(Scalar::from_u32(f.powi(i).to_bits()), dest)?;
288             }
289
290             "powif64" => {
291                 let &[ref f, ref i] = check_arg_count(args)?;
292                 // FIXME: Using host floats.
293                 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
294                 let i = this.read_scalar(i)?.to_i32()?;
295                 this.write_scalar(Scalar::from_u64(f.powi(i).to_bits()), dest)?;
296             }
297
298             "float_to_int_unchecked" => {
299                 let &[ref val] = check_arg_count(args)?;
300                 let val = this.read_immediate(val)?;
301
302                 let res = match val.layout.ty.kind() {
303                     ty::Float(FloatTy::F32) =>
304                         this.float_to_int_unchecked(val.to_scalar()?.to_f32()?, dest.layout.ty)?,
305                     ty::Float(FloatTy::F64) =>
306                         this.float_to_int_unchecked(val.to_scalar()?.to_f64()?, dest.layout.ty)?,
307                     _ =>
308                         bug!(
309                             "`float_to_int_unchecked` called with non-float input type {:?}",
310                             val.layout.ty
311                         ),
312                 };
313
314                 this.write_scalar(res, dest)?;
315             }
316
317             // SIMD operations
318             #[rustfmt::skip]
319             | "simd_neg"
320             | "simd_fabs" => {
321                 let &[ref op] = check_arg_count(args)?;
322                 let (op, op_len) = this.operand_to_simd(op)?;
323                 let (dest, dest_len) = this.place_to_simd(dest)?;
324
325                 assert_eq!(dest_len, op_len);
326
327                 enum Op {
328                     MirOp(mir::UnOp),
329                     Abs,
330                 }
331                 let which = match intrinsic_name {
332                     "simd_neg" => Op::MirOp(mir::UnOp::Neg),
333                     "simd_fabs" => Op::Abs,
334                     _ => unreachable!(),
335                 };
336
337                 for i in 0..dest_len {
338                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
339                     let dest = this.mplace_index(&dest, i)?;
340                     let val = match which {
341                         Op::MirOp(mir_op) => this.unary_op(mir_op, &op)?.to_scalar()?,
342                         Op::Abs => {
343                             // Works for f32 and f64.
344                             let ty::Float(float_ty) = op.layout.ty.kind() else {
345                                 bug!("simd_fabs operand is not a float")
346                             };
347                             let op = op.to_scalar()?;
348                             // FIXME: Using host floats.
349                             match float_ty {
350                                 FloatTy::F32 => Scalar::from_f32(op.to_f32()?.abs()),
351                                 FloatTy::F64 => Scalar::from_f64(op.to_f64()?.abs()),
352                             }
353                         }
354                     };
355                     this.write_scalar(val, &dest.into())?;
356                 }
357             }
358             #[rustfmt::skip]
359             | "simd_add"
360             | "simd_sub"
361             | "simd_mul"
362             | "simd_div"
363             | "simd_rem"
364             | "simd_shl"
365             | "simd_shr"
366             | "simd_and"
367             | "simd_or"
368             | "simd_xor"
369             | "simd_eq"
370             | "simd_ne"
371             | "simd_lt"
372             | "simd_le"
373             | "simd_gt"
374             | "simd_ge" => {
375                 use mir::BinOp;
376
377                 let &[ref left, ref right] = check_arg_count(args)?;
378                 let (left, left_len) = this.operand_to_simd(left)?;
379                 let (right, right_len) = this.operand_to_simd(right)?;
380                 let (dest, dest_len) = this.place_to_simd(dest)?;
381
382                 assert_eq!(dest_len, left_len);
383                 assert_eq!(dest_len, right_len);
384
385                 let mir_op = match intrinsic_name {
386                     "simd_add" => BinOp::Add,
387                     "simd_sub" => BinOp::Sub,
388                     "simd_mul" => BinOp::Mul,
389                     "simd_div" => BinOp::Div,
390                     "simd_rem" => BinOp::Rem,
391                     "simd_shl" => BinOp::Shl,
392                     "simd_shr" => BinOp::Shr,
393                     "simd_and" => BinOp::BitAnd,
394                     "simd_or" => BinOp::BitOr,
395                     "simd_xor" => BinOp::BitXor,
396                     "simd_eq" => BinOp::Eq,
397                     "simd_ne" => BinOp::Ne,
398                     "simd_lt" => BinOp::Lt,
399                     "simd_le" => BinOp::Le,
400                     "simd_gt" => BinOp::Gt,
401                     "simd_ge" => BinOp::Ge,
402                     _ => unreachable!(),
403                 };
404
405                 for i in 0..dest_len {
406                     let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
407                     let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
408                     let dest = this.mplace_index(&dest, i)?;
409                     let (val, overflowed, ty) = this.overflowing_binary_op(mir_op, &left, &right)?;
410                     if matches!(mir_op, BinOp::Shl | BinOp::Shr) {
411                         // Shifts have extra UB as SIMD operations that the MIR binop does not have.
412                         // See <https://github.com/rust-lang/rust/issues/91237>.
413                         if overflowed {
414                             let r_val = right.to_scalar()?.to_bits(right.layout.size)?;
415                             throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
416                         }
417                     }
418                     if matches!(mir_op, BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge) {
419                         // Special handling for boolean-returning operations
420                         assert_eq!(ty, this.tcx.types.bool);
421                         let val = val.to_bool().unwrap();
422                         let val = bool_to_simd_element(val, dest.layout.size);
423                         this.write_scalar(val, &dest.into())?;
424                     } else {
425                         assert_ne!(ty, this.tcx.types.bool);
426                         assert_eq!(ty, dest.layout.ty);
427                         this.write_scalar(val, &dest.into())?;
428                     }
429                 }
430             }
431             #[rustfmt::skip]
432             | "simd_reduce_and"
433             | "simd_reduce_or"
434             | "simd_reduce_xor"
435             | "simd_reduce_any"
436             | "simd_reduce_all"
437             | "simd_reduce_max"
438             | "simd_reduce_min" => {
439                 use mir::BinOp;
440
441                 let &[ref op] = check_arg_count(args)?;
442                 let (op, op_len) = this.operand_to_simd(op)?;
443
444                 let imm_from_bool =
445                     |b| ImmTy::from_scalar(Scalar::from_bool(b), this.machine.layouts.bool);
446
447                 enum Op {
448                     MirOp(BinOp),
449                     MirOpBool(BinOp),
450                     Max,
451                     Min,
452                 }
453                 let which = match intrinsic_name {
454                     "simd_reduce_and" => Op::MirOp(BinOp::BitAnd),
455                     "simd_reduce_or" => Op::MirOp(BinOp::BitOr),
456                     "simd_reduce_xor" => Op::MirOp(BinOp::BitXor),
457                     "simd_reduce_any" => Op::MirOpBool(BinOp::BitOr),
458                     "simd_reduce_all" => Op::MirOpBool(BinOp::BitAnd),
459                     "simd_reduce_max" => Op::Max,
460                     "simd_reduce_min" => Op::Min,
461                     _ => unreachable!(),
462                 };
463
464                 // Initialize with first lane, then proceed with the rest.
465                 let mut res = this.read_immediate(&this.mplace_index(&op, 0)?.into())?;
466                 if matches!(which, Op::MirOpBool(_)) {
467                     // Convert to `bool` scalar.
468                     res = imm_from_bool(simd_element_to_bool(res)?);
469                 }
470                 for i in 1..op_len {
471                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
472                     res = match which {
473                         Op::MirOp(mir_op) => {
474                             this.binary_op(mir_op, &res, &op)?
475                         }
476                         Op::MirOpBool(mir_op) => {
477                             let op = imm_from_bool(simd_element_to_bool(op)?);
478                             this.binary_op(mir_op, &res, &op)?
479                         }
480                         Op::Max => {
481                             // if `op > res`...
482                             if this.binary_op(BinOp::Gt, &op, &res)?.to_scalar()?.to_bool()? {
483                                 // update accumulator
484                                 op
485                             } else {
486                                 // no change
487                                 res
488                             }
489                         }
490                         Op::Min => {
491                             // if `op < res`...
492                             if this.binary_op(BinOp::Lt, &op, &res)?.to_scalar()?.to_bool()? {
493                                 // update accumulator
494                                 op
495                             } else {
496                                 // no change
497                                 res
498                             }
499                         }
500                     };
501                 }
502                 this.write_immediate(*res, dest)?;
503             }
504             #[rustfmt::skip]
505             | "simd_reduce_add_ordered"
506             | "simd_reduce_mul_ordered" => {
507                 use mir::BinOp;
508
509                 let &[ref op, ref init] = check_arg_count(args)?;
510                 let (op, op_len) = this.operand_to_simd(op)?;
511                 let init = this.read_immediate(init)?;
512
513                 let mir_op = match intrinsic_name {
514                     "simd_reduce_add_ordered" => BinOp::Add,
515                     "simd_reduce_mul_ordered" => BinOp::Mul,
516                     _ => unreachable!(),
517                 };
518
519                 let mut res = init;
520                 for i in 0..op_len {
521                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
522                     res = this.binary_op(mir_op, &res, &op)?;
523                 }
524                 this.write_immediate(*res, dest)?;
525             }
526             "simd_select" => {
527                 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
528                 let (mask, mask_len) = this.operand_to_simd(mask)?;
529                 let (yes, yes_len) = this.operand_to_simd(yes)?;
530                 let (no, no_len) = this.operand_to_simd(no)?;
531                 let (dest, dest_len) = this.place_to_simd(dest)?;
532
533                 assert_eq!(dest_len, mask_len);
534                 assert_eq!(dest_len, yes_len);
535                 assert_eq!(dest_len, no_len);
536
537                 for i in 0..dest_len {
538                     let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
539                     let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
540                     let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
541                     let dest = this.mplace_index(&dest, i)?;
542
543                     let mask = simd_element_to_bool(mask)?;
544                     let val = if mask { yes } else { no };
545                     this.write_immediate(*val, &dest.into())?;
546                 }
547             }
548             #[rustfmt::skip]
549             "simd_cast" | "simd_as" => {
550                 let &[ref op] = check_arg_count(args)?;
551                 let (op, op_len) = this.operand_to_simd(op)?;
552                 let (dest, dest_len) = this.place_to_simd(dest)?;
553
554                 assert_eq!(dest_len, op_len);
555
556                 let safe_cast = intrinsic_name == "simd_as";
557
558                 for i in 0..dest_len {
559                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
560                     let dest = this.mplace_index(&dest, i)?;
561
562                     let val = match (op.layout.ty.kind(), dest.layout.ty.kind()) {
563                         // Int-to-(int|float): always safe
564                         (ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_)) =>
565                             this.misc_cast(&op, dest.layout.ty)?,
566                         // Float-to-float: always safe
567                         (ty::Float(_), ty::Float(_)) =>
568                             this.misc_cast(&op, dest.layout.ty)?,
569                         // Float-to-int in safe mode
570                         (ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
571                             this.misc_cast(&op, dest.layout.ty)?,
572                         // Float-to-int in unchecked mode
573                         (ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
574                             this.float_to_int_unchecked(op.to_scalar()?.to_f32()?, dest.layout.ty)?.into(),
575                         (ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
576                             this.float_to_int_unchecked(op.to_scalar()?.to_f64()?, dest.layout.ty)?.into(),
577                         _ =>
578                             throw_unsup_format!(
579                                 "Unsupported SIMD cast from element type {} to {}",
580                                 op.layout.ty,
581                                 dest.layout.ty
582                             ),
583                     };
584                     this.write_immediate(val, &dest.into())?;
585                 }
586             }
587
588             // Atomic operations
589             "atomic_load" => this.atomic_load(args, dest, AtomicReadOp::SeqCst)?,
590             "atomic_load_relaxed" => this.atomic_load(args, dest, AtomicReadOp::Relaxed)?,
591             "atomic_load_acq" => this.atomic_load(args, dest, AtomicReadOp::Acquire)?,
592
593             "atomic_store" => this.atomic_store(args, AtomicWriteOp::SeqCst)?,
594             "atomic_store_relaxed" => this.atomic_store(args, AtomicWriteOp::Relaxed)?,
595             "atomic_store_rel" => this.atomic_store(args, AtomicWriteOp::Release)?,
596
597             "atomic_fence_acq" => this.atomic_fence(args, AtomicFenceOp::Acquire)?,
598             "atomic_fence_rel" => this.atomic_fence(args, AtomicFenceOp::Release)?,
599             "atomic_fence_acqrel" => this.atomic_fence(args, AtomicFenceOp::AcqRel)?,
600             "atomic_fence" => this.atomic_fence(args, AtomicFenceOp::SeqCst)?,
601
602             "atomic_singlethreadfence_acq" => this.compiler_fence(args, AtomicFenceOp::Acquire)?,
603             "atomic_singlethreadfence_rel" => this.compiler_fence(args, AtomicFenceOp::Release)?,
604             "atomic_singlethreadfence_acqrel" =>
605                 this.compiler_fence(args, AtomicFenceOp::AcqRel)?,
606             "atomic_singlethreadfence" => this.compiler_fence(args, AtomicFenceOp::SeqCst)?,
607
608             "atomic_xchg" => this.atomic_exchange(args, dest, AtomicRwOp::SeqCst)?,
609             "atomic_xchg_acq" => this.atomic_exchange(args, dest, AtomicRwOp::Acquire)?,
610             "atomic_xchg_rel" => this.atomic_exchange(args, dest, AtomicRwOp::Release)?,
611             "atomic_xchg_acqrel" => this.atomic_exchange(args, dest, AtomicRwOp::AcqRel)?,
612             "atomic_xchg_relaxed" => this.atomic_exchange(args, dest, AtomicRwOp::Relaxed)?,
613
614             #[rustfmt::skip]
615             "atomic_cxchg" =>
616                 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
617             #[rustfmt::skip]
618             "atomic_cxchg_acq" =>
619                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
620             #[rustfmt::skip]
621             "atomic_cxchg_rel" =>
622                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
623             #[rustfmt::skip]
624             "atomic_cxchg_acqrel" =>
625                 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
626             #[rustfmt::skip]
627             "atomic_cxchg_relaxed" =>
628                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
629             #[rustfmt::skip]
630             "atomic_cxchg_acq_failrelaxed" =>
631                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
632             #[rustfmt::skip]
633             "atomic_cxchg_acqrel_failrelaxed" =>
634                 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
635             #[rustfmt::skip]
636             "atomic_cxchg_failrelaxed" =>
637                 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
638             #[rustfmt::skip]
639             "atomic_cxchg_failacq" =>
640                 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
641
642             #[rustfmt::skip]
643             "atomic_cxchgweak" =>
644                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
645             #[rustfmt::skip]
646             "atomic_cxchgweak_acq" =>
647                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
648             #[rustfmt::skip]
649             "atomic_cxchgweak_rel" =>
650                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
651             #[rustfmt::skip]
652             "atomic_cxchgweak_acqrel" =>
653                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
654             #[rustfmt::skip]
655             "atomic_cxchgweak_relaxed" =>
656                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
657             #[rustfmt::skip]
658             "atomic_cxchgweak_acq_failrelaxed" =>
659                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
660             #[rustfmt::skip]
661             "atomic_cxchgweak_acqrel_failrelaxed" =>
662                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
663             #[rustfmt::skip]
664             "atomic_cxchgweak_failrelaxed" =>
665                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
666             #[rustfmt::skip]
667             "atomic_cxchgweak_failacq" =>
668                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
669
670             #[rustfmt::skip]
671             "atomic_or" =>
672                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::SeqCst)?,
673             #[rustfmt::skip]
674             "atomic_or_acq" =>
675                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Acquire)?,
676             #[rustfmt::skip]
677             "atomic_or_rel" =>
678                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Release)?,
679             #[rustfmt::skip]
680             "atomic_or_acqrel" =>
681                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::AcqRel)?,
682             #[rustfmt::skip]
683             "atomic_or_relaxed" =>
684                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Relaxed)?,
685             #[rustfmt::skip]
686             "atomic_xor" =>
687                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::SeqCst)?,
688             #[rustfmt::skip]
689             "atomic_xor_acq" =>
690                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Acquire)?,
691             #[rustfmt::skip]
692             "atomic_xor_rel" =>
693                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Release)?,
694             #[rustfmt::skip]
695             "atomic_xor_acqrel" =>
696                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::AcqRel)?,
697             #[rustfmt::skip]
698             "atomic_xor_relaxed" =>
699                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Relaxed)?,
700             #[rustfmt::skip]
701             "atomic_and" =>
702                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::SeqCst)?,
703             #[rustfmt::skip]
704             "atomic_and_acq" =>
705                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Acquire)?,
706             #[rustfmt::skip]
707             "atomic_and_rel" =>
708                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Release)?,
709             #[rustfmt::skip]
710             "atomic_and_acqrel" =>
711                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::AcqRel)?,
712             #[rustfmt::skip]
713             "atomic_and_relaxed" =>
714                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Relaxed)?,
715             #[rustfmt::skip]
716             "atomic_nand" =>
717                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::SeqCst)?,
718             #[rustfmt::skip]
719             "atomic_nand_acq" =>
720                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Acquire)?,
721             #[rustfmt::skip]
722             "atomic_nand_rel" =>
723                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Release)?,
724             #[rustfmt::skip]
725             "atomic_nand_acqrel" =>
726                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::AcqRel)?,
727             #[rustfmt::skip]
728             "atomic_nand_relaxed" =>
729                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Relaxed)?,
730             #[rustfmt::skip]
731             "atomic_xadd" =>
732                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::SeqCst)?,
733             #[rustfmt::skip]
734             "atomic_xadd_acq" =>
735                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Acquire)?,
736             #[rustfmt::skip]
737             "atomic_xadd_rel" =>
738                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Release)?,
739             #[rustfmt::skip]
740             "atomic_xadd_acqrel" =>
741                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::AcqRel)?,
742             #[rustfmt::skip]
743             "atomic_xadd_relaxed" =>
744                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Relaxed)?,
745             #[rustfmt::skip]
746             "atomic_xsub" =>
747                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::SeqCst)?,
748             #[rustfmt::skip]
749             "atomic_xsub_acq" =>
750                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Acquire)?,
751             #[rustfmt::skip]
752             "atomic_xsub_rel" =>
753                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Release)?,
754             #[rustfmt::skip]
755             "atomic_xsub_acqrel" =>
756                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::AcqRel)?,
757             #[rustfmt::skip]
758             "atomic_xsub_relaxed" =>
759                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Relaxed)?,
760             "atomic_min" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
761             "atomic_min_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
762             "atomic_min_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
763             "atomic_min_acqrel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
764             "atomic_min_relaxed" =>
765                 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
766             "atomic_max" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
767             "atomic_max_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
768             "atomic_max_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
769             "atomic_max_acqrel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
770             "atomic_max_relaxed" =>
771                 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
772             "atomic_umin" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
773             "atomic_umin_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
774             "atomic_umin_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
775             "atomic_umin_acqrel" =>
776                 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
777             "atomic_umin_relaxed" =>
778                 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
779             "atomic_umax" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
780             "atomic_umax_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
781             "atomic_umax_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
782             "atomic_umax_acqrel" =>
783                 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
784             "atomic_umax_relaxed" =>
785                 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
786
787             // Other
788             "exact_div" => {
789                 let &[ref num, ref denom] = check_arg_count(args)?;
790                 this.exact_div(&this.read_immediate(num)?, &this.read_immediate(denom)?, dest)?;
791             }
792
793             "try" => return this.handle_try(args, dest, ret),
794
795             "breakpoint" => {
796                 let &[] = check_arg_count(args)?;
797                 // normally this would raise a SIGTRAP, which aborts if no debugger is connected
798                 throw_machine_stop!(TerminationInfo::Abort("Trace/breakpoint trap".to_string()))
799             }
800
801             name => throw_unsup_format!("unimplemented intrinsic: {}", name),
802         }
803
804         trace!("{:?}", this.dump_place(**dest));
805         this.go_to_block(ret);
806         Ok(())
807     }
808
809     fn atomic_load(
810         &mut self,
811         args: &[OpTy<'tcx, Tag>],
812         dest: &PlaceTy<'tcx, Tag>,
813         atomic: AtomicReadOp,
814     ) -> InterpResult<'tcx> {
815         let this = self.eval_context_mut();
816
817         let &[ref place] = check_arg_count(args)?;
818         let place = this.deref_operand(place)?;
819
820         // make sure it fits into a scalar; otherwise it cannot be atomic
821         let val = this.read_scalar_atomic(&place, atomic)?;
822
823         // Check alignment requirements. Atomics must always be aligned to their size,
824         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
825         // be 8-aligned).
826         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
827         this.memory.check_ptr_access_align(
828             place.ptr,
829             place.layout.size,
830             align,
831             CheckInAllocMsg::MemoryAccessTest,
832         )?;
833         // Perform regular access.
834         this.write_scalar(val, dest)?;
835         Ok(())
836     }
837
838     fn atomic_store(
839         &mut self,
840         args: &[OpTy<'tcx, Tag>],
841         atomic: AtomicWriteOp,
842     ) -> InterpResult<'tcx> {
843         let this = self.eval_context_mut();
844
845         let &[ref place, ref val] = check_arg_count(args)?;
846         let place = this.deref_operand(place)?;
847         let val = this.read_scalar(val)?; // make sure it fits into a scalar; otherwise it cannot be atomic
848
849         // Check alignment requirements. Atomics must always be aligned to their size,
850         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
851         // be 8-aligned).
852         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
853         this.memory.check_ptr_access_align(
854             place.ptr,
855             place.layout.size,
856             align,
857             CheckInAllocMsg::MemoryAccessTest,
858         )?;
859
860         // Perform atomic store
861         this.write_scalar_atomic(val, &place, atomic)?;
862         Ok(())
863     }
864
865     fn compiler_fence(
866         &mut self,
867         args: &[OpTy<'tcx, Tag>],
868         atomic: AtomicFenceOp,
869     ) -> InterpResult<'tcx> {
870         let &[] = check_arg_count(args)?;
871         let _ = atomic;
872         //FIXME: compiler fences are currently ignored
873         Ok(())
874     }
875
876     fn atomic_fence(
877         &mut self,
878         args: &[OpTy<'tcx, Tag>],
879         atomic: AtomicFenceOp,
880     ) -> InterpResult<'tcx> {
881         let this = self.eval_context_mut();
882         let &[] = check_arg_count(args)?;
883         this.validate_atomic_fence(atomic)?;
884         Ok(())
885     }
886
887     fn atomic_op(
888         &mut self,
889         args: &[OpTy<'tcx, Tag>],
890         dest: &PlaceTy<'tcx, Tag>,
891         atomic_op: AtomicOp,
892         atomic: AtomicRwOp,
893     ) -> InterpResult<'tcx> {
894         let this = self.eval_context_mut();
895
896         let &[ref place, ref rhs] = check_arg_count(args)?;
897         let place = this.deref_operand(place)?;
898
899         if !place.layout.ty.is_integral() {
900             bug!("Atomic arithmetic operations only work on integer types");
901         }
902         let rhs = this.read_immediate(rhs)?;
903
904         // Check alignment requirements. Atomics must always be aligned to their size,
905         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
906         // be 8-aligned).
907         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
908         this.memory.check_ptr_access_align(
909             place.ptr,
910             place.layout.size,
911             align,
912             CheckInAllocMsg::MemoryAccessTest,
913         )?;
914
915         match atomic_op {
916             AtomicOp::Min => {
917                 let old = this.atomic_min_max_scalar(&place, rhs, true, atomic)?;
918                 this.write_immediate(*old, &dest)?; // old value is returned
919                 Ok(())
920             }
921             AtomicOp::Max => {
922                 let old = this.atomic_min_max_scalar(&place, rhs, false, atomic)?;
923                 this.write_immediate(*old, &dest)?; // old value is returned
924                 Ok(())
925             }
926             AtomicOp::MirOp(op, neg) => {
927                 let old = this.atomic_op_immediate(&place, &rhs, op, neg, atomic)?;
928                 this.write_immediate(*old, dest)?; // old value is returned
929                 Ok(())
930             }
931         }
932     }
933
934     fn atomic_exchange(
935         &mut self,
936         args: &[OpTy<'tcx, Tag>],
937         dest: &PlaceTy<'tcx, Tag>,
938         atomic: AtomicRwOp,
939     ) -> InterpResult<'tcx> {
940         let this = self.eval_context_mut();
941
942         let &[ref place, ref new] = check_arg_count(args)?;
943         let place = this.deref_operand(place)?;
944         let new = this.read_scalar(new)?;
945
946         // Check alignment requirements. Atomics must always be aligned to their size,
947         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
948         // be 8-aligned).
949         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
950         this.memory.check_ptr_access_align(
951             place.ptr,
952             place.layout.size,
953             align,
954             CheckInAllocMsg::MemoryAccessTest,
955         )?;
956
957         let old = this.atomic_exchange_scalar(&place, new, atomic)?;
958         this.write_scalar(old, dest)?; // old value is returned
959         Ok(())
960     }
961
962     fn atomic_compare_exchange_impl(
963         &mut self,
964         args: &[OpTy<'tcx, Tag>],
965         dest: &PlaceTy<'tcx, Tag>,
966         success: AtomicRwOp,
967         fail: AtomicReadOp,
968         can_fail_spuriously: bool,
969     ) -> InterpResult<'tcx> {
970         let this = self.eval_context_mut();
971
972         let &[ref place, ref expect_old, ref new] = check_arg_count(args)?;
973         let place = this.deref_operand(place)?;
974         let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
975         let new = this.read_scalar(new)?;
976
977         // Check alignment requirements. Atomics must always be aligned to their size,
978         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
979         // be 8-aligned).
980         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
981         this.memory.check_ptr_access_align(
982             place.ptr,
983             place.layout.size,
984             align,
985             CheckInAllocMsg::MemoryAccessTest,
986         )?;
987
988         let old = this.atomic_compare_exchange_scalar(
989             &place,
990             &expect_old,
991             new,
992             success,
993             fail,
994             can_fail_spuriously,
995         )?;
996
997         // Return old value.
998         this.write_immediate(old, dest)?;
999         Ok(())
1000     }
1001
1002     fn atomic_compare_exchange(
1003         &mut self,
1004         args: &[OpTy<'tcx, Tag>],
1005         dest: &PlaceTy<'tcx, Tag>,
1006         success: AtomicRwOp,
1007         fail: AtomicReadOp,
1008     ) -> InterpResult<'tcx> {
1009         self.atomic_compare_exchange_impl(args, dest, success, fail, false)
1010     }
1011
1012     fn atomic_compare_exchange_weak(
1013         &mut self,
1014         args: &[OpTy<'tcx, Tag>],
1015         dest: &PlaceTy<'tcx, Tag>,
1016         success: AtomicRwOp,
1017         fail: AtomicReadOp,
1018     ) -> InterpResult<'tcx> {
1019         self.atomic_compare_exchange_impl(args, dest, success, fail, true)
1020     }
1021
1022     fn float_to_int_unchecked<F>(
1023         &self,
1024         f: F,
1025         dest_ty: ty::Ty<'tcx>,
1026     ) -> InterpResult<'tcx, Scalar<Tag>>
1027     where
1028         F: Float + Into<Scalar<Tag>>,
1029     {
1030         let this = self.eval_context_ref();
1031
1032         // Step 1: cut off the fractional part of `f`. The result of this is
1033         // guaranteed to be precisely representable in IEEE floats.
1034         let f = f.round_to_integral(Round::TowardZero).value;
1035
1036         // Step 2: Cast the truncated float to the target integer type and see if we lose any information in this step.
1037         Ok(match dest_ty.kind() {
1038             // Unsigned
1039             ty::Uint(t) => {
1040                 let size = Integer::from_uint_ty(this, *t).size();
1041                 let res = f.to_u128(size.bits_usize());
1042                 if res.status.is_empty() {
1043                     // No status flags means there was no further rounding or other loss of precision.
1044                     Scalar::from_uint(res.value, size)
1045                 } else {
1046                     // `f` was not representable in this integer type.
1047                     throw_ub_format!(
1048                         "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1049                         f,
1050                         dest_ty,
1051                     );
1052                 }
1053             }
1054             // Signed
1055             ty::Int(t) => {
1056                 let size = Integer::from_int_ty(this, *t).size();
1057                 let res = f.to_i128(size.bits_usize());
1058                 if res.status.is_empty() {
1059                     // No status flags means there was no further rounding or other loss of precision.
1060                     Scalar::from_int(res.value, size)
1061                 } else {
1062                     // `f` was not representable in this integer type.
1063                     throw_ub_format!(
1064                         "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1065                         f,
1066                         dest_ty,
1067                     );
1068                 }
1069             }
1070             // Nothing else
1071             _ => bug!("`float_to_int_unchecked` called with non-int output type {:?}", dest_ty),
1072         })
1073     }
1074 }