5 use rustc_apfloat::{Float, Round};
6 use rustc_middle::ty::layout::{HasParamEnv, IntegerExt, LayoutOf};
7 use rustc_middle::{mir, mir::BinOp, ty, ty::FloatTy};
8 use rustc_target::abi::{Align, Endian, HasDataLayout, Integer, Size};
11 use helpers::check_arg_count;
14 MirOp(mir::BinOp, bool),
19 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
20 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
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();
30 if this.emulate_intrinsic(instance, args, ret)? {
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),
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)?;
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)?;
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)?;
62 "const_deallocate" => {
66 // Raw memory accesses
68 let &[ref place] = check_arg_count(args)?;
69 let place = this.deref_operand(place)?;
70 this.copy_op(&place.into(), dest)?;
73 let &[ref place, ref dest] = check_arg_count(args)?;
74 let place = this.deref_operand(place)?;
75 this.copy_op(dest, &place.into())?;
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 // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
86 // but no actual allocation can be big enough for the difference to be noticeable.
87 let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
88 err_ub_format!("overflow computing total size of `{}`", intrinsic_name)
92 iter::repeat(val_byte).take(byte_count.bytes() as usize),
96 // Floating-point operations
98 let &[ref f] = check_arg_count(args)?;
99 let f = this.read_scalar(f)?.to_f32()?;
100 // Can be implemented in soft-floats.
101 this.write_scalar(Scalar::from_f32(f.abs()), dest)?;
104 let &[ref f] = check_arg_count(args)?;
105 let f = this.read_scalar(f)?.to_f64()?;
106 // Can be implemented in soft-floats.
107 this.write_scalar(Scalar::from_f64(f.abs()), dest)?;
123 let &[ref f] = check_arg_count(args)?;
124 // FIXME: Using host floats.
125 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
126 let f = match intrinsic_name {
129 "sqrtf32" => f.sqrt(),
131 "exp2f32" => f.exp2(),
133 "log10f32" => f.log10(),
134 "log2f32" => f.log2(),
135 "floorf32" => f.floor(),
136 "ceilf32" => f.ceil(),
137 "truncf32" => f.trunc(),
138 "roundf32" => f.round(),
141 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
158 let &[ref f] = check_arg_count(args)?;
159 // FIXME: Using host floats.
160 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
161 let f = match intrinsic_name {
164 "sqrtf64" => f.sqrt(),
166 "exp2f64" => f.exp2(),
168 "log10f64" => f.log10(),
169 "log2f64" => f.log2(),
170 "floorf64" => f.floor(),
171 "ceilf64" => f.ceil(),
172 "truncf64" => f.trunc(),
173 "roundf64" => f.round(),
176 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
186 let &[ref a, ref b] = check_arg_count(args)?;
187 let a = this.read_immediate(a)?;
188 let b = this.read_immediate(b)?;
189 let op = match intrinsic_name {
190 "fadd_fast" => mir::BinOp::Add,
191 "fsub_fast" => mir::BinOp::Sub,
192 "fmul_fast" => mir::BinOp::Mul,
193 "fdiv_fast" => mir::BinOp::Div,
194 "frem_fast" => mir::BinOp::Rem,
197 let float_finite = |x: ImmTy<'tcx, _>| -> InterpResult<'tcx, bool> {
198 Ok(match x.layout.ty.kind() {
199 ty::Float(FloatTy::F32) => x.to_scalar()?.to_f32()?.is_finite(),
200 ty::Float(FloatTy::F64) => x.to_scalar()?.to_f64()?.is_finite(),
202 "`{}` called with non-float input type {:?}",
208 match (float_finite(a)?, float_finite(b)?) {
209 (false, false) => throw_ub_format!(
210 "`{}` intrinsic called with non-finite value as both parameters",
213 (false, _) => throw_ub_format!(
214 "`{}` intrinsic called with non-finite value as first parameter",
217 (_, false) => throw_ub_format!(
218 "`{}` intrinsic called with non-finite value as second parameter",
223 this.binop_ignore_overflow(op, &a, &b, dest)?;
231 let &[ref a, ref b] = check_arg_count(args)?;
232 let a = this.read_scalar(a)?.to_f32()?;
233 let b = this.read_scalar(b)?.to_f32()?;
234 let res = match intrinsic_name {
235 "minnumf32" => a.min(b),
236 "maxnumf32" => a.max(b),
237 "copysignf32" => a.copy_sign(b),
240 this.write_scalar(Scalar::from_f32(res), dest)?;
248 let &[ref a, ref b] = check_arg_count(args)?;
249 let a = this.read_scalar(a)?.to_f64()?;
250 let b = this.read_scalar(b)?.to_f64()?;
251 let res = match intrinsic_name {
252 "minnumf64" => a.min(b),
253 "maxnumf64" => a.max(b),
254 "copysignf64" => a.copy_sign(b),
257 this.write_scalar(Scalar::from_f64(res), dest)?;
261 let &[ref f, ref f2] = check_arg_count(args)?;
262 // FIXME: Using host floats.
263 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
264 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
265 this.write_scalar(Scalar::from_u32(f.powf(f2).to_bits()), dest)?;
269 let &[ref f, ref f2] = check_arg_count(args)?;
270 // FIXME: Using host floats.
271 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
272 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
273 this.write_scalar(Scalar::from_u64(f.powf(f2).to_bits()), dest)?;
277 let &[ref a, ref b, ref c] = check_arg_count(args)?;
278 let a = this.read_scalar(a)?.to_f32()?;
279 let b = this.read_scalar(b)?.to_f32()?;
280 let c = this.read_scalar(c)?.to_f32()?;
281 let res = a.mul_add(b, c).value;
282 this.write_scalar(Scalar::from_f32(res), dest)?;
286 let &[ref a, ref b, ref c] = check_arg_count(args)?;
287 let a = this.read_scalar(a)?.to_f64()?;
288 let b = this.read_scalar(b)?.to_f64()?;
289 let c = this.read_scalar(c)?.to_f64()?;
290 let res = a.mul_add(b, c).value;
291 this.write_scalar(Scalar::from_f64(res), dest)?;
295 let &[ref f, ref i] = check_arg_count(args)?;
296 // FIXME: Using host floats.
297 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
298 let i = this.read_scalar(i)?.to_i32()?;
299 this.write_scalar(Scalar::from_u32(f.powi(i).to_bits()), dest)?;
303 let &[ref f, ref i] = check_arg_count(args)?;
304 // FIXME: Using host floats.
305 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
306 let i = this.read_scalar(i)?.to_i32()?;
307 this.write_scalar(Scalar::from_u64(f.powi(i).to_bits()), dest)?;
310 "float_to_int_unchecked" => {
311 let &[ref val] = check_arg_count(args)?;
312 let val = this.read_immediate(val)?;
314 let res = match val.layout.ty.kind() {
315 ty::Float(FloatTy::F32) =>
316 this.float_to_int_unchecked(val.to_scalar()?.to_f32()?, dest.layout.ty)?,
317 ty::Float(FloatTy::F64) =>
318 this.float_to_int_unchecked(val.to_scalar()?.to_f64()?, dest.layout.ty)?,
321 "`float_to_int_unchecked` called with non-float input type {:?}",
326 this.write_scalar(res, dest)?;
338 let &[ref op] = check_arg_count(args)?;
339 let (op, op_len) = this.operand_to_simd(op)?;
340 let (dest, dest_len) = this.place_to_simd(dest)?;
342 assert_eq!(dest_len, op_len);
344 #[derive(Copy, Clone)]
352 #[derive(Copy, Clone)]
358 let which = match intrinsic_name {
359 "simd_neg" => Op::MirOp(mir::UnOp::Neg),
360 "simd_fabs" => Op::Abs,
361 "simd_ceil" => Op::HostOp(HostFloatOp::Ceil),
362 "simd_floor" => Op::HostOp(HostFloatOp::Floor),
363 "simd_round" => Op::HostOp(HostFloatOp::Round),
364 "simd_trunc" => Op::HostOp(HostFloatOp::Trunc),
365 "simd_fsqrt" => Op::HostOp(HostFloatOp::Sqrt),
369 for i in 0..dest_len {
370 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
371 let dest = this.mplace_index(&dest, i)?;
372 let val = match which {
373 Op::MirOp(mir_op) => this.unary_op(mir_op, &op)?.to_scalar()?,
375 // Works for f32 and f64.
376 let ty::Float(float_ty) = op.layout.ty.kind() else {
377 bug!("{} operand is not a float", intrinsic_name)
379 let op = op.to_scalar()?;
381 FloatTy::F32 => Scalar::from_f32(op.to_f32()?.abs()),
382 FloatTy::F64 => Scalar::from_f64(op.to_f64()?.abs()),
385 Op::HostOp(host_op) => {
386 let ty::Float(float_ty) = op.layout.ty.kind() else {
387 bug!("{} operand is not a float", intrinsic_name)
389 // FIXME using host floats
392 let f = f32::from_bits(op.to_scalar()?.to_u32()?);
393 let res = match host_op {
394 HostFloatOp::Ceil => f.ceil(),
395 HostFloatOp::Floor => f.floor(),
396 HostFloatOp::Round => f.round(),
397 HostFloatOp::Trunc => f.trunc(),
398 HostFloatOp::Sqrt => f.sqrt(),
400 Scalar::from_u32(res.to_bits())
403 let f = f64::from_bits(op.to_scalar()?.to_u64()?);
404 let res = match host_op {
405 HostFloatOp::Ceil => f.ceil(),
406 HostFloatOp::Floor => f.floor(),
407 HostFloatOp::Round => f.round(),
408 HostFloatOp::Trunc => f.trunc(),
409 HostFloatOp::Sqrt => f.sqrt(),
411 Scalar::from_u64(res.to_bits())
417 this.write_scalar(val, &dest.into())?;
439 | "simd_saturating_add"
440 | "simd_saturating_sub" => {
443 let &[ref left, ref right] = check_arg_count(args)?;
444 let (left, left_len) = this.operand_to_simd(left)?;
445 let (right, right_len) = this.operand_to_simd(right)?;
446 let (dest, dest_len) = this.place_to_simd(dest)?;
448 assert_eq!(dest_len, left_len);
449 assert_eq!(dest_len, right_len);
457 let which = match intrinsic_name {
458 "simd_add" => Op::MirOp(BinOp::Add),
459 "simd_sub" => Op::MirOp(BinOp::Sub),
460 "simd_mul" => Op::MirOp(BinOp::Mul),
461 "simd_div" => Op::MirOp(BinOp::Div),
462 "simd_rem" => Op::MirOp(BinOp::Rem),
463 "simd_shl" => Op::MirOp(BinOp::Shl),
464 "simd_shr" => Op::MirOp(BinOp::Shr),
465 "simd_and" => Op::MirOp(BinOp::BitAnd),
466 "simd_or" => Op::MirOp(BinOp::BitOr),
467 "simd_xor" => Op::MirOp(BinOp::BitXor),
468 "simd_eq" => Op::MirOp(BinOp::Eq),
469 "simd_ne" => Op::MirOp(BinOp::Ne),
470 "simd_lt" => Op::MirOp(BinOp::Lt),
471 "simd_le" => Op::MirOp(BinOp::Le),
472 "simd_gt" => Op::MirOp(BinOp::Gt),
473 "simd_ge" => Op::MirOp(BinOp::Ge),
474 "simd_fmax" => Op::FMax,
475 "simd_fmin" => Op::FMin,
476 "simd_saturating_add" => Op::SaturatingOp(BinOp::Add),
477 "simd_saturating_sub" => Op::SaturatingOp(BinOp::Sub),
481 for i in 0..dest_len {
482 let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
483 let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
484 let dest = this.mplace_index(&dest, i)?;
485 let val = match which {
486 Op::MirOp(mir_op) => {
487 let (val, overflowed, ty) = this.overflowing_binary_op(mir_op, &left, &right)?;
488 if matches!(mir_op, BinOp::Shl | BinOp::Shr) {
489 // Shifts have extra UB as SIMD operations that the MIR binop does not have.
490 // See <https://github.com/rust-lang/rust/issues/91237>.
492 let r_val = right.to_scalar()?.to_bits(right.layout.size)?;
493 throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
496 if matches!(mir_op, BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge) {
497 // Special handling for boolean-returning operations
498 assert_eq!(ty, this.tcx.types.bool);
499 let val = val.to_bool().unwrap();
500 bool_to_simd_element(val, dest.layout.size)
502 assert_ne!(ty, this.tcx.types.bool);
503 assert_eq!(ty, dest.layout.ty);
508 fmax_op(&left, &right)?
511 fmin_op(&left, &right)?
513 Op::SaturatingOp(mir_op) => {
514 this.saturating_arith(mir_op, &left, &right)?
517 this.write_scalar(val, &dest.into())?;
521 let &[ref a, ref b, ref c] = check_arg_count(args)?;
522 let (a, a_len) = this.operand_to_simd(a)?;
523 let (b, b_len) = this.operand_to_simd(b)?;
524 let (c, c_len) = this.operand_to_simd(c)?;
525 let (dest, dest_len) = this.place_to_simd(dest)?;
527 assert_eq!(dest_len, a_len);
528 assert_eq!(dest_len, b_len);
529 assert_eq!(dest_len, c_len);
531 for i in 0..dest_len {
532 let a = this.read_immediate(&this.mplace_index(&a, i)?.into())?.to_scalar()?;
533 let b = this.read_immediate(&this.mplace_index(&b, i)?.into())?.to_scalar()?;
534 let c = this.read_immediate(&this.mplace_index(&c, i)?.into())?.to_scalar()?;
535 let dest = this.mplace_index(&dest, i)?;
537 // Works for f32 and f64.
538 let ty::Float(float_ty) = dest.layout.ty.kind() else {
539 bug!("{} operand is not a float", intrinsic_name)
541 let val = match float_ty {
543 Scalar::from_f32(a.to_f32()?.mul_add(b.to_f32()?, c.to_f32()?).value),
545 Scalar::from_f64(a.to_f64()?.mul_add(b.to_f64()?, c.to_f64()?).value),
547 this.write_scalar(val, &dest.into())?;
557 | "simd_reduce_min" => {
560 let &[ref op] = check_arg_count(args)?;
561 let (op, op_len) = this.operand_to_simd(op)?;
564 |b| ImmTy::from_scalar(Scalar::from_bool(b), this.machine.layouts.bool);
572 let which = match intrinsic_name {
573 "simd_reduce_and" => Op::MirOp(BinOp::BitAnd),
574 "simd_reduce_or" => Op::MirOp(BinOp::BitOr),
575 "simd_reduce_xor" => Op::MirOp(BinOp::BitXor),
576 "simd_reduce_any" => Op::MirOpBool(BinOp::BitOr),
577 "simd_reduce_all" => Op::MirOpBool(BinOp::BitAnd),
578 "simd_reduce_max" => Op::Max,
579 "simd_reduce_min" => Op::Min,
583 // Initialize with first lane, then proceed with the rest.
584 let mut res = this.read_immediate(&this.mplace_index(&op, 0)?.into())?;
585 if matches!(which, Op::MirOpBool(_)) {
586 // Convert to `bool` scalar.
587 res = imm_from_bool(simd_element_to_bool(res)?);
590 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
592 Op::MirOp(mir_op) => {
593 this.binary_op(mir_op, &res, &op)?
595 Op::MirOpBool(mir_op) => {
596 let op = imm_from_bool(simd_element_to_bool(op)?);
597 this.binary_op(mir_op, &res, &op)?
600 if matches!(res.layout.ty.kind(), ty::Float(_)) {
601 ImmTy::from_scalar(fmax_op(&res, &op)?, res.layout)
603 // Just boring integers, so NaNs to worry about
604 if this.binary_op(BinOp::Ge, &res, &op)?.to_scalar()?.to_bool()? {
612 if matches!(res.layout.ty.kind(), ty::Float(_)) {
613 ImmTy::from_scalar(fmin_op(&res, &op)?, res.layout)
615 // Just boring integers, so NaNs to worry about
616 if this.binary_op(BinOp::Le, &res, &op)?.to_scalar()?.to_bool()? {
625 this.write_immediate(*res, dest)?;
628 | "simd_reduce_add_ordered"
629 | "simd_reduce_mul_ordered" => {
632 let &[ref op, ref init] = check_arg_count(args)?;
633 let (op, op_len) = this.operand_to_simd(op)?;
634 let init = this.read_immediate(init)?;
636 let mir_op = match intrinsic_name {
637 "simd_reduce_add_ordered" => BinOp::Add,
638 "simd_reduce_mul_ordered" => BinOp::Mul,
644 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
645 res = this.binary_op(mir_op, &res, &op)?;
647 this.write_immediate(*res, dest)?;
650 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
651 let (mask, mask_len) = this.operand_to_simd(mask)?;
652 let (yes, yes_len) = this.operand_to_simd(yes)?;
653 let (no, no_len) = this.operand_to_simd(no)?;
654 let (dest, dest_len) = this.place_to_simd(dest)?;
656 assert_eq!(dest_len, mask_len);
657 assert_eq!(dest_len, yes_len);
658 assert_eq!(dest_len, no_len);
660 for i in 0..dest_len {
661 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
662 let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
663 let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
664 let dest = this.mplace_index(&dest, i)?;
666 let val = if simd_element_to_bool(mask)? { yes } else { no };
667 this.write_immediate(*val, &dest.into())?;
670 "simd_select_bitmask" => {
671 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
672 let (yes, yes_len) = this.operand_to_simd(yes)?;
673 let (no, no_len) = this.operand_to_simd(no)?;
674 let (dest, dest_len) = this.place_to_simd(dest)?;
675 let bitmask_len = dest_len.max(8);
677 assert!(mask.layout.ty.is_integral());
678 assert!(bitmask_len <= 64);
679 assert_eq!(bitmask_len, mask.layout.size.bits());
680 assert_eq!(dest_len, yes_len);
681 assert_eq!(dest_len, no_len);
686 .to_bits(mask.layout.size)?
689 for i in 0..dest_len {
691 mask & (1 << simd_bitmask_index(i, dest_len, this.data_layout().endian));
692 let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
693 let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
694 let dest = this.mplace_index(&dest, i)?;
696 let val = if mask != 0 { yes } else { no };
697 this.write_immediate(*val, &dest.into())?;
699 for i in dest_len..bitmask_len {
700 // If the mask is "padded", ensure that padding is all-zero.
701 let mask = mask & (1 << i);
704 "a SIMD bitmask less than 8 bits long must be filled with 0s for the remaining bits"
710 "simd_cast" | "simd_as" => {
711 let &[ref op] = check_arg_count(args)?;
712 let (op, op_len) = this.operand_to_simd(op)?;
713 let (dest, dest_len) = this.place_to_simd(dest)?;
715 assert_eq!(dest_len, op_len);
717 let safe_cast = intrinsic_name == "simd_as";
719 for i in 0..dest_len {
720 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
721 let dest = this.mplace_index(&dest, i)?;
723 let val = match (op.layout.ty.kind(), dest.layout.ty.kind()) {
724 // Int-to-(int|float): always safe
725 (ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_)) =>
726 this.misc_cast(&op, dest.layout.ty)?,
727 // Float-to-float: always safe
728 (ty::Float(_), ty::Float(_)) =>
729 this.misc_cast(&op, dest.layout.ty)?,
730 // Float-to-int in safe mode
731 (ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
732 this.misc_cast(&op, dest.layout.ty)?,
733 // Float-to-int in unchecked mode
734 (ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
735 this.float_to_int_unchecked(op.to_scalar()?.to_f32()?, dest.layout.ty)?.into(),
736 (ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
737 this.float_to_int_unchecked(op.to_scalar()?.to_f64()?, dest.layout.ty)?.into(),
740 "Unsupported SIMD cast from element type {} to {}",
745 this.write_immediate(val, &dest.into())?;
749 let &[ref left, ref right, ref index] = check_arg_count(args)?;
750 let (left, left_len) = this.operand_to_simd(left)?;
751 let (right, right_len) = this.operand_to_simd(right)?;
752 let (dest, dest_len) = this.place_to_simd(dest)?;
754 // `index` is an array, not a SIMD type
755 let ty::Array(_, index_len) = index.layout.ty.kind() else {
756 bug!("simd_shuffle index argument has non-array type {}", index.layout.ty)
758 let index_len = index_len.eval_usize(*this.tcx, this.param_env());
760 assert_eq!(left_len, right_len);
761 assert_eq!(index_len, dest_len);
763 for i in 0..dest_len {
764 let src_index: u64 = this
765 .read_immediate(&this.operand_index(&index, i)?.into())?
769 let dest = this.mplace_index(&dest, i)?;
771 let val = if src_index < left_len {
772 this.read_immediate(&this.mplace_index(&left, src_index)?.into())?
773 } else if src_index < left_len.checked_add(right_len).unwrap() {
775 &this.mplace_index(&right, src_index - left_len)?.into(),
779 "simd_shuffle index {} is out of bounds for 2 vectors of size {}",
784 this.write_immediate(*val, &dest.into())?;
788 let &[ref passthru, ref ptrs, ref mask] = check_arg_count(args)?;
789 let (passthru, passthru_len) = this.operand_to_simd(passthru)?;
790 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
791 let (mask, mask_len) = this.operand_to_simd(mask)?;
792 let (dest, dest_len) = this.place_to_simd(dest)?;
794 assert_eq!(dest_len, passthru_len);
795 assert_eq!(dest_len, ptrs_len);
796 assert_eq!(dest_len, mask_len);
798 for i in 0..dest_len {
799 let passthru = this.read_immediate(&this.mplace_index(&passthru, i)?.into())?;
800 let ptr = this.read_immediate(&this.mplace_index(&ptrs, i)?.into())?;
801 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
802 let dest = this.mplace_index(&dest, i)?;
804 let val = if simd_element_to_bool(mask)? {
805 let place = this.deref_operand(&ptr.into())?;
806 this.read_immediate(&place.into())?
810 this.write_immediate(*val, &dest.into())?;
814 let &[ref value, ref ptrs, ref mask] = check_arg_count(args)?;
815 let (value, value_len) = this.operand_to_simd(value)?;
816 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
817 let (mask, mask_len) = this.operand_to_simd(mask)?;
819 assert_eq!(ptrs_len, value_len);
820 assert_eq!(ptrs_len, mask_len);
822 for i in 0..ptrs_len {
823 let value = this.read_immediate(&this.mplace_index(&value, i)?.into())?;
824 let ptr = this.read_immediate(&this.mplace_index(&ptrs, i)?.into())?;
825 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
827 if simd_element_to_bool(mask)? {
828 let place = this.deref_operand(&ptr.into())?;
829 this.write_immediate(*value, &place.into())?;
834 let &[ref op] = check_arg_count(args)?;
835 let (op, op_len) = this.operand_to_simd(op)?;
836 let bitmask_len = op_len.max(8);
838 assert!(dest.layout.ty.is_integral());
839 assert!(bitmask_len <= 64);
840 assert_eq!(bitmask_len, dest.layout.size.bits());
844 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
845 if simd_element_to_bool(op)? {
846 res |= 1 << simd_bitmask_index(i, op_len, this.data_layout().endian);
849 this.write_int(res, dest)?;
853 "atomic_load" => this.atomic_load(args, dest, AtomicReadOp::SeqCst)?,
854 "atomic_load_relaxed" => this.atomic_load(args, dest, AtomicReadOp::Relaxed)?,
855 "atomic_load_acq" => this.atomic_load(args, dest, AtomicReadOp::Acquire)?,
857 "atomic_store" => this.atomic_store(args, AtomicWriteOp::SeqCst)?,
858 "atomic_store_relaxed" => this.atomic_store(args, AtomicWriteOp::Relaxed)?,
859 "atomic_store_rel" => this.atomic_store(args, AtomicWriteOp::Release)?,
861 "atomic_fence_acq" => this.atomic_fence(args, AtomicFenceOp::Acquire)?,
862 "atomic_fence_rel" => this.atomic_fence(args, AtomicFenceOp::Release)?,
863 "atomic_fence_acqrel" => this.atomic_fence(args, AtomicFenceOp::AcqRel)?,
864 "atomic_fence" => this.atomic_fence(args, AtomicFenceOp::SeqCst)?,
866 "atomic_singlethreadfence_acq" => this.compiler_fence(args, AtomicFenceOp::Acquire)?,
867 "atomic_singlethreadfence_rel" => this.compiler_fence(args, AtomicFenceOp::Release)?,
868 "atomic_singlethreadfence_acqrel" =>
869 this.compiler_fence(args, AtomicFenceOp::AcqRel)?,
870 "atomic_singlethreadfence" => this.compiler_fence(args, AtomicFenceOp::SeqCst)?,
872 "atomic_xchg" => this.atomic_exchange(args, dest, AtomicRwOp::SeqCst)?,
873 "atomic_xchg_acq" => this.atomic_exchange(args, dest, AtomicRwOp::Acquire)?,
874 "atomic_xchg_rel" => this.atomic_exchange(args, dest, AtomicRwOp::Release)?,
875 "atomic_xchg_acqrel" => this.atomic_exchange(args, dest, AtomicRwOp::AcqRel)?,
876 "atomic_xchg_relaxed" => this.atomic_exchange(args, dest, AtomicRwOp::Relaxed)?,
880 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
882 "atomic_cxchg_acq" =>
883 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
885 "atomic_cxchg_rel" =>
886 this.atomic_compare_exchange(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
888 "atomic_cxchg_acqrel" =>
889 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
891 "atomic_cxchg_relaxed" =>
892 this.atomic_compare_exchange(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
894 "atomic_cxchg_acq_failrelaxed" =>
895 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
897 "atomic_cxchg_acqrel_failrelaxed" =>
898 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
900 "atomic_cxchg_failrelaxed" =>
901 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
903 "atomic_cxchg_failacq" =>
904 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
907 "atomic_cxchgweak" =>
908 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
910 "atomic_cxchgweak_acq" =>
911 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
913 "atomic_cxchgweak_rel" =>
914 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
916 "atomic_cxchgweak_acqrel" =>
917 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
919 "atomic_cxchgweak_relaxed" =>
920 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
922 "atomic_cxchgweak_acq_failrelaxed" =>
923 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
925 "atomic_cxchgweak_acqrel_failrelaxed" =>
926 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
928 "atomic_cxchgweak_failrelaxed" =>
929 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
931 "atomic_cxchgweak_failacq" =>
932 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
936 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::SeqCst)?,
939 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Acquire)?,
942 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Release)?,
944 "atomic_or_acqrel" =>
945 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::AcqRel)?,
947 "atomic_or_relaxed" =>
948 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Relaxed)?,
951 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::SeqCst)?,
954 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Acquire)?,
957 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Release)?,
959 "atomic_xor_acqrel" =>
960 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::AcqRel)?,
962 "atomic_xor_relaxed" =>
963 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Relaxed)?,
966 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::SeqCst)?,
969 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Acquire)?,
972 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Release)?,
974 "atomic_and_acqrel" =>
975 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::AcqRel)?,
977 "atomic_and_relaxed" =>
978 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Relaxed)?,
981 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::SeqCst)?,
984 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Acquire)?,
987 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Release)?,
989 "atomic_nand_acqrel" =>
990 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::AcqRel)?,
992 "atomic_nand_relaxed" =>
993 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Relaxed)?,
996 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::SeqCst)?,
999 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Acquire)?,
1001 "atomic_xadd_rel" =>
1002 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Release)?,
1004 "atomic_xadd_acqrel" =>
1005 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::AcqRel)?,
1007 "atomic_xadd_relaxed" =>
1008 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Relaxed)?,
1011 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::SeqCst)?,
1013 "atomic_xsub_acq" =>
1014 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Acquire)?,
1016 "atomic_xsub_rel" =>
1017 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Release)?,
1019 "atomic_xsub_acqrel" =>
1020 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::AcqRel)?,
1022 "atomic_xsub_relaxed" =>
1023 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Relaxed)?,
1024 "atomic_min" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
1025 "atomic_min_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
1026 "atomic_min_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
1027 "atomic_min_acqrel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
1028 "atomic_min_relaxed" =>
1029 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
1030 "atomic_max" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
1031 "atomic_max_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
1032 "atomic_max_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
1033 "atomic_max_acqrel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
1034 "atomic_max_relaxed" =>
1035 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
1036 "atomic_umin" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
1037 "atomic_umin_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
1038 "atomic_umin_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
1039 "atomic_umin_acqrel" =>
1040 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
1041 "atomic_umin_relaxed" =>
1042 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
1043 "atomic_umax" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
1044 "atomic_umax_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
1045 "atomic_umax_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
1046 "atomic_umax_acqrel" =>
1047 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
1048 "atomic_umax_relaxed" =>
1049 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
1053 let &[ref num, ref denom] = check_arg_count(args)?;
1054 this.exact_div(&this.read_immediate(num)?, &this.read_immediate(denom)?, dest)?;
1057 "try" => return this.handle_try(args, dest, ret),
1060 let &[] = check_arg_count(args)?;
1061 // normally this would raise a SIGTRAP, which aborts if no debugger is connected
1062 throw_machine_stop!(TerminationInfo::Abort("Trace/breakpoint trap".to_string()))
1065 name => throw_unsup_format!("unimplemented intrinsic: {}", name),
1068 trace!("{:?}", this.dump_place(**dest));
1069 this.go_to_block(ret);
1075 args: &[OpTy<'tcx, Tag>],
1076 dest: &PlaceTy<'tcx, Tag>,
1077 atomic: AtomicReadOp,
1078 ) -> InterpResult<'tcx> {
1079 let this = self.eval_context_mut();
1081 let &[ref place] = check_arg_count(args)?;
1082 let place = this.deref_operand(place)?;
1084 // make sure it fits into a scalar; otherwise it cannot be atomic
1085 let val = this.read_scalar_atomic(&place, atomic)?;
1087 // Check alignment requirements. Atomics must always be aligned to their size,
1088 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1090 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1091 this.check_ptr_access_align(
1095 CheckInAllocMsg::MemoryAccessTest,
1097 // Perform regular access.
1098 this.write_scalar(val, dest)?;
1104 args: &[OpTy<'tcx, Tag>],
1105 atomic: AtomicWriteOp,
1106 ) -> InterpResult<'tcx> {
1107 let this = self.eval_context_mut();
1109 let &[ref place, ref val] = check_arg_count(args)?;
1110 let place = this.deref_operand(place)?;
1111 let val = this.read_scalar(val)?; // make sure it fits into a scalar; otherwise it cannot be atomic
1113 // Check alignment requirements. Atomics must always be aligned to their size,
1114 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1116 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1117 this.check_ptr_access_align(
1121 CheckInAllocMsg::MemoryAccessTest,
1124 // Perform atomic store
1125 this.write_scalar_atomic(val, &place, atomic)?;
1131 args: &[OpTy<'tcx, Tag>],
1132 atomic: AtomicFenceOp,
1133 ) -> InterpResult<'tcx> {
1134 let &[] = check_arg_count(args)?;
1136 //FIXME: compiler fences are currently ignored
1142 args: &[OpTy<'tcx, Tag>],
1143 atomic: AtomicFenceOp,
1144 ) -> InterpResult<'tcx> {
1145 let this = self.eval_context_mut();
1146 let &[] = check_arg_count(args)?;
1147 this.validate_atomic_fence(atomic)?;
1153 args: &[OpTy<'tcx, Tag>],
1154 dest: &PlaceTy<'tcx, Tag>,
1155 atomic_op: AtomicOp,
1157 ) -> InterpResult<'tcx> {
1158 let this = self.eval_context_mut();
1160 let &[ref place, ref rhs] = check_arg_count(args)?;
1161 let place = this.deref_operand(place)?;
1163 if !place.layout.ty.is_integral() {
1164 bug!("Atomic arithmetic operations only work on integer types");
1166 let rhs = this.read_immediate(rhs)?;
1168 // Check alignment requirements. Atomics must always be aligned to their size,
1169 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1171 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1172 this.check_ptr_access_align(
1176 CheckInAllocMsg::MemoryAccessTest,
1181 let old = this.atomic_min_max_scalar(&place, rhs, true, atomic)?;
1182 this.write_immediate(*old, &dest)?; // old value is returned
1186 let old = this.atomic_min_max_scalar(&place, rhs, false, atomic)?;
1187 this.write_immediate(*old, &dest)?; // old value is returned
1190 AtomicOp::MirOp(op, neg) => {
1191 let old = this.atomic_op_immediate(&place, &rhs, op, neg, atomic)?;
1192 this.write_immediate(*old, dest)?; // old value is returned
1200 args: &[OpTy<'tcx, Tag>],
1201 dest: &PlaceTy<'tcx, Tag>,
1203 ) -> InterpResult<'tcx> {
1204 let this = self.eval_context_mut();
1206 let &[ref place, ref new] = check_arg_count(args)?;
1207 let place = this.deref_operand(place)?;
1208 let new = this.read_scalar(new)?;
1210 // Check alignment requirements. Atomics must always be aligned to their size,
1211 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1213 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1214 this.check_ptr_access_align(
1218 CheckInAllocMsg::MemoryAccessTest,
1221 let old = this.atomic_exchange_scalar(&place, new, atomic)?;
1222 this.write_scalar(old, dest)?; // old value is returned
1226 fn atomic_compare_exchange_impl(
1228 args: &[OpTy<'tcx, Tag>],
1229 dest: &PlaceTy<'tcx, Tag>,
1230 success: AtomicRwOp,
1232 can_fail_spuriously: bool,
1233 ) -> InterpResult<'tcx> {
1234 let this = self.eval_context_mut();
1236 let &[ref place, ref expect_old, ref new] = check_arg_count(args)?;
1237 let place = this.deref_operand(place)?;
1238 let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
1239 let new = this.read_scalar(new)?;
1241 // Check alignment requirements. Atomics must always be aligned to their size,
1242 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1244 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1245 this.check_ptr_access_align(
1249 CheckInAllocMsg::MemoryAccessTest,
1252 let old = this.atomic_compare_exchange_scalar(
1258 can_fail_spuriously,
1261 // Return old value.
1262 this.write_immediate(old, dest)?;
1266 fn atomic_compare_exchange(
1268 args: &[OpTy<'tcx, Tag>],
1269 dest: &PlaceTy<'tcx, Tag>,
1270 success: AtomicRwOp,
1272 ) -> InterpResult<'tcx> {
1273 self.atomic_compare_exchange_impl(args, dest, success, fail, false)
1276 fn atomic_compare_exchange_weak(
1278 args: &[OpTy<'tcx, Tag>],
1279 dest: &PlaceTy<'tcx, Tag>,
1280 success: AtomicRwOp,
1282 ) -> InterpResult<'tcx> {
1283 self.atomic_compare_exchange_impl(args, dest, success, fail, true)
1286 fn float_to_int_unchecked<F>(
1289 dest_ty: ty::Ty<'tcx>,
1290 ) -> InterpResult<'tcx, Scalar<Tag>>
1292 F: Float + Into<Scalar<Tag>>,
1294 let this = self.eval_context_ref();
1296 // Step 1: cut off the fractional part of `f`. The result of this is
1297 // guaranteed to be precisely representable in IEEE floats.
1298 let f = f.round_to_integral(Round::TowardZero).value;
1300 // Step 2: Cast the truncated float to the target integer type and see if we lose any information in this step.
1301 Ok(match dest_ty.kind() {
1304 let size = Integer::from_uint_ty(this, *t).size();
1305 let res = f.to_u128(size.bits_usize());
1306 if res.status.is_empty() {
1307 // No status flags means there was no further rounding or other loss of precision.
1308 Scalar::from_uint(res.value, size)
1310 // `f` was not representable in this integer type.
1312 "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1320 let size = Integer::from_int_ty(this, *t).size();
1321 let res = f.to_i128(size.bits_usize());
1322 if res.status.is_empty() {
1323 // No status flags means there was no further rounding or other loss of precision.
1324 Scalar::from_int(res.value, size)
1326 // `f` was not representable in this integer type.
1328 "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1335 _ => bug!("`float_to_int_unchecked` called with non-int output type {:?}", dest_ty),
1341 left: &ImmTy<'tcx, Tag>,
1342 right: &ImmTy<'tcx, Tag>,
1343 ) -> InterpResult<'tcx, Scalar<Tag>> {
1344 assert_eq!(left.layout.ty, right.layout.ty);
1345 let ty::Float(float_ty) = left.layout.ty.kind() else {
1346 bug!("fmax operand is not a float")
1348 let left = left.to_scalar()?;
1349 let right = right.to_scalar()?;
1351 FloatTy::F32 => Scalar::from_f32(left.to_f32()?.max(right.to_f32()?)),
1352 FloatTy::F64 => Scalar::from_f64(left.to_f64()?.max(right.to_f64()?)),
1357 left: &ImmTy<'tcx, Tag>,
1358 right: &ImmTy<'tcx, Tag>,
1359 ) -> InterpResult<'tcx, Scalar<Tag>> {
1360 assert_eq!(left.layout.ty, right.layout.ty);
1361 let ty::Float(float_ty) = left.layout.ty.kind() else {
1362 bug!("fmin operand is not a float")
1364 let left = left.to_scalar()?;
1365 let right = right.to_scalar()?;
1367 FloatTy::F32 => Scalar::from_f32(left.to_f32()?.min(right.to_f32()?)),
1368 FloatTy::F64 => Scalar::from_f64(left.to_f64()?.min(right.to_f64()?)),
1372 fn bool_to_simd_element(b: bool, size: Size) -> Scalar<Tag> {
1373 // SIMD uses all-1 as pattern for "true"
1374 let val = if b { -1 } else { 0 };
1375 Scalar::from_int(val, size)
1378 fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, bool> {
1379 let val = elem.to_scalar()?.to_int(elem.layout.size)?;
1383 _ => throw_ub_format!("each element of a SIMD mask must be all-0-bits or all-1-bits"),
1387 fn simd_bitmask_index(idx: u64, vec_len: u64, endianess: Endian) -> u64 {
1388 assert!(idx < vec_len);
1390 Endian::Little => idx,
1391 Endian::Big => vec_len - 1 - idx, // reverse order of bits