1 use std::convert::TryInto;
6 use rustc_apfloat::{Float, Round};
7 use rustc_middle::ty::layout::{HasParamEnv, IntegerExt, LayoutOf};
8 use rustc_middle::{mir, mir::BinOp, ty, ty::FloatTy};
9 use rustc_target::abi::{Align, Endian, HasDataLayout, Integer, Size};
12 use helpers::check_arg_count;
15 MirOp(mir::BinOp, bool),
20 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
21 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
24 instance: ty::Instance<'tcx>,
25 args: &[OpTy<'tcx, Tag>],
26 ret: Option<(&PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
27 _unwind: StackPopUnwind,
28 ) -> InterpResult<'tcx> {
29 let this = self.eval_context_mut();
31 if this.emulate_intrinsic(instance, args, ret)? {
35 // All supported intrinsics have a return place.
36 let intrinsic_name = this.tcx.item_name(instance.def_id());
37 let intrinsic_name = intrinsic_name.as_str();
38 let (dest, ret) = match ret {
39 None => throw_unsup_format!("unimplemented (diverging) intrinsic: {}", intrinsic_name),
43 // Then handle terminating intrinsics.
44 match intrinsic_name {
45 // Miri overwriting CTFE intrinsics.
46 "ptr_guaranteed_eq" => {
47 let &[ref left, ref right] = check_arg_count(args)?;
48 let left = this.read_immediate(left)?;
49 let right = this.read_immediate(right)?;
50 this.binop_ignore_overflow(mir::BinOp::Eq, &left, &right, dest)?;
52 "ptr_guaranteed_ne" => {
53 let &[ref left, ref right] = check_arg_count(args)?;
54 let left = this.read_immediate(left)?;
55 let right = this.read_immediate(right)?;
56 this.binop_ignore_overflow(mir::BinOp::Ne, &left, &right, dest)?;
59 // For now, for compatibility with the run-time implementation of this, we just return null.
60 // See <https://github.com/rust-lang/rust/issues/93935>.
61 this.write_null(dest)?;
63 "const_deallocate" => {
67 // Raw memory accesses
69 let &[ref place] = check_arg_count(args)?;
70 let place = this.deref_operand(place)?;
71 this.copy_op(&place.into(), dest)?;
74 let &[ref place, ref dest] = check_arg_count(args)?;
75 let place = this.deref_operand(place)?;
76 this.copy_op(dest, &place.into())?;
79 "write_bytes" | "volatile_set_memory" => {
80 let &[ref ptr, ref val_byte, ref count] = check_arg_count(args)?;
81 let ty = instance.substs.type_at(0);
82 let ty_layout = this.layout_of(ty)?;
83 let val_byte = this.read_scalar(val_byte)?.to_u8()?;
84 let ptr = this.read_pointer(ptr)?;
85 let count = this.read_scalar(count)?.to_machine_usize(this)?;
86 // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
87 // but no actual allocation can be big enough for the difference to be noticeable.
88 let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
89 err_ub_format!("overflow computing total size of `{}`", intrinsic_name)
93 iter::repeat(val_byte).take(byte_count.bytes() as usize),
97 // Floating-point operations
99 let &[ref f] = check_arg_count(args)?;
100 let f = this.read_scalar(f)?.to_f32()?;
101 // Can be implemented in soft-floats.
102 this.write_scalar(Scalar::from_f32(f.abs()), dest)?;
105 let &[ref f] = check_arg_count(args)?;
106 let f = this.read_scalar(f)?.to_f64()?;
107 // Can be implemented in soft-floats.
108 this.write_scalar(Scalar::from_f64(f.abs()), dest)?;
124 let &[ref f] = check_arg_count(args)?;
125 // FIXME: Using host floats.
126 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
127 let f = match intrinsic_name {
130 "sqrtf32" => f.sqrt(),
132 "exp2f32" => f.exp2(),
134 "log10f32" => f.log10(),
135 "log2f32" => f.log2(),
136 "floorf32" => f.floor(),
137 "ceilf32" => f.ceil(),
138 "truncf32" => f.trunc(),
139 "roundf32" => f.round(),
142 this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
159 let &[ref f] = check_arg_count(args)?;
160 // FIXME: Using host floats.
161 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
162 let f = match intrinsic_name {
165 "sqrtf64" => f.sqrt(),
167 "exp2f64" => f.exp2(),
169 "log10f64" => f.log10(),
170 "log2f64" => f.log2(),
171 "floorf64" => f.floor(),
172 "ceilf64" => f.ceil(),
173 "truncf64" => f.trunc(),
174 "roundf64" => f.round(),
177 this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;
187 let &[ref a, ref b] = check_arg_count(args)?;
188 let a = this.read_immediate(a)?;
189 let b = this.read_immediate(b)?;
190 let op = match intrinsic_name {
191 "fadd_fast" => mir::BinOp::Add,
192 "fsub_fast" => mir::BinOp::Sub,
193 "fmul_fast" => mir::BinOp::Mul,
194 "fdiv_fast" => mir::BinOp::Div,
195 "frem_fast" => mir::BinOp::Rem,
198 let float_finite = |x: ImmTy<'tcx, _>| -> InterpResult<'tcx, bool> {
199 Ok(match x.layout.ty.kind() {
200 ty::Float(FloatTy::F32) => x.to_scalar()?.to_f32()?.is_finite(),
201 ty::Float(FloatTy::F64) => x.to_scalar()?.to_f64()?.is_finite(),
203 "`{}` called with non-float input type {:?}",
209 match (float_finite(a)?, float_finite(b)?) {
210 (false, false) => throw_ub_format!(
211 "`{}` intrinsic called with non-finite value as both parameters",
214 (false, _) => throw_ub_format!(
215 "`{}` intrinsic called with non-finite value as first parameter",
218 (_, false) => throw_ub_format!(
219 "`{}` intrinsic called with non-finite value as second parameter",
224 this.binop_ignore_overflow(op, &a, &b, dest)?;
232 let &[ref a, ref b] = check_arg_count(args)?;
233 let a = this.read_scalar(a)?.to_f32()?;
234 let b = this.read_scalar(b)?.to_f32()?;
235 let res = match intrinsic_name {
236 "minnumf32" => a.min(b),
237 "maxnumf32" => a.max(b),
238 "copysignf32" => a.copy_sign(b),
241 this.write_scalar(Scalar::from_f32(res), dest)?;
249 let &[ref a, ref b] = check_arg_count(args)?;
250 let a = this.read_scalar(a)?.to_f64()?;
251 let b = this.read_scalar(b)?.to_f64()?;
252 let res = match intrinsic_name {
253 "minnumf64" => a.min(b),
254 "maxnumf64" => a.max(b),
255 "copysignf64" => a.copy_sign(b),
258 this.write_scalar(Scalar::from_f64(res), dest)?;
262 let &[ref f, ref f2] = check_arg_count(args)?;
263 // FIXME: Using host floats.
264 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
265 let f2 = f32::from_bits(this.read_scalar(f2)?.to_u32()?);
266 this.write_scalar(Scalar::from_u32(f.powf(f2).to_bits()), dest)?;
270 let &[ref f, ref f2] = check_arg_count(args)?;
271 // FIXME: Using host floats.
272 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
273 let f2 = f64::from_bits(this.read_scalar(f2)?.to_u64()?);
274 this.write_scalar(Scalar::from_u64(f.powf(f2).to_bits()), dest)?;
278 let &[ref a, ref b, ref c] = check_arg_count(args)?;
279 let a = this.read_scalar(a)?.to_f32()?;
280 let b = this.read_scalar(b)?.to_f32()?;
281 let c = this.read_scalar(c)?.to_f32()?;
282 let res = a.mul_add(b, c).value;
283 this.write_scalar(Scalar::from_f32(res), dest)?;
287 let &[ref a, ref b, ref c] = check_arg_count(args)?;
288 let a = this.read_scalar(a)?.to_f64()?;
289 let b = this.read_scalar(b)?.to_f64()?;
290 let c = this.read_scalar(c)?.to_f64()?;
291 let res = a.mul_add(b, c).value;
292 this.write_scalar(Scalar::from_f64(res), dest)?;
296 let &[ref f, ref i] = check_arg_count(args)?;
297 // FIXME: Using host floats.
298 let f = f32::from_bits(this.read_scalar(f)?.to_u32()?);
299 let i = this.read_scalar(i)?.to_i32()?;
300 this.write_scalar(Scalar::from_u32(f.powi(i).to_bits()), dest)?;
304 let &[ref f, ref i] = check_arg_count(args)?;
305 // FIXME: Using host floats.
306 let f = f64::from_bits(this.read_scalar(f)?.to_u64()?);
307 let i = this.read_scalar(i)?.to_i32()?;
308 this.write_scalar(Scalar::from_u64(f.powi(i).to_bits()), dest)?;
311 "float_to_int_unchecked" => {
312 let &[ref val] = check_arg_count(args)?;
313 let val = this.read_immediate(val)?;
315 let res = match val.layout.ty.kind() {
316 ty::Float(FloatTy::F32) =>
317 this.float_to_int_unchecked(val.to_scalar()?.to_f32()?, dest.layout.ty)?,
318 ty::Float(FloatTy::F64) =>
319 this.float_to_int_unchecked(val.to_scalar()?.to_f64()?, dest.layout.ty)?,
322 "`float_to_int_unchecked` called with non-float input type {:?}",
327 this.write_scalar(res, dest)?;
339 let &[ref op] = check_arg_count(args)?;
340 let (op, op_len) = this.operand_to_simd(op)?;
341 let (dest, dest_len) = this.place_to_simd(dest)?;
343 assert_eq!(dest_len, op_len);
345 #[derive(Copy, Clone)]
353 #[derive(Copy, Clone)]
359 let which = match intrinsic_name {
360 "simd_neg" => Op::MirOp(mir::UnOp::Neg),
361 "simd_fabs" => Op::Abs,
362 "simd_ceil" => Op::HostOp(HostFloatOp::Ceil),
363 "simd_floor" => Op::HostOp(HostFloatOp::Floor),
364 "simd_round" => Op::HostOp(HostFloatOp::Round),
365 "simd_trunc" => Op::HostOp(HostFloatOp::Trunc),
366 "simd_fsqrt" => Op::HostOp(HostFloatOp::Sqrt),
370 for i in 0..dest_len {
371 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
372 let dest = this.mplace_index(&dest, i)?;
373 let val = match which {
374 Op::MirOp(mir_op) => this.unary_op(mir_op, &op)?.to_scalar()?,
376 // Works for f32 and f64.
377 let ty::Float(float_ty) = op.layout.ty.kind() else {
378 bug!("{} operand is not a float", intrinsic_name)
380 let op = op.to_scalar()?;
382 FloatTy::F32 => Scalar::from_f32(op.to_f32()?.abs()),
383 FloatTy::F64 => Scalar::from_f64(op.to_f64()?.abs()),
386 Op::HostOp(host_op) => {
387 let ty::Float(float_ty) = op.layout.ty.kind() else {
388 bug!("{} operand is not a float", intrinsic_name)
390 // FIXME using host floats
393 let f = f32::from_bits(op.to_scalar()?.to_u32()?);
394 let res = match host_op {
395 HostFloatOp::Ceil => f.ceil(),
396 HostFloatOp::Floor => f.floor(),
397 HostFloatOp::Round => f.round(),
398 HostFloatOp::Trunc => f.trunc(),
399 HostFloatOp::Sqrt => f.sqrt(),
401 Scalar::from_u32(res.to_bits())
404 let f = f64::from_bits(op.to_scalar()?.to_u64()?);
405 let res = match host_op {
406 HostFloatOp::Ceil => f.ceil(),
407 HostFloatOp::Floor => f.floor(),
408 HostFloatOp::Round => f.round(),
409 HostFloatOp::Trunc => f.trunc(),
410 HostFloatOp::Sqrt => f.sqrt(),
412 Scalar::from_u64(res.to_bits())
418 this.write_scalar(val, &dest.into())?;
440 | "simd_saturating_add"
441 | "simd_saturating_sub" => {
444 let &[ref left, ref right] = check_arg_count(args)?;
445 let (left, left_len) = this.operand_to_simd(left)?;
446 let (right, right_len) = this.operand_to_simd(right)?;
447 let (dest, dest_len) = this.place_to_simd(dest)?;
449 assert_eq!(dest_len, left_len);
450 assert_eq!(dest_len, right_len);
458 let which = match intrinsic_name {
459 "simd_add" => Op::MirOp(BinOp::Add),
460 "simd_sub" => Op::MirOp(BinOp::Sub),
461 "simd_mul" => Op::MirOp(BinOp::Mul),
462 "simd_div" => Op::MirOp(BinOp::Div),
463 "simd_rem" => Op::MirOp(BinOp::Rem),
464 "simd_shl" => Op::MirOp(BinOp::Shl),
465 "simd_shr" => Op::MirOp(BinOp::Shr),
466 "simd_and" => Op::MirOp(BinOp::BitAnd),
467 "simd_or" => Op::MirOp(BinOp::BitOr),
468 "simd_xor" => Op::MirOp(BinOp::BitXor),
469 "simd_eq" => Op::MirOp(BinOp::Eq),
470 "simd_ne" => Op::MirOp(BinOp::Ne),
471 "simd_lt" => Op::MirOp(BinOp::Lt),
472 "simd_le" => Op::MirOp(BinOp::Le),
473 "simd_gt" => Op::MirOp(BinOp::Gt),
474 "simd_ge" => Op::MirOp(BinOp::Ge),
475 "simd_fmax" => Op::FMax,
476 "simd_fmin" => Op::FMin,
477 "simd_saturating_add" => Op::SaturatingOp(BinOp::Add),
478 "simd_saturating_sub" => Op::SaturatingOp(BinOp::Sub),
482 for i in 0..dest_len {
483 let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
484 let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
485 let dest = this.mplace_index(&dest, i)?;
486 let val = match which {
487 Op::MirOp(mir_op) => {
488 let (val, overflowed, ty) = this.overflowing_binary_op(mir_op, &left, &right)?;
489 if matches!(mir_op, BinOp::Shl | BinOp::Shr) {
490 // Shifts have extra UB as SIMD operations that the MIR binop does not have.
491 // See <https://github.com/rust-lang/rust/issues/91237>.
493 let r_val = right.to_scalar()?.to_bits(right.layout.size)?;
494 throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
497 if matches!(mir_op, BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge) {
498 // Special handling for boolean-returning operations
499 assert_eq!(ty, this.tcx.types.bool);
500 let val = val.to_bool().unwrap();
501 bool_to_simd_element(val, dest.layout.size)
503 assert_ne!(ty, this.tcx.types.bool);
504 assert_eq!(ty, dest.layout.ty);
509 fmax_op(&left, &right)?
512 fmin_op(&left, &right)?
514 Op::SaturatingOp(mir_op) => {
515 this.saturating_arith(mir_op, &left, &right)?
518 this.write_scalar(val, &dest.into())?;
522 let &[ref a, ref b, ref c] = check_arg_count(args)?;
523 let (a, a_len) = this.operand_to_simd(a)?;
524 let (b, b_len) = this.operand_to_simd(b)?;
525 let (c, c_len) = this.operand_to_simd(c)?;
526 let (dest, dest_len) = this.place_to_simd(dest)?;
528 assert_eq!(dest_len, a_len);
529 assert_eq!(dest_len, b_len);
530 assert_eq!(dest_len, c_len);
532 for i in 0..dest_len {
533 let a = this.read_immediate(&this.mplace_index(&a, i)?.into())?.to_scalar()?;
534 let b = this.read_immediate(&this.mplace_index(&b, i)?.into())?.to_scalar()?;
535 let c = this.read_immediate(&this.mplace_index(&c, i)?.into())?.to_scalar()?;
536 let dest = this.mplace_index(&dest, i)?;
538 // Works for f32 and f64.
539 let ty::Float(float_ty) = dest.layout.ty.kind() else {
540 bug!("{} operand is not a float", intrinsic_name)
542 let val = match float_ty {
544 Scalar::from_f32(a.to_f32()?.mul_add(b.to_f32()?, c.to_f32()?).value),
546 Scalar::from_f64(a.to_f64()?.mul_add(b.to_f64()?, c.to_f64()?).value),
548 this.write_scalar(val, &dest.into())?;
558 | "simd_reduce_min" => {
561 let &[ref op] = check_arg_count(args)?;
562 let (op, op_len) = this.operand_to_simd(op)?;
565 |b| ImmTy::from_scalar(Scalar::from_bool(b), this.machine.layouts.bool);
573 let which = match intrinsic_name {
574 "simd_reduce_and" => Op::MirOp(BinOp::BitAnd),
575 "simd_reduce_or" => Op::MirOp(BinOp::BitOr),
576 "simd_reduce_xor" => Op::MirOp(BinOp::BitXor),
577 "simd_reduce_any" => Op::MirOpBool(BinOp::BitOr),
578 "simd_reduce_all" => Op::MirOpBool(BinOp::BitAnd),
579 "simd_reduce_max" => Op::Max,
580 "simd_reduce_min" => Op::Min,
584 // Initialize with first lane, then proceed with the rest.
585 let mut res = this.read_immediate(&this.mplace_index(&op, 0)?.into())?;
586 if matches!(which, Op::MirOpBool(_)) {
587 // Convert to `bool` scalar.
588 res = imm_from_bool(simd_element_to_bool(res)?);
591 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
593 Op::MirOp(mir_op) => {
594 this.binary_op(mir_op, &res, &op)?
596 Op::MirOpBool(mir_op) => {
597 let op = imm_from_bool(simd_element_to_bool(op)?);
598 this.binary_op(mir_op, &res, &op)?
601 if matches!(res.layout.ty.kind(), ty::Float(_)) {
602 ImmTy::from_scalar(fmax_op(&res, &op)?, res.layout)
604 // Just boring integers, so NaNs to worry about
605 if this.binary_op(BinOp::Ge, &res, &op)?.to_scalar()?.to_bool()? {
613 if matches!(res.layout.ty.kind(), ty::Float(_)) {
614 ImmTy::from_scalar(fmin_op(&res, &op)?, res.layout)
616 // Just boring integers, so NaNs to worry about
617 if this.binary_op(BinOp::Le, &res, &op)?.to_scalar()?.to_bool()? {
626 this.write_immediate(*res, dest)?;
629 | "simd_reduce_add_ordered"
630 | "simd_reduce_mul_ordered" => {
633 let &[ref op, ref init] = check_arg_count(args)?;
634 let (op, op_len) = this.operand_to_simd(op)?;
635 let init = this.read_immediate(init)?;
637 let mir_op = match intrinsic_name {
638 "simd_reduce_add_ordered" => BinOp::Add,
639 "simd_reduce_mul_ordered" => BinOp::Mul,
645 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
646 res = this.binary_op(mir_op, &res, &op)?;
648 this.write_immediate(*res, dest)?;
651 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
652 let (mask, mask_len) = this.operand_to_simd(mask)?;
653 let (yes, yes_len) = this.operand_to_simd(yes)?;
654 let (no, no_len) = this.operand_to_simd(no)?;
655 let (dest, dest_len) = this.place_to_simd(dest)?;
657 assert_eq!(dest_len, mask_len);
658 assert_eq!(dest_len, yes_len);
659 assert_eq!(dest_len, no_len);
661 for i in 0..dest_len {
662 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
663 let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
664 let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
665 let dest = this.mplace_index(&dest, i)?;
667 let val = if simd_element_to_bool(mask)? { yes } else { no };
668 this.write_immediate(*val, &dest.into())?;
671 "simd_select_bitmask" => {
672 let &[ref mask, ref yes, ref no] = check_arg_count(args)?;
673 let (yes, yes_len) = this.operand_to_simd(yes)?;
674 let (no, no_len) = this.operand_to_simd(no)?;
675 let (dest, dest_len) = this.place_to_simd(dest)?;
676 let bitmask_len = dest_len.max(8);
678 assert!(mask.layout.ty.is_integral());
679 assert!(bitmask_len <= 64);
680 assert_eq!(bitmask_len, mask.layout.size.bits());
681 assert_eq!(dest_len, yes_len);
682 assert_eq!(dest_len, no_len);
687 .to_bits(mask.layout.size)?
690 for i in 0..dest_len {
692 mask & (1 << simd_bitmask_index(i, dest_len, this.data_layout().endian));
693 let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
694 let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
695 let dest = this.mplace_index(&dest, i)?;
697 let val = if mask != 0 { yes } else { no };
698 this.write_immediate(*val, &dest.into())?;
700 for i in dest_len..bitmask_len {
701 // If the mask is "padded", ensure that padding is all-zero.
702 let mask = mask & (1 << i);
705 "a SIMD bitmask less than 8 bits long must be filled with 0s for the remaining bits"
711 "simd_cast" | "simd_as" => {
712 let &[ref op] = check_arg_count(args)?;
713 let (op, op_len) = this.operand_to_simd(op)?;
714 let (dest, dest_len) = this.place_to_simd(dest)?;
716 assert_eq!(dest_len, op_len);
718 let safe_cast = intrinsic_name == "simd_as";
720 for i in 0..dest_len {
721 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
722 let dest = this.mplace_index(&dest, i)?;
724 let val = match (op.layout.ty.kind(), dest.layout.ty.kind()) {
725 // Int-to-(int|float): always safe
726 (ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_)) =>
727 this.misc_cast(&op, dest.layout.ty)?,
728 // Float-to-float: always safe
729 (ty::Float(_), ty::Float(_)) =>
730 this.misc_cast(&op, dest.layout.ty)?,
731 // Float-to-int in safe mode
732 (ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
733 this.misc_cast(&op, dest.layout.ty)?,
734 // Float-to-int in unchecked mode
735 (ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
736 this.float_to_int_unchecked(op.to_scalar()?.to_f32()?, dest.layout.ty)?.into(),
737 (ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if !safe_cast =>
738 this.float_to_int_unchecked(op.to_scalar()?.to_f64()?, dest.layout.ty)?.into(),
741 "Unsupported SIMD cast from element type {} to {}",
746 this.write_immediate(val, &dest.into())?;
750 let &[ref left, ref right, ref index] = check_arg_count(args)?;
751 let (left, left_len) = this.operand_to_simd(left)?;
752 let (right, right_len) = this.operand_to_simd(right)?;
753 let (dest, dest_len) = this.place_to_simd(dest)?;
755 // `index` is an array, not a SIMD type
756 let ty::Array(_, index_len) = index.layout.ty.kind() else {
757 bug!("simd_shuffle index argument has non-array type {}", index.layout.ty)
759 let index_len = index_len.eval_usize(*this.tcx, this.param_env());
761 assert_eq!(left_len, right_len);
762 assert_eq!(index_len, dest_len);
764 for i in 0..dest_len {
765 let src_index: u64 = this
766 .read_immediate(&this.operand_index(&index, i)?.into())?
770 let dest = this.mplace_index(&dest, i)?;
772 let val = if src_index < left_len {
773 this.read_immediate(&this.mplace_index(&left, src_index)?.into())?
774 } else if src_index < left_len.checked_add(right_len).unwrap() {
776 &this.mplace_index(&right, src_index - left_len)?.into(),
780 "simd_shuffle index {} is out of bounds for 2 vectors of size {}",
785 this.write_immediate(*val, &dest.into())?;
789 let &[ref passthru, ref ptrs, ref mask] = check_arg_count(args)?;
790 let (passthru, passthru_len) = this.operand_to_simd(passthru)?;
791 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
792 let (mask, mask_len) = this.operand_to_simd(mask)?;
793 let (dest, dest_len) = this.place_to_simd(dest)?;
795 assert_eq!(dest_len, passthru_len);
796 assert_eq!(dest_len, ptrs_len);
797 assert_eq!(dest_len, mask_len);
799 for i in 0..dest_len {
800 let passthru = this.read_immediate(&this.mplace_index(&passthru, i)?.into())?;
801 let ptr = this.read_immediate(&this.mplace_index(&ptrs, i)?.into())?;
802 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
803 let dest = this.mplace_index(&dest, i)?;
805 let val = if simd_element_to_bool(mask)? {
806 let place = this.deref_operand(&ptr.into())?;
807 this.read_immediate(&place.into())?
811 this.write_immediate(*val, &dest.into())?;
815 let &[ref value, ref ptrs, ref mask] = check_arg_count(args)?;
816 let (value, value_len) = this.operand_to_simd(value)?;
817 let (ptrs, ptrs_len) = this.operand_to_simd(ptrs)?;
818 let (mask, mask_len) = this.operand_to_simd(mask)?;
820 assert_eq!(ptrs_len, value_len);
821 assert_eq!(ptrs_len, mask_len);
823 for i in 0..ptrs_len {
824 let value = this.read_immediate(&this.mplace_index(&value, i)?.into())?;
825 let ptr = this.read_immediate(&this.mplace_index(&ptrs, i)?.into())?;
826 let mask = this.read_immediate(&this.mplace_index(&mask, i)?.into())?;
828 if simd_element_to_bool(mask)? {
829 let place = this.deref_operand(&ptr.into())?;
830 this.write_immediate(*value, &place.into())?;
835 let &[ref op] = check_arg_count(args)?;
836 let (op, op_len) = this.operand_to_simd(op)?;
837 let bitmask_len = op_len.max(8);
839 assert!(dest.layout.ty.is_integral());
840 assert!(bitmask_len <= 64);
841 assert_eq!(bitmask_len, dest.layout.size.bits());
845 let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
846 if simd_element_to_bool(op)? {
847 res |= 1 << simd_bitmask_index(i, op_len, this.data_layout().endian);
850 this.write_int(res, dest)?;
854 "atomic_load" => this.atomic_load(args, dest, AtomicReadOp::SeqCst)?,
855 "atomic_load_relaxed" => this.atomic_load(args, dest, AtomicReadOp::Relaxed)?,
856 "atomic_load_acq" => this.atomic_load(args, dest, AtomicReadOp::Acquire)?,
858 "atomic_store" => this.atomic_store(args, AtomicWriteOp::SeqCst)?,
859 "atomic_store_relaxed" => this.atomic_store(args, AtomicWriteOp::Relaxed)?,
860 "atomic_store_rel" => this.atomic_store(args, AtomicWriteOp::Release)?,
862 "atomic_fence_acq" => this.atomic_fence(args, AtomicFenceOp::Acquire)?,
863 "atomic_fence_rel" => this.atomic_fence(args, AtomicFenceOp::Release)?,
864 "atomic_fence_acqrel" => this.atomic_fence(args, AtomicFenceOp::AcqRel)?,
865 "atomic_fence" => this.atomic_fence(args, AtomicFenceOp::SeqCst)?,
867 "atomic_singlethreadfence_acq" => this.compiler_fence(args, AtomicFenceOp::Acquire)?,
868 "atomic_singlethreadfence_rel" => this.compiler_fence(args, AtomicFenceOp::Release)?,
869 "atomic_singlethreadfence_acqrel" =>
870 this.compiler_fence(args, AtomicFenceOp::AcqRel)?,
871 "atomic_singlethreadfence" => this.compiler_fence(args, AtomicFenceOp::SeqCst)?,
873 "atomic_xchg" => this.atomic_exchange(args, dest, AtomicRwOp::SeqCst)?,
874 "atomic_xchg_acq" => this.atomic_exchange(args, dest, AtomicRwOp::Acquire)?,
875 "atomic_xchg_rel" => this.atomic_exchange(args, dest, AtomicRwOp::Release)?,
876 "atomic_xchg_acqrel" => this.atomic_exchange(args, dest, AtomicRwOp::AcqRel)?,
877 "atomic_xchg_relaxed" => this.atomic_exchange(args, dest, AtomicRwOp::Relaxed)?,
881 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
883 "atomic_cxchg_acq" =>
884 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
886 "atomic_cxchg_rel" =>
887 this.atomic_compare_exchange(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
889 "atomic_cxchg_acqrel" =>
890 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
892 "atomic_cxchg_relaxed" =>
893 this.atomic_compare_exchange(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
895 "atomic_cxchg_acq_failrelaxed" =>
896 this.atomic_compare_exchange(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
898 "atomic_cxchg_acqrel_failrelaxed" =>
899 this.atomic_compare_exchange(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
901 "atomic_cxchg_failrelaxed" =>
902 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
904 "atomic_cxchg_failacq" =>
905 this.atomic_compare_exchange(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
908 "atomic_cxchgweak" =>
909 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::SeqCst)?,
911 "atomic_cxchgweak_acq" =>
912 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Acquire)?,
914 "atomic_cxchgweak_rel" =>
915 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Release, AtomicReadOp::Relaxed)?,
917 "atomic_cxchgweak_acqrel" =>
918 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Acquire)?,
920 "atomic_cxchgweak_relaxed" =>
921 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Relaxed, AtomicReadOp::Relaxed)?,
923 "atomic_cxchgweak_acq_failrelaxed" =>
924 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::Acquire, AtomicReadOp::Relaxed)?,
926 "atomic_cxchgweak_acqrel_failrelaxed" =>
927 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::AcqRel, AtomicReadOp::Relaxed)?,
929 "atomic_cxchgweak_failrelaxed" =>
930 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Relaxed)?,
932 "atomic_cxchgweak_failacq" =>
933 this.atomic_compare_exchange_weak(args, dest, AtomicRwOp::SeqCst, AtomicReadOp::Acquire)?,
937 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::SeqCst)?,
940 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Acquire)?,
943 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Release)?,
945 "atomic_or_acqrel" =>
946 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::AcqRel)?,
948 "atomic_or_relaxed" =>
949 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitOr, false), AtomicRwOp::Relaxed)?,
952 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::SeqCst)?,
955 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Acquire)?,
958 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Release)?,
960 "atomic_xor_acqrel" =>
961 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::AcqRel)?,
963 "atomic_xor_relaxed" =>
964 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitXor, false), AtomicRwOp::Relaxed)?,
967 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::SeqCst)?,
970 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Acquire)?,
973 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Release)?,
975 "atomic_and_acqrel" =>
976 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::AcqRel)?,
978 "atomic_and_relaxed" =>
979 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, false), AtomicRwOp::Relaxed)?,
982 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::SeqCst)?,
985 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Acquire)?,
988 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Release)?,
990 "atomic_nand_acqrel" =>
991 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::AcqRel)?,
993 "atomic_nand_relaxed" =>
994 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::BitAnd, true), AtomicRwOp::Relaxed)?,
997 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::SeqCst)?,
1000 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Acquire)?,
1002 "atomic_xadd_rel" =>
1003 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Release)?,
1005 "atomic_xadd_acqrel" =>
1006 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::AcqRel)?,
1008 "atomic_xadd_relaxed" =>
1009 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Add, false), AtomicRwOp::Relaxed)?,
1012 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::SeqCst)?,
1014 "atomic_xsub_acq" =>
1015 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Acquire)?,
1017 "atomic_xsub_rel" =>
1018 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Release)?,
1020 "atomic_xsub_acqrel" =>
1021 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::AcqRel)?,
1023 "atomic_xsub_relaxed" =>
1024 this.atomic_op(args, dest, AtomicOp::MirOp(BinOp::Sub, false), AtomicRwOp::Relaxed)?,
1025 "atomic_min" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
1026 "atomic_min_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
1027 "atomic_min_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
1028 "atomic_min_acqrel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
1029 "atomic_min_relaxed" =>
1030 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
1031 "atomic_max" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
1032 "atomic_max_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
1033 "atomic_max_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
1034 "atomic_max_acqrel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
1035 "atomic_max_relaxed" =>
1036 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
1037 "atomic_umin" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::SeqCst)?,
1038 "atomic_umin_acq" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Acquire)?,
1039 "atomic_umin_rel" => this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Release)?,
1040 "atomic_umin_acqrel" =>
1041 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::AcqRel)?,
1042 "atomic_umin_relaxed" =>
1043 this.atomic_op(args, dest, AtomicOp::Min, AtomicRwOp::Relaxed)?,
1044 "atomic_umax" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::SeqCst)?,
1045 "atomic_umax_acq" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Acquire)?,
1046 "atomic_umax_rel" => this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Release)?,
1047 "atomic_umax_acqrel" =>
1048 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::AcqRel)?,
1049 "atomic_umax_relaxed" =>
1050 this.atomic_op(args, dest, AtomicOp::Max, AtomicRwOp::Relaxed)?,
1054 let &[ref num, ref denom] = check_arg_count(args)?;
1055 this.exact_div(&this.read_immediate(num)?, &this.read_immediate(denom)?, dest)?;
1058 "try" => return this.handle_try(args, dest, ret),
1061 let &[] = check_arg_count(args)?;
1062 // normally this would raise a SIGTRAP, which aborts if no debugger is connected
1063 throw_machine_stop!(TerminationInfo::Abort("Trace/breakpoint trap".to_string()))
1066 name => throw_unsup_format!("unimplemented intrinsic: {}", name),
1069 trace!("{:?}", this.dump_place(**dest));
1070 this.go_to_block(ret);
1076 args: &[OpTy<'tcx, Tag>],
1077 dest: &PlaceTy<'tcx, Tag>,
1078 atomic: AtomicReadOp,
1079 ) -> InterpResult<'tcx> {
1080 let this = self.eval_context_mut();
1082 let &[ref place] = check_arg_count(args)?;
1083 let place = this.deref_operand(place)?;
1085 // make sure it fits into a scalar; otherwise it cannot be atomic
1086 let val = this.read_scalar_atomic(&place, atomic)?;
1088 // Check alignment requirements. Atomics must always be aligned to their size,
1089 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1091 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1092 this.check_ptr_access_align(
1096 CheckInAllocMsg::MemoryAccessTest,
1098 // Perform regular access.
1099 this.write_scalar(val, dest)?;
1105 args: &[OpTy<'tcx, Tag>],
1106 atomic: AtomicWriteOp,
1107 ) -> InterpResult<'tcx> {
1108 let this = self.eval_context_mut();
1110 let &[ref place, ref val] = check_arg_count(args)?;
1111 let place = this.deref_operand(place)?;
1112 let val = this.read_scalar(val)?; // make sure it fits into a scalar; otherwise it cannot be atomic
1114 // Check alignment requirements. Atomics must always be aligned to their size,
1115 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1117 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1118 this.check_ptr_access_align(
1122 CheckInAllocMsg::MemoryAccessTest,
1125 // Perform atomic store
1126 this.write_scalar_atomic(val, &place, atomic)?;
1132 args: &[OpTy<'tcx, Tag>],
1133 atomic: AtomicFenceOp,
1134 ) -> InterpResult<'tcx> {
1135 let &[] = check_arg_count(args)?;
1137 //FIXME: compiler fences are currently ignored
1143 args: &[OpTy<'tcx, Tag>],
1144 atomic: AtomicFenceOp,
1145 ) -> InterpResult<'tcx> {
1146 let this = self.eval_context_mut();
1147 let &[] = check_arg_count(args)?;
1148 this.validate_atomic_fence(atomic)?;
1154 args: &[OpTy<'tcx, Tag>],
1155 dest: &PlaceTy<'tcx, Tag>,
1156 atomic_op: AtomicOp,
1158 ) -> InterpResult<'tcx> {
1159 let this = self.eval_context_mut();
1161 let &[ref place, ref rhs] = check_arg_count(args)?;
1162 let place = this.deref_operand(place)?;
1164 if !place.layout.ty.is_integral() {
1165 bug!("Atomic arithmetic operations only work on integer types");
1167 let rhs = this.read_immediate(rhs)?;
1169 // Check alignment requirements. Atomics must always be aligned to their size,
1170 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1172 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1173 this.check_ptr_access_align(
1177 CheckInAllocMsg::MemoryAccessTest,
1182 let old = this.atomic_min_max_scalar(&place, rhs, true, atomic)?;
1183 this.write_immediate(*old, &dest)?; // old value is returned
1187 let old = this.atomic_min_max_scalar(&place, rhs, false, atomic)?;
1188 this.write_immediate(*old, &dest)?; // old value is returned
1191 AtomicOp::MirOp(op, neg) => {
1192 let old = this.atomic_op_immediate(&place, &rhs, op, neg, atomic)?;
1193 this.write_immediate(*old, dest)?; // old value is returned
1201 args: &[OpTy<'tcx, Tag>],
1202 dest: &PlaceTy<'tcx, Tag>,
1204 ) -> InterpResult<'tcx> {
1205 let this = self.eval_context_mut();
1207 let &[ref place, ref new] = check_arg_count(args)?;
1208 let place = this.deref_operand(place)?;
1209 let new = this.read_scalar(new)?;
1211 // Check alignment requirements. Atomics must always be aligned to their size,
1212 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1214 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1215 this.check_ptr_access_align(
1219 CheckInAllocMsg::MemoryAccessTest,
1222 let old = this.atomic_exchange_scalar(&place, new, atomic)?;
1223 this.write_scalar(old, dest)?; // old value is returned
1227 fn atomic_compare_exchange_impl(
1229 args: &[OpTy<'tcx, Tag>],
1230 dest: &PlaceTy<'tcx, Tag>,
1231 success: AtomicRwOp,
1233 can_fail_spuriously: bool,
1234 ) -> InterpResult<'tcx> {
1235 let this = self.eval_context_mut();
1237 let &[ref place, ref expect_old, ref new] = check_arg_count(args)?;
1238 let place = this.deref_operand(place)?;
1239 let expect_old = this.read_immediate(expect_old)?; // read as immediate for the sake of `binary_op()`
1240 let new = this.read_scalar(new)?;
1242 // Check alignment requirements. Atomics must always be aligned to their size,
1243 // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1245 let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1246 this.check_ptr_access_align(
1250 CheckInAllocMsg::MemoryAccessTest,
1253 let old = this.atomic_compare_exchange_scalar(
1259 can_fail_spuriously,
1262 // Return old value.
1263 this.write_immediate(old, dest)?;
1267 fn atomic_compare_exchange(
1269 args: &[OpTy<'tcx, Tag>],
1270 dest: &PlaceTy<'tcx, Tag>,
1271 success: AtomicRwOp,
1273 ) -> InterpResult<'tcx> {
1274 self.atomic_compare_exchange_impl(args, dest, success, fail, false)
1277 fn atomic_compare_exchange_weak(
1279 args: &[OpTy<'tcx, Tag>],
1280 dest: &PlaceTy<'tcx, Tag>,
1281 success: AtomicRwOp,
1283 ) -> InterpResult<'tcx> {
1284 self.atomic_compare_exchange_impl(args, dest, success, fail, true)
1287 fn float_to_int_unchecked<F>(
1290 dest_ty: ty::Ty<'tcx>,
1291 ) -> InterpResult<'tcx, Scalar<Tag>>
1293 F: Float + Into<Scalar<Tag>>,
1295 let this = self.eval_context_ref();
1297 // Step 1: cut off the fractional part of `f`. The result of this is
1298 // guaranteed to be precisely representable in IEEE floats.
1299 let f = f.round_to_integral(Round::TowardZero).value;
1301 // Step 2: Cast the truncated float to the target integer type and see if we lose any information in this step.
1302 Ok(match dest_ty.kind() {
1305 let size = Integer::from_uint_ty(this, *t).size();
1306 let res = f.to_u128(size.bits_usize());
1307 if res.status.is_empty() {
1308 // No status flags means there was no further rounding or other loss of precision.
1309 Scalar::from_uint(res.value, size)
1311 // `f` was not representable in this integer type.
1313 "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1321 let size = Integer::from_int_ty(this, *t).size();
1322 let res = f.to_i128(size.bits_usize());
1323 if res.status.is_empty() {
1324 // No status flags means there was no further rounding or other loss of precision.
1325 Scalar::from_int(res.value, size)
1327 // `f` was not representable in this integer type.
1329 "`float_to_int_unchecked` intrinsic called on {} which cannot be represented in target type `{:?}`",
1336 _ => bug!("`float_to_int_unchecked` called with non-int output type {:?}", dest_ty),
1342 left: &ImmTy<'tcx, Tag>,
1343 right: &ImmTy<'tcx, Tag>,
1344 ) -> InterpResult<'tcx, Scalar<Tag>> {
1345 assert_eq!(left.layout.ty, right.layout.ty);
1346 let ty::Float(float_ty) = left.layout.ty.kind() else {
1347 bug!("fmax operand is not a float")
1349 let left = left.to_scalar()?;
1350 let right = right.to_scalar()?;
1352 FloatTy::F32 => Scalar::from_f32(left.to_f32()?.max(right.to_f32()?)),
1353 FloatTy::F64 => Scalar::from_f64(left.to_f64()?.max(right.to_f64()?)),
1358 left: &ImmTy<'tcx, Tag>,
1359 right: &ImmTy<'tcx, Tag>,
1360 ) -> InterpResult<'tcx, Scalar<Tag>> {
1361 assert_eq!(left.layout.ty, right.layout.ty);
1362 let ty::Float(float_ty) = left.layout.ty.kind() else {
1363 bug!("fmin operand is not a float")
1365 let left = left.to_scalar()?;
1366 let right = right.to_scalar()?;
1368 FloatTy::F32 => Scalar::from_f32(left.to_f32()?.min(right.to_f32()?)),
1369 FloatTy::F64 => Scalar::from_f64(left.to_f64()?.min(right.to_f64()?)),
1373 fn bool_to_simd_element(b: bool, size: Size) -> Scalar<Tag> {
1374 // SIMD uses all-1 as pattern for "true"
1375 let val = if b { -1 } else { 0 };
1376 Scalar::from_int(val, size)
1379 fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, bool> {
1380 let val = elem.to_scalar()?.to_int(elem.layout.size)?;
1384 _ => throw_ub_format!("each element of a SIMD mask must be all-0-bits or all-1-bits"),
1388 fn simd_bitmask_index(idx: u64, vec_len: u64, endianess: Endian) -> u64 {
1389 assert!(idx < vec_len);
1391 Endian::Little => idx,
1392 Endian::Big => vec_len - 1 - idx, // reverse order of bits