]> git.lizzy.rs Git - rust.git/blob - src/shims/intrinsics.rs
implement simd_saturating intrinsics
[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                             match float_ty {
349                                 FloatTy::F32 => Scalar::from_f32(op.to_f32()?.abs()),
350                                 FloatTy::F64 => Scalar::from_f64(op.to_f64()?.abs()),
351                             }
352                         }
353                     };
354                     this.write_scalar(val, &dest.into())?;
355                 }
356             }
357             #[rustfmt::skip]
358             | "simd_add"
359             | "simd_sub"
360             | "simd_mul"
361             | "simd_div"
362             | "simd_rem"
363             | "simd_shl"
364             | "simd_shr"
365             | "simd_and"
366             | "simd_or"
367             | "simd_xor"
368             | "simd_eq"
369             | "simd_ne"
370             | "simd_lt"
371             | "simd_le"
372             | "simd_gt"
373             | "simd_ge"
374             | "simd_fmax"
375             | "simd_fmin"
376             | "simd_saturating_add"
377             | "simd_saturating_sub" => {
378                 use mir::BinOp;
379
380                 let &[ref left, ref right] = check_arg_count(args)?;
381                 let (left, left_len) = this.operand_to_simd(left)?;
382                 let (right, right_len) = this.operand_to_simd(right)?;
383                 let (dest, dest_len) = this.place_to_simd(dest)?;
384
385                 assert_eq!(dest_len, left_len);
386                 assert_eq!(dest_len, right_len);
387
388                 enum Op {
389                     MirOp(BinOp),
390                     SaturatingOp(BinOp),
391                     FMax,
392                     FMin,
393                 }
394                 let which = match intrinsic_name {
395                     "simd_add" => Op::MirOp(BinOp::Add),
396                     "simd_sub" => Op::MirOp(BinOp::Sub),
397                     "simd_mul" => Op::MirOp(BinOp::Mul),
398                     "simd_div" => Op::MirOp(BinOp::Div),
399                     "simd_rem" => Op::MirOp(BinOp::Rem),
400                     "simd_shl" => Op::MirOp(BinOp::Shl),
401                     "simd_shr" => Op::MirOp(BinOp::Shr),
402                     "simd_and" => Op::MirOp(BinOp::BitAnd),
403                     "simd_or" => Op::MirOp(BinOp::BitOr),
404                     "simd_xor" => Op::MirOp(BinOp::BitXor),
405                     "simd_eq" => Op::MirOp(BinOp::Eq),
406                     "simd_ne" => Op::MirOp(BinOp::Ne),
407                     "simd_lt" => Op::MirOp(BinOp::Lt),
408                     "simd_le" => Op::MirOp(BinOp::Le),
409                     "simd_gt" => Op::MirOp(BinOp::Gt),
410                     "simd_ge" => Op::MirOp(BinOp::Ge),
411                     "simd_fmax" => Op::FMax,
412                     "simd_fmin" => Op::FMin,
413                     "simd_saturating_add" => Op::SaturatingOp(BinOp::Add),
414                     "simd_saturating_sub" => Op::SaturatingOp(BinOp::Sub),
415                     _ => unreachable!(),
416                 };
417
418                 for i in 0..dest_len {
419                     let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
420                     let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
421                     let dest = this.mplace_index(&dest, i)?;
422                     let val = match which {
423                         Op::MirOp(mir_op) => {
424                             let (val, overflowed, ty) = this.overflowing_binary_op(mir_op, &left, &right)?;
425                             if matches!(mir_op, BinOp::Shl | BinOp::Shr) {
426                                 // Shifts have extra UB as SIMD operations that the MIR binop does not have.
427                                 // See <https://github.com/rust-lang/rust/issues/91237>.
428                                 if overflowed {
429                                     let r_val = right.to_scalar()?.to_bits(right.layout.size)?;
430                                     throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
431                                 }
432                             }
433                             if matches!(mir_op, BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge) {
434                                 // Special handling for boolean-returning operations
435                                 assert_eq!(ty, this.tcx.types.bool);
436                                 let val = val.to_bool().unwrap();
437                                 bool_to_simd_element(val, dest.layout.size)
438                             } else {
439                                 assert_ne!(ty, this.tcx.types.bool);
440                                 assert_eq!(ty, dest.layout.ty);
441                                 val
442                             }
443                         }
444                         Op::FMax => {
445                             fmax_op(&left, &right)?
446                         }
447                         Op::FMin => {
448                             fmin_op(&left, &right)?
449                         }
450                         Op::SaturatingOp(mir_op) => {
451                             this.saturating_arith(mir_op, &left, &right)?
452                         }
453                     };
454                     this.write_scalar(val, &dest.into())?;
455                 }
456             }
457             #[rustfmt::skip]
458             | "simd_reduce_and"
459             | "simd_reduce_or"
460             | "simd_reduce_xor"
461             | "simd_reduce_any"
462             | "simd_reduce_all"
463             | "simd_reduce_max"
464             | "simd_reduce_min" => {
465                 use mir::BinOp;
466
467                 let &[ref op] = check_arg_count(args)?;
468                 let (op, op_len) = this.operand_to_simd(op)?;
469
470                 let imm_from_bool =
471                     |b| ImmTy::from_scalar(Scalar::from_bool(b), this.machine.layouts.bool);
472
473                 enum Op {
474                     MirOp(BinOp),
475                     MirOpBool(BinOp),
476                     Max,
477                     Min,
478                 }
479                 let which = match intrinsic_name {
480                     "simd_reduce_and" => Op::MirOp(BinOp::BitAnd),
481                     "simd_reduce_or" => Op::MirOp(BinOp::BitOr),
482                     "simd_reduce_xor" => Op::MirOp(BinOp::BitXor),
483                     "simd_reduce_any" => Op::MirOpBool(BinOp::BitOr),
484                     "simd_reduce_all" => Op::MirOpBool(BinOp::BitAnd),
485                     "simd_reduce_max" => Op::Max,
486                     "simd_reduce_min" => Op::Min,
487                     _ => unreachable!(),
488                 };
489
490                 // Initialize with first lane, then proceed with the rest.
491                 let mut res = this.read_immediate(&this.mplace_index(&op, 0)?.into())?;
492                 if matches!(which, Op::MirOpBool(_)) {
493                     // Convert to `bool` scalar.
494                     res = imm_from_bool(simd_element_to_bool(res)?);
495                 }
496                 for i in 1..op_len {
497                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
498                     res = match which {
499                         Op::MirOp(mir_op) => {
500                             this.binary_op(mir_op, &res, &op)?
501                         }
502                         Op::MirOpBool(mir_op) => {
503                             let op = imm_from_bool(simd_element_to_bool(op)?);
504                             this.binary_op(mir_op, &res, &op)?
505                         }
506                         Op::Max => {
507                             if matches!(res.layout.ty.kind(), ty::Float(_)) {
508                                 ImmTy::from_scalar(fmax_op(&res, &op)?, res.layout)
509                             } else {
510                                 // Just boring integers, so NaNs to worry about
511                                 if this.binary_op(BinOp::Ge, &res, &op)?.to_scalar()?.to_bool()? {
512                                     res
513                                 } else {
514                                     op
515                                 }
516                             }
517                         }
518                         Op::Min => {
519                             if matches!(res.layout.ty.kind(), ty::Float(_)) {
520                                 ImmTy::from_scalar(fmin_op(&res, &op)?, res.layout)
521                             } else {
522                                 // Just boring integers, so NaNs to worry about
523                                 if this.binary_op(BinOp::Le, &res, &op)?.to_scalar()?.to_bool()? {
524                                     res
525                                 } else {
526                                     op
527                                 }
528                             }
529                         }
530                     };
531                 }
532                 this.write_immediate(*res, dest)?;
533             }
534             #[rustfmt::skip]
535             | "simd_reduce_add_ordered"
536             | "simd_reduce_mul_ordered" => {
537                 use mir::BinOp;
538
539                 let &[ref op, ref init] = check_arg_count(args)?;
540                 let (op, op_len) = this.operand_to_simd(op)?;
541                 let init = this.read_immediate(init)?;
542
543                 let mir_op = match intrinsic_name {
544                     "simd_reduce_add_ordered" => BinOp::Add,
545                     "simd_reduce_mul_ordered" => BinOp::Mul,
546                     _ => unreachable!(),
547                 };
548
549                 let mut res = init;
550                 for i in 0..op_len {
551                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
552                     res = this.binary_op(mir_op, &res, &op)?;
553                 }
554                 this.write_immediate(*res, dest)?;
555             }
556             "simd_select" => {
557                 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
558                 let (mask, mask_len) = this.operand_to_simd(mask)?;
559                 let (yes, yes_len) = this.operand_to_simd(yes)?;
560                 let (no, no_len) = this.operand_to_simd(no)?;
561                 let (dest, dest_len) = this.place_to_simd(dest)?;
562
563                 assert_eq!(dest_len, mask_len);
564                 assert_eq!(dest_len, yes_len);
565                 assert_eq!(dest_len, no_len);
566
567                 for i in 0..dest_len {
568                     let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
569                     let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
570                     let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
571                     let dest = this.mplace_index(&dest, i)?;
572
573                     let mask = simd_element_to_bool(mask)?;
574                     let val = if mask { yes } else { no };
575                     this.write_immediate(*val, &dest.into())?;
576                 }
577             }
578             #[rustfmt::skip]
579             "simd_cast" | "simd_as" => {
580                 let &[ref op] = check_arg_count(args)?;
581                 let (op, op_len) = this.operand_to_simd(op)?;
582                 let (dest, dest_len) = this.place_to_simd(dest)?;
583
584                 assert_eq!(dest_len, op_len);
585
586                 let safe_cast = intrinsic_name == "simd_as";
587
588                 for i in 0..dest_len {
589                     let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
590                     let dest = this.mplace_index(&dest, i)?;
591
592                     let val = match (op.layout.ty.kind(), dest.layout.ty.kind()) {
593                         // Int-to-(int|float): always safe
594                         (ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_)) =>
595                             this.misc_cast(&op, dest.layout.ty)?,
596                         // Float-to-float: always safe
597                         (ty::Float(_), ty::Float(_)) =>
598                             this.misc_cast(&op, dest.layout.ty)?,
599                         // Float-to-int in safe mode
600                         (ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
601                             this.misc_cast(&op, dest.layout.ty)?,
602                         // Float-to-int in unchecked mode
603                         (ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
604                             this.float_to_int_unchecked(op.to_scalar()?.to_f32()?, dest.layout.ty)?.into(),
605                         (ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
606                             this.float_to_int_unchecked(op.to_scalar()?.to_f64()?, dest.layout.ty)?.into(),
607                         _ =>
608                             throw_unsup_format!(
609                                 "Unsupported SIMD cast from element type {} to {}",
610                                 op.layout.ty,
611                                 dest.layout.ty
612                             ),
613                     };
614                     this.write_immediate(val, &dest.into())?;
615                 }
616             }
617
618             // Atomic operations
619             "atomic_load" => this.atomic_load(args, dest, AtomicReadOp::SeqCst)?,
620             "atomic_load_relaxed" => this.atomic_load(args, dest, AtomicReadOp::Relaxed)?,
621             "atomic_load_acq" => this.atomic_load(args, dest, AtomicReadOp::Acquire)?,
622
623             "atomic_store" => this.atomic_store(args, AtomicWriteOp::SeqCst)?,
624             "atomic_store_relaxed" => this.atomic_store(args, AtomicWriteOp::Relaxed)?,
625             "atomic_store_rel" => this.atomic_store(args, AtomicWriteOp::Release)?,
626
627             "atomic_fence_acq" => this.atomic_fence(args, AtomicFenceOp::Acquire)?,
628             "atomic_fence_rel" => this.atomic_fence(args, AtomicFenceOp::Release)?,
629             "atomic_fence_acqrel" => this.atomic_fence(args, AtomicFenceOp::AcqRel)?,
630             "atomic_fence" => this.atomic_fence(args, AtomicFenceOp::SeqCst)?,
631
632             "atomic_singlethreadfence_acq" => this.compiler_fence(args, AtomicFenceOp::Acquire)?,
633             "atomic_singlethreadfence_rel" => this.compiler_fence(args, AtomicFenceOp::Release)?,
634             "atomic_singlethreadfence_acqrel" =>
635                 this.compiler_fence(args, AtomicFenceOp::AcqRel)?,
636             "atomic_singlethreadfence" => this.compiler_fence(args, AtomicFenceOp::SeqCst)?,
637
638             "atomic_xchg" => this.atomic_exchange(args, dest, AtomicRwOp::SeqCst)?,
639             "atomic_xchg_acq" => this.atomic_exchange(args, dest, AtomicRwOp::Acquire)?,
640             "atomic_xchg_rel" => this.atomic_exchange(args, dest, AtomicRwOp::Release)?,
641             "atomic_xchg_acqrel" => this.atomic_exchange(args, dest, AtomicRwOp::AcqRel)?,
642             "atomic_xchg_relaxed" => this.atomic_exchange(args, dest, AtomicRwOp::Relaxed)?,
643
644             #[rustfmt::skip]
645             "atomic_cxchg" =>
646                 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
647             #[rustfmt::skip]
648             "atomic_cxchg_acq" =>
649                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
650             #[rustfmt::skip]
651             "atomic_cxchg_rel" =>
652                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
653             #[rustfmt::skip]
654             "atomic_cxchg_acqrel" =>
655                 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
656             #[rustfmt::skip]
657             "atomic_cxchg_relaxed" =>
658                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
659             #[rustfmt::skip]
660             "atomic_cxchg_acq_failrelaxed" =>
661                 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
662             #[rustfmt::skip]
663             "atomic_cxchg_acqrel_failrelaxed" =>
664                 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
665             #[rustfmt::skip]
666             "atomic_cxchg_failrelaxed" =>
667                 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
668             #[rustfmt::skip]
669             "atomic_cxchg_failacq" =>
670                 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
671
672             #[rustfmt::skip]
673             "atomic_cxchgweak" =>
674                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
675             #[rustfmt::skip]
676             "atomic_cxchgweak_acq" =>
677                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
678             #[rustfmt::skip]
679             "atomic_cxchgweak_rel" =>
680                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
681             #[rustfmt::skip]
682             "atomic_cxchgweak_acqrel" =>
683                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
684             #[rustfmt::skip]
685             "atomic_cxchgweak_relaxed" =>
686                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
687             #[rustfmt::skip]
688             "atomic_cxchgweak_acq_failrelaxed" =>
689                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
690             #[rustfmt::skip]
691             "atomic_cxchgweak_acqrel_failrelaxed" =>
692                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
693             #[rustfmt::skip]
694             "atomic_cxchgweak_failrelaxed" =>
695                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
696             #[rustfmt::skip]
697             "atomic_cxchgweak_failacq" =>
698                 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
699
700             #[rustfmt::skip]
701             "atomic_or" =>
702                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::SeqCst)?,
703             #[rustfmt::skip]
704             "atomic_or_acq" =>
705                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Acquire)?,
706             #[rustfmt::skip]
707             "atomic_or_rel" =>
708                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Release)?,
709             #[rustfmt::skip]
710             "atomic_or_acqrel" =>
711                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::AcqRel)?,
712             #[rustfmt::skip]
713             "atomic_or_relaxed" =>
714                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Relaxed)?,
715             #[rustfmt::skip]
716             "atomic_xor" =>
717                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::SeqCst)?,
718             #[rustfmt::skip]
719             "atomic_xor_acq" =>
720                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Acquire)?,
721             #[rustfmt::skip]
722             "atomic_xor_rel" =>
723                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Release)?,
724             #[rustfmt::skip]
725             "atomic_xor_acqrel" =>
726                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::AcqRel)?,
727             #[rustfmt::skip]
728             "atomic_xor_relaxed" =>
729                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Relaxed)?,
730             #[rustfmt::skip]
731             "atomic_and" =>
732                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::SeqCst)?,
733             #[rustfmt::skip]
734             "atomic_and_acq" =>
735                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Acquire)?,
736             #[rustfmt::skip]
737             "atomic_and_rel" =>
738                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Release)?,
739             #[rustfmt::skip]
740             "atomic_and_acqrel" =>
741                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::AcqRel)?,
742             #[rustfmt::skip]
743             "atomic_and_relaxed" =>
744                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Relaxed)?,
745             #[rustfmt::skip]
746             "atomic_nand" =>
747                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::SeqCst)?,
748             #[rustfmt::skip]
749             "atomic_nand_acq" =>
750                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Acquire)?,
751             #[rustfmt::skip]
752             "atomic_nand_rel" =>
753                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Release)?,
754             #[rustfmt::skip]
755             "atomic_nand_acqrel" =>
756                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::AcqRel)?,
757             #[rustfmt::skip]
758             "atomic_nand_relaxed" =>
759                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Relaxed)?,
760             #[rustfmt::skip]
761             "atomic_xadd" =>
762                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::SeqCst)?,
763             #[rustfmt::skip]
764             "atomic_xadd_acq" =>
765                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Acquire)?,
766             #[rustfmt::skip]
767             "atomic_xadd_rel" =>
768                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Release)?,
769             #[rustfmt::skip]
770             "atomic_xadd_acqrel" =>
771                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::AcqRel)?,
772             #[rustfmt::skip]
773             "atomic_xadd_relaxed" =>
774                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Relaxed)?,
775             #[rustfmt::skip]
776             "atomic_xsub" =>
777                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::SeqCst)?,
778             #[rustfmt::skip]
779             "atomic_xsub_acq" =>
780                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Acquire)?,
781             #[rustfmt::skip]
782             "atomic_xsub_rel" =>
783                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Release)?,
784             #[rustfmt::skip]
785             "atomic_xsub_acqrel" =>
786                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::AcqRel)?,
787             #[rustfmt::skip]
788             "atomic_xsub_relaxed" =>
789                 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Relaxed)?,
790             "atomic_min" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
791             "atomic_min_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
792             "atomic_min_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
793             "atomic_min_acqrel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
794             "atomic_min_relaxed" =>
795                 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
796             "atomic_max" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
797             "atomic_max_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
798             "atomic_max_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
799             "atomic_max_acqrel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
800             "atomic_max_relaxed" =>
801                 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
802             "atomic_umin" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
803             "atomic_umin_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
804             "atomic_umin_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
805             "atomic_umin_acqrel" =>
806                 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
807             "atomic_umin_relaxed" =>
808                 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
809             "atomic_umax" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
810             "atomic_umax_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
811             "atomic_umax_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
812             "atomic_umax_acqrel" =>
813                 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
814             "atomic_umax_relaxed" =>
815                 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
816
817             // Other
818             "exact_div" => {
819                 let &[ref num, ref denom] = check_arg_count(args)?;
820                 this.exact_div(&this.read_immediate(num)?, &this.read_immediate(denom)?, dest)?;
821             }
822
823             "try" => return this.handle_try(args, dest, ret),
824
825             "breakpoint" => {
826                 let &[] = check_arg_count(args)?;
827                 // normally this would raise a SIGTRAP, which aborts if no debugger is connected
828                 throw_machine_stop!(TerminationInfo::Abort("Trace/breakpoint trap".to_string()))
829             }
830
831             name => throw_unsup_format!("unimplemented intrinsic: {}", name),
832         }
833
834         trace!("{:?}", this.dump_place(**dest));
835         this.go_to_block(ret);
836         Ok(())
837     }
838
839     fn atomic_load(
840         &mut self,
841         args: &[OpTy<'tcx, Tag>],
842         dest: &PlaceTy<'tcx, Tag>,
843         atomic: AtomicReadOp,
844     ) -> InterpResult<'tcx> {
845         let this = self.eval_context_mut();
846
847         let &[ref place] = check_arg_count(args)?;
848         let place = this.deref_operand(place)?;
849
850         // make sure it fits into a scalar; otherwise it cannot be atomic
851         let val = this.read_scalar_atomic(&place, atomic)?;
852
853         // Check alignment requirements. Atomics must always be aligned to their size,
854         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
855         // be 8-aligned).
856         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
857         this.memory.check_ptr_access_align(
858             place.ptr,
859             place.layout.size,
860             align,
861             CheckInAllocMsg::MemoryAccessTest,
862         )?;
863         // Perform regular access.
864         this.write_scalar(val, dest)?;
865         Ok(())
866     }
867
868     fn atomic_store(
869         &mut self,
870         args: &[OpTy<'tcx, Tag>],
871         atomic: AtomicWriteOp,
872     ) -> InterpResult<'tcx> {
873         let this = self.eval_context_mut();
874
875         let &[ref place, ref val] = check_arg_count(args)?;
876         let place = this.deref_operand(place)?;
877         let val = this.read_scalar(val)?; // make sure it fits into a scalar; otherwise it cannot be atomic
878
879         // Check alignment requirements. Atomics must always be aligned to their size,
880         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
881         // be 8-aligned).
882         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
883         this.memory.check_ptr_access_align(
884             place.ptr,
885             place.layout.size,
886             align,
887             CheckInAllocMsg::MemoryAccessTest,
888         )?;
889
890         // Perform atomic store
891         this.write_scalar_atomic(val, &place, atomic)?;
892         Ok(())
893     }
894
895     fn compiler_fence(
896         &mut self,
897         args: &[OpTy<'tcx, Tag>],
898         atomic: AtomicFenceOp,
899     ) -> InterpResult<'tcx> {
900         let &[] = check_arg_count(args)?;
901         let _ = atomic;
902         //FIXME: compiler fences are currently ignored
903         Ok(())
904     }
905
906     fn atomic_fence(
907         &mut self,
908         args: &[OpTy<'tcx, Tag>],
909         atomic: AtomicFenceOp,
910     ) -> InterpResult<'tcx> {
911         let this = self.eval_context_mut();
912         let &[] = check_arg_count(args)?;
913         this.validate_atomic_fence(atomic)?;
914         Ok(())
915     }
916
917     fn atomic_op(
918         &mut self,
919         args: &[OpTy<'tcx, Tag>],
920         dest: &PlaceTy<'tcx, Tag>,
921         atomic_op: AtomicOp,
922         atomic: AtomicRwOp,
923     ) -> InterpResult<'tcx> {
924         let this = self.eval_context_mut();
925
926         let &[ref place, ref rhs] = check_arg_count(args)?;
927         let place = this.deref_operand(place)?;
928
929         if !place.layout.ty.is_integral() {
930             bug!("Atomic arithmetic operations only work on integer types");
931         }
932         let rhs = this.read_immediate(rhs)?;
933
934         // Check alignment requirements. Atomics must always be aligned to their size,
935         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
936         // be 8-aligned).
937         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
938         this.memory.check_ptr_access_align(
939             place.ptr,
940             place.layout.size,
941             align,
942             CheckInAllocMsg::MemoryAccessTest,
943         )?;
944
945         match atomic_op {
946             AtomicOp::Min => {
947                 let old = this.atomic_min_max_scalar(&place, rhs, true, atomic)?;
948                 this.write_immediate(*old, &dest)?; // old value is returned
949                 Ok(())
950             }
951             AtomicOp::Max => {
952                 let old = this.atomic_min_max_scalar(&place, rhs, false, atomic)?;
953                 this.write_immediate(*old, &dest)?; // old value is returned
954                 Ok(())
955             }
956             AtomicOp::MirOp(op, neg) => {
957                 let old = this.atomic_op_immediate(&place, &rhs, op, neg, atomic)?;
958                 this.write_immediate(*old, dest)?; // old value is returned
959                 Ok(())
960             }
961         }
962     }
963
964     fn atomic_exchange(
965         &mut self,
966         args: &[OpTy<'tcx, Tag>],
967         dest: &PlaceTy<'tcx, Tag>,
968         atomic: AtomicRwOp,
969     ) -> InterpResult<'tcx> {
970         let this = self.eval_context_mut();
971
972         let &[ref place, ref new] = check_arg_count(args)?;
973         let place = this.deref_operand(place)?;
974         let new = this.read_scalar(new)?;
975
976         // Check alignment requirements. Atomics must always be aligned to their size,
977         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
978         // be 8-aligned).
979         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
980         this.memory.check_ptr_access_align(
981             place.ptr,
982             place.layout.size,
983             align,
984             CheckInAllocMsg::MemoryAccessTest,
985         )?;
986
987         let old = this.atomic_exchange_scalar(&place, new, atomic)?;
988         this.write_scalar(old, dest)?; // old value is returned
989         Ok(())
990     }
991
992     fn atomic_compare_exchange_impl(
993         &mut self,
994         args: &[OpTy<'tcx, Tag>],
995         dest: &PlaceTy<'tcx, Tag>,
996         success: AtomicRwOp,
997         fail: AtomicReadOp,
998         can_fail_spuriously: bool,
999     ) -> InterpResult<'tcx> {
1000         let this = self.eval_context_mut();
1001
1002         let &[ref place, ref expect_old, ref new] = check_arg_count(args)?;
1003         let place = this.deref_operand(place)?;
1004         let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
1005         let new = this.read_scalar(new)?;
1006
1007         // Check alignment requirements. Atomics must always be aligned to their size,
1008         // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1009         // be 8-aligned).
1010         let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1011         this.memory.check_ptr_access_align(
1012             place.ptr,
1013             place.layout.size,
1014             align,
1015             CheckInAllocMsg::MemoryAccessTest,
1016         )?;
1017
1018         let old = this.atomic_compare_exchange_scalar(
1019             &place,
1020             &expect_old,
1021             new,
1022             success,
1023             fail,
1024             can_fail_spuriously,
1025         )?;
1026
1027         // Return old value.
1028         this.write_immediate(old, dest)?;
1029         Ok(())
1030     }
1031
1032     fn atomic_compare_exchange(
1033         &mut self,
1034         args: &[OpTy<'tcx, Tag>],
1035         dest: &PlaceTy<'tcx, Tag>,
1036         success: AtomicRwOp,
1037         fail: AtomicReadOp,
1038     ) -> InterpResult<'tcx> {
1039         self.atomic_compare_exchange_impl(args, dest, success, fail, false)
1040     }
1041
1042     fn atomic_compare_exchange_weak(
1043         &mut self,
1044         args: &[OpTy<'tcx, Tag>],
1045         dest: &PlaceTy<'tcx, Tag>,
1046         success: AtomicRwOp,
1047         fail: AtomicReadOp,
1048     ) -> InterpResult<'tcx> {
1049         self.atomic_compare_exchange_impl(args, dest, success, fail, true)
1050     }
1051
1052     fn float_to_int_unchecked<F>(
1053         &self,
1054         f: F,
1055         dest_ty: ty::Ty<'tcx>,
1056     ) -> InterpResult<'tcx, Scalar<Tag>>
1057     where
1058         F: Float + Into<Scalar<Tag>>,
1059     {
1060         let this = self.eval_context_ref();
1061
1062         // Step 1: cut off the fractional part of `f`. The result of this is
1063         // guaranteed to be precisely representable in IEEE floats.
1064         let f = f.round_to_integral(Round::TowardZero).value;
1065
1066         // Step 2: Cast the truncated float to the target integer type and see if we lose any information in this step.
1067         Ok(match dest_ty.kind() {
1068             // Unsigned
1069             ty::Uint(t) => {
1070                 let size = Integer::from_uint_ty(this, *t).size();
1071                 let res = f.to_u128(size.bits_usize());
1072                 if res.status.is_empty() {
1073                     // No status flags means there was no further rounding or other loss of precision.
1074                     Scalar::from_uint(res.value, size)
1075                 } else {
1076                     // `f` was not representable in this integer type.
1077                     throw_ub_format!(
1078                         "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1079                         f,
1080                         dest_ty,
1081                     );
1082                 }
1083             }
1084             // Signed
1085             ty::Int(t) => {
1086                 let size = Integer::from_int_ty(this, *t).size();
1087                 let res = f.to_i128(size.bits_usize());
1088                 if res.status.is_empty() {
1089                     // No status flags means there was no further rounding or other loss of precision.
1090                     Scalar::from_int(res.value, size)
1091                 } else {
1092                     // `f` was not representable in this integer type.
1093                     throw_ub_format!(
1094                         "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1095                         f,
1096                         dest_ty,
1097                     );
1098                 }
1099             }
1100             // Nothing else
1101             _ => bug!("`float_to_int_unchecked` called with non-int output type {:?}", dest_ty),
1102         })
1103     }
1104 }
1105
1106 fn fmax_op<'tcx>(
1107     left: &ImmTy<'tcx, Tag>,
1108     right: &ImmTy<'tcx, Tag>,
1109 ) -> InterpResult<'tcx, Scalar<Tag>> {
1110     assert_eq!(left.layout.ty, right.layout.ty);
1111     let ty::Float(float_ty) = left.layout.ty.kind() else {
1112         bug!("fmax operand is not a float")
1113     };
1114     let left = left.to_scalar()?;
1115     let right = right.to_scalar()?;
1116     Ok(match float_ty {
1117         FloatTy::F32 => Scalar::from_f32(left.to_f32()?.max(right.to_f32()?)),
1118         FloatTy::F64 => Scalar::from_f64(left.to_f64()?.max(right.to_f64()?)),
1119     })
1120 }
1121
1122 fn fmin_op<'tcx>(
1123     left: &ImmTy<'tcx, Tag>,
1124     right: &ImmTy<'tcx, Tag>,
1125 ) -> InterpResult<'tcx, Scalar<Tag>> {
1126     assert_eq!(left.layout.ty, right.layout.ty);
1127     let ty::Float(float_ty) = left.layout.ty.kind() else {
1128         bug!("fmin operand is not a float")
1129     };
1130     let left = left.to_scalar()?;
1131     let right = right.to_scalar()?;
1132     Ok(match float_ty {
1133         FloatTy::F32 => Scalar::from_f32(left.to_f32()?.min(right.to_f32()?)),
1134         FloatTy::F64 => Scalar::from_f64(left.to_f64()?.min(right.to_f64()?)),
1135     })
1136 }